closure-webpack-plugin
raw JSON → 2.6.1 verified Sat Apr 25 auth: no javascript
A webpack plugin integrating Google Closure Compiler and Closure Library for JavaScript optimization and transpilation. Version 2.6.1 (webpack 4 branch) requires google-closure-compiler >=20200830.0.0 and webpack 4.x. Offers three platforms (native, Java, JavaScript) and two modes (STANDARD for minification/transpilation, AGGRESSIVE_BUNDLE for advanced module hoisting and optimization). Unlike TerserPlugin or Babel, it provides full Closure Compiler optimizations including dead code elimination, type checking, and cross-module analysis. Not compatible with webpack 5.
Common errors
error TypeError: ClosurePlugin is not a constructor ↓
cause Importing the plugin incorrectly (using default import instead of require).
fix
Use
const ClosurePlugin = require('closure-webpack-plugin'); error Error: Cannot find module 'google-closure-compiler' ↓
cause Required peer dependency not installed.
fix
Run
npm install --save-dev google-closure-compiler error Error: webpack version 5.x is not supported ↓
cause Using the plugin with webpack 5.
fix
Downgrade to webpack 4.x, or use an alternative plugin (e.g., terser-webpack-plugin) for webpack 5.
Warnings
breaking Plugin only works with webpack 4.x; not compatible with webpack 5. ↓
fix Use webpack 4 or switch to an alternative plugin for webpack 5.
gotcha Do not set compiler flags module_resolution, output_wrapper, dependency_mode, create_source_map, module, or entry_point—they are controlled by the plugin and will conflict. ↓
fix Remove these flags from the compiler options object. They are overridden internally.
deprecated The 'platform' option accepts strings 'native', 'java', 'javascript', but old documentation used case-insensitive values. ↓
fix Use lowercase strings as above. The plugin may also accept uppercase but it's not guaranteed.
gotcha AGGRESSIVE_BUNDLE mode hoists require() calls, which can cause out-of-order execution if polyfills or side-effects are interleaved. ↓
fix Ensure modules with side-effects are separated into different chunks or mark them with sideEffects: false in package.json.
gotcha childCompilations defaults to false—plugins like html-webpack-plugin may not get optimized output. ↓
fix Set childCompilations: true or a custom function if child compilations need closure optimization.
Install
npm install closure-webpack-plugin yarn add closure-webpack-plugin pnpm add closure-webpack-plugin Imports
- ClosurePlugin wrong
import ClosurePlugin from 'closure-webpack-plugin';correctconst ClosurePlugin = require('closure-webpack-plugin'); - ClosurePlugin wrong
import * as ClosurePlugin from 'closure-webpack-plugin';correctimport ClosurePlugin = require('closure-webpack-plugin'); - ClosurePlugin as ClosurePlugin wrong
const { default: ClosurePlugin } = require('closure-webpack-plugin');correctconst { ClosurePlugin } = require('closure-webpack-plugin');
Quickstart
const ClosurePlugin = require('closure-webpack-plugin');
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: __dirname + '/dist'
},
optimization: {
minimizer: [
new ClosurePlugin({
mode: 'STANDARD',
platform: 'javascript'
}, {
// compiler flags
formatting: 'PRETTY_PRINT',
debug: true,
renaming: false
})
]
}
};