使用Google Closure Compiler应用程序将所有.js文件压缩成一个文件

14
我想使用Google Closure Compiler在命令行中将同一目录下所有的 .js 文件压缩成一个文件。
对于单个文件,可以这样操作:
java -jar compiler.jar --js test.js --js_output_file final.js

但是我在文档中没有找到如何将我的另一个文件放在final.js的末尾而不覆盖最后一个压缩文件的方法?

我想要的是这样的:

java -jar compiler.jar --js --option *.js --js_output_file final.js

我能否将所有文件添加到一个文件中,然后压缩它?如果可以,是否需要编写一个程序来完成此操作?谢谢!


顺便说一下,我写了一个脚本来完成这个任务。我建议你也这样做。 - Camilo Martin
7个回答

14
java -jar path/to/closure-compiler/build/compiler.jar \ 
--js input_one.js \ 
--js input_two.js \ 
... \ 
--js_output_file compiled_output.js 

当我像这样使用jar时,我得到了这个! - Adelin

7
我尝试了Orbits的答案,但是可能由于我使用的版本较新,它并没有真正起作用。我使用的命令是:
java -jar compiler.jar --js file1.js file2.js file3.js  --js_output_file --js_output_file compiled_output.js. 

4
我使用了以下命令:java -jar lib/compiler.jar --js \find ./src/*.js` --js_output_file build/out.cjs` - lcapra

6

假设您已在Mac上使用Homebrew安装了Closure Compiler,您还可以将所有文件连接起来,然后将它们导入Closure(这种方法也适用于java -jar方式,但我尚未验证):

cat $(ls scripts/*.js) | closure-compiler --js_output_file main.js

ls scripts/*.js 不会递归遍历子文件夹。 - hrdwdmrbl

3

以下是涵盖多个输入文件的五种globbing技术,文档提取自CommandLineRunner类:


(1) 这是muka的技巧的一种变化形式,去除了不必要的--js标志:

java -jar compiler.jar \
    --js_output_file build/out.js `find ./src/*.js`

根据文档:

--js 标志名是可选的,因为参数默认被解释为文件。

这将包括 /src/ 中所有的 .js 文件,但不会包括 /src/ 的子目录中的任何文件。


(2)1类似,但将包括 /src/ 及其所有子目录中的所有 .js 文件:

java -jar compiler.jar \
    --js_output_file build/out.js `find ./src/ -name '*.js'`

(3) 类似于2,但使用 xargs

find ./src/ -name '*.js' \
    | xargs java -jar compiler.jar \
                --js_output_file build/out.js \
                --manage_closure_dependencies

从文档中得知:

It is convenient to leverage the additional arguments feature when using the Closure Compiler in combination with find and xargs:

find MY_JS_SRC_DIR -name '*.js' \
    | xargs java -jar compiler.jar --manage_closure_dependencies

The find command will produce a list of '*.js' source files in the MY_JS_SRC_DIR directory while xargs will convert them to a single, space-delimited set of arguments that are appended to the java command to run the Compiler.

Note that it is important to use the --manage_closure_dependencies option in this case because the order produced by find is unlikely to be sorted correctly with respect to goog.provide() and goog.requires().


(4) 在发布版本v20140625中,添加了对**(globstar)通配符的支持,该通配符递归匹配所有子目录。

例如,这将包括/src/及其所有子目录中的所有.js文件:

java -jar compiler.jar \
    --js_output_file build/out.js './src/**.js'

更多信息在此处。根据文档:

You may also use minimatch-style glob patterns. For example, use:

--js='**.js' --js='!**_test.js'

to recursively include all js files that do not end in _test.js

Java文档中得知:

以下规则用于解释glob模式:

  • *字符匹配零个或多个名称组件的字符,不跨越目录边界。
  • **字符匹配零个或多个字符,跨越目录边界。

(5) v20140625版本还添加了一个新功能:如果输入路径是目录,则该目录及其所有子目录中的所有.js文件都将被包含。

例如,这将包括/src/及其所有子目录中的所有.js文件:

java -jar compiler.jar \
    --js_output_file build/out.js './src/'

更多信息请点击 此处


2

1

    <exec executable="java">
        <arg line="-jar ../lib/compiler.jar --compilation_level SIMPLE_OPTIMIZATIONS --language_in ECMASCRIPT5 --js_output_file=${compressdir}/js/controllers/jscontrollersfiles.js ${webappdir}/js/controllers/*.js" />
    </exec>


这里的compresseddir是一个包含压缩js文件的目录。 - user5013986
webdir在我们的项目中指向webapp。 - user5013986

0

Google Closure文档的Github给出了以下命令。

如果您有多个脚本,应该使用一个编译命令将它们全部编译在一起。

java -jar compiler.jar --js_output_file=out.js in1.js in2.js in3.js ...

你也可以使用类似 minimatch 的通配符。

# Recursively include all js files in subdirs
java -jar compiler.jar --js_output_file=out.js 'src/**.js'

# Recursively include all js files in subdirs, excluding test files.
# Use single-quotes, so that bash doesn't try to expand the '!'
java -jar compiler.jar --js_output_file=out.js 'src/**.js' '!**_test.js'

来源:https://github.com/google/closure-compiler#compiling-multiple-scripts


不要忘记使用单引号。因为这个问题,我浪费了很多时间。没有它们,在Windows上可以工作;但在Linux上不行。 - Bancarel Valentin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接