javascript 使用 Google Closure Compiler Application 将所有文件 .js 压缩到一个文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4043402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 10:15:39  来源:igfitidea点击:

Compress all file .js with Google Closure Compiler Application in one File

javascriptcompressiongoogle-closure-compiler

提问by j3j3

I would like to Compress all my file .jsin a same directory in one file with Google Closure Compiler in a command line.

我想.js在命令行中使用 Google Closure Compiler 将同一目录中的所有文件压缩到一个文件中。

For one file it's :

对于一个文件,它是:

java -jar compiler.jar --js test.js --js_output_file final.js

But I didn't find in the doc how put my other file at the end of final.jswithout write over the last compress file ?

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

I would like something like that :

我想要这样的东西:

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

It's possible or must I do a programm who add all file in a file and after compress it ? Thank you if you can help me !

有可能还是我必须做一个程序,将所有文件添加到一个文件中并在压缩之后?谢谢你,如果你能帮助我!

回答by Orbit

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

回答by Adelin

I tried Orbits' answer, but It didn't really work perhaps the version I use is a newer version. The command I use is :

我尝试了 Orbits 的回答,但它并没有真正起作用,也许我使用的版本是较新的版本。我使用的命令是:

java -jar compiler.jar --js file1.js file2.js file3.js  --js_output_file --js_output_file compiled_output.js. 

回答by Rafael Bugajewski

Assuming that you've installed the Closure Compiler with Homebrew on a Mac, you can also concatenate all files and then pipe them to to Closure (this should also work the java -jarway, but I haven't verified it):

假设您已经在 Mac 上安装了带有 Homebrew 的 Closure Compiler,您还可以连接所有文件,然后通过管道将它们传送到 Closure(这也应该可以java -jar,但我还没有验证过):

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

回答by Oleksandr Knyga

You can use KjsCompiler: https://github.com/knyga/kjscompiler. Compiles multiple JavaScript files with Google Closure Compiler application in a right order

您可以使用 KjsCompiler:https: //github.com/knyga/kjscompiler。使用 Google Closure Compiler 应用程序按正确顺序编译多个 JavaScript 文件

How to solve your problem: 1. Add annotations to js files, like this:

如何解决你的问题: 1. 给js文件添加注解,像这样:

/** * @depends {lib/somefile.js} **/If you do not care about compilation chain, you can ignore this step. 2. java -jar kjscompile.jar

/** * @depends {lib/somefile.js} **/如果不关心编译链,可以忽略这一步。2.java -jar kjscompile.jar

You can look for example here: https://github.com/knyga/kjscompiler/tree/master/examples

您可以在这里查看示例:https: //github.com/knyga/kjscompiler/tree/master/examples

回答by TachyonVortex

Here are five globbing techniques for including multiple input files, with documentation extracted from the CommandLineRunnerclass:

以下是用于包含多个输入文件的五种通配技术,以及从CommandLineRunner该类中提取的文档:



(1)This is a variation of muka's technique, removing the --jsflag, which is not needed:

(1)这是muka 技术的一种变体,移除了--js不需要的标志:

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

From the docs:

从文档:

The --jsflag name is optional, because args are interpreted as files by default.

--js标志的名称是可选的,因为ARG游戏解释为默认文件。

This will include all .jsfiles in /src/, but won't include any files in subdirectories of /src/.

这将包括 中的所有.js文件/src/,但不包括/src/.



(2)Similar to 1, but will include all .jsfiles in /src/and all its subdirectories:

(2)类似于1,但将包含所有.js文件/src/及其所有子目录

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


(3)Similar to 2, but uses xargs:

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

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

From the docs:

从文档:

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

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

The findcommand will produce a list of '*.js' source files in the MY_JS_SRC_DIRdirectory while xargswill convert them to a single, space-delimited set of arguments that are appended to the javacommand to run the Compiler.

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

将闭包编译器与find和结合使用时,可以方便地利用附加参数功能xargs

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

find命令将在目录中生成一个“*.js”源文件列表,MY_JS_SRC_DIR同时xargs将它们转换为单个、以空格分隔的参数集,这些参数附加到 java命令以运行编译器。

请注意,--manage_closure_dependencies在这种情况下使用该选项很重要, 因为 by 产生的顺序find不太可能相对于goog.provide()和正确排序goog.requires()



(4)The v20140625release added support for the **(globstar) wildcard, which recursively matches all subdirectories.

(4)所述的v20140625释放增加了对支持**(globstar)通配符,其递归地匹配所有子目录。

For example, this will include all .jsfiles in /src/and all its subdirectories:

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

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

More info here. From the docs:

更多信息在这里。从文档:

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

你也可以使用 minimatch 风格的 glob 模式。例如,使用:

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

递归包含所有不以 _test.js 结尾的 js 文件

From the Java docs:

Java 文档

The following rules are used to interpret glob patterns:

  • The *character matches zero or more characters of a name component without crossing directory boundaries.
  • The **characters matches zero or more characters crossing directory boundaries.

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

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


(5)The v20140625release also added a new feature: if the input path is a directory, then all .jsfiles in that directory and all subdirectories will be included.

(5)v20140625版本还增加了一个新功能:如果输入的路径是一个目录,那么所有.js在该目录中的文件和所有子目录将包括在内。

For example, this will include all .jsfiles in /src/and all its subdirectories:

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

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

More info here.

更多信息在这里

回答by TachyonVortex

    <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>

回答by SuperNova

The github of google closure doc gives the below command.

google 关闭文档的 github 给出了以下命令。

If you have multiple scripts, you should compile them all together with one compile command.

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

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


You can also use minimatch-style globs.

您还可以使用 minimatch 样式的 glob。

# 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'

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

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