Sponsor webpack and get apparel from the official shop! All proceeds go to our open collective!

ag-grid is proud to partner with webpack

UglifyjsWebpackPlugin

This plugin uses UglifyJS v3 (uglify-es) to minify your JavaScript

ℹ️ webpack < v4.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 = {
  //...
  optimization: {
    minimizer: [
      new UglifyJsPlugin()
    ]
  }
}

Options

Name
Type
Default
Description
Name
test
Type
{RegExp|Array<RegExp>}
Default
/\.js$/i
Description
Test to match files against
Name
include
Type
{RegExp|Array<RegExp>}
Default
undefined
Description
Files to include
Name
exclude
Type
{RegExp|Array<RegExp>}
Default
undefined
Description
Files to exclude
Name
cache
Type
{Boolean|String}
Default
false
Description
Enable file caching
Name
cacheKeys
Type
{Function(defaultCacheKeys, file) -> {Object}}
Default
defaultCacheKeys => defaultCacheKeys
Description
Allows you to override default cache keys
Name
parallel
Type
{Boolean|Number}
Default
false
Description
Use multi-process parallel running to improve the build speed
Name
sourceMap
Type
{Boolean}
Default
false
Description
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
Name
minify
Type
{Function}
Default
undefined
Description
Allows you to override default minify function
Name
uglifyOptions
Type
{Object}
Default
Description
uglify Options
Name
extractComments
Type
{Boolean|RegExp|Function<(node, comment) -> {Boolean|Object}>}
Default
false
Description
Whether comments shall be extracted to a separate file, (see details (webpack >= 2.3.0)
Name
warningsFilter
Type
{Function(source) -> {Boolean}}
Default
() => true
Description
Allow to filter uglify warnings

##

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

If you use your own minify function please read the minify section for cache invalidation correctly.

{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.

cacheKeys

webpack.config.js

[
  new UglifyJsPlugin({
    cache: true,
    cacheKeys: (defaultCacheKeys, file) => {
      defaultCacheKeys.myCacheKey = 'myCacheKeyValue';

      return defaultCacheKeys;
    },
  })
]

Allows you to override default cache keys.

Default keys:

{
  'uglify-es': versions.uglify, // uglify version
  'uglifyjs-webpack-plugin': versions.plugin, // plugin version
  'uglifyjs-webpack-plugin-options': this.options, // plugin options
  path: compiler.outputPath ? `${compiler.outputPath}/${file}` : file, // asset path
  hash: crypto.createHash('md4').update(input).digest('hex'), // source file hash
}

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

If you use your own minify function please read the minify section for handling source maps correctly.

webpack.config.js

[
  new UglifyJsPlugin({
    sourceMap: true
  })
]

⚠️ cheap-source-map options don't work with this plugin

minify

⚠️ Always use require inside minify function when parallel option enabled

webpack.config.js

[
  new UglifyJsPlugin({
     minify(file, sourceMap) {
       const extractedComments = [];

       // Custom logic for extract comments

       const { error, map, code, warnings } = require('uglify-module') // Or require('./path/to/uglify-module')
         .minify(
           file,
           { /* Your options for minification */ },
         );

       return { error, map, code, warnings, extractedComments };
      }
  })
]

By default plugin uses uglify-es package.

Examples:

uglify-js

npm i -D uglify-js

webpack.config.js

[
  new UglifyJsPlugin({
    // Uncomment lines below for cache invalidation correctly
    // cache: true,
    // cacheKeys(defaultCacheKeys) {
    //   return Object.assign(
    //     {},
    //     defaultCacheKeys,
    //     { 'uglify-js': require('uglify-js/package.json').version },
    //   );
    // },
    minify(file, sourceMap) {
      // https://github.com/mishoo/UglifyJS2#minify-options
      const uglifyJsOptions = { /* your `uglify-js` package options */ };

      if (sourceMap) {
        uglifyJsOptions.sourceMap = {
          content: sourceMap,
        };
      }

      return require('uglify-js').minify(file, uglifyJsOptions);
    }
  })
]

terser

npm i -D terser

webpack.config.js

[
  new UglifyJsPlugin({
    // Uncomment lines below for cache invalidation correctly
    // cache: true,
    // cacheKeys(defaultCacheKeys) {
    //   return Object.assign(
    //     {},
    //     defaultCacheKeys,
    //     { terser: require('terser/package.json').version },
    //   );
    // },
    minify(file, sourceMap) {
      // https://github.com/fabiosantoscode/terser#minify-options
      const terserOptions = { /* your `terser` package options */ };

      if (sourceMap) {
        terserOption.sourceMap = {
          content: sourceMap,
        };
      }

      return require('terser').minify(file, terserOptions);
    }
  })
]

uglifyOptions uglifyOptions" class="icon-link" href="#uglifyoptions">

Name
Type
Default
Description
Name
ecma
Type
{Number}
Default
undefined
Description
Supported ECMAScript Version (5, 6, 7 or 8). Affects parse, compress && output options
Name
warnings
Type
{Boolean}
Default
false
Description
Display Warnings
Name
Type
{Object}
Default
{}
Description
Additional Parse Options
Name
Type
{Boolean|Object}
Default
true
Description
Additional Compress Options
Name
Type
{Boolean|Object}
Default
{inline: false}
Description
Enable Name Mangling (See Mangle Properties for advanced setups, use with ⚠️)
Name
Type
{Object}
Default
{comments: extractComments ? false : /^\**!|@preserve|@license|@cc_on/,}
Description
Additional Output Options (The defaults are optimized for best compression)
Name
toplevel
Type
{Boolean}
Default
false
Description
Enable top level variable and function name mangling and to drop unused variables and functions
Name
nameCache
Type
{Object}
Default
null
Description
Enable cache of mangled variable and property names across multiple invocations
Name
ie8
Type
{Boolean}
Default
false
Description
Enable IE8 Support
Name
keep_classnames
Type
{Boolean}
Default
undefined
Description
Enable prevent discarding or mangling of class names
Name
keep_fnames
Type
{Boolean}
Default
false
Description
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
Name
safari10
Type
{Boolean}
Default
false
Description
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: {
          // mangle property options
        }
      },
      output: {
        comments: false,
        beautify: false,
        ...options
      },
      toplevel: false,
      nameCache: null,
      ie8: false,
      keep_classnames: undefined,
      keep_fnames: false,
      safari10: false,
    }
  })
]

extractComments

{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.

{RegExp|String} or {Function<(node, comment) -> {Boolean}>}

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
Name
condition
Type
{Regex|Function}
Default
Description
Regular Expression or function (see previous point)
Name
filename
Type
{String|Function}
Default
${file}.LICENSE
Description
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
Name
banner
Type
{Boolean|String|Function}
Default
/*! For license information please see ${filename}.js.LICENSE */
Description
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


      Steven Hargrove


      Juho Vepsäläinen


      Joshua Wiens


      Michael Ciniawsky


      Alexander Krasnoyarov