Google Closure Tools for grunt:
- Compiler Compile your JS code using the powerfull google closure compiler
- Builder Concatenate your JS codebase to a single file, optionally also compile it
- DepsWriter Calculate dependencies of your JS files and generate
deps.js
Install the module with: npm install grunt-closure-tools
npm install grunt-closure-tools
Then register the task by adding the following line to your grunt.js
:
grunt.loadNpmTasks('grunt-closure-tools');
All three tasks (compiler, builder and depswriter) are multitasks, so you can specify several targets depending on the complexity of your project.
The Closure Compiler task has two requirements.
closureCompiler
The location of the compiler. Find the latest compiler.jar file here.js
The js files you want to compile.
You can fully configure how the compiler will behave, by setting directives in the options
. Every key will be used as a directive for the compiler.
You can use grunt file syntax (<config:...>
or path/**/*.js
) for the js
directive and for declaring externs, set in options.externs
.
Read more about the closure compiler here.
closureCompiler: {
// any name that describes your task
targerName: {
// [Required] Path to closure compiler
closureCompiler: 'path/to/closure/compiler.jar',
// [Required] Target files to compile. Can be a string, an array of strings
// or grunt file syntax (<config:...>, *)
js: 'path/to/file.js',
// [Optional] set an output file
output_file: 'path/to/compiled_file.js',
// [Optional] Set Closure Compiler Directives here
options: {
/**
* Keys will be used as directives for the compiler
* values can be strings or arrays.
* If no value is required use null
*
* The directive 'externs' is treated as a special case
* allowing a grunt file syntax (<config:...>, *)
*
* Following are some directive samples...
*/
compilation_level: 'ADVANCED_OPTIMIZATIONS',
externs: ['path/to/file.js', '/source/**/*.js'],
define: ["'goog.DEBUG=false'"],
warning_level: 'verbose',
jscomp_off: ['checkTypes', 'fileoverviewTags'],
summary_detail_level: 3,
output_wrapper: '(function(){%output%}).call(this);'
}
}
}
The Closure Builder task has 2 required directives:
- A way to find the
closurebuilder.py
file. One of the following two directives are required: *closureLibraryPath
A path to the Google Closure Library. From there we can infer the location of the closurebuilder.py file *builder
Directly reference the closurebuilder.py file - An input method must be defined. One of the following two directives is required:
*
inputs
String, array or grunt file syntax to define build targets *namespaces
String or array to define namespaces to build
The builder has the ability to compile on-the-fly the built files. To enable this option you need to set the option compile
to boolean true
and then set the location of the compiler.jar
file via the compiler
directive.
You can set all the compiler directives in the compiler_options
object. The js
directive is no longer required, as the builder takes care of that. The compiler directives follow the same logic as the ones in the Closure Compiler multitask described above.
Read more about the closure builder in this link.
closureBuilder: {
// any name that describes your operation
targetName: {
// [Required] To find the builder executable we need either the path to
// closure library or directly the filepath to the builder:
closureLibraryPath: 'path/to/closure-library', // path to closure library
builder: 'path/to/closurebuilder.py', // filepath to builder
// [Required] One of the two following options are required:
inputs: 'string|Array', // input files (can just be the entry point)
namespaces: 'string|Array', // namespaces
// [Optional] paths to be traversed to build the dependencies
root: 'string|Array',
// [Optional] if not set, will output to stdout
output_file: '',
// [Optional] output_mode can be 'list', 'script' or 'compiled'.
// If compile is set to true, 'compiled' mode is enforced
output_mode: '',
// [Optional] if we want builder to also compile
compile: false, // boolean
compiler: '', // the location of the compiler.jar
compiler_options: {
/**
* Go wild here...
* any key will be used as a directive for the compiler
* value can be a string or an array
* If no value is required use null
*/
}
}
}
The Closure DepsWriter task has 1 required directive:
- A way to find the
depswriter.py
file. One of the following two directives is required: *closureLibraryPath
A path to the Google Closure Library. From there we can infer the location of the depswriter.py file *depswriter
Directly reference the depswriter.py file
Read more about depswriter here.
closureDepsWriter: {
// any name that describes your operation
targetName: {
// [Required] To find the depswriter executable we need either the path to
// closure library or directly the filepath to the depswriter:
closureLibraryPath: 'path/to/closure-library', // path to closure library
depswriter: 'path/to/depswriter.py', // filepath to depswriter
// [Optional] Set file targets. Can be a string, array or
// grunt file syntax (<config:...> or *)
files: 'path/to/awesome.js',
// [Optional] If not set, will output to stdout
output_file: '',
options: {
// [Optional] Root directory to scan. Can be string or array
root: ['source/ss', 'source/closure-library', 'source/showcase'],
// [Optional] Root with prefix takes a pair of strings separated with a space,
// so proper way to use it is to suround with quotes.
// can be a string or array
root_with_prefix: '"source/ss ../../ss"',
// [Optional] string or array
path_with_depspath: ''
}
}
}
You can view the full release history in the changelog
Copyright (c) 2012 Thanasis Polychronakis Licensed under the APACHE2 license.