3

我需要使用 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 等感到绝望,因此非常感谢任何帮助。

4

3 回答 3

2

您可以使用 ant 来自动使用闭包 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任务。

于 2013-03-07T12:33:48.763 回答
1

指定文件时也可以使用通配符。您可以将示例更改为:

java -jar compiler.jar --js *.js --js_output_file game.min.js

这应该将当前工作目录中的所有 .js 文件合并到您指定的输出文件中。

于 2014-01-16T20:52:06.337 回答
-1

在应用 Google Closure 编译器之前,您应该连接所有源文件。

对于所有相关任务,您可以使用 Ant 构建工具。另外,还有一个很棒的Grunt.js项目,对 JS 来说更方便。Grunt.js有grunt-contrib-concatgrunt-shellnpm 模块,第一个用于连接,另一个用于运行控制台命令。

您的 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']);
}; 
于 2013-03-07T12:47:37.950 回答