Permalink
Browse files

Initial commit

  • Loading branch information...
1 parent a74dad7 commit a16b2ef4c9683fb2323ab7d557e4b1f07f6d9166 @gmarty committed May 29, 2012
Showing with 359 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +1 −0 .npmignore
  3. +22 −0 LICENSE-MIT
  4. 0 README
  5. +134 −0 README.md
  6. +2 −0 bin/grunt-closure-compiler
  7. +37 −0 grunt.js
  8. +44 −0 package.json
  9. +117 −0 tasks/closure-compiler.js
View
@@ -0,0 +1,2 @@
+.idea
+node_modules
View
@@ -0,0 +1 @@
+node_modules
View
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Guillaume Marty
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
View
0 README
No changes.
View
134 README.md
@@ -0,0 +1,134 @@
+# grunt-closure-compiler
+
+A Grunt task for Closure Compiler.
+
+## Getting Started
+
+First you need to build Closure Compiler from the source. Set up an environment variable called `CLOSURE_PATH` that points to your Closure Compiler dir (see details below).
+
+Install this module on your project's [grunt.js gruntfile][getting_started] with: `npm install grunt-closure-compiler`
+
+Then register the task by adding the following line to your `grunt.js` gruntfile:
+```javascript
+grunt.loadNpmTasks('grunt-closure-compiler');
+```
+
+Then you can minify JavaScript calling:
+```javascript
+grunt.initConfig({
+ 'closure-compiler': {
+ frontend: {
+ js: 'static/src/frontend.js',
+ jsOutputFile: 'static/js/frontend.min.js',
+ options: {
+ compilation_level: 'ADVANCED_OPTIMIZATIONS',
+ language_in: 'ECMASCRIPT5_STRICT'
+ }
+ }
+ }
+});
+```
+
+`js` is the only property required.
+
+If `jsOutputFile` property is set, the script will be minified and save to the file specified. Otherwise it will be output to the command line.
+
+Optionally, several parameters can be passed to `options` object.
+
+## Documentation
+
+### Closure Compiler installation
+
+Install dependencies:
+```
+sudo apt-get install svn ant openjdk-6-jdk
+```
+
+Then checkout the source from SVN and build:
+```
+svn checkout http://closure-compiler.googlecode.com/svn/trunk/ closure-compiler
+cd closure-compiler
+ant
+```
+
+To refresh your build, simply call:
+```
+svn up
+ant clean
+ant
+```
+
+When creating the `CLOSURE_PATH` environment vairable, make sure to have it point to the `closure-compiler` dir created earlier (and not to the `build` subdirectory where the jar is located).
+
+This method is preferred because doing so make it possible to use easily contributed externs. In case you're wondering, Closure Compiler utilizes continuous integration, so it's unlikely to break.
+
+### `js` property
+
+This task is a [multi task][types_of_tasks], you can specify several targets. The task can minify many scripts at a time.
+
+`js` can be an array if you need to concatenate several files to a target. You can use Grunt `<config:...>` or `*` based syntax to have the file list expanded:
+```javascript
+grunt.initConfig({
+ 'closure-compiler': {
+ frontend: {
+ js: 'static/src/frontend.js',
+ jsOutputFile: 'static/js/frontend.min.js',
+ },
+ frontend_debug: {
+ js: [
+ '<config:closure-compiler.frontend.js>',
+ 'static/src/debug.*.js'
+ ],
+ jsOutputFile: 'static/js/frontend.debug.js',
+ options: {
+ debug: true,
+ formatting: 'PRETTY_PRINT'
+ }
+ },
+ }
+});
+```
+
+### `options` properties
+
+Properties in `options` are mapped to Closure Compiler command line. Just pass options as a map of option-value.
+
+If you need to pass the same options several times, make it an array:
+```javascript
+grunt.initConfig({
+ 'closure-compiler': {
+ frontend: {
+ js: 'static/src/frontend.js',
+ jsOutputFile: 'static/js/frontend.min.js',
+ options: {
+ externs: [
+ 'framework.js',
+ 'service_api.js',
+ 'new_shim.js'
+ ]
+ }
+ }
+ }
+});
+```
+
+When defining externs, you can easily reference Closure Compiler builtin externs using <%= process.env.CLOSURE_PATH %> Grunt template:
+```javascript
+grunt.initConfig({
+ 'closure-compiler': {
+ frontend: {
+ js: 'static/src/frontend.js',
+ jsOutputFile: 'static/js/frontend.min.js',
+ options: {
+ externs: '<%= process.env.CLOSURE_PATH %>/contrib/externs/jquery-1.7.js'
+ }
+ }
+ }
+});
+```
+
+## Note
+
+grunt-closure-compiler development was fonded by [Dijiwan](http://www.dijiwan.com/). It is used on a daily basis to minify our frontend JavaScript.
+
+The directory structure was inspired by [grunt-less](https://github.com/jharding/grunt-less), a Grunt task for Less.
@@ -0,0 +1,2 @@
+#!/usr/bin/env node
+require('grunt').npmTasks('grunt-closure-compiler').cli();
View
@@ -0,0 +1,37 @@
+module.exports = function(grunt) {
+ grunt.initConfig({
+ test: {
+ files: ['test/**/*.js']
+ },
+ lint: {
+ files: ['grunt.js', 'tasks/**/*.js', 'test/**/*.js']
+ },
+ watch: {
+ files: '<config:lint.files>',
+ tasks: 'default'
+ },
+ jshint: {
+ options: {
+ curly: true,
+ eqeqeq: true,
+ immed: true,
+ latedef: true,
+ newcap: true,
+ noarg: true,
+ sub: true,
+ undef: true,
+ boss: true,
+ eqnull: true,
+ node: true,
+ es5: true
+ },
+ globals: {}
+ }
+ });
+
+ // Load local tasks.
+ grunt.loadTasks('tasks');
+
+ // Default task.
+ grunt.registerTask('default', 'lint test');
+};
View
@@ -0,0 +1,44 @@
+{
+ "name": "grunt-closure-compiler",
+ "description": "A Grunt task for Closure Compiler.",
+ "version": "0.0.1",
+ "homepage": "https://github.com/gmarty/grunt-closure-compiler",
+ "author": {
+ "name": "Guillaume Marty",
+ "email": "edo999@gmail.com"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/gmarty/grunt-closure-compiler.git"
+ },
+ "bugs": {
+ "url": "https://github.com/gmarty/grunt-closure-compiler/issues"
+ },
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "https://github.com/gmarty/grunt-closure-compiler/blob/master/LICENSE-MIT"
+ }
+ ],
+ "main": "grunt.js",
+ "bin": {
+ "grunt-closure-compiler": "bin/grunt-closure-compiler"
+ },
+ "engines": {
+ "node": ">=0.6.0"
+ },
+ "scripts": {
+ "test": "grunt test"
+ },
+ "dependencies": {
+ "grunt": "~0.3.7"
+ },
+ "devDependencies": {
+ "grunt": "~0.3.7"
+ },
+ "keywords": [
+ "Closure Compiler",
+ "Minification",
+ "Performance"
+ ]
+}
View
@@ -0,0 +1,117 @@
+module.exports = function(grunt) {
+
+ var exec = require('child_process').exec,
+ fs = require('fs'),
+ gzip = require('zlib').gzip;
+
+ // ==========================================================================
+ // TASKS
+ // ==========================================================================
+
+ grunt.registerMultiTask('closure-compiler', 'Minify JS files using Closure Compiler.', function() {
+
+ // Check for env var.
+ if (process.env.CLOSURE_PATH === undefined) {
+ grunt.log.error('' +
+ '/!\\'.red +
+ ' Set an environment variable called ' +
+ 'CLOSURE_PATH'.red +
+ ' and\nmake it point to your root install of Closure Compiler.' +
+ '\n');
+
+ // Return an error and stop grunt.
+ return false;
+ }
+
+ var closureCompilerDir = process.env.CLOSURE_PATH,
+ command = 'java -jar ' + closureCompilerDir + '/build/compiler.jar',
+ reportFile = '',
+ data = this.data,
+ done = this.async();
+
+ data.js = grunt.file.expandFiles(data.js);
+ data.externs = grunt.file.expandFiles(data.externs);
+
+ // Sanitize options passed.
+ if (!data.js.length) {
+ // This task requires a minima an input file.
+ grunt.warn('Missing js property.');
+ return false;
+ }
+
+ // Build command line.
+ command += ' --js ' + data.js.join(' --js ');
+
+ if (data.jsOutputFile) {
+ command += ' --js_output_file ' + data.jsOutputFile;
+ reportFile = data.jsOutputFile + '.report.txt';
+ }
+
+ data.externs.forEach(function(file) {
+ command += ' --externs ' + file;
+ });
+
+ for (var directive in data.options) {
+ if (Array.isArray(data.options)) {
+ command += ' --' + directive + ' ' + data.js.join(' --' + directive + ' ');
+ } else {
+ command += ' --' + directive + ' ' + String(data.options[directive]);
+ }
+ }
+
+ // Minify WebGraph class.
+ exec(command, function(err, stdout, stderr) {
+ if (err) {
+ grunt.warn(err);
+ done(false);
+ }
+
+ if (stdout) {
+ grunt.log.writeln(stdout);
+ }
+
+ // If OK, calculate gzipped file size.
+ if (data.jsOutputFile) {
+ var min = fs.readFileSync(data.jsOutputFile, 'utf8');
+ grunt.helper('min_info', min, function(err) {
+ if (err) {
+ grunt.warn(err);
+ done(false);
+ }
+
+ // Write compile report to a file.
+ fs.writeFile(reportFile, stderr, function(err) {
+ if (err) {
+ grunt.warn(err);
+ done(false);
+ }
+
+ grunt.log.writeln('A report is saved in ' + reportFile + '.');
+ done();
+ });
+
+ });
+ }
+
+ });
+
+ });
+
+ // ==========================================================================
+ // HELPERS
+ // ==========================================================================
+ // Output some size info about a file.
+ grunt.registerHelper('min_info', function(min, onComplete) {
+ gzip(min, function(err, buffer) {
+ if (err) {
+ onComplete.call(this, err);
+ }
+
+ var gzipSize = buffer.toString().length;
+ grunt.log.writeln('Compressed size: ' + String((gzipSize / 1024).toFixed(2)).green + ' kb gzipped (' + String(gzipSize).green + ' bytes).');
+
+ onComplete.call(this, null);
+ });
+ });
+
+};

0 comments on commit a16b2ef

Please sign in to comment.