3

I need to use the Google Closure compiler.jar to minify a huge project I am working on. I have multiple js files that I want to compile into a single game.min.js file. I know I can use the following:

java -jar compiler.jar --js file1.js --js file2.js --js etc, etc --js_output_file game.min.js

...but I have a LOT of files and as I understand it Closure doesn't have support for adding a directory and finding all the *.js files residing under that directory. My fumbling google searches are not giving me any tools that I can use for the job (or nothing that works at any rate).

Has anyone out there found / used / written a script that loops through a directory and spits out all the .js files into a single minified file? I am hopeless with php, python, etc, so any help greatly appreciated.

Simple Simon
  • 712
  • 2
  • 8
  • 23
  • We should modify the compiler command-line runner so that normal shell wildcards can be used, but if you are building a script you should build one that simply creates a flag file for the compiler with all the sources listed. – John Mar 07 '13 at 23:44

3 Answers3

2

You can use ant to automate the use of the closure compiler.

I do it in two separate steps, concatenation then compilation :

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

Be careful that the order of the files is important. That's why you can't simply pass a fileset to the jscomp task.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Thanks for answering. Forgive my ignorance, but I assume I still have to add each directory and each file within that directory explicitly here? No wildcards allowed? – Simple Simon Mar 07 '13 at 12:48
  • I found an old batch file we used to use with JSmin and modified that to use Google Closure. I'll give you the best answer as it seems ant is pretty handy for this sort of thing! – Simple Simon Mar 08 '13 at 07:21
1

You can also use wildcards when specifying files. You could change your example to:

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

This should combine all of the .js files in your current working directory into the output file you've specified.

HotN
  • 4,216
  • 3
  • 40
  • 51
-1

You should concatenate all your source files before you apply Google Closure compiler.

For all related tasks you could use Ant build tool. Also, there is a great Grunt.js project, which is more convenient for JS. There are grunt-contrib-concat and grunt-shell npm modules for Grunt.js, the first is for concatenation, the other is for running console commands.

Your Gruntfile.js could look like this:

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']);
}; 
eur00t
  • 1,400
  • 10
  • 7
  • 1
    That makes any error or warning more difficult to locate. If you are going find and concatenating all the source, you might as well build a proper command line or flag file. – John Mar 07 '13 at 23:41
  • This is the approach that I've arrived at for similar reasons, but the problem I have now is that I can't generate useful source maps anymore, because they all point at the concatinated file. What are these flag files - any references for where I could find out about this? Originally I had two shell scripts for this build, but maintaining the list of files for both Mac and Windows got tiresome, which is why I tried out a cross-platform Grunt solution - and now I have no useful source maps! – James Ford Nov 21 '13 at 11:16
  • The closure compiler is designed to take multiple source js files and output a single compiled file - in this sense it does the concate for you. – Elemental Aug 15 '15 at 13:00