What is uglifyjs-webpack-plugin?
The uglifyjs-webpack-plugin is a plugin for Webpack that uses UglifyJS to minify JavaScript files. It helps in reducing the size of JavaScript files by removing unnecessary characters, comments, and whitespace, and by optimizing the code.
What are uglifyjs-webpack-plugin's main functionalities?
Basic Minification
This feature allows you to perform basic minification of JavaScript files using UglifyJS. The code sample demonstrates how to configure the plugin in a Webpack configuration file to minimize the output JavaScript files.
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
module.exports = {
optimization: {
minimizer: [new UglifyJsPlugin()]
}
};
Custom UglifyJS Options
This feature allows you to customize the UglifyJS options. The code sample shows how to configure the plugin to remove console statements from the output JavaScript files.
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
module.exports = {
optimization: {
minimizer: [
new UglifyJsPlugin({
uglifyOptions: {
compress: {
drop_console: true,
},
},
}),
],
},
};
Source Map Support
This feature enables source map support, which helps in debugging minified code. The code sample demonstrates how to configure the plugin to generate source maps for the minified JavaScript files.
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
module.exports = {
devtool: 'source-map',
optimization: {
minimizer: [
new UglifyJsPlugin({
sourceMap: true,
}),
],
},
};
Other packages similar to uglifyjs-webpack-plugin
terser-webpack-plugin
The terser-webpack-plugin is a Webpack plugin that uses Terser to minify JavaScript files. Terser is a fork of UglifyJS with better support for ES6+ syntax. It is often preferred over uglifyjs-webpack-plugin for modern JavaScript projects due to its better handling of newer JavaScript features.
babel-minify-webpack-plugin
The babel-minify-webpack-plugin is a Webpack plugin that uses Babel Minify (babel-minify) to minify JavaScript files. It is designed to work seamlessly with Babel and offers a wide range of minification options. It is a good alternative to uglifyjs-webpack-plugin, especially if you are already using Babel in your project.
closure-webpack-plugin
The closure-webpack-plugin is a Webpack plugin that uses Google Closure Compiler to minify JavaScript files. It offers advanced optimizations and is capable of performing aggressive code transformations. It is suitable for large projects that require high levels of optimization and code analysis.
ℹ️ webpack =< v3.0.0
currently contains v0.4.6
of this plugin under webpack.optimize.UglifyJsPlugin
as an alias. For usage of the latest version (v1.0.0
), please follow the instructions below. Aliasing v1.0.0
as webpack.optimize.UglifyJsPlugin
is scheduled for webpack v4.0.0
Install
npm i -D uglifyjs-webpack-plugin
Usage
webpack.config.js
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
module.exports = {
plugins: [
new UglifyJsPlugin()
]
}
Options
Name | Type | Default | Description |
---|
test | {RegExp|Array<RegExp>} | /\.js$/i | Test to match files against |
include | {RegExp|Array<RegExp>} | undefined | Files to include |
exclude | {RegExp|Array<RegExp>} | undefined | Files to exclude |
cache | {Boolean|String} | false | Enable file caching |
parallel | {Boolean|Number} | false | Use multi-process parallel running to improve the build speed |
sourceMap | {Boolean} | false | Use source maps to map error message locations to modules (This slows down the compilation) ⚠️ cheap-source-map options don't work with this plugin |
uglifyOptions | {Object} | {...defaults} | uglify Options |
extractComments | {Boolean|RegExp|Function<(node, comment) -> {Boolean|Object}>} | false | Whether comments shall be extracted to a separate file, (see details (webpack >= 2.3.0 ) |
warningsFilter | {Function(source) -> {Boolean}} | () => true | Allow to filter uglify warnings |
test
webpack.config.js
[
new UglifyJsPlugin({
test: /\.js($|\?)/i
})
]
include
webpack.config.js
[
new UglifyJsPlugin({
include: /\/includes/
})
]
exclude
webpack.config.js
[
new UglifyJsPlugin({
exclude: /\/excludes/
})
]
cache
{Boolean}
webpack.config.js
[
new UglifyJsPlugin({
cache: true
})
]
Enable file caching.
Default path to cache directory: node_modules/.cache/uglifyjs-webpack-plugin
.
{String}
webpack.config.js
[
new UglifyJsPlugin({
cache: 'path/to/cache'
})
]
Path to cache directory.
parallel
{Boolean}
webpack.config.js
[
new UglifyJsPlugin({
parallel: true
})
]
Enable parallelization.
Default number of concurrent runs: os.cpus().length - 1
.
{Number}
webpack.config.js
[
new UglifyJsPlugin({
parallel: 4
})
]
Number of concurrent runs.
ℹ️ Parallelization can speedup your build significantly and is therefore highly recommended
sourceMap
webpack.config.js
[
new UglifyJsPlugin({
sourceMap: true
})
]
⚠️ cheap-source-map
options don't work with this plugin
Name | Type | Default | Description |
---|
ecma | {Number} | undefined | Supported ECMAScript Version (5 , 6 , 7 or 8 ). Affects parse , compress && output options |
warnings | {Boolean} | false | Display Warnings |
parse | {Object} | {} | Additional Parse Options |
compress | {Boolean|Object} | true | Additional Compress Options |
mangle | {Boolean|Object} | true | Enable Name Mangling (See Mangle Properties for advanced setups, use with ⚠️) |
output | {Object} | {} | Additional Output Options (The defaults are optimized for best compression) |
toplevel | {Boolean} | false | Enable top level variable and function name mangling and to drop unused variables and functions |
nameCache | {Object} | null | Enable cache of mangled variable and property names across multiple invocations |
ie8 | {Boolean} | false | Enable IE8 Support |
keep_classnames | {Boolean} | undefined | Enable prevent discarding or mangling of class names |
keep_fnames | {Boolean} | false | Enable prevent discarding or mangling of function names. Useful for code relying on Function.prototype.name . If the top level minify option keep_classnames is undefined it will be overriden with the value of the top level minify option keep_fnames |
safari10 | {Boolean} | false | Enable work around Safari 10/11 bugs in loop scoping and await |
webpack.config.js
[
new UglifyJsPlugin({
uglifyOptions: {
ecma: 8,
warnings: false,
parse: {...options},
compress: {...options},
mangle: {
...options,
properties: {
}
},
output: {
comments: false,
beautify: false,
...options
},
toplevel: false,
nameCache: null,
ie8: false,
keep_classnames: undefined,
keep_fnames: false,
safari10: false,
}
})
]
{Boolean}
All comments that normally would be preserved by the comments
option will be moved to a separate file. If the original file is named foo.js
, then the comments will be stored to foo.js.LICENSE
.
All comments that match the given expression (resp. are evaluated to true
by the function) will be extracted to the separate file. The comments
option specifies whether the comment will be preserved, i.e. it is possible to preserve some comments (e.g. annotations) while extracting others or even preserving comments that have been extracted.
{Object}
Name | Type | Default | Description |
---|
condition | {Regex|Function} | `` | Regular Expression or function (see previous point) |
filename | {String|Function} | ${file}.LICENSE | The file where the extracted comments will be stored. Can be either a {String} or a {Function<(string) -> {String}>} , which will be given the original filename. Default is to append the suffix .LICENSE to the original filename |
banner | {Boolean|String|Function} | /*! For license information please see ${filename}.js.LICENSE */ | The banner text that points to the extracted file and will be added on top of the original file. Can be false (no banner), a {String} , or a {Function<(string) -> {String} that will be called with the filename where extracted comments have been stored. Will be wrapped into comment |
warningsFilter
webpack.config.js
[
new UglifyJsPlugin({
warningsFilter: (src) => true
})
]
Maintainers