UNPKG

19.8 kBMarkdownView Raw
1# grunt-contrib-uglify v5.0.0 [![Build Status](https://github.com/gruntjs/grunt-contrib-uglify/workflows/Tests/badge.svg)](https://github.com/gruntjs/grunt-contrib-uglify/actions?workflow=Tests)
2
3> Minify JavaScript files with UglifyJS
4
5
6
7## Getting Started
8
9If you haven't used [Grunt](https://gruntjs.com/) before, be sure to check out the [Getting Started](https://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](https://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
10
11```shell
12npm install grunt-contrib-uglify --save-dev
13```
14
15Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
16
17```js
18grunt.loadNpmTasks('grunt-contrib-uglify');
19```
20
21
22
23
24## Uglify task
25_Run this task with the `grunt uglify` command._
26
27Task targets, files and options may be specified according to the grunt [Configuring tasks](https://gruntjs.com/configuring-tasks) guide.
28
29### Options
30
31This task primarily delegates to [UglifyJS](https://github.com/mishoo/UglifyJS2), so please consider their documentation as required reading for advanced configuration.
32
33
34###### Deprecated options from `2.x`
35
36Option | Replacement
37----------------------- | -----------
38ASCIIOnly | output.ascii_only
39enclose | —
40exportAll | —
41expression | parse.expression
42indentLevel | output.indent_level
43mangleProperties | mangle.properties
44maxLineLen | output.max\_line_len
45preserveComments | output.comments
46quoteStyle | output.quote_style
47screwIE8 | !ie8
48sourceMapIncludeSources | sourceMap.includeSources
49sourceMapRoot | sourceMap.root
50sourceMapUrl | sourceMap.url
51
52
53#### mangle
54Type: `Boolean` `Object`
55Default: `{}`
56
57Turn on or off mangling with default options. If an `Object` is specified, it is passed directly to `ast.mangle_names()` *and* `ast.compute_char_frequency()` (mimicking command line behavior). [View all options here](https://github.com/mishoo/UglifyJS2#mangle-options).
58
59#### compress
60Type: `Boolean` `Object`
61Default: `{}`
62
63Turn on or off source compression with default options. If an `Object` is specified, it is passed as options to `UglifyJS.Compressor()`. [View all options here](https://github.com/mishoo/UglifyJS2#compress-options).
64
65#### beautify
66Type: `Boolean` `Object`
67Default: `false`
68
69Turns on beautification of the generated source code. [View all options here](https://github.com/mishoo/UglifyJS2#output-options)
70
71###### parse.expression
72Type: `Boolean`
73Default: `false`
74
75Parse a single expression, rather than a program (for parsing JSON)
76
77#### report
78Type: `string`
79Choices: `'min'`, `'gzip'`
80Default: `'min'`
81
82Report minification result or both minification and gzip results.
83This is useful to see exactly how well uglify-js is performing but using `'gzip'` will make the task take 5-10x longer to complete. [Example output](https://github.com/sindresorhus/maxmin#readme).
84
85#### sourceMap
86Type: `Boolean`
87Default: `false`
88
89If `true`, a source map file will be generated in the same directory as the `dest` file. By default it will have the same basename as the `dest` file, but with a `.map` extension.
90
91#### sourceMapName
92Type: `String` `Function`
93Default: `undefined`
94
95To customize the name or location of the generated source map, pass a string to indicate where to write the source map to. If a function is provided, the uglify destination is passed as the argument and the return value will be used as the file name.
96
97#### sourceMapIn
98Type: `String` `Function`
99Default: `undefined`
100
101The location of an input source map from an earlier compilation, e.g. from CoffeeScript. If a function is provided, the
102uglify source is passed as the argument and the return value will be used as the sourceMap name. This only makes sense
103when there's one source file.
104
105#### sourceMap.includeSources
106Type: `Boolean`
107Default: `false`
108
109Pass this flag if you want to include the content of source files in the source map as sourcesContent property.
110
111###### sourceMap.root
112Type: `String`
113Default: `undefined`
114
115With this option you can customize root URL that browser will use when looking for sources.
116
117If the sources are not absolute URLs after prepending of the `sourceMap.root`, the sources are resolved relative to the source map.
118
119#### sourceMap.url
120Type: `String`
121Default: `undefined`
122
123Override the calculated value for `sourceMappingURL` in the source map. This is useful if the source map location is not relative to the base path of the minified file, i.e. when using a CDN
124
125#### wrap
126Type: `String`
127Default: `undefined`
128
129Wrap all of the code in a closure, an easy way to make sure nothing is leaking.
130For variables that need to be public `exports` and `global` variables are made available.
131The value of wrap is the global variable exports will be available as.
132
133#### output.ascii_only
134Type: `Boolean`
135Default: `false`
136
137Enables to encode non-ASCII characters as \uXXXX.
138
139#### output.comments
140Type: `Boolean` `String` `Function`
141Default: `undefined`
142Options: `false` `'all'` `'some'`
143
144Turn on preservation of comments.
145
146- `false` will strip all comments
147- `'all'` will preserve all comments in code blocks that have not been squashed or dropped
148- `'some'` will preserve all comments that include a closure compiler style directive (`@preserve` `@license` `@cc_on`)
149- `Function` specify your own comment preservation function. You will be passed the current node and the current comment and are expected to return either `true` or `false`
150- `RegExp` `'/[RegExp]/'` will preserve comments matching given RegExp or stringified RegExp
151
152#### banner
153Type: `String`
154Default: `''`
155
156This string will be prepended to the minified output. Template strings (e.g. `<%= config.value %>`) will be expanded automatically.
157
158#### footer
159Type: `String`
160Default: `''`
161
162This string will be appended to the minified output. Template strings (e.g. `<%= config.value %>`) will be expanded automatically.
163
164#### ie8
165Type: `Boolean`
166Default: `false`
167
168Set this to `true` if you still care about full compliance with Internet Explorer 6-8 quirks.
169
170#### mangle.properties
171Type: `Boolean` `Object`
172Default: `false`
173
174Turn on or off property mangling with default options. If an `Object` is specified, it is passed directly to `ast.mangle_properties()` (mimicking command line behavior). [View all options here](https://github.com/mishoo/UglifyJS2#mangle-options).
175
176#### reserveDOMProperties
177Type: `Boolean`
178Default: `false`
179
180Use this flag in conjunction with `mangle.properties` to prevent built-in browser object properties from being mangled.
181
182#### exceptionsFiles
183Type: `Array`
184Default: `[]`
185
186Use this with `mangle.properties` to pass one or more JSON files containing a list of variables and object properties
187that should not be mangled. See the [UglifyJS docs](https://www.npmjs.com/package/uglify-js) for more info on the file syntax.
188
189#### nameCache
190Type: `String`
191Default: `''`
192
193A string that is a path to a JSON cache file that uglify will create and use to coordinate symbol mangling between
194multiple runs of uglify. Note: this generated file uses the same JSON format as the `exceptionsFiles` files.
195
196#### output.quote_style
197Type: `Integer`
198Default: `0`
199
200Preserve or enforce quotation mark style.
201
202* `0` will use single or double quotes such as to minimize the number of bytes (prefers double quotes when both will do)
203* `1` will always use single quotes
204* `2` will always use double quotes
205* `3` will preserve original quotation marks
206
207### Usage examples
208
209#### Basic compression
210
211This configuration will compress and mangle the input files using the default options.
212
213```js
214// Project configuration.
215grunt.initConfig({
216 uglify: {
217 my_target: {
218 files: {
219 'dest/output.min.js': ['src/input1.js', 'src/input2.js']
220 }
221 }
222 }
223});
224```
225
226#### No mangling
227
228Specify `mangle: false` to prevent changes to your variable and function names.
229
230```js
231// Project configuration.
232grunt.initConfig({
233 uglify: {
234 options: {
235 mangle: false
236 },
237 my_target: {
238 files: {
239 'dest/output.min.js': ['src/input.js']
240 }
241 }
242 }
243});
244```
245
246#### Reserved identifiers
247
248You can specify identifiers to leave untouched with an `reserved` array in the `mangle` options.
249
250```js
251// Project configuration.
252grunt.initConfig({
253 uglify: {
254 options: {
255 mangle: {
256 reserved: ['jQuery', 'Backbone']
257 }
258 },
259 my_target: {
260 files: {
261 'dest/output.min.js': ['src/input.js']
262 }
263 }
264 }
265});
266```
267
268#### Source maps
269
270Generate a source map by setting the `sourceMap` option to `true`. The generated
271source map will be in the same directory as the destination file. Its name will be
272the basename of the destination file with a `.map` extension. Override these
273defaults with the `sourceMapName` attribute.
274
275```js
276// Project configuration.
277grunt.initConfig({
278 uglify: {
279 my_target: {
280 options: {
281 sourceMap: true,
282 sourceMapName: 'path/to/sourcemap.map'
283 },
284 files: {
285 'dest/output.min.js': ['src/input.js']
286 }
287 }
288 }
289});
290```
291
292#### Advanced source maps
293
294Set the `sourceMap.includeSources` option to `true` to embed your sources directly into the map. To include
295a source map from a previous compilation pass it as the value of the `sourceMapIn` option.
296
297```js
298// Project configuration.
299grunt.initConfig({
300 uglify: {
301 my_target: {
302 options: {
303 sourceMap: {
304 includeSources: true
305 },
306 sourceMapIn: 'example/coffeescript-sourcemap.js', // input sourcemap from a previous compilation
307 },
308 files: {
309 'dest/output.min.js': ['src/input.js'],
310 },
311 },
312 },
313});
314```
315
316Refer to the [UglifyJS SourceMap Documentation](https://github.com/mishoo/UglifyJS2#source-map-options) for more information.
317
318#### Turn off console warnings
319
320Specify `drop_console: true` as part of the `compress` options to discard calls to `console.*` functions.
321This will suppress warning messages in the console.
322
323```js
324// Project configuration.
325grunt.initConfig({
326 uglify: {
327 options: {
328 compress: {
329 drop_console: true
330 }
331 },
332 my_target: {
333 files: {
334 'dest/output.min.js': ['src/input.js']
335 }
336 }
337 }
338});
339```
340
341#### Beautify
342
343Specify `beautify: true` to beautify your code for debugging/troubleshooting purposes.
344Pass an object to manually configure any other output options.
345
346See [UglifyJS documentation](https://github.com/mishoo/UglifyJS2#output-options) for more information.
347
348```js
349// Project configuration.
350grunt.initConfig({
351 uglify: {
352 my_target: {
353 options: {
354 beautify: true
355 },
356 files: {
357 'dest/output.min.js': ['src/input.js']
358 }
359 },
360 my_advanced_target: {
361 options: {
362 beautify: {
363 width: 80
364 }
365 },
366 files: {
367 'dest/output.min.js': ['src/input.js']
368 }
369 }
370 }
371});
372```
373
374#### Banner comments
375
376In this example, running `grunt uglify:my_target` will prepend a banner created by interpolating the `banner` template string with the config object. Here, those properties are the values imported from the `package.json` file (which are available via the `pkg` config property) plus today's date.
377
378_Note: you don't have to use an external JSON file. It's also valid to create the `pkg` object inline in the config. That being said, if you already have a JSON file, you might as well reference it._
379
380```js
381// Project configuration.
382grunt.initConfig({
383 pkg: grunt.file.readJSON('package.json'),
384 uglify: {
385 options: {
386 banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
387 '<%= grunt.template.today("yyyy-mm-dd") %> */'
388 },
389 my_target: {
390 files: {
391 'dest/output.min.js': ['src/input.js']
392 }
393 }
394 }
395});
396```
397
398#### Conditional compilation
399
400You can also enable UglifyJS conditional compilation. This is commonly used to remove debug code blocks for production builds. This is equivalent to the command line `--define` option.
401
402See [UglifyJS global definitions documentation](https://github.com/mishoo/UglifyJS2#conditional-compilation) for more information.
403
404```js
405// Project configuration.
406grunt.initConfig({
407 uglify: {
408 options: {
409 compress: {
410 global_defs: {
411 'DEBUG': false
412 },
413 dead_code: true
414 }
415 },
416 my_target: {
417 files: {
418 'dest/output.min.js': ['src/input.js']
419 }
420 }
421 }
422});
423```
424
425#### Compiling all files in a folder dynamically
426
427This configuration will compress and mangle the files dynamically.
428
429```js
430// Project configuration.
431grunt.initConfig({
432 uglify: {
433 my_target: {
434 files: [{
435 expand: true,
436 cwd: 'src/js',
437 src: '**/*.js',
438 dest: 'dest/js'
439 }]
440 }
441 }
442});
443```
444
445#### Compiling all files separately on the each their path
446
447This configuration will compress and mangle all js files separately in each folder.
448
449Also exclude jQuery for mangling and ignore all `*.min.js` files.
450
451```js
452// Project configuration.
453uglify: {
454 dev: {
455 options: {
456 mangle: {
457 reserved: ['jQuery']
458 }
459 },
460 files: [{
461 expand: true,
462 src: ['dist/assets/js/*.js', '!dist/assets/js/*.min.js'],
463 dest: 'dist/assets',
464 cwd: '.',
465 rename: function (dst, src) {
466 // To keep the source js files and make new files as `*.min.js`:
467 // return dst + '/' + src.replace('.js', '.min.js');
468 // Or to override to src:
469 return src;
470 }
471 }]
472 }
473},
474```
475
476#### Turn on object property name mangling
477
478This configuration will turn on object property name mangling, but not mangle built-in browser object properties.
479Additionally, variables and object properties listed in the `myExceptionsFile.json` will be mangled. For more info,
480on the format of the exception file format please see the [UglifyJS docs](https://www.npmjs.com/package/uglify-js).
481
482```js
483// Project configuration.
484grunt.initConfig({
485 uglify: {
486 options: {
487 mangle: {
488 properties: true
489 },
490 reserveDOMCache: true,
491 exceptionsFiles: [ 'myExceptionsFile.json' ]
492 },
493 my_target: {
494 files: {
495 'dest/output.min.js': ['src/input.js']
496 }
497 }
498 }
499});
500```
501
502#### Turn on use of name mangling cache
503
504Turn on use of name mangling cache to coordinate mangled symbols between outputted uglify files. uglify will the
505generate a JSON cache file with the name provided in the options. Note: this generated file uses the same JSON format
506as the `exceptionsFiles` files.
507
508```js
509// Project configuration.
510grunt.initConfig({
511 uglify: {
512 options: {
513 nameCache: '.tmp/grunt-uglify-cache.json',
514 },
515 my_target: {
516 files: {
517 'dest/output1.min.js': ['src/input1.js'],
518 'dest/output2.min.js': ['src/input2.js']
519 }
520 }
521 }
522});
523```
524
525
526## Release History
527
528 * 2020-07-16   v5.0.0   Requires node 10+. Update dependencies.
529 * 2019-03-21   v4.0.1   Update uglify-js to v3.5.0. Update dependencies.
530 * 2018-08-26   v4.0.0   Update dependencies. Requires Node.js >= 6.
531 * 2018-07-29   v3.4.0   Update uglify-js to v3.4.0.
532 * 2017-12-24   v3.3.0   Update uglify-js to v3.3.0.
533 * 2017-11-26   v3.2.1   Update uglify-js to v3.2.0.
534 * 2017-09-11   v3.1.0   Update uglify-js to v3.1.0.
535 * 2017-05-20   v3.0.1   Fix toplevel option.
536 * 2017-05-12   v3.0.0   Update uglify-js to v3.0.4.
537 * 2017-04-05   v2.3.0   Make CLI output less verbose.
538 * 2017-03-31   v2.2.1   Fix banner option.
539 * 2017-03-01   v2.2.0   Update uglify-js to v2.8.3.
540 * 2017-02-08   v2.1.0   Show size changes in output. Switch to `object.assign`.
541 * 2016-07-19   v2.0.0   Update uglify-js to v2.7.0. `screwIE8` is enabled by default.
542 * 2016-07-19   v1.0.2   Update grunt to ^1.0.0. Fix `beautify` when passed as an object. Fix docs about `report` values.
543 * 2016-03-16   v1.0.1   Downgrade maxmin for Node.js 0.10.
544 * 2016-03-04   v1.0.0   Use uglify-js ~2.6.2 to fix sourcemap issue. Improve docs for `global-defs` and `--define` options. Add `sourceMapUrl` option. Add `bare_returns` option. Optionally set report verbosity level using report option.
545 * 2016-01-29   v0.11.1   Update lodash to ^4.0.1. Update grunt-contrib-clean to ^0.7.0. Update grunt-contrib-jshint to ^0.12.0.
546 * 2015-11-20   v0.11.0   Update uglify-js to ~2.6.0.
547 * 2015-11-12   v0.10.1   Update uglify-js to ~2.5.
548 * 2015-10-27   v0.10.0   Update uglify-js to ^2.5.
549 * 2015-08-24   v0.9.2   Update uglify-js to ^2.4.24
550 * 2015-04-07   v0.9.1   More fixes for `mangle` options.
551 * 2015-04-07   v0.9.0   Add hook into uglify-js's mangling functionality.
552 * 2015-03-30   v0.8.1   Lock uglify-js to 2.4.17 due to breaking changes.
553 * 2015-02-19   v0.8.0   Add `screwIE8` option. Fix issue with explicit `compress` in Node.js 0.12.0.
554 * 2014-12-23   v0.7.0   Add `sourceMapRoot` options. Update readme descriptions. Remove reference to clean-css.
555 * 2014-09-17   v0.6.0   Output fixes. Add `ASCIIOnly` option. Other fixes.
556 * 2014-07-25   v0.5.1   Update chalk to ^0.5.1. Output updates.
557 * 2014-03-01   v0.4.0   Remove grunt-lib-contrib dependency and add more colors.
558 * 2014-02-27   v0.3.3   Remove unnecessary calls to `grunt.template.process`.
559 * 2014-01-22   v0.3.2   Fix handling of `sourceMapIncludeSources` option.
560 * 2014-01-20   v0.3.1   Fix relative path issue in sourcemaps.
561 * 2014-01-16   v0.3.0   Refactor sourcemap support.
562 * 2013-11-09   v0.2.7   Prepend banner if `sourceMap` option not set, addresses #109.
563 * 2013-11-08   v0.2.6   Merge #45, #53, #85 (#105 by way of duping #53). Add support for banners in uglified files with sourcemaps. Update docs.
564 * 2013-10-28   v0.2.5   Add warning for banners when using sourcemaps.
565 * 2013-09-02   v0.2.4   Update sourcemap format via #83.
566 * 2013-06-10   v0.2.3   Add `footer` option.
567 * 2013-05-31   v0.2.2   Revert #56 due to #58 until [chrome/239660](https://code.google.com/p/chromium/issues/detail?id=239660&q=sourcemappingurl&colspec=ID%20Pri%20M%20Iteration%20ReleaseBlock%20Cr%20Status%20Owner%20Summary%20OS%20Modified) [firefox/870361](https://bugzilla.mozilla.org/show_bug.cgi?id=870361) drop.
568 * 2013-05-22   v0.2.1   Update uglify-js to ~2.3.5 #55 #40. Change `sourcemappingUrl` syntax #56. Disable sorting of names for consistent mangling #44. Update docs for `sourceMapRoot` #47 #25.
569 * 2013-03-14   v0.2.0   No longer report gzip results by default. Support `report` option.
570 * 2013-01-30   v0.1.2   Add better error reporting. Support for dynamic names of multiple sourcemaps.
571 * 2013-02-15   v0.1.1   First official release for Grunt 0.4.0.
572 * 2013-01-18   v0.1.1rc6   Update grunt/gruntplugin dependencies to rc6. Change in-development grunt/gruntplugin dependency versions from tilde version ranges to specific versions.
573 * 2013-01-09   v0.1.1rc5   Update to work with grunt v0.4.0rc5. Switch back to `this.files` API.
574 * 2012-11-28   v0.1.0   Work in progress, not officially released yet.
575
576---
577
578Task submitted by ["Cowboy" Ben Alman](http://benalman.com)
579
580*This file was generated on Thu Jul 16 2020 10:08:35.*