grunt-closure-tools
raw JSON → 1.0.0 verified Fri May 01 auth: no javascript maintenance
Grunt plugin integrating Google Closure Library tools (Compiler, Builder, DepsWriter) for JavaScript minification, dependency calculation, and bundling. Version 1.0.0 (stable, last release 2019) targets Grunt 0.4.x. Supports Closure Compiler jar-based compilation, closurebuilder.py concatenation, and depswriter.py deps generation. Key differentiators: full Closure toolchain integration in Grunt, support for advanced compilation options, and file-level change detection (checkModified). Alternative to gulp-closure-compiler or webpack closure plugins.
Common errors
error Warning: Task "closureCompiler" not found. Use --force to continue. ↓
cause The task was not loaded via grunt.loadNpmTasks('grunt-closure-tools').
fix
Add
grunt.loadNpmTasks('grunt-closure-tools'); to your Gruntfile. error Fatal error: java.io.IOException: Cannot run program "/path/to/closure-compiler.jar": error=2, No such file or directory ↓
cause compilerFile path is incorrect or missing; Java cannot find the jar.
fix
Verify the compilerFile path exists, or use an absolute path. For Windows, ensure forward slashes or escaped backslashes.
error Fatal error: maxBuffer exceeded ↓
cause The output from the compiler command exceeds Node's default buffer size (200KB).
fix
Increase the buffer by setting
execOpts: { maxBuffer: 999999 * 1024 } in the task options. error Warning: DepsWriter: No files found to process. ↓
cause The root directory in depsWriter options does not contain any .js files specified by the includes/excludes.
fix
Ensure the
root path is correct and use the includes option (string or array) to specify which files to scan. Warnings
breaking Grunt 0.4.x migration: Task configuration structure changed. Options must be nested under `options` key, not top-level. ↓
fix Move `compilerFile`, `compilerOpts`, `execOpts` inside `options: {}` block.
deprecated Builder and DepsWriter depend on Python scripts (closurebuilder.py, depswriter.py) which are deprecated by Google. ↓
fix Consider migrating to pure Node alternatives like 'google-closure-compiler' for compilation and manual dependency management.
gotcha compilerFile must point to an actual closure-compiler.jar. If missing, task fails silently or with Java errors. ↓
fix Download compiler.jar from https://dl.google.com/closure-compiler/compiler-latest.zip or use npm package 'google-closure-compiler-java' and set path to its jar.
gotcha Max buffer exceeded: Node default 200*1024 may be insufficient for large files, causing 'Error: maxBuffer exceeded'. ↓
fix Set execOpts.maxBuffer in options, e.g., `execOpts: { maxBuffer: 999999 * 1024 }`.
gotcha File paths with glob patterns (e.g., 'src/**/*.js') must be in src array. The grunt file syntax (<config:...>) is NOT supported in src. ↓
fix Use grunt's usual array of file patterns in src; do not use template syntax like `'<%= pkg.name %>'` in src.
Install
npm install grunt-closure-tools yarn add grunt-closure-tools pnpm add grunt-closure-tools Imports
- default (task registration) wrong
require('grunt-closure-tools');correctgrunt.loadNpmTasks('grunt-closure-tools'); - closureCompiler config wrong
closureCompiler: { compilerFile: 'compiler.jar' }correctclosureCompiler: { options: { compilerFile: 'compiler.jar', compilerOpts: { compilation_level: 'ADVANCED_OPTIMIZATIONS' } } } - closureBuilder config wrong
closureBuilder: { builder: 'closurebuilder.py' }correctclosureBuilder: { options: { builder: 'closurebuilder.py', root: 'src/', output_mode: 'compiled', compiler: 'compiler.jar' } } - closureDepsWriter config wrong
closureDepsWriter: { root: 'src/', deps: 'deps.js' }correctclosureDepsWriter: { options: { root: 'src/', deps: 'deps.js' } }
Quickstart
// Install: npm install grunt-closure-tools --save-dev
// Gruntfile.js
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-closure-tools');
grunt.initConfig({
closureCompiler: {
options: {
compilerFile: 'node_modules/google-closure-compiler-java/compiler.jar',
checkModified: true,
compilerOpts: {
compilation_level: 'SIMPLE_OPTIMIZATIONS',
warning_level: 'verbose'
}
},
dist: {
src: ['src/**/*.js'],
dest: 'dist/bundle.min.js'
}
}
});
grunt.registerTask('default', ['closureCompiler']);
};