node package manager
Easy sharing. Manage teams and permissions with one click. Create a free org »



Grunt task for compiling client-side CommonJS modules into one file.

It takes only required files, not everything.


npm install grunt-commonjs-compiler

Also you can add --save or --save-dev to save this dependency in package.json:

npm install grunt-commonjs-compiler --save-dev

Adding Grunt task

Modify Gruntfile.js:

module.exports = function (grunt) {
        'commonjs-compiler': {
            main: {
                cwd         : 'js',                  // scripts path, optional 
                compilerPath: '..',                  // compiler.jar location 
                entryModule : 'main.js',
                output      : '../build.js',         // output file location 
                externs     : ['externs/jquery.js'], // optional 
                report      : 'build-report.txt'     // optional 
                define      : 'SOME_VAR=true'        // @define, optional 


grunt-commonjs-compiler uses Closure Compiler to compile and optimize CommonJS modules.

You can obtain it here

Quick example

To see it in action without smoking too much of documentation:

Download stuff

git clone .
npm install
curl | tar zx

Compile example project

grunt test

grunt-commonjs-compiler will build a CommonJS module tree starting from test/client/main.js and write the compiled code to test/build.js

Notice that the compiled build doesn't include test/client/unused.js as no module requires it.