赞助 webpack,同时从官方商店购买衣服 所有收益将转到我们的 open collective

UglifyjsWebpackPlugin

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

ℹ️ 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

安装

npm i -D uglifyjs-webpack-plugin

用法

webpack.config.js

const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

module.exports = {
  plugins: [
    new UglifyJsPlugin()
  ]
}

选项

属性名称
类型
默认值
描述
属性名称
test
类型
{RegExp|Array<RegExp>}
默认值
/.js($|\?)/i
描述
测试匹配的文件
属性名称
include
类型
{RegExp|Array<RegExp>}
默认值
undefined
描述
包含的文件
属性名称
exclude
类型
{RegExp|Array<RegExp>}
默认值
undefined
描述
排除的文件。
属性名称
cache
类型
{Boolean|String}
默认值
false
描述
启用文件缓存
属性名称
parallel
类型
{Boolean|Object}
默认值
false
描述
使用多进程并行运行和文件缓存来提高构建速度
属性名称
sourceMap
类型
{Boolean}
默认值
false
描述
使用 source map 将错误信息的位置映射到模块(这会减慢编译的速度) ⚠️ cheap-source-map 选项不适用于此插件
属性名称
uglifyOptions
类型
{Object}
默认值
描述
uglify 选项
属性名称
extractComments
类型
{Boolean|RegExp|Function<(node, comment) -> {Boolean|Object}>}
默认值
false
描述
是否将注释提取到单独的文件,(查看详细
属性名称
warningsFilter
类型
{Function(source) -> {Boolean}}
默认值
() => true
描述
允许过滤 uglify 警告

##

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

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

Name
Type
Default
Description
Name
ie8
Type
{Boolean}
Default
false
Description
Enable IE8 Support
Name
ecma
Type
{Number}
Default
undefined
Description
Supported ECMAScript Version (5, 6, 7 or 8). Affects parse, compress && output options
Name
Type
{Object}
Default
{}
Description
Additional Parse Options
Name
Type
{Boolean|Object}
Default
true
Description
Enable Name Mangling (See Mangle Properties for advanced setups, use with ⚠️)
Name
Type
{Object}
Default
{}
Description
Additional Output Options (The defaults are optimized for best compression)
Name
Type
{Boolean|Object}
Default
true
Description
Additional Compress Options
Name
warnings
Type
{Boolean}
Default
false
Description
Display Warnings

webpack.config.js

[
  new UglifyJsPlugin({
    uglifyOptions: {
      ie8: false,
      ecma: 8,
      parse: {...options},
      mangle: {
        ...options,
        properties: {
          // mangle property options
        }
      },
      output: {
        comments: false,
        beautify: false,
        ...options
      },
      compress: {...options},
      warnings: 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
通常表达式或者相应函数(见上文)
Name
filename
Type
{String|Function}
Default
${file}.LICENSE
Description
提取注释的文件会被存储。可以使一个 {String} 字符串或者是一个 {Function<(string) -> {String}>} 返回字符串的函数,作为原文件名。默认加上文件后缀名 .LICENSE
Name
banner
Type
{Boolean|String|Function}
Default
/*! For license information please see ${filename}.js.LICENSE */
Description
banner 文本会在原文件的头部指出被提取的文件,会在源文件加入该信息。可以是 false(表示没有 banner),一个 {String},或者一个 {Function<(string) -> {String} 返回字符串的函数,会在提取已经被存储注释的时候被调用。注释会被覆盖。

warningsFilter

webpack.config.js

[
  new UglifyJsPlugin({
    warningsFilter: (src) => true
  })
]

维护人员


      Steven Hargrove


      Juho Vepsäläinen


      Joshua Wiens


      Michael Ciniawsky


      Alexander Krasnoyarov