├── .gitignore ├── .npmignore ├── Gruntfile.js ├── LICENSE-MIT ├── README.md ├── bin └── grunt-closure-compiler ├── package.json ├── tasks └── closure-compiler.js └── test └── closure-compiler_test.js /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | node_modules 3 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .idea 2 | node_modules 3 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | module.exports = function(grunt) { 2 | grunt.initConfig({ 3 | test: { 4 | files: ['test/**/*.js'] 5 | }, 6 | lint: { 7 | files: ['grunt.js', 'tasks/**/*.js', 'test/**/*.js'] 8 | }, 9 | watch: { 10 | files: '', 11 | tasks: 'default' 12 | }, 13 | jshint: { 14 | options: { 15 | curly: true, 16 | eqeqeq: true, 17 | immed: true, 18 | latedef: true, 19 | newcap: true, 20 | noarg: true, 21 | sub: true, 22 | undef: true, 23 | boss: true, 24 | eqnull: true, 25 | node: true, 26 | es5: true 27 | }, 28 | globals: {} 29 | } 30 | }); 31 | 32 | // Load local tasks. 33 | grunt.loadTasks('tasks'); 34 | 35 | // Default task. 36 | grunt.registerTask('default', 'lint test'); 37 | }; 38 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Guillaume Marty 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # grunt-closure-compiler 2 | 3 | A Grunt task for [Closure Compiler](https://developers.google.com/closure/compiler/). 4 | 5 | ## Getting Started 6 | 7 | First you need to download a [build of Closure Compiler](http://code.google.com/p/closure-compiler/downloads/list) or build it [from the source](http://code.google.com/p/closure-compiler/source/checkout) (see [details below](#closure-compiler-installation-from-source)). 8 | 9 | Optionally, you can set up an environment variable called `CLOSURE_PATH` that points to your Closure Compiler dir (see [details below](#set-up-the-environment-variable)). 10 | 11 | Install this module on your project's [grunt.js gruntfile](https://github.com/cowboy/grunt/blob/master/docs/getting_started.md): 12 | ```bash 13 | $ npm install grunt-closure-compiler 14 | ``` 15 | 16 | Then register the task by adding the following line to your `grunt.js` gruntfile: 17 | ```javascript 18 | grunt.loadNpmTasks('grunt-closure-compiler'); 19 | ``` 20 | 21 | Then you can minify JavaScript calling: 22 | ```javascript 23 | grunt.initConfig({ 24 | 'closure-compiler': { 25 | frontend: { 26 | closurePath: '/src/to/closure-compiler', 27 | js: 'static/src/frontend.js', 28 | jsOutputFile: 'static/js/frontend.min.js', 29 | maxBuffer: 500, 30 | options: { 31 | compilation_level: 'ADVANCED_OPTIMIZATIONS', 32 | language_in: 'ECMASCRIPT5_STRICT' 33 | } 34 | } 35 | } 36 | }); 37 | ``` 38 | 39 | `closurePath` is required if you choose not to set up the `CLOSURE_PATH` environment variable. In this case, it should point to the install dir of Closure Compiler (not the subdirectory where the `compiler.jar` file is located). 40 | 41 | `js` property is always required. 42 | 43 | If `jsOutputFile` property is set, the script will be minified and saved to the file specified. Otherwise it will be output to the command line. 44 | 45 | `maxBuffer` property 46 | 47 | If the buffer returned by closure compiler is more than 200kb, you will get an error saying "maxBuffer exceeded". To prevent this, you can set the maxBuffer to the preffered size you want (in kb) 48 | 49 | Use `cwd` to specify the working directory where closure compiler is called. Useful in when you want 50 | to process common js modules. 51 | 52 | Optionally, several parameters can be passed to `options` object. 53 | 54 | ## Documentation 55 | 56 | ### Closure Compiler installation from source 57 | 58 | Install dependencies: 59 | ```bash 60 | $ sudo apt-get install git ant openjdk-7-jdk 61 | ``` 62 | 63 | Then checkout the source from Git and build: 64 | ```bash 65 | $ git clone https://code.google.com/p/closure-compiler/ 66 | $ cd closure-compiler 67 | $ ant 68 | ``` 69 | 70 | To refresh your build, simply call: 71 | ```bash 72 | $ git pull 73 | $ ant clean 74 | $ ant 75 | ``` 76 | 77 | #### Mac 78 | 79 | Mac users can install it from brew: 80 | ```bash 81 | $ brew install closure-compiler 82 | ``` 83 | 84 | ### Set up the environment variable 85 | 86 | Setting up a `CLOSURE_PATH` environment variable is preferred because: 87 | 88 | * You don't have to specify the `closurePath` each time. 89 | * It makes it easy to use contributed externs. 90 | 91 | In case you're wondering, Closure Compiler utilizes continuous integration, so it's unlikely to break. 92 | 93 | If you create the `CLOSURE_PATH` environment variable, make sure to have it pointing to the `closure-compiler` dir created earlier (and not to the `build` subdirectory where the jar is located). 94 | 95 | #### Mac 96 | 97 | On Mac, when installed with brew, you can get the install path using: 98 | ```bash 99 | $ brew --prefix closure-compiler 100 | /usr/local/Cellar/closure-compiler/20120710 101 | ``` 102 | 103 | Just append `/libexec` to what you get. In this example, you should use the following path: 104 | ``` 105 | /usr/local/Cellar/closure-compiler/20120710/libexec/ 106 | ``` 107 | 108 | ### Minification report 109 | 110 | By default, a report file is generated next to the built file. 111 | 112 | You can specify the path and name where the report will be saved using the `reportFile` property. 113 | 114 | To deactivate report creation, set `noreport` to `true`. 115 | 116 | ### `js` property 117 | 118 | This task is a [multi task](https://github.com/cowboy/grunt/blob/master/docs/types_of_tasks.md), you can specify several targets. The task can minify many scripts at a time. 119 | 120 | `js` can be an array if you need to concatenate several files to a target. 121 | 122 | You can use Grunt `<%= somePropInitConfig.sub.sub.prop %>` or `*` based syntax to have the file list expanded: 123 | ```javascript 124 | grunt.initConfig({ 125 | 'closure-compiler': { 126 | frontend: { 127 | js: 'static/src/frontend.js', 128 | jsOutputFile: 'static/js/frontend.min.js', 129 | }, 130 | frontend_debug: { 131 | js: [ 132 | '<%= closure-compiler.frontend.js %>', 133 | // Will expand to 'static/src/frontend.js' 134 | 'static/src/debug.*.js' 135 | // Will expand to 'static/src/debug.api.js', 136 | // 'static/src/debug.console.js'... 137 | ], 138 | jsOutputFile: 'static/js/frontend.debug.js', 139 | options: { 140 | debug: true, 141 | formatting: 'PRETTY_PRINT' 142 | } 143 | }, 144 | } 145 | }); 146 | ``` 147 | 148 | ### `options` properties 149 | 150 | Properties in `options` are mapped to Closure Compiler command line. Just pass options as a map of option-value. 151 | 152 | If you need to pass the same options several times, make it an array. See `define` below: 153 | ```javascript 154 | grunt.initConfig({ 155 | 'closure-compiler': { 156 | frontend: { 157 | js: 'static/src/frontend.js', 158 | jsOutputFile: 'static/js/frontend.min.js', 159 | options: { 160 | compilation_level: 'ADVANCED_OPTIMIZATIONS', 161 | language_in: 'ECMASCRIPT5_STRICT', 162 | define: [ 163 | '"DEBUG=false"', 164 | '"UI_DELAY=500"' 165 | ], 166 | } 167 | } 168 | } 169 | }); 170 | ``` 171 | 172 | When defining externs, if you added the `CLOSURE_PATH` environment variable you can easily reference Closure Compiler builtin externs using `<%= process.env.CLOSURE_PATH %>` Grunt template: 173 | ```javascript 174 | grunt.initConfig({ 175 | 'closure-compiler': { 176 | frontend: { 177 | js: 'static/src/frontend.js', 178 | jsOutputFile: 'static/js/frontend.min.js', 179 | options: { 180 | externs: '<%= process.env.CLOSURE_PATH %>/contrib/externs/jquery-1.7.js', 181 | } 182 | } 183 | } 184 | }); 185 | ``` 186 | 187 | Otherwise, use the `<%= %>` Grunt template: 188 | ```javascript 189 | grunt.initConfig({ 190 | 'closure-compiler': { 191 | frontend: { 192 | closurePath: '/src/to/closure-compiler', 193 | js: 'static/src/frontend.js', 194 | jsOutputFile: 'static/js/frontend.min.js', 195 | options: { 196 | externs: '<%= closure-compiler.frontend.closurePath %>/contrib/externs/jquery-1.7.js' 197 | } 198 | } 199 | } 200 | }); 201 | ``` 202 | 203 | To specify boolean options (such as `process_common_js_modules`, i.e. no value are required), set its value to `undefined` (or `null`): 204 | ```javascript 205 | grunt.initConfig({ 206 | 'closure-compiler': { 207 | frontend: { 208 | js: 'static/src/frontend.js', 209 | jsOutputFile: 'static/js/frontend.min.js', 210 | options: { 211 | process_common_js_modules: undefined, 212 | common_js_entry_module: 'exports' 213 | } 214 | } 215 | } 216 | }); 217 | ``` 218 | 219 | For automatic resolving common js modules you can use 220 | ```javascript 221 | grunt.initConfig({ 222 | 'closure-compiler': { 223 | frontend: { 224 | cwd: 'static/src/' 225 | js: '*.js', 226 | jsOutputFile: 'static/js/frontend.min.js', 227 | options: { 228 | common_js_entry_module: 'frontend.js', 229 | transform_amd_modules: undefined, 230 | process_common_js_modules: undefined 231 | } 232 | } 233 | } 234 | }); 235 | ``` 236 | 237 | 238 | ## Note 239 | 240 | grunt-closure-compiler initial development was founded by [Dijiwan](http://www.dijiwan.com/). 241 | 242 | The directory structure was inspired by [grunt-less](https://github.com/jharding/grunt-less), a Grunt task for Less. 243 | 244 | ## License 245 | 246 | Copyright (c) 2013 Guillaume Marty 247 | 248 | Permission is hereby granted, free of charge, to any person 249 | obtaining a copy of this software and associated documentation 250 | files (the "Software"), to deal in the Software without 251 | restriction, including without limitation the rights to use, 252 | copy, modify, merge, publish, distribute, sublicense, and/or sell 253 | copies of the Software, and to permit persons to whom the 254 | Software is furnished to do so, subject to the following 255 | conditions: 256 | 257 | The above copyright notice and this permission notice shall be 258 | included in all copies or substantial portions of the Software. 259 | 260 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 261 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 262 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 263 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 264 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 265 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 266 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 267 | OTHER DEALINGS IN THE SOFTWARE. 268 | -------------------------------------------------------------------------------- /bin/grunt-closure-compiler: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | require('grunt').npmTasks('grunt-closure-compiler').cli(); 3 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "grunt-closure-compiler", 3 | "description": "A Grunt task for Closure Compiler.", 4 | "version": "0.0.21", 5 | "homepage": "https://github.com/gmarty/grunt-closure-compiler", 6 | "author": { 7 | "name": "Guillaume Marty", 8 | "email": "edo999@gmail.com" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git://github.com/gmarty/grunt-closure-compiler.git" 13 | }, 14 | "bugs": { 15 | "url": "https://github.com/gmarty/grunt-closure-compiler/issues" 16 | }, 17 | "licenses": [ 18 | { 19 | "type": "MIT", 20 | "url": "https://github.com/gmarty/grunt-closure-compiler/blob/master/LICENSE-MIT" 21 | } 22 | ], 23 | "main": "grunt.js", 24 | "bin": { 25 | "grunt-closure-compiler": "bin/grunt-closure-compiler" 26 | }, 27 | "engines": { 28 | "node": ">=0.6.0" 29 | }, 30 | "scripts": { 31 | "test": "grunt test" 32 | }, 33 | "dependencies": { 34 | "grunt": "~0.4.0" 35 | }, 36 | "keywords": [ 37 | "Closure Compiler", 38 | "Minification", 39 | "Performance", 40 | "gruntplugin" 41 | ] 42 | } 43 | -------------------------------------------------------------------------------- /tasks/closure-compiler.js: -------------------------------------------------------------------------------- 1 | module.exports = function(grunt) { 2 | 3 | 'use strict'; 4 | 5 | var exec = require('child_process').exec, 6 | fs = require('fs'), 7 | path = require('path'), 8 | gzip = require('zlib').gzip; 9 | 10 | // ========================================================================== 11 | // TASKS 12 | // ========================================================================== 13 | 14 | grunt.registerMultiTask('closure-compiler', 'Minify JS files using Closure Compiler.', function() { 15 | 16 | var closurePath = '', 17 | reportFile = '', 18 | data = this.data, 19 | done = this.async(); 20 | 21 | // Check for closure path. 22 | if (data.closurePath) { 23 | closurePath = data.closurePath; 24 | } else if (process.env.CLOSURE_PATH) { 25 | closurePath = process.env.CLOSURE_PATH; 26 | } else { 27 | grunt.log.error('' + 28 | '/!\\'.red + 29 | ' Set an environment variable called ' + 30 | 'CLOSURE_PATH'.red + ' or the build parameter' + 'closurePath'.red + 31 | ' and\nmake it point to your root install of Closure Compiler.' + 32 | '\n'); 33 | return false; 34 | } 35 | 36 | var command = 'java -jar "' + closurePath + '/build/compiler.jar"'; 37 | 38 | data.cwd = data.cwd || './'; 39 | 40 | data.js = grunt.file.expand({cwd: data.cwd}, data.js); 41 | 42 | // Sanitize options passed. 43 | if (!data.js.length) { 44 | // This task requires a minima an input file. 45 | grunt.warn('Missing js property.'); 46 | return false; 47 | } 48 | 49 | // Build command line. 50 | command += ' --js "' + data.js.join('" --js "') + '"'; 51 | 52 | if (data.jsOutputFile) { 53 | if (!grunt.file.isPathAbsolute(data.jsOutputFile)) { 54 | data.jsOutputFile = path.resolve('./') + '/' + data.jsOutputFile; 55 | } 56 | command += ' --js_output_file "' + data.jsOutputFile + '"'; 57 | reportFile = data.reportFile || data.jsOutputFile + '.report.txt'; 58 | } 59 | 60 | if (data.externs) { 61 | data.externs = grunt.file.expand(data.externs); 62 | command += ' --externs ' + data.externs.join(' --externs '); 63 | 64 | if (!data.externs.length) { 65 | delete data.externs; 66 | } 67 | } 68 | 69 | if (data.options.externs) { 70 | data.options.externs = grunt.file.expand(data.options.externs); 71 | 72 | if (!data.options.externs.length) { 73 | delete data.options.externs; 74 | } 75 | } 76 | 77 | for (var directive in data.options) { 78 | if (Array.isArray(data.options[directive])) { 79 | command += ' --' + directive + ' ' + data.options[directive].join(' --' + directive + ' '); 80 | } else if (data.options[directive] === undefined || data.options[directive] === null) { 81 | command += ' --' + directive; 82 | } else { 83 | command += ' --' + directive + ' "' + String(data.options[directive]) + '"'; 84 | } 85 | } 86 | 87 | // because closure compiler does not create dirs. 88 | grunt.file.write(data.jsOutputFile, ''); 89 | 90 | // Minify WebGraph class. 91 | exec(command, { maxBuffer: data.maxBuffer * 1024, cwd: data.cwd }, function(err, stdout, stderr) { 92 | if (err) { 93 | grunt.warn(err); 94 | done(false); 95 | } 96 | 97 | if (stdout) { 98 | grunt.log.writeln(stdout); 99 | } 100 | 101 | // If OK, calculate gzipped file size. 102 | if (reportFile.length) { 103 | var min = fs.readFileSync(data.jsOutputFile, 'utf8'); 104 | min_info(min, function(err) { 105 | if (err) { 106 | grunt.warn(err); 107 | done(false); 108 | } 109 | 110 | if (data.noreport) { 111 | done(); 112 | } else { 113 | // Write compile report to a file. 114 | fs.writeFile(reportFile, stderr, function(err) { 115 | if (err) { 116 | grunt.warn(err); 117 | done(false); 118 | } 119 | 120 | grunt.log.writeln('A report is saved in ' + reportFile + '.'); 121 | done(); 122 | }); 123 | } 124 | 125 | }); 126 | } else { 127 | if (data.report) { 128 | grunt.log.error(stderr); 129 | } 130 | done(); 131 | } 132 | 133 | }); 134 | 135 | }); 136 | 137 | // Output some size info about a file. 138 | function min_info(min, onComplete) { 139 | gzip(min, function(err, buffer) { 140 | if (err) { 141 | onComplete.call(this, err); 142 | } 143 | 144 | var gzipSize = buffer.toString().length; 145 | grunt.log.writeln('Compressed size: ' + String((gzipSize / 1024).toFixed(2)).green + ' kb gzipped (' + String(gzipSize).green + ' bytes).'); 146 | 147 | onComplete.call(this, null); 148 | }); 149 | } 150 | 151 | }; 152 | -------------------------------------------------------------------------------- /test/closure-compiler_test.js: -------------------------------------------------------------------------------- 1 | var grunt = require('grunt'); 2 | 3 | /* 4 | ======== A Handy Little Nodeunit Reference ======== 5 | https://github.com/caolan/nodeunit 6 | 7 | Test methods: 8 | test.expect(numAssertions) 9 | test.done() 10 | Test assertions: 11 | test.ok(value, [message]) 12 | test.equal(actual, expected, [message]) 13 | test.notEqual(actual, expected, [message]) 14 | test.deepEqual(actual, expected, [message]) 15 | test.notDeepEqual(actual, expected, [message]) 16 | test.strictEqual(actual, expected, [message]) 17 | test.notStrictEqual(actual, expected, [message]) 18 | test.throws(block, [error], [message]) 19 | test.doesNotThrow(block, [error], [message]) 20 | test.ifError(value) 21 | */ 22 | 23 | exports['less'] = { 24 | setUp: function(done) { 25 | // setup here 26 | done(); 27 | }, 28 | 'helper': function(test) { 29 | test.expect(1); 30 | // tests here 31 | test.ok(true); // good news, true === true 32 | test.done(); 33 | } 34 | }; 35 | --------------------------------------------------------------------------------