• Stars
    star
    181
  • Rank 212,110 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Grunt task for Closure Compiler

grunt-closure-compiler

A Grunt task for Closure Compiler.

Getting Started

First you need to download a build of Closure Compiler or build it from the source (see details below).

Optionally, you can 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:

$ npm install grunt-closure-compiler

Then register the task by adding the following line to your grunt.js gruntfile:

grunt.loadNpmTasks('grunt-closure-compiler');

Then you can minify JavaScript calling:

grunt.initConfig({
  'closure-compiler': {
    frontend: {
      closurePath: '/src/to/closure-compiler',
      js: 'static/src/frontend.js',
      jsOutputFile: 'static/js/frontend.min.js',
      maxBuffer: 500,
      options: {
        compilation_level: 'ADVANCED_OPTIMIZATIONS',
        language_in: 'ECMASCRIPT5_STRICT'
      }
    }
  }
});

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

js property is always required.

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.

maxBuffer property

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)

Use cwd to specify the working directory where closure compiler is called. Useful in when you want to process common js modules.

Optionally, several parameters can be passed to options object.

Documentation

Closure Compiler installation from source

Install dependencies:

$ sudo apt-get install git ant openjdk-7-jdk

Then checkout the source from Git and build:

$ git clone https://code.google.com/p/closure-compiler/
$ cd closure-compiler
$ ant

To refresh your build, simply call:

$ git pull
$ ant clean
$ ant

Mac

Mac users can install it from brew:

$ brew install closure-compiler

Set up the environment variable

Setting up a CLOSURE_PATH environment variable is preferred because:

  • You don't have to specify the closurePath each time.
  • It makes it easy to use contributed externs.

In case you're wondering, Closure Compiler utilizes continuous integration, so it's unlikely to break.

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

Mac

On Mac, when installed with brew, you can get the install path using:

$ brew --prefix closure-compiler
/usr/local/Cellar/closure-compiler/20120710

Just append /libexec to what you get. In this example, you should use the following path:

/usr/local/Cellar/closure-compiler/20120710/libexec/

Minification report

By default, a report file is generated next to the built file.

You can specify the path and name where the report will be saved using the reportFile property.

To deactivate report creation, set noreport to true.

js property

This task is a multi task, 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 <%= somePropInitConfig.sub.sub.prop %> or * based syntax to have the file list expanded:

grunt.initConfig({
  'closure-compiler': {
    frontend: {
      js: 'static/src/frontend.js',
      jsOutputFile: 'static/js/frontend.min.js',
    },
    frontend_debug: {
      js: [
        '<%= closure-compiler.frontend.js %>',
        // Will expand to 'static/src/frontend.js'
        'static/src/debug.*.js'
        // Will expand to 'static/src/debug.api.js',
        //   'static/src/debug.console.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. See define below:

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',
        define: [
          '"DEBUG=false"',
          '"UI_DELAY=500"'
        ],
      }
    }
  }
});

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:

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',
      }
    }
  }
});

Otherwise, use the <%= %> Grunt template:

grunt.initConfig({
  'closure-compiler': {
    frontend: {
      closurePath: '/src/to/closure-compiler',
      js: 'static/src/frontend.js',
      jsOutputFile: 'static/js/frontend.min.js',
      options: {
        externs: '<%= closure-compiler.frontend.closurePath %>/contrib/externs/jquery-1.7.js'
      }
    }
  }
});

To specify boolean options (such as process_common_js_modules, i.e. no value are required), set its value to undefined (or null):

grunt.initConfig({
  'closure-compiler': {
    frontend: {
      js: 'static/src/frontend.js',
      jsOutputFile: 'static/js/frontend.min.js',
      options: {
        process_common_js_modules: undefined,
        common_js_entry_module: 'exports'
      }
    }
  }
});

For automatic resolving common js modules you can use

grunt.initConfig({
  'closure-compiler': {
    frontend: {
      cwd: 'static/src/'
      js: '*.js',
      jsOutputFile: 'static/js/frontend.min.js',
      options: {
        common_js_entry_module: 'frontend.js',
        transform_amd_modules: undefined,
        process_common_js_modules: undefined
      }
    }
  }
});

Note

grunt-closure-compiler initial development was founded by Dijiwan.

The directory structure was inspired by grunt-less, a Grunt task for Less.

License

Copyright (c) 2013 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.

More Repositories

1

DVD.js

Playing DVD in JavaScript for the sake of interoperability
TypeScript
163
star
2

jsSMS

JavaScript Sega Master System & Game Gear dynamic recompiling emulator.
JavaScript
145
star
3

xgettext

Extracts translatable strings from source. Identical to xgettext but for template languages.
JavaScript
77
star
4

x-video

An enhanced video player for modern browsers with a consistent UI and extra features!
JavaScript
61
star
5

hough-transform-js

A simple script to perform Hough transform in JavaScript given a canvas.
JavaScript
56
star
6

web-workers-benchmark

Benchmark various aspects of Web workers.
JavaScript
44
star
7

SCD.js

Pixel based video scene change detection in JavaScript.
JavaScript
35
star
8

jsMSX

The first MSX emulator 100% written in Javascript (unofficial mirror)
JavaScript
12
star
9

image8bit

jQuery plugin to give images the aspect of good old 8-bit graphics.
JavaScript
11
star
10

all-saints-ar

An AR experiment using computer vision in the browser.
JavaScript
10
star
11

vsop87

🪐 A fast implementation of the VSOP87 theory in JavaScript.
JavaScript
8
star
12

wifi-columns

Play Columns with your friends locally, no Internet connection required!
JavaScript
8
star
13

Mozilla-Dev-Derby-2011-07

My submission to the Mozilla Dev Derby of July 2011
JavaScript
7
star
14

Editor.js

Web-based video editor in JavaScript. Requires Firefogg extension.
JavaScript
5
star
15

c2js

A collection of regexp to speed porting of C applications to JavaScript.
JavaScript
5
star
16

nesCompiler

Please welcome the first webifier of NES games!
TypeScript
4
star
17

photo-browse

Display photos of Hello Kitty, one thread at a time
JavaScript
4
star
18

syntactic-machine-translation

Hacking a quick and dirty syntactic machine translation for English and Japanese.
JavaScript
3
star
19

Mozilla-Dev-Derby-2011-08

My submission to the Mozilla Dev Derby of August 2011
JavaScript
3
star
20

wordlist-generator

Generate words list XML files suitable for IME dictionaries.
Python
3
star
21

scumm-nes

An app to explore and modify the game Maniac Mansion on NES 🎮.
JavaScript
2
star
22

blog

A blog by G.C. Marty
Astro
2
star
23

VR-diary

VR Diary (VRにっき)
2
star
24

fxos-physical-web

Walk up and use anything
JavaScript
2
star
25

vr-landscapes

Imaginary virtual landscapes
HTML
1
star
26

jsconf-2014-talk-play-dvd-in-js

Slides for my talk at JS Conf 2014 - Playing DVD in JavaScript for the sake of interoperability
1
star
27

2015-06-MLOC.js

Slides for crash course in compile-to-the-web
1
star
28

bluetooth-demo

Experiment with Bluetooth
JavaScript
1
star
29

media-experiments

1
star
30

fxos-homescreen-template

A home screen template for Firefox OS with the bare minimum structure
JavaScript
1
star
31

pluto99

♇ A fast implementation of the so-called pluto99 theory in JavaScript.
JavaScript
1
star
32

latin-editor

An editor to ease the reading and understanding of texts written in classical Latin.
Python
1
star
33

2018-05-Unusual-Career-Paths

Slides for a talk about unusual career paths
JavaScript
1
star
34

SMSStaRec

[DEPRECATED] An attempt to compile statically SMS/GG ROMs to pure JavaScript.
JavaScript
1
star