google-closure-compiler
Check, compile, optimize and compress Javascript with Closure-Compiler
This repository tracks issues related to the publication to npmjs.org and associated plugins.
Any bugs not related to the plugins themselves should be reported to the
main repository.
Getting Started
Closure-compiler requires java to be installed and in the path.
If you are new to Closure-Compiler, make
sure to read and understand the
compilation levels as
the compiler works very differently depending on the compilation level selected.
For help or questions with the compiler, the best resource is
Stack Overflow. Posts there
are monitored by multiple Closure Compiler team members.
You may also post in the
Closure Compiler Discuss Google Group.
Please don't cross post to both Stackoverflow and Closure Compiler Discuss.
Usage
The compiler package now includes build tool plugins for Grunt and
Gulp.
Installation
npm install --save google-closure-compiler
Configuration
The compiler has a large number of flags. The best documentation for the flags can be found by
running the --help
command of the compiler.jar found inside the
node_modules/google-closure-compiler
folder:
java -jar compiler.jar --help
Specifying Options
Both the grunt and gulp tasks take options objects. The option parameters map directly to the
compiler flags without the leading '--' characters.
Values are either strings or booleans. Options which have multiple values can be arrays.
{
js: ['/file-one.js', '/file-two.js'],
compilation_level: 'ADVANCED',
js_output_file: 'out.js',
debug: true
}
For advanced usages, the options may be specified as an array of strings. These values include
the "--" characters and are directly passed to the compiler in the order specified:
[
'--js', '/file-one.js',
'--js', '/file-two.js',
'--compilation_level', 'ADVANCED',
'--js_output_file', 'out.js',
'--debug'
]
When an array of flags is passed, the input files should not be specified via the build tools, but
rather as compilation flags directly.
Some shells (particularly windows) try to do expansion on globs rather than passing the string on
to the compiler. To prevent this it is necessary to quote certain arguments:
{
js: '"my/quoted/glob/**.js"',
compilation_level: 'ADVANCED',
js_output_file: 'out.js',
debug: true
}
Using the Grunt Task
Include the plugin in your Gruntfile.js:
require('google-closure-compiler').grunt(grunt);
Task targets, files and options may be specified according to the grunt
Configuring tasks guide.
Basic Configuration Example:
require('google-closure-compiler').grunt(grunt);
grunt.initConfig({
'closure-compiler': {
my_target: {
files: {
'dest/output.min.js': ['src/js/**/*.js']
},
options: {
compilation_level: 'SIMPLE',
language_in: 'ECMASCRIPT5_STRICT',
create_source_map: 'dest/output.min.js.map',
output_wrapper: '(function(){\n%output%\n}).call(this)\n//# sourceMappingURL=output.min.js.map'
}
}
}
});
Closure Library Example:
var compilerPackage = require('google-closure-compiler');
compilerPackage.grunt(grunt);
grunt.initConfig({
'closure-compiler': {
my_target: {
files: {
'dest/output.min.js': ['src/js/**/*.js']
},
options: {
js: '/node_modules/google-closure-library/**.js'
externs: compilerPackage.compiler.CONTRIB_PATH + '/externs/jquery-1.9.js',
compilation_level: 'SIMPLE',
manage_closure_dependencies: true,
language_in: 'ECMASCRIPT5_STRICT',
create_source_map: 'dest/output.min.js.map',
output_wrapper: '(function(){\n%output%\n}).call(this)\n//# sourceMappingURL=output.min.js.map'
}
}
}
});
Advanced Usage with Arguments Array:
grunt.initConfig({
'closure-compiler': {
my_target: {
options: {
args: [
'--js', '/file-one.js',
'--js', '/file-two.js',
'--compilation_level', 'ADVANCED',
'--js_output_file', 'out.js',
'--debug'
]
}
}
}
});
Windows Path Length Limitations
Windows command shells have a maximum length for a command. This is surprisingly easy to hit when
you allow the build tools to expand globs for large sets of source files for the compiler.
This can be avoided by specifying the input globs as compiler arguments via the --js
flag and
let it expand the files. You can even mix these techniques. Files specified via js
option will
be included first.
'closure-compiler': {
my_target: {
files: {
'dest/out.min.js': ['src/**/*.js']
},
options: {
js: 'node_modules/google-closure-library/**.js'
}
}
}
Note the globbing syntax used by the compiler is slightly different than the standard node syntax.
A flagfile can also be used to workaround this issue.
Using the Gulp Plugin
The gulp plugin supports piping multiple files through the compiler.
Options are a direct match to the compiler flags without the leading "--".
Basic Configuration Example:
var closureCompiler = require('google-closure-compiler').gulp();
gulp.task('js-compile', function () {
return gulp.src('./src/js/**/*.js', {base: './'})
.pipe(closureCompiler({
compilation_level: 'SIMPLE',
warning_level: 'VERBOSE',
language_in: 'ECMASCRIPT6_STRICT',
language_out: 'ECMASCRIPT5_STRICT',
output_wrapper: '(function(){\n%output%\n}).call(this)',
js_output_file: 'output.min.js'
}))
.pipe(gulp.dest('./dist/js'));
});
Use without gulp.src
Gulp files are all read into memory, transformed into a JSON stream, and piped through the
compiler. With large source sets this may require a large amount of memory.
Closure-compiler can natively expand file globs which will greatly alleviate this issue.
var compilerPackage = require('google-closure-compiler');
var closureCompiler = compilerPackage.gulp();
gulp.task('js-compile', function () {
return closureCompiler({
js: './src/js/**.js',
externs: compilerPackage.compiler.CONTRIB_PATH + '/externs/jquery-1.9.js',
compilation_level: 'SIMPLE',
warning_level: 'VERBOSE',
language_in: 'ECMASCRIPT6_STRICT',
language_out: 'ECMASCRIPT5_STRICT',
output_wrapper: '(function(){\n%output%\n}).call(this)',
js_output_file: 'output.min.js'
})
.pipe(gulp.dest('./dist/js'));
});
gulp.src base option
Gulp attempts to set the base of a glob from the point of the first wildcard. This isn't always
what is desired. Users can specify the { base: 'path' } option to gulp.src
calls to override
this behavior.
Advanced Usage with Arguments Array:
var closureCompiler = require('google-closure-compiler').gulp();
gulp.task('js-compile', function () {
return closureCompiler([
'--js', '/file-one.js',
'--js', '/file-two.js',
'--compilation_level', 'ADVANCED',
'--js_output_file', 'out.js',
'--debug'
])
.pipe(gulp.dest('./dist/js'));
});
Gulp Sourcemaps
The gulp plugin supports gulp sourcemaps.
var closureCompiler = require('google-closure-compiler').gulp();
var sourcemaps = require('gulp-sourcemaps');
gulp.task('js-compile', function () {
return gulp.src('./src/js/**/*.js', {base: './'})
.pipe(sourcemaps.init())
.pipe(closureCompiler({
compilation_level: 'SIMPLE',
warning_level: 'VERBOSE',
language_in: 'ECMASCRIPT6_STRICT',
language_out: 'ECMASCRIPT5_STRICT',
output_wrapper: '(function(){\n%output%\n}).call(this)',
js_output_file: 'output.min.js'
}))
.pipe(souremaps.write('/'))
.pipe(gulp.dest('./dist/js'));
});
Plugin Authors and Native Node Usage
A low-level node class is included to facilitate spawning the compiler jar as a process from Node.
In addition, it exposes a static property with the path to the compiler jar file.
var ClosureCompiler = require('google-closure-compiler').compiler;
console.log(ClosureCompiler.COMPILER_PATH);
console.log(ClosureCompiler.CONTRIB_PATH);
var closureCompiler = new ClosureCompiler({
js: 'file-one.js',
compilation_level: 'ADVANCED'
});
var compilerProcess = closureCompiler.run(function(exitCode, stdOut, stdErr) {
});
License
Copyright 2015 The Closure Compiler Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Version History
Closure Compiler release notes can be found on the
main repository wiki.