UNPKG

10 kBMarkdownView Raw
1[![npm][npm]][npm-url]
2[![node][node]][node-url]
3[![deps][deps]][deps-url]
4[![test][test]][test-url]
5[![coverage][cover]][cover-url]
6[![chat][chat]][chat-url]
7
8
9<div align="center">
10 <a href="https://github.com/webpack/webpack">
11 <img width="200" height="200"
12 src="https://cdn.rawgit.com/webpack/media/e7485eb2/logo/icon.svg">
13 </a>
14 <h1>UglifyJS Webpack Plugin</h1>
15 <p>This plugin uses <a href="https://github.com/mishoo/UglifyJS2/tree/harmony">UglifyJS v3 </a><a href="https://npmjs.com/package/uglify-es">(`uglify-es`)</a> to minify your JavaScript</p>
16</div>
17
18> ℹ️ `webpack < v4.0.0` currently contains [`v0.4.6`](https://github.com/webpack-contrib/uglifyjs-webpack-plugin/tree/version-0.4) 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`
19
20<h2 align="center">Install</h2>
21
22```bash
23npm i -D uglifyjs-webpack-plugin
24```
25
26<h2 align="center">Usage</h2>
27
28**webpack.config.js**
29```js
30const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
31
32module.exports = {
33 plugins: [
34 new UglifyJsPlugin()
35 ]
36}
37```
38
39<h2 align="center">Options</h2>
40
41|Name|Type|Default|Description|
42|:--:|:--:|:-----:|:----------|
43|**`test`**|`{RegExp\|Array<RegExp>}`| <code>/\\.js$/i</code>|Test to match files against|
44|**`include`**|`{RegExp\|Array<RegExp>}`|`undefined`|Files to `include`|
45|**`exclude`**|`{RegExp\|Array<RegExp>}`|`undefined`|Files to `exclude`|
46|**`cache`**|`{Boolean\|String}`|`false`|Enable file caching|
47|**`parallel`**|`{Boolean\|Number}`|`false`|Use multi-process parallel running to improve the build speed|
48|**`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**|
49|**`uglifyOptions`**|`{Object}`|[`{...defaults}`](https://github.com/webpack-contrib/uglifyjs-webpack-plugin/tree/master#uglifyoptions)|`uglify` [Options](https://github.com/mishoo/UglifyJS2/tree/harmony#minify-options)|
50|**`extractComments`**|`{Boolean\|RegExp\|Function<(node, comment) -> {Boolean\|Object}>}`|`false`|Whether comments shall be extracted to a separate file, (see [details](https://github.com/webpack/webpack/commit/71933e979e51c533b432658d5e37917f9e71595a) (`webpack >= 2.3.0`)|
51|**`warningsFilter`**|`{Function(source) -> {Boolean}}`|`() => true`|Allow to filter uglify warnings|
52
53### `test`
54
55**webpack.config.js**
56```js
57[
58 new UglifyJsPlugin({
59 test: /\.js($|\?)/i
60 })
61]
62```
63
64### `include`
65
66**webpack.config.js**
67```js
68[
69 new UglifyJsPlugin({
70 include: /\/includes/
71 })
72]
73```
74
75### `exclude`
76
77**webpack.config.js**
78```js
79[
80 new UglifyJsPlugin({
81 exclude: /\/excludes/
82 })
83]
84```
85
86### `cache`
87
88#### `{Boolean}`
89
90**webpack.config.js**
91```js
92[
93 new UglifyJsPlugin({
94 cache: true
95 })
96]
97```
98
99Enable file caching.
100Default path to cache directory: `node_modules/.cache/uglifyjs-webpack-plugin`.
101
102#### `{String}`
103
104**webpack.config.js**
105```js
106[
107 new UglifyJsPlugin({
108 cache: 'path/to/cache'
109 })
110]
111```
112
113Path to cache directory.
114
115### `parallel`
116
117#### `{Boolean}`
118
119**webpack.config.js**
120```js
121[
122 new UglifyJsPlugin({
123 parallel: true
124 })
125]
126```
127
128Enable parallelization.
129Default number of concurrent runs: `os.cpus().length - 1`.
130
131#### `{Number}`
132
133**webpack.config.js**
134```js
135[
136 new UglifyJsPlugin({
137 parallel: 4
138 })
139]
140```
141
142Number of concurrent runs.
143
144> ℹ️ Parallelization can speedup your build significantly and is therefore **highly recommended**
145
146### `sourceMap`
147
148**webpack.config.js**
149```js
150[
151 new UglifyJsPlugin({
152 sourceMap: true
153 })
154]
155```
156
157> ⚠️ **`cheap-source-map` options don't work with this plugin**
158
159### [`uglifyOptions`](https://github.com/mishoo/UglifyJS2/tree/harmony#minify-options)
160
161|Name|Type|Default|Description|
162|:--:|:--:|:-----:|:----------|
163|**`ecma`**|`{Number}`|`undefined`|Supported ECMAScript Version (`5`, `6`, `7` or `8`). Affects `parse`, `compress` && `output` options|
164|**`warnings`**|`{Boolean}`|`false`|Display Warnings|
165|**[`parse`](https://github.com/mishoo/UglifyJS2/tree/harmony#parse-options)**|`{Object}`|`{}`|Additional Parse Options|
166|**[`compress`](https://github.com/mishoo/UglifyJS2/tree/harmony#compress-options)**|`{Boolean\|Object}`|`true`|Additional Compress Options|
167|**[`mangle`](https://github.com/mishoo/UglifyJS2/tree/harmony#mangle-options)**|`{Boolean\|Object}`|`{inline: false}`|Enable Name Mangling (See [Mangle Properties](https://github.com/mishoo/UglifyJS2/tree/harmony#mangle-properties-options) for advanced setups, use with ⚠️)|
168|**[`output`](https://github.com/mishoo/UglifyJS2/tree/harmony#output-options)**|`{Object}`|`{comments: extractComments ? false : /^\**!\|@preserve\|@license\|@cc_on/,}`|Additional Output Options (The defaults are optimized for best compression)|
169|**`toplevel`**|`{Boolean}`|`false`|Enable top level variable and function name mangling and to drop unused variables and functions|
170|**`nameCache`**|`{Object}`|`null`|Enable cache of mangled variable and property names across multiple invocations|
171|**`ie8`**|`{Boolean}`|`false`|Enable IE8 Support|
172|**`keep_classnames`**|`{Boolean}`|`undefined`|Enable prevent discarding or mangling of class names|
173|**`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`|
174|**`safari10`**|`{Boolean}`|`false`|Enable work around Safari 10/11 bugs in loop scoping and `await`|
175
176**webpack.config.js**
177```js
178[
179 new UglifyJsPlugin({
180 uglifyOptions: {
181 ecma: 8,
182 warnings: false,
183 parse: {...options},
184 compress: {...options},
185 mangle: {
186 ...options,
187 properties: {
188 // mangle property options
189 }
190 },
191 output: {
192 comments: false,
193 beautify: false,
194 ...options
195 },
196 toplevel: false,
197 nameCache: null,
198 ie8: false,
199 keep_classnames: undefined,
200 keep_fnames: false,
201 safari10: false,
202 }
203 })
204]
205```
206
207### `extractComments`
208
209#### `{Boolean}`
210
211All 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`.
212
213#### `{RegExp|String}` or `{Function<(node, comment) -> {Boolean}>}`
214
215All 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.
216
217#### `{Object}`
218
219|Name|Type|Default|Description|
220|:--:|:--:|:-----:|:----------|
221|**`condition`**|`{Regex\|Function}`|``|Regular Expression or function (see previous point)|
222|**`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|
223|**`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|
224
225### `warningsFilter`
226
227**webpack.config.js**
228```js
229[
230 new UglifyJsPlugin({
231 warningsFilter: (src) => true
232 })
233]
234```
235
236<h2 align="center">Maintainers</h2>
237
238<table>
239 <tbody>
240 <tr>
241 <td align="center">
242 <a href="https://github.com/hulkish">
243 <img width="150" height="150" src="https://github.com/hulkish.png?size=150">
244 </br>
245 Steven Hargrove
246 </a>
247 </td>
248 <td align="center">
249 <a href="https://github.com/bebraw">
250 <img width="150" height="150" src="https://github.com/bebraw.png?v=3&s=150">
251 </br>
252 Juho Vepsäläinen
253 </a>
254 </td>
255 <td align="center">
256 <a href="https://github.com/d3viant0ne">
257 <img width="150" height="150" src="https://github.com/d3viant0ne.png?v=3&s=150">
258 </br>
259 Joshua Wiens
260 </a>
261 </td>
262 <td align="center">
263 <a href="https://github.com/michael-ciniawsky">
264 <img width="150" height="150" src="https://github.com/michael-ciniawsky.png?v=3&s=150">
265 </br>
266 Michael Ciniawsky
267 </a>
268 </td>
269 <td align="center">
270 <a href="https://github.com/evilebottnawi">
271 <img width="150" height="150" src="https://github.com/evilebottnawi.png?v=3&s=150">
272 </br>
273 Alexander Krasnoyarov
274 </a>
275 </td>
276 </tr>
277 <tbody>
278</table>
279
280
281[npm]: https://img.shields.io/npm/v/uglifyjs-webpack-plugin.svg
282[npm-url]: https://npmjs.com/package/uglifyjs-webpack-plugin
283
284[node]: https://img.shields.io/node/v/uglifyjs-webpack-plugin.svg
285[node-url]: https://nodejs.org
286
287[deps]: https://david-dm.org/webpack-contrib/uglifyjs-webpack-plugin.svg
288[deps-url]: https://david-dm.org/webpack-contrib/uglifyjs-webpack-plugin
289
290[test]: https://img.shields.io/circleci/project/github/webpack-contrib/uglifyjs-webpack-plugin.svg
291[test-url]: https://circleci.com/gh/webpack-contrib/uglifyjs-webpack-plugin
292
293[cover]: https://codecov.io/gh/webpack-contrib/uglifyjs-webpack-plugin/branch/master/graph/badge.svg
294[cover-url]: https://codecov.io/gh/webpack-contrib/uglifyjs-webpack-plugin
295
296[chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
297[chat-url]: https://gitter.im/webpack/webpack