Skip to content

Commit

Permalink
Update build.gradle to use org.gradle-webtools.minify:gradle-minify-p…
Browse files Browse the repository at this point in the history
…lugin instead of the eriwen plugin, plus small changes to .gitignore and the clean task extension to handle extra .min.js files
  • Loading branch information
jonesde committed Apr 6, 2022
1 parent 3a4efbd commit c92369e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

screen/store/components/combined.*
screen/store/components/*.min.js

# gradle/build files
.gradle
Expand Down
56 changes: 40 additions & 16 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,56 @@
*/

import java.nio.charset.StandardCharsets
plugins {
id "com.eriwen.gradle.js" version "2.14.1"

buildscript {
repositories {
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath "org.gradle-webtools.minify:gradle-minify-plugin:1.3.1"
}
}
apply plugin: "org.gradlewebtools.minify"

def compsPath = "screen/store/components"
// source JS files in load order
def compsJsFiles = [compsPath + "/utilities.js", compsPath + "/ApiServices.js", compsPath + "/ComponentsNav.js",
compsPath + "/ComponentsProduct.js", compsPath + "/ComponentsAccount.js", compsPath + "/ComponentsCheckout.js", compsPath + "/main.js"]

task combineBaseJs(type: com.eriwen.gradle.js.tasks.CombineJsTask) {
encoding = "UTF-8"
source = compsJsFiles
dest = file("${compsPath}/combined.js")
}
task minifyCombined(type: com.eriwen.gradle.js.tasks.MinifyJsTask) {
source = file("${compsPath}/combined.js")
dest = file("${compsPath}/combined.min.js")
closure {
compilerOptions.setOutputCharset(StandardCharsets.UTF_8)
compilerOptions.setLanguageIn(com.google.javascript.jscomp.CompilerOptions.LanguageMode.fromString("ECMASCRIPT6"))
compilerOptions.setLanguageOut(com.google.javascript.jscomp.CompilerOptions.LanguageMode.fromString("ES5"))
task combineBaseJs {
doLast {
def destFile = file("${compsPath}/combined.js")
destFile.write("")
compsJsFiles.each({
destFile.append(file(it).getText("UTF-8"))
destFile.append("\n")
})
}
}
task minifyCombined(type: org.gradlewebtools.minify.JsMinifyTask) {
dependsOn combineBaseJs

// see: https://github.com/gradle-webtools/gradle-minify-plugin
// NOTE: JsMinifyTask (and the css one, etc) always recursively minify in sub-directories, no way to disable! (based on source review 2022-04-05)
srcDir = file(compsPath)
dstDir = file(compsPath)
options.ignoreMinFiles = true
options.emitUseStrict = false
options.strictModeInput = false
options.compilationLevel = com.google.javascript.jscomp.CompilationLevel.SIMPLE_OPTIMIZATIONS
options.env = com.google.javascript.jscomp.CompilerOptions.Environment.BROWSER
options.warningLevel = com.google.javascript.jscomp.WarningLevel.QUIET
options.charset = StandardCharsets.UTF_8
// options.outputCharset = StandardCharsets.UTF_8
options.setLanguageIn(com.google.javascript.jscomp.CompilerOptions.LanguageMode.fromString("ECMASCRIPT6"))
options.setLanguageOut(com.google.javascript.jscomp.CompilerOptions.LanguageMode.fromString("ES5"))
}

task clean { doLast { delete fileTree(dir: "${compsPath}", include: 'combined.*') }}
task clean { doLast {
delete fileTree(dir: compsPath, include: 'combined.*')
delete fileTree(dir: compsPath, include: '*.min.js')
}}
task build {
dependsOn combineBaseJs
dependsOn minifyCombined
}

0 comments on commit c92369e

Please sign in to comment.