我需要使用Google Closure compiler.jar来缩小我正在处理的大型项目。我有多个js文件,我想编译成一个game.min.js文件。我知道我可以使用以下内容:
java -jar compiler.jar --js file1.js --js file2.js --js etc, etc --js_output_file game.min.js
...但我有很多文件,据我所知,Closure不支持添加目录并查找驻留在该目录下的所有* .js文件。我笨拙的谷歌搜索没有给我任何工具,我可以用于工作(或任何无效的工作)。
有没有人在那里找到/使用/编写了一个遍历目录的脚本,并将所有.js文件吐出到一个缩小的文件中?我对php,python等无望,所以任何帮助都非常感激。
答案 0 :(得分:2)
你可以use ant to automate the use of the closure compile r。
我是在两个单独的步骤中完成的,连接然后编译:
<concat destfile="src/somepath/app.concat.js">
<filelist dir="src/somepath">
<file name="a.js" />
<file name="b.js" />
<file name="c.js" />
<file name="d.js" />
</filelist>
</concat>
<jscomp compilationLevel="simple" warning="quiet" debug="false" output="src/app.min.js">
<sources dir="src/somepath">
<file name="app.concat.js" />
</sources>
</jscomp>
请注意文件的顺序很重要。这就是为什么你不能简单地将文件集传递给jscomp
任务。
答案 1 :(得分:1)
您还可以在指定文件时使用通配符。您可以将示例更改为:
java -jar compiler.jar --js *.js --js_output_file game.min.js
这应该将当前工作目录中的所有.js文件合并到您指定的输出文件中。
答案 2 :(得分:-1)
在应用Google Closure编译器之前,应该连接所有源文件。
对于所有相关任务,您可以使用Ant构建工具。此外,还有一个很棒的Grunt.js项目,对JS来说更方便。 Grunt.js有grunt-contrib-concat
和grunt-shell
个npm模块,第一个用于连接,另一个用于运行控制台命令。
你的Gruntfile.js看起来像这样:
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
concat: {
js: {
src: ['src/js/*.js'],
dest: 'dist/pre-build.js'
}
},
shell: {
optimize: {
command: 'google closure compiler command here',
stdout: true
}
}
});
grunt.loadNpmTasks('grunt-shell');
grunt.loadNpmTasks('grunt-contrib-concat');
// Default task.
grunt.registerTask('default', ['concat', 'shell:optimize']);
};