webpack-closure-compiler
raw JSON → 2.1.6 verified Sat Apr 25 auth: no javascript
A Webpack plugin that integrates Google Closure Compiler for advanced JavaScript optimization, including dead code elimination, function inlining, and tree-shaking for AMD, CommonJS, and ES2015 modules. The current stable version is 2.1.6, with sporadic releases. It is one of the few plugins to offer Closure Compiler's ADVANCED mode optimizations directly in Webpack, producing smaller bundles than UglifyJS. It supports both Java-based and pure JavaScript (jsCompiler) modes, with concurrency options for parallel compilation. Maintained as open-source, but note limited activity and no recent major updates.
Common errors
error Error: Cannot find module 'google-closure-compiler' ↓
cause Missing peer dependency google-closure-compiler.
fix
Run: npm install --save-dev google-closure-compiler
error ClosureCompilerPlugin is not a constructor ↓
cause Forgetting 'new' keyword when instantiating the plugin.
fix
Use: new ClosureCompilerPlugin({...})
error Error: Java not found. Please install Java to use the Java-based compiler or set jsCompiler: true. ↓
cause Java is not installed and jsCompiler option is not set.
fix
Install Java SDK or add jsCompiler: true to plugin options.
error TypeError: Cannot read property 'options' of undefined ↓
cause The plugin is passed something other than an object (e.g., array or null).
fix
Ensure you pass an object with 'compiler' key: new ClosureCompilerPlugin({ compiler: {...} })
Warnings
gotcha The plugin requires google-closure-compiler to be installed as a peer dependency, but it is not automatically installed. ↓
fix Run: npm i -D google-closure-compiler
gotcha JavaScript mode (jsCompiler: true) is significantly slower (2x) than the default Java-based compiler. ↓
fix Use default Java mode unless you cannot install Java.
gotcha The concurrency option is mutually exclusive with jsCompiler: true. ↓
fix Do not set both concurrency and jsCompiler; choose one concurrency mode.
breaking Webpack 5 compatibility is not guaranteed; this plugin was last updated before Webpack 5 release. ↓
fix Use a newer alternative or test with Webpack 5; consider terra-untitled/webpack-closure-compiler fork.
deprecated The 'test' option (RegExp) to filter files is deprecated and may be removed in future versions. ↓
fix Use webpack's module.rules to process only certain files.
Install
npm install webpack-closure-compiler yarn add webpack-closure-compiler pnpm add webpack-closure-compiler Imports
- ClosureCompilerPlugin wrong
import ClosureCompilerPlugin from 'webpack-closure-compiler';correctconst ClosureCompilerPlugin = require('webpack-closure-compiler'); - ClosureCompilerPlugin wrong
const { ClosureCompiler } = require('webpack-closure-compiler');correctconst { ClosureCompilerPlugin } = require('webpack-closure-compiler'); - ClosureCompilerPlugin (Webpack 4+ syntax) wrong
plugins: [ ClosureCompilerPlugin({...}) ]correctconst ClosureCompilerPlugin = require('webpack-closure-compiler'); // In webpack.config.js: plugins: [ new ClosureCompilerPlugin({...}) ]
Quickstart
const path = require('path');
const ClosureCompilerPlugin = require('webpack-closure-compiler');
module.exports = {
entry: path.join(__dirname, 'app.js'),
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.min.js',
},
plugins: [
new ClosureCompilerPlugin({
compiler: {
language_in: 'ECMASCRIPT6',
language_out: 'ECMASCRIPT5',
compilation_level: 'ADVANCED',
},
concurrency: 3,
}),
],
};