{"id":13494562,"url":"https://github.com/webpack-contrib/uglifyjs-webpack-plugin","last_synced_at":"2025-09-27T08:30:57.895Z","repository":{"id":44395315,"uuid":"79637359","full_name":"webpack-contrib/uglifyjs-webpack-plugin","owner":"webpack-contrib","description":"[deprecated] UglifyJS Plugin","archived":true,"fork":false,"pushed_at":"2019-12-05T12:01:04.000Z","size":1443,"stargazers_count":1384,"open_issues_count":4,"forks_count":179,"subscribers_count":30,"default_branch":"master","last_synced_at":"2025-01-12T20:34:11.407Z","etag":null,"topics":["minify","minimizer","uglify","webpack","webpack-plugin"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/webpack-contrib.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null},"funding":{"open_collective":"webpack"}},"created_at":"2017-01-21T09:49:17.000Z","updated_at":"2024-11-26T11:04:35.000Z","dependencies_parsed_at":"2022-07-14T20:17:21.660Z","dependency_job_id":null,"html_url":"https://github.com/webpack-contrib/uglifyjs-webpack-plugin","commit_stats":null,"previous_names":[],"tags_count":48,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-contrib%2Fuglifyjs-webpack-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-contrib%2Fuglifyjs-webpack-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-contrib%2Fuglifyjs-webpack-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-contrib%2Fuglifyjs-webpack-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/webpack-contrib","download_url":"https://codeload.github.com/webpack-contrib/uglifyjs-webpack-plugin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233846271,"owners_count":18739428,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["minify","minimizer","uglify","webpack","webpack-plugin"],"created_at":"2024-07-31T19:01:26.033Z","updated_at":"2025-09-27T08:30:52.296Z","avatar_url":"https://github.com/webpack-contrib.png","language":"JavaScript","readme":"# DEPRECATED\n\nPlease use https://github.com/webpack-contrib/terser-webpack-plugin\n\n\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://github.com/webpack/webpack\"\u003e\n    \u003cimg width=\"200\" height=\"200\" src=\"https://webpack.js.org/assets/icon-square-big.svg\"\u003e\n  \u003c/a\u003e\n\u003c/div\u003e\n\n[![npm][npm]][npm-url]\n[![node][node]][node-url]\n[![deps][deps]][deps-url]\n[![tests][tests]][tests-url]\n[![cover][cover]][cover-url]\n[![chat][chat]][chat-url]\n[![size][size]][size-url]\n\n# UglifyJS Webpack Plugin\n\nThis plugin uses [uglify-js](https://github.com/mishoo/UglifyJS2) to minify your JavaScript.\n\n## Requirements\n\nThis module requires a minimum of Node v6.9.0 and Webpack v4.0.0.\n\n## Getting Started\n\nTo begin, you'll need to install `uglifyjs-webpack-plugin`:\n\n```console\n$ npm install uglifyjs-webpack-plugin --save-dev\n```\n\nThen add the plugin to your `webpack` config. For example:\n\n**webpack.config.js**\n\n```js\nconst UglifyJsPlugin = require('uglifyjs-webpack-plugin');\n\nmodule.exports = {\n  optimization: {\n    minimizer: [new UglifyJsPlugin()],\n  },\n};\n```\n\nAnd run `webpack` via your preferred method.\n\n## Options\n\n### `test`\n\nType: `String|RegExp|Array\u003cString|RegExp\u003e`\nDefault: `/\\.js(\\?.*)?$/i`\n\nTest to match files against.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        test: /\\.js(\\?.*)?$/i,\n      }),\n    ],\n  },\n};\n```\n\n### `include`\n\nType: `String|RegExp|Array\u003cString|RegExp\u003e`\nDefault: `undefined`\n\nFiles to include.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        include: /\\/includes/,\n      }),\n    ],\n  },\n};\n```\n\n### `exclude`\n\nType: `String|RegExp|Array\u003cString|RegExp\u003e`\nDefault: `undefined`\n\nFiles to exclude.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        exclude: /\\/excludes/,\n      }),\n    ],\n  },\n};\n```\n\n### `chunkFilter`\n\nType: `Function\u003c(chunk) -\u003e boolean\u003e`\nDefault: `() =\u003e true`\n\nAllowing to filter which chunks should be uglified (by default all chunks are uglified).\nReturn `true` to uglify the chunk, `false` otherwise.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        chunkFilter: (chunk) =\u003e {\n          // Exclude uglification for the `vendor` chunk\n          if (chunk.name === 'vendor') {\n            return false;\n          }\n\n          return true;\n        },\n      }),\n    ],\n  },\n};\n```\n\n### `cache`\n\nType: `Boolean|String`\nDefault: `false`\n\nEnable file caching.\nDefault path to cache directory: `node_modules/.cache/uglifyjs-webpack-plugin`.\n\n\u003e ℹ️ If you use your own `minify` function please read the `minify` section for cache invalidation correctly.\n\n#### `Boolean`\n\nEnable/disable file caching.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        cache: true,\n      }),\n    ],\n  },\n};\n```\n\n#### `String`\n\nEnable file caching and set path to cache directory.\n\n**webpack.config.js**\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        cache: 'path/to/cache',\n      }),\n    ],\n  },\n};\n```\n\n### `cacheKeys`\n\nType: `Function\u003c(defaultCacheKeys, file) -\u003e Object\u003e`\nDefault: `defaultCacheKeys =\u003e defaultCacheKeys`\n\nAllows you to override default cache keys.\n\nDefault cache keys:\n\n```js\n({\n  'uglify-js': require('uglify-js/package.json').version, // uglify version\n  'uglifyjs-webpack-plugin': require('../package.json').version, // plugin version\n  'uglifyjs-webpack-plugin-options': this.options, // plugin options\n  path: compiler.outputPath ? `${compiler.outputPath}/${file}` : file, // asset path\n  hash: crypto\n    .createHash('md4')\n    .update(input)\n    .digest('hex'), // source file hash\n});\n```\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        cache: true,\n        cacheKeys: (defaultCacheKeys, file) =\u003e {\n          defaultCacheKeys.myCacheKey = 'myCacheKeyValue';\n\n          return defaultCacheKeys;\n        },\n      }),\n    ],\n  },\n};\n```\n\n### `parallel`\n\nType: `Boolean|Number`\nDefault: `false`\n\nUse multi-process parallel running to improve the build speed.\nDefault number of concurrent runs: `os.cpus().length - 1`.\n\n\u003e ℹ️ Parallelization can speedup your build significantly and is therefore **highly recommended**.\n\n#### `Boolean`\n\nEnable/disable multi-process parallel running.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        parallel: true,\n      }),\n    ],\n  },\n};\n```\n\n#### `Number`\n\nEnable multi-process parallel running and set number of concurrent runs.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        parallel: 4,\n      }),\n    ],\n  },\n};\n```\n\n### `sourceMap`\n\nType: `Boolean`\nDefault: `false`\n\nUse source maps to map error message locations to modules (this slows down the compilation).\nIf you use your own `minify` function please read the `minify` section for handling source maps correctly.\n\n\u003e ⚠️ **`cheap-source-map` options don't work with this plugin**.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        sourceMap: true,\n      }),\n    ],\n  },\n};\n```\n\n### `minify`\n\nType: `Function`\nDefault: `undefined`\n\nAllows you to override default minify function.\nBy default plugin uses [uglify-js](https://github.com/mishoo/UglifyJS2) package.\nUseful for using and testing unpublished versions or forks.\n\n\u003e ⚠️ **Always use `require` inside `minify` function when `parallel` option enabled**.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        minify(file, sourceMap) {\n          const extractedComments = [];\n\n          // Custom logic for extract comments\n\n          const { error, map, code, warnings } = require('uglify-module') // Or require('./path/to/uglify-module')\n            .minify(file, {\n              /* Your options for minification */\n            });\n\n          return { error, map, code, warnings, extractedComments };\n        },\n      }),\n    ],\n  },\n};\n```\n\n### `uglifyOptions`\n\nType: `Object`\nDefault: [default](https://github.com/mishoo/UglifyJS2#minify-options)\n\nUglifyJS minify [options](https://github.com/mishoo/UglifyJS2#minify-options).\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        uglifyOptions: {\n          warnings: false,\n          parse: {},\n          compress: {},\n          mangle: true, // Note `mangle.properties` is `false` by default.\n          output: null,\n          toplevel: false,\n          nameCache: null,\n          ie8: false,\n          keep_fnames: false,\n        },\n      }),\n    ],\n  },\n};\n```\n\n### `extractComments`\n\nType: `Boolean|String|RegExp|Function\u003c(node, comment) -\u003e Boolean|Object\u003e`\nDefault: `false`\n\nWhether comments shall be extracted to a separate file, (see [details](https://github.com/webpack/webpack/commit/71933e979e51c533b432658d5e37917f9e71595a)).\nBy default extract only comments using `/^\\**!|@preserve|@license|@cc_on/i` regexp condition and remove remaining comments.\nIf the original file is named `foo.js`, then the comments will be stored to `foo.js.LICENSE`.\nThe `uglifyOptions.output.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.\n\n#### `Boolean`\n\nEnable/disable extracting comments.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        extractComments: true,\n      }),\n    ],\n  },\n};\n```\n\n#### `String`\n\nExtract `all` or `some` (use `/^\\**!|@preserve|@license|@cc_on/i` RegExp) comments.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        extractComments: 'all',\n      }),\n    ],\n  },\n};\n```\n\n#### `RegExp`\n\nAll comments that match the given expression will be extracted to the separate file.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        extractComments: /@extract/i,\n      }),\n    ],\n  },\n};\n```\n\n#### `Function\u003c(node, comment) -\u003e Boolean\u003e`\n\nAll comments that match the given expression will be extracted to the separate file.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        extractComments: function(astNode, comment) {\n          if (/@extract/i.test(comment.value)) {\n            return true;\n          }\n\n          return false;\n        },\n      }),\n    ],\n  },\n};\n```\n\n#### `Object`\n\nAllow to customize condition for extract comments, specify extracted file name and banner.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        extractComments: {\n          condition: /^\\**!|@preserve|@license|@cc_on/i,\n          filename(file) {\n            return `${file}.LICENSE`;\n          },\n          banner(licenseFile) {\n            return `License information can be found in ${licenseFile}`;\n          },\n        },\n      }),\n    ],\n  },\n};\n```\n\n##### `condition`\n\nType: `Boolean|String|RegExp|Function\u003c(node, comment) -\u003e Boolean|Object\u003e`\n\nCondition what comments you need extract.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        extractComments: {\n          condition: 'some',\n          filename(file) {\n            return `${file}.LICENSE`;\n          },\n          banner(licenseFile) {\n            return `License information can be found in ${licenseFile}`;\n          },\n        },\n      }),\n    ],\n  },\n};\n```\n\n##### `filename`\n\nType: `Regex|Function\u003c(string) -\u003e String\u003e`\nDefault: `${file}.LICENSE`\n\nThe file where the extracted comments will be stored.\nDefault is to append the suffix `.LICENSE` to the original filename.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        extractComments: {\n          condition: /^\\**!|@preserve|@license|@cc_on/i,\n          filename: 'extracted-comments.js',\n          banner(licenseFile) {\n            return `License information can be found in ${licenseFile}`;\n          },\n        },\n      }),\n    ],\n  },\n};\n```\n\n##### `banner`\n\nType: `Boolean|String|Function\u003c(string) -\u003e String\u003e`\nDefault: `/*! For license information please see ${commentsFile} */`\n\nThe banner text that points to the extracted file and will be added on top of the original file.\nCan be `false` (no banner), a `String`, or a `Function\u003c(string) -\u003e String\u003e` that will be called with the filename where extracted comments have been stored.\nWill be wrapped into comment.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        extractComments: {\n          condition: true,\n          filename(file) {\n            return `${file}.LICENSE`;\n          },\n          banner(commentsFile) {\n            return `My custom banner about license information ${commentsFile}`;\n          },\n        },\n      }),\n    ],\n  },\n};\n```\n\n### `warningsFilter`\n\nType: `Function\u003c(warning, source) -\u003e Boolean\u003e`\nDefault: `() =\u003e true`\n\nAllow to filter [uglify-js](https://github.com/mishoo/UglifyJS2) warnings.\nReturn `true` to keep the warning, `false` otherwise.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        warningsFilter: (warning, source) =\u003e {\n          if (/Dropping unreachable code/i.test(warning)) {\n            return true;\n          }\n\n          if (/filename\\.js/i.test(source)) {\n            return true;\n          }\n\n          return false;\n        },\n      }),\n    ],\n  },\n};\n```\n\n## Examples\n\n### Cache And Parallel\n\nEnable cache and multi-process parallel running.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        cache: true,\n        parallel: true,\n      }),\n    ],\n  },\n};\n```\n\n### Preserve Comments\n\nExtract all legal comments (i.e. `/^\\**!|@preserve|@license|@cc_on/i`) and preserve `/@license/i` comments.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        uglifyOptions: {\n          output: {\n            comments: /@license/i,\n          },\n        },\n        extractComments: true,\n      }),\n    ],\n  },\n};\n```\n\n### Remove Comments\n\nIf you avoid building with comments, set **uglifyOptions.output.comments** to **false** as in this config:\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        uglifyOptions: {\n          output: {\n            comments: false,\n          },\n        },\n      }),\n    ],\n  },\n};\n```\n\n### Custom Minify Function\n\nOverride default minify function - use [terser](https://github.com/fabiosantoscode/terser) for minification.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  optimization: {\n    minimizer: [\n      new UglifyJsPlugin({\n        // Uncomment lines below for cache invalidation correctly\n        // cache: true,\n        // cacheKeys(defaultCacheKeys) {\n        //   delete defaultCacheKeys['uglify-js'];\n        //\n        //   return Object.assign(\n        //     {},\n        //     defaultCacheKeys,\n        //     { 'uglify-js': require('uglify-js/package.json').version },\n        //   );\n        // },\n        minify(file, sourceMap) {\n          // https://github.com/mishoo/UglifyJS2#minify-options\n          const uglifyJsOptions = {\n            /* your `uglify-js` package options */\n          };\n\n          if (sourceMap) {\n            uglifyJsOptions.sourceMap = {\n              content: sourceMap,\n            };\n          }\n\n          return require('terser').minify(file, uglifyJsOptions);\n        },\n      }),\n    ],\n  },\n};\n```\n\n## Contributing\n\nPlease take a moment to read our contributing guidelines if you haven't yet done so.\n\n[CONTRIBUTING](./.github/CONTRIBUTING.md)\n\n## License\n\n[MIT](./LICENSE)\n\n[npm]: https://img.shields.io/npm/v/uglifyjs-webpack-plugin.svg\n[npm-url]: https://npmjs.com/package/uglifyjs-webpack-plugin\n[node]: https://img.shields.io/node/v/uglifyjs-webpack-plugin.svg\n[node-url]: https://nodejs.org\n[deps]: https://david-dm.org/webpack-contrib/uglifyjs-webpack-plugin.svg\n[deps-url]: https://david-dm.org/webpack-contrib/uglifyjs-webpack-plugin\n[tests]: https://dev.azure.com/webpack-contrib/uglifyjs-webpack-plugin/_apis/build/status/webpack-contrib.uglifyjs-webpack-plugin?branchName=master\n[tests-url]: https://dev.azure.com/webpack-contrib/uglifyjs-webpack-plugin/_build/latest?definitionId=8\u0026branchName=master\n[cover]: https://codecov.io/gh/webpack-contrib/uglifyjs-webpack-plugin/branch/master/graph/badge.svg\n[cover-url]: https://codecov.io/gh/webpack-contrib/uglifyjs-webpack-plugin\n[chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg\n[chat-url]: https://gitter.im/webpack/webpack\n[size]: https://packagephobia.now.sh/badge?p=uglifyjs-webpack-plugin\n[size-url]: https://packagephobia.now.sh/result?p=uglifyjs-webpack-plugin\n","funding_links":["https://opencollective.com/webpack"],"categories":["JavaScript","打包工具"],"sub_categories":["webpack 辅助工具、Loader 和插件"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebpack-contrib%2Fuglifyjs-webpack-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebpack-contrib%2Fuglifyjs-webpack-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebpack-contrib%2Fuglifyjs-webpack-plugin/lists"}