使用 Google 闭包编译器和 Gradle 缩小 JavaScript
Minify JavaScript using the Google Closure Compiler and Gradle
我有一个小型 Java Web 应用程序正在使用 Gradle 构建,其中包含一些自定义香草 JavaScript。我想使用 Google Closure Compiler 缩小自定义 JS 代码。
Closure Compiler 的所有文档似乎都是围绕使用 CLI 或 JSON API 来缩小 JS 的。我更愿意直接从 Gradle 中调用 Java API复制任务。
我想避免
- 节点解决方案
- 调用 CLI 并使用
java -jar
- HTTP 调用 JSON API
This example is out-of-date and does not reflect the most recent Java API. This question 已有数年历史,尽管大多数 API 调用看起来与当前的 Java API.
相似
是否有人直接使用 Google Closure Compiler Java API 缩小了 Java 来自 Gradle 的脚本?
我有一个可行的解决方案:
task processWebapp(type: Copy) {
from('src/main/webapp') {
include "**/*"
}
eachFile {
if (it.sourceName.endsWith('.js') && !it.sourceName.endsWith('.min.js')) {
it.exclude()
Reader reader = it.file.newReader()
String source = ""
try {
source = reader.readLines().join("\r\n")
} finally {
reader.close()
}
com.google.javascript.jscomp.Compiler compiler = new com.google.javascript.jscomp.Compiler(System.err)
CompilerOptions options = new CompilerOptions()
CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(
options)
SourceFile extern = SourceFile.fromCode("externs.js", "")
SourceFile input = SourceFile.fromCode(it.sourceName, source)
compiler.compile(extern, input, options)
String transpiled = compiler.toSource()
def directoryPath = it.relativePath - it.sourceName
File theDir = new File("build/resources/main/${directoryPath}")
if (!theDir.exists()) {
theDir.mkdirs()
}
PrintWriter writer = new PrintWriter("build/resources/main/${it.relativeSourcePath}", "UTF-8")
try {
writer.println(transpiled)
} finally {
writer.close()
}
}
}
destinationDir = file('build/resources/main')
}
此任务复制从 src/main/webapp
到 build/resources/main
的所有内容,同时转译(缩小)所有以 .js
结尾(但不以 .min.js
结尾)的文件。 Gradle 然后将这些资源打包并嵌入到生成的 jar 中。
希望这对使用 Google Closure 编译器和 Gradle 的其他人有所帮助。
我有一个小型 Java Web 应用程序正在使用 Gradle 构建,其中包含一些自定义香草 JavaScript。我想使用 Google Closure Compiler 缩小自定义 JS 代码。
Closure Compiler 的所有文档似乎都是围绕使用 CLI 或 JSON API 来缩小 JS 的。我更愿意直接从 Gradle 中调用 Java API复制任务。
我想避免
- 节点解决方案
- 调用 CLI 并使用
java -jar
- HTTP 调用 JSON API
This example is out-of-date and does not reflect the most recent Java API. This question 已有数年历史,尽管大多数 API 调用看起来与当前的 Java API.
相似是否有人直接使用 Google Closure Compiler Java API 缩小了 Java 来自 Gradle 的脚本?
我有一个可行的解决方案:
task processWebapp(type: Copy) {
from('src/main/webapp') {
include "**/*"
}
eachFile {
if (it.sourceName.endsWith('.js') && !it.sourceName.endsWith('.min.js')) {
it.exclude()
Reader reader = it.file.newReader()
String source = ""
try {
source = reader.readLines().join("\r\n")
} finally {
reader.close()
}
com.google.javascript.jscomp.Compiler compiler = new com.google.javascript.jscomp.Compiler(System.err)
CompilerOptions options = new CompilerOptions()
CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(
options)
SourceFile extern = SourceFile.fromCode("externs.js", "")
SourceFile input = SourceFile.fromCode(it.sourceName, source)
compiler.compile(extern, input, options)
String transpiled = compiler.toSource()
def directoryPath = it.relativePath - it.sourceName
File theDir = new File("build/resources/main/${directoryPath}")
if (!theDir.exists()) {
theDir.mkdirs()
}
PrintWriter writer = new PrintWriter("build/resources/main/${it.relativeSourcePath}", "UTF-8")
try {
writer.println(transpiled)
} finally {
writer.close()
}
}
}
destinationDir = file('build/resources/main')
}
此任务复制从 src/main/webapp
到 build/resources/main
的所有内容,同时转译(缩小)所有以 .js
结尾(但不以 .min.js
结尾)的文件。 Gradle 然后将这些资源打包并嵌入到生成的 jar 中。
希望这对使用 Google Closure 编译器和 Gradle 的其他人有所帮助。