1 | UglifyJS 3
|
2 | ==========
|
3 |
|
4 | UglifyJS is a JavaScript parser, minifier, compressor and beautifier toolkit.
|
5 |
|
6 | #### Note:
|
7 | - **`uglify-js@3` has a simplified [API](#api-reference) and [CLI](#command-line-usage) that is not backwards compatible with [`uglify-js@2`](https://github.com/mishoo/UglifyJS2/tree/v2.x)**.
|
8 | - **Documentation for UglifyJS `2.x` releases can be found [here](https://github.com/mishoo/UglifyJS2/tree/v2.x)**.
|
9 | - `uglify-js` only supports JavaScript (ECMAScript 5).
|
10 | - To minify ECMAScript 2015 or above, transpile using tools like [Babel](https://babeljs.io/).
|
11 |
|
12 | Install
|
13 | -------
|
14 |
|
15 | First make sure you have installed the latest version of [node.js](http://nodejs.org/)
|
16 | (You may need to restart your computer after this step).
|
17 |
|
18 | From NPM for use as a command line app:
|
19 |
|
20 | npm install uglify-js -g
|
21 |
|
22 | From NPM for programmatic use:
|
23 |
|
24 | npm install uglify-js
|
25 |
|
26 | # Command line usage
|
27 |
|
28 | uglifyjs [input files] [options]
|
29 |
|
30 | UglifyJS can take multiple input files. It's recommended that you pass the
|
31 | input files first, then pass the options. UglifyJS will parse input files
|
32 | in sequence and apply any compression options. The files are parsed in the
|
33 | same global scope, that is, a reference from a file to some
|
34 | variable/function declared in another file will be matched properly.
|
35 |
|
36 | If no input file is specified, UglifyJS will read from STDIN.
|
37 |
|
38 | If you wish to pass your options before the input files, separate the two with
|
39 | a double dash to prevent input files being used as option arguments:
|
40 |
|
41 | uglifyjs --compress --mangle -- input.js
|
42 |
|
43 | ### Command line options
|
44 |
|
45 | ```
|
46 | -h, --help Print usage information.
|
47 | `--help options` for details on available options.
|
48 | -V, --version Print version number.
|
49 | -p, --parse <options> Specify parser options:
|
50 | `acorn` Use Acorn for parsing.
|
51 | `bare_returns` Allow return outside of functions.
|
52 | Useful when minifying CommonJS
|
53 | modules and Userscripts that may
|
54 | be anonymous function wrapped (IIFE)
|
55 | by the .user.js engine `caller`.
|
56 | `expression` Parse a single expression, rather than
|
57 | a program (for parsing JSON).
|
58 | `spidermonkey` Assume input files are SpiderMonkey
|
59 | AST format (as JSON).
|
60 | -c, --compress [options] Enable compressor/specify compressor options:
|
61 | `pure_funcs` List of functions that can be safely
|
62 | removed when their return values are
|
63 | not used.
|
64 | -m, --mangle [options] Mangle names/specify mangler options:
|
65 | `reserved` List of names that should not be mangled.
|
66 | --mangle-props [options] Mangle properties/specify mangler options:
|
67 | `builtins` Mangle property names that overlaps
|
68 | with standard JavaScript globals.
|
69 | `debug` Add debug prefix and suffix.
|
70 | `domprops` Mangle property names that overlaps
|
71 | with DOM properties.
|
72 | `keep_quoted` Only mangle unquoted properties.
|
73 | `regex` Only mangle matched property names.
|
74 | `reserved` List of names that should not be mangled.
|
75 | -b, --beautify [options] Beautify output/specify output options:
|
76 | `beautify` Enabled with `--beautify` by default.
|
77 | `preamble` Preamble to prepend to the output. You
|
78 | can use this to insert a comment, for
|
79 | example for licensing information.
|
80 | This will not be parsed, but the source
|
81 | map will adjust for its presence.
|
82 | `quote_style` Quote style:
|
83 | 0 - auto
|
84 | 1 - single
|
85 | 2 - double
|
86 | 3 - original
|
87 | `wrap_iife` Wrap IIFEs in parenthesis. Note: you may
|
88 | want to disable `negate_iife` under
|
89 | compressor options.
|
90 | -O, --output-opts [options] Specify output options (`beautify` disabled by default).
|
91 | -o, --output <file> Output file path (default STDOUT). Specify `ast` or
|
92 | `spidermonkey` to write UglifyJS or SpiderMonkey AST
|
93 | as JSON to STDOUT respectively.
|
94 | --comments [filter] Preserve copyright comments in the output. By
|
95 | default this works like Google Closure, keeping
|
96 | JSDoc-style comments that contain "@license" or
|
97 | "@preserve". You can optionally pass one of the
|
98 | following arguments to this flag:
|
99 | - "all" to keep all comments
|
100 | - a valid JS RegExp like `/foo/` or `/^!/` to
|
101 | keep only matching comments.
|
102 | Note that currently not *all* comments can be
|
103 | kept when compression is on, because of dead
|
104 | code removal or cascading statements into
|
105 | sequences.
|
106 | --config-file <file> Read `minify()` options from JSON file.
|
107 | -d, --define <expr>[=value] Global definitions.
|
108 | -e, --enclose [arg[:value]] Embed everything in a big function, with configurable
|
109 | argument(s) & value(s).
|
110 | --ie8 Support non-standard Internet Explorer 8.
|
111 | Equivalent to setting `ie8: true` in `minify()`
|
112 | for `compress`, `mangle` and `output` options.
|
113 | By default UglifyJS will not try to be IE-proof.
|
114 | --keep-fnames Do not mangle/drop function names. Useful for
|
115 | code relying on Function.prototype.name.
|
116 | --name-cache <file> File to hold mangled name mappings.
|
117 | --self Build UglifyJS as a library (implies --wrap UglifyJS)
|
118 | --source-map [options] Enable source map/specify source map options:
|
119 | `base` Path to compute relative paths from input files.
|
120 | `content` Input source map, useful if you're compressing
|
121 | JS that was generated from some other original
|
122 | code. Specify "inline" if the source map is
|
123 | included within the sources.
|
124 | `filename` Filename and/or location of the output source
|
125 | (sets `file` attribute in source map).
|
126 | `includeSources` Pass this flag if you want to include
|
127 | the content of source files in the
|
128 | source map as sourcesContent property.
|
129 | `root` Path to the original source to be included in
|
130 | the source map.
|
131 | `url` If specified, path to the source map to append in
|
132 | `//# sourceMappingURL`.
|
133 | --timings Display operations run time on STDERR.
|
134 | --toplevel Compress and/or mangle variables in top level scope.
|
135 | --verbose Print diagnostic messages.
|
136 | --warn Print warning messages.
|
137 | --wrap <name> Embed everything in a big function, making the
|
138 | “exports” and “global” variables available. You
|
139 | need to pass an argument to this option to
|
140 | specify the name that your module will take
|
141 | when included in, say, a browser.
|
142 | ```
|
143 |
|
144 | Specify `--output` (`-o`) to declare the output file. Otherwise the output
|
145 | goes to STDOUT.
|
146 |
|
147 | ## CLI source map options
|
148 |
|
149 | UglifyJS can generate a source map file, which is highly useful for
|
150 | debugging your compressed JavaScript. To get a source map, pass
|
151 | `--source-map --output output.js` (source map will be written out to
|
152 | `output.js.map`).
|
153 |
|
154 | Additional options:
|
155 |
|
156 | - `--source-map "filename='<NAME>'"` to specify the name of the source map. The value of
|
157 | `filename` is only used to set `file` attribute (see [the spec][sm-spec])
|
158 | in source map file.
|
159 |
|
160 | - `--source-map "root='<URL>'"` to pass the URL where the original files can be found.
|
161 |
|
162 | - `--source-map "url='<URL>'"` to specify the URL where the source map can be found.
|
163 | Otherwise UglifyJS assumes HTTP `X-SourceMap` is being used and will omit the
|
164 | `//# sourceMappingURL=` directive.
|
165 |
|
166 | For example:
|
167 |
|
168 | uglifyjs js/file1.js js/file2.js \
|
169 | -o foo.min.js -c -m \
|
170 | --source-map "root='http://foo.com/src',url='foo.min.js.map'"
|
171 |
|
172 | The above will compress and mangle `file1.js` and `file2.js`, will drop the
|
173 | output in `foo.min.js` and the source map in `foo.min.js.map`. The source
|
174 | mapping will refer to `http://foo.com/src/js/file1.js` and
|
175 | `http://foo.com/src/js/file2.js` (in fact it will list `http://foo.com/src`
|
176 | as the source map root, and the original files as `js/file1.js` and
|
177 | `js/file2.js`).
|
178 |
|
179 | ### Composed source map
|
180 |
|
181 | When you're compressing JS code that was output by a compiler such as
|
182 | CoffeeScript, mapping to the JS code won't be too helpful. Instead, you'd
|
183 | like to map back to the original code (i.e. CoffeeScript). UglifyJS has an
|
184 | option to take an input source map. Assuming you have a mapping from
|
185 | CoffeeScript → compiled JS, UglifyJS can generate a map from CoffeeScript →
|
186 | compressed JS by mapping every token in the compiled JS to its original
|
187 | location.
|
188 |
|
189 | To use this feature pass `--source-map "content='/path/to/input/source.map'"`
|
190 | or `--source-map "content=inline"` if the source map is included inline with
|
191 | the sources.
|
192 |
|
193 | ## CLI compress options
|
194 |
|
195 | You need to pass `--compress` (`-c`) to enable the compressor. Optionally
|
196 | you can pass a comma-separated list of [compress options](#compress-options).
|
197 |
|
198 | Options are in the form `foo=bar`, or just `foo` (the latter implies
|
199 | a boolean option that you want to set `true`; it's effectively a
|
200 | shortcut for `foo=true`).
|
201 |
|
202 | Example:
|
203 |
|
204 | uglifyjs file.js -c toplevel,sequences=false
|
205 |
|
206 | ## CLI mangle options
|
207 |
|
208 | To enable the mangler you need to pass `--mangle` (`-m`). The following
|
209 | (comma-separated) options are supported:
|
210 |
|
211 | - `toplevel` (default `false`) -- mangle names declared in the top level scope.
|
212 |
|
213 | - `eval` (default `false`) -- mangle names visible in scopes where `eval` or `with` are used.
|
214 |
|
215 | When mangling is enabled but you want to prevent certain names from being
|
216 | mangled, you can declare those names with `--mangle reserved` — pass a
|
217 | comma-separated list of names. For example:
|
218 |
|
219 | uglifyjs ... -m reserved=['$','require','exports']
|
220 |
|
221 | to prevent the `require`, `exports` and `$` names from being changed.
|
222 |
|
223 | ### CLI mangling property names (`--mangle-props`)
|
224 |
|
225 | **Note:** THIS WILL PROBABLY BREAK YOUR CODE. Mangling property names
|
226 | is a separate step, different from variable name mangling. Pass
|
227 | `--mangle-props` to enable it. It will mangle all properties in the
|
228 | input code with the exception of built in DOM properties and properties
|
229 | in core JavaScript classes. For example:
|
230 |
|
231 | ```javascript
|
232 | // example.js
|
233 | var x = {
|
234 | baz_: 0,
|
235 | foo_: 1,
|
236 | calc: function() {
|
237 | return this.foo_ + this.baz_;
|
238 | }
|
239 | };
|
240 | x.bar_ = 2;
|
241 | x["baz_"] = 3;
|
242 | console.log(x.calc());
|
243 | ```
|
244 | Mangle all properties (except for JavaScript `builtins`):
|
245 | ```bash
|
246 | $ uglifyjs example.js -c -m --mangle-props
|
247 | ```
|
248 | ```javascript
|
249 | var x={o:0,_:1,l:function(){return this._+this.o}};x.t=2,x.o=3,console.log(x.l());
|
250 | ```
|
251 | Mangle all properties except for `reserved` properties:
|
252 | ```bash
|
253 | $ uglifyjs example.js -c -m --mangle-props reserved=[foo_,bar_]
|
254 | ```
|
255 | ```javascript
|
256 | var x={o:0,foo_:1,_:function(){return this.foo_+this.o}};x.bar_=2,x.o=3,console.log(x._());
|
257 | ```
|
258 | Mangle all properties matching a `regex`:
|
259 | ```bash
|
260 | $ uglifyjs example.js -c -m --mangle-props regex=/_$/
|
261 | ```
|
262 | ```javascript
|
263 | var x={o:0,_:1,calc:function(){return this._+this.o}};x.l=2,x.o=3,console.log(x.calc());
|
264 | ```
|
265 |
|
266 | Combining mangle properties options:
|
267 | ```bash
|
268 | $ uglifyjs example.js -c -m --mangle-props regex=/_$/,reserved=[bar_]
|
269 | ```
|
270 | ```javascript
|
271 | var x={o:0,_:1,calc:function(){return this._+this.o}};x.bar_=2,x.o=3,console.log(x.calc());
|
272 | ```
|
273 |
|
274 | In order for this to be of any use, we avoid mangling standard JS names by
|
275 | default (`--mangle-props builtins` to override).
|
276 |
|
277 | A default exclusion file is provided in `tools/domprops.json` which should
|
278 | cover most standard JS and DOM properties defined in various browsers. Pass
|
279 | `--mangle-props domprops` to disable this feature.
|
280 |
|
281 | A regular expression can be used to define which property names should be
|
282 | mangled. For example, `--mangle-props regex=/^_/` will only mangle property
|
283 | names that start with an underscore.
|
284 |
|
285 | When you compress multiple files using this option, in order for them to
|
286 | work together in the end we need to ensure somehow that one property gets
|
287 | mangled to the same name in all of them. For this, pass `--name-cache filename.json`
|
288 | and UglifyJS will maintain these mappings in a file which can then be reused.
|
289 | It should be initially empty. Example:
|
290 |
|
291 | ```bash
|
292 | $ rm -f /tmp/cache.json # start fresh
|
293 | $ uglifyjs file1.js file2.js --mangle-props --name-cache /tmp/cache.json -o part1.js
|
294 | $ uglifyjs file3.js file4.js --mangle-props --name-cache /tmp/cache.json -o part2.js
|
295 | ```
|
296 |
|
297 | Now, `part1.js` and `part2.js` will be consistent with each other in terms
|
298 | of mangled property names.
|
299 |
|
300 | Using the name cache is not necessary if you compress all your files in a
|
301 | single call to UglifyJS.
|
302 |
|
303 | ### Mangling unquoted names (`--mangle-props keep_quoted`)
|
304 |
|
305 | Using quoted property name (`o["foo"]`) reserves the property name (`foo`)
|
306 | so that it is not mangled throughout the entire script even when used in an
|
307 | unquoted style (`o.foo`). Example:
|
308 |
|
309 | ```javascript
|
310 | // stuff.js
|
311 | var o = {
|
312 | "foo": 1,
|
313 | bar: 3
|
314 | };
|
315 | o.foo += o.bar;
|
316 | console.log(o.foo);
|
317 | ```
|
318 | ```bash
|
319 | $ uglifyjs stuff.js --mangle-props keep_quoted -c -m
|
320 | ```
|
321 | ```javascript
|
322 | var o={foo:1,o:3};o.foo+=o.o,console.log(o.foo);
|
323 | ```
|
324 |
|
325 | ### Debugging property name mangling
|
326 |
|
327 | You can also pass `--mangle-props debug` in order to mangle property names
|
328 | without completely obscuring them. For example the property `o.foo`
|
329 | would mangle to `o._$foo$_` with this option. This allows property mangling
|
330 | of a large codebase while still being able to debug the code and identify
|
331 | where mangling is breaking things.
|
332 |
|
333 | ```bash
|
334 | $ uglifyjs stuff.js --mangle-props debug -c -m
|
335 | ```
|
336 | ```javascript
|
337 | var o={_$foo$_:1,_$bar$_:3};o._$foo$_+=o._$bar$_,console.log(o._$foo$_);
|
338 | ```
|
339 |
|
340 | You can also pass a custom suffix using `--mangle-props debug=XYZ`. This would then
|
341 | mangle `o.foo` to `o._$foo$XYZ_`. You can change this each time you compile a
|
342 | script to identify how a property got mangled. One technique is to pass a
|
343 | random number on every compile to simulate mangling changing with different
|
344 | inputs (e.g. as you update the input script with new properties), and to help
|
345 | identify mistakes like writing mangled keys to storage.
|
346 |
|
347 |
|
348 | # API Reference
|
349 |
|
350 | Assuming installation via NPM, you can load UglifyJS in your application
|
351 | like this:
|
352 | ```javascript
|
353 | var UglifyJS = require("uglify-js");
|
354 | ```
|
355 |
|
356 | There is a single high level function, **`minify(code, options)`**,
|
357 | which will perform all minification [phases](#minify-options) in a configurable
|
358 | manner. By default `minify()` will enable the options [`compress`](#compress-options)
|
359 | and [`mangle`](#mangle-options). Example:
|
360 | ```javascript
|
361 | var code = "function add(first, second) { return first + second; }";
|
362 | var result = UglifyJS.minify(code);
|
363 | console.log(result.error); // runtime error, or `undefined` if no error
|
364 | console.log(result.code); // minified output: function add(n,d){return n+d}
|
365 | ```
|
366 |
|
367 | You can `minify` more than one JavaScript file at a time by using an object
|
368 | for the first argument where the keys are file names and the values are source
|
369 | code:
|
370 | ```javascript
|
371 | var code = {
|
372 | "file1.js": "function add(first, second) { return first + second; }",
|
373 | "file2.js": "console.log(add(1 + 2, 3 + 4));"
|
374 | };
|
375 | var result = UglifyJS.minify(code);
|
376 | console.log(result.code);
|
377 | // function add(d,n){return d+n}console.log(add(3,7));
|
378 | ```
|
379 |
|
380 | The `toplevel` option:
|
381 | ```javascript
|
382 | var code = {
|
383 | "file1.js": "function add(first, second) { return first + second; }",
|
384 | "file2.js": "console.log(add(1 + 2, 3 + 4));"
|
385 | };
|
386 | var options = { toplevel: true };
|
387 | var result = UglifyJS.minify(code, options);
|
388 | console.log(result.code);
|
389 | // console.log(3+7);
|
390 | ```
|
391 |
|
392 | The `nameCache` option:
|
393 | ```javascript
|
394 | var options = {
|
395 | mangle: {
|
396 | toplevel: true,
|
397 | },
|
398 | nameCache: {}
|
399 | };
|
400 | var result1 = UglifyJS.minify({
|
401 | "file1.js": "function add(first, second) { return first + second; }"
|
402 | }, options);
|
403 | var result2 = UglifyJS.minify({
|
404 | "file2.js": "console.log(add(1 + 2, 3 + 4));"
|
405 | }, options);
|
406 | console.log(result1.code);
|
407 | // function n(n,r){return n+r}
|
408 | console.log(result2.code);
|
409 | // console.log(n(3,7));
|
410 | ```
|
411 |
|
412 | You may persist the name cache to the file system in the following way:
|
413 | ```javascript
|
414 | var cacheFileName = "/tmp/cache.json";
|
415 | var options = {
|
416 | mangle: {
|
417 | properties: true,
|
418 | },
|
419 | nameCache: JSON.parse(fs.readFileSync(cacheFileName, "utf8"))
|
420 | };
|
421 | fs.writeFileSync("part1.js", UglifyJS.minify({
|
422 | "file1.js": fs.readFileSync("file1.js", "utf8"),
|
423 | "file2.js": fs.readFileSync("file2.js", "utf8")
|
424 | }, options).code, "utf8");
|
425 | fs.writeFileSync("part2.js", UglifyJS.minify({
|
426 | "file3.js": fs.readFileSync("file3.js", "utf8"),
|
427 | "file4.js": fs.readFileSync("file4.js", "utf8")
|
428 | }, options).code, "utf8");
|
429 | fs.writeFileSync(cacheFileName, JSON.stringify(options.nameCache), "utf8");
|
430 | ```
|
431 |
|
432 | An example of a combination of `minify()` options:
|
433 | ```javascript
|
434 | var code = {
|
435 | "file1.js": "function add(first, second) { return first + second; }",
|
436 | "file2.js": "console.log(add(1 + 2, 3 + 4));"
|
437 | };
|
438 | var options = {
|
439 | toplevel: true,
|
440 | compress: {
|
441 | global_defs: {
|
442 | "@console.log": "alert"
|
443 | },
|
444 | passes: 2
|
445 | },
|
446 | output: {
|
447 | beautify: false,
|
448 | preamble: "/* uglified */"
|
449 | }
|
450 | };
|
451 | var result = UglifyJS.minify(code, options);
|
452 | console.log(result.code);
|
453 | // /* uglified */
|
454 | // alert(10);"
|
455 | ```
|
456 |
|
457 | To produce warnings:
|
458 | ```javascript
|
459 | var code = "function f(){ var u; return 2 + 3; }";
|
460 | var options = { warnings: true };
|
461 | var result = UglifyJS.minify(code, options);
|
462 | console.log(result.error); // runtime error, `undefined` in this case
|
463 | console.log(result.warnings); // [ 'Dropping unused variable u [0:1,18]' ]
|
464 | console.log(result.code); // function f(){return 5}
|
465 | ```
|
466 |
|
467 | An error example:
|
468 | ```javascript
|
469 | var result = UglifyJS.minify({"foo.js" : "if (0) else console.log(1);"});
|
470 | console.log(JSON.stringify(result.error));
|
471 | // {"message":"Unexpected token: keyword (else)","filename":"foo.js","line":1,"col":7,"pos":7}
|
472 | ```
|
473 | Note: unlike `uglify-js@2.x`, the `3.x` API does not throw errors. To
|
474 | achieve a similar effect one could do the following:
|
475 | ```javascript
|
476 | var result = UglifyJS.minify(code, options);
|
477 | if (result.error) throw result.error;
|
478 | ```
|
479 |
|
480 | ## Minify options
|
481 |
|
482 | - `compress` (default `{}`) — pass `false` to skip compressing entirely.
|
483 | Pass an object to specify custom [compress options](#compress-options).
|
484 |
|
485 | - `ie8` (default `false`) -- set to `true` to support IE8.
|
486 |
|
487 | - `keep_fnames` (default: `false`) -- pass `true` to prevent discarding or mangling
|
488 | of function names. Useful for code relying on `Function.prototype.name`.
|
489 |
|
490 | - `mangle` (default `true`) — pass `false` to skip mangling names, or pass
|
491 | an object to specify [mangle options](#mangle-options) (see below).
|
492 |
|
493 | - `mangle.properties` (default `false`) — a subcategory of the mangle option.
|
494 | Pass an object to specify custom [mangle property options](#mangle-properties-options).
|
495 |
|
496 | - `nameCache` (default `null`) -- pass an empty object `{}` or a previously
|
497 | used `nameCache` object if you wish to cache mangled variable and
|
498 | property names across multiple invocations of `minify()`. Note: this is
|
499 | a read/write property. `minify()` will read the name cache state of this
|
500 | object and update it during minification so that it may be
|
501 | reused or externally persisted by the user.
|
502 |
|
503 | - `output` (default `null`) — pass an object if you wish to specify
|
504 | additional [output options](#output-options). The defaults are optimized
|
505 | for best compression.
|
506 |
|
507 | - `parse` (default `{}`) — pass an object if you wish to specify some
|
508 | additional [parse options](#parse-options).
|
509 |
|
510 | - `sourceMap` (default `false`) -- pass an object if you wish to specify
|
511 | [source map options](#source-map-options).
|
512 |
|
513 | - `toplevel` (default `false`) -- set to `true` if you wish to enable top level
|
514 | variable and function name mangling and to drop unused variables and functions.
|
515 |
|
516 | - `warnings` (default `false`) — pass `true` to return compressor warnings
|
517 | in `result.warnings`. Use the value `"verbose"` for more detailed warnings.
|
518 |
|
519 | ## Minify options structure
|
520 |
|
521 | ```javascript
|
522 | {
|
523 | parse: {
|
524 | // parse options
|
525 | },
|
526 | compress: {
|
527 | // compress options
|
528 | },
|
529 | mangle: {
|
530 | // mangle options
|
531 |
|
532 | properties: {
|
533 | // mangle property options
|
534 | }
|
535 | },
|
536 | output: {
|
537 | // output options
|
538 | },
|
539 | sourceMap: {
|
540 | // source map options
|
541 | },
|
542 | nameCache: null, // or specify a name cache object
|
543 | toplevel: false,
|
544 | ie8: false,
|
545 | warnings: false,
|
546 | }
|
547 | ```
|
548 |
|
549 | ### Source map options
|
550 |
|
551 | To generate a source map:
|
552 | ```javascript
|
553 | var result = UglifyJS.minify({"file1.js": "var a = function() {};"}, {
|
554 | sourceMap: {
|
555 | filename: "out.js",
|
556 | url: "out.js.map"
|
557 | }
|
558 | });
|
559 | console.log(result.code); // minified output
|
560 | console.log(result.map); // source map
|
561 | ```
|
562 |
|
563 | Note that the source map is not saved in a file, it's just returned in
|
564 | `result.map`. The value passed for `sourceMap.url` is only used to set
|
565 | `//# sourceMappingURL=out.js.map` in `result.code`. The value of
|
566 | `filename` is only used to set `file` attribute (see [the spec][sm-spec])
|
567 | in source map file.
|
568 |
|
569 | You can set option `sourceMap.url` to be `"inline"` and source map will
|
570 | be appended to code.
|
571 |
|
572 | You can also specify sourceRoot property to be included in source map:
|
573 | ```javascript
|
574 | var result = UglifyJS.minify({"file1.js": "var a = function() {};"}, {
|
575 | sourceMap: {
|
576 | root: "http://example.com/src",
|
577 | url: "out.js.map"
|
578 | }
|
579 | });
|
580 | ```
|
581 |
|
582 | If you're compressing compiled JavaScript and have a source map for it, you
|
583 | can use `sourceMap.content`:
|
584 | ```javascript
|
585 | var result = UglifyJS.minify({"compiled.js": "compiled code"}, {
|
586 | sourceMap: {
|
587 | content: "content from compiled.js.map",
|
588 | url: "minified.js.map"
|
589 | }
|
590 | });
|
591 | // same as before, it returns `code` and `map`
|
592 | ```
|
593 |
|
594 | If you're using the `X-SourceMap` header instead, you can just omit `sourceMap.url`.
|
595 |
|
596 | ## Parse options
|
597 |
|
598 | - `bare_returns` (default `false`) -- support top level `return` statements
|
599 |
|
600 | - `html5_comments` (default `true`)
|
601 |
|
602 | - `shebang` (default `true`) -- support `#!command` as the first line
|
603 |
|
604 | ## Compress options
|
605 |
|
606 | - `arguments` (default: `true`) -- replace `arguments[index]` with function
|
607 | parameter name whenever possible.
|
608 |
|
609 | - `assignments` (default: `true`) -- apply optimizations to assignment expressions.
|
610 |
|
611 | - `booleans` (default: `true`) -- various optimizations for boolean context,
|
612 | for example `!!a ? b : c → a ? b : c`
|
613 |
|
614 | - `collapse_vars` (default: `true`) -- Collapse single-use non-constant variables,
|
615 | side effects permitting.
|
616 |
|
617 | - `comparisons` (default: `true`) -- apply certain optimizations to binary nodes,
|
618 | e.g. `!(a <= b) → a > b`, attempts to negate binary nodes, e.g.
|
619 | `a = !b && !c && !d && !e → a=!(b||c||d||e)` etc.
|
620 |
|
621 | - `conditionals` (default: `true`) -- apply optimizations for `if`-s and conditional
|
622 | expressions
|
623 |
|
624 | - `dead_code` (default: `true`) -- remove unreachable code
|
625 |
|
626 | - `directives` (default: `true`) -- remove redundant or non-standard directives
|
627 |
|
628 | - `drop_console` (default: `false`) -- Pass `true` to discard calls to
|
629 | `console.*` functions. If you wish to drop a specific function call
|
630 | such as `console.info` and/or retain side effects from function arguments
|
631 | after dropping the function call then use `pure_funcs` instead.
|
632 |
|
633 | - `drop_debugger` (default: `true`) -- remove `debugger;` statements
|
634 |
|
635 | - `evaluate` (default: `true`) -- Evaluate expression for shorter constant
|
636 | representation. Pass `"eager"` to always replace function calls whenever
|
637 | possible, or a positive integer to specify an upper bound for each individual
|
638 | evaluation in number of characters.
|
639 |
|
640 | - `expression` (default: `false`) -- Pass `true` to preserve completion values
|
641 | from terminal statements without `return`, e.g. in bookmarklets.
|
642 |
|
643 | - `functions` (default: `true`) -- convert declarations from `var`to `function`
|
644 | whenever possible.
|
645 |
|
646 | - `global_defs` (default: `{}`) -- see [conditional compilation](#conditional-compilation)
|
647 |
|
648 | - `hoist_funs` (default: `false`) -- hoist function declarations
|
649 |
|
650 | - `hoist_props` (default: `true`) -- hoist properties from constant object and
|
651 | array literals into regular variables subject to a set of constraints. For example:
|
652 | `var o={p:1, q:2}; f(o.p, o.q);` is converted to `f(1, 2);`. Note: `hoist_props`
|
653 | works best with `mangle` enabled, the `compress` option `passes` set to `2` or higher,
|
654 | and the `compress` option `toplevel` enabled.
|
655 |
|
656 | - `hoist_vars` (default: `false`) -- hoist `var` declarations (this is `false`
|
657 | by default because it seems to increase the size of the output in general)
|
658 |
|
659 | - `if_return` (default: `true`) -- optimizations for if/return and if/continue
|
660 |
|
661 | - `inline` (default: `true`) -- inline calls to function with simple/`return` statement:
|
662 | - `false` -- same as `0`
|
663 | - `0` -- disabled inlining
|
664 | - `1` -- inline simple functions
|
665 | - `2` -- inline functions with arguments
|
666 | - `3` -- inline functions with arguments and variables
|
667 | - `true` -- same as `3`
|
668 |
|
669 | - `join_vars` (default: `true`) -- join consecutive `var` statements
|
670 |
|
671 | - `keep_fargs` (default: `strict`) -- Discard unused function arguments. Code
|
672 | which relies on `Function.length` will break if this is done indiscriminately,
|
673 | i.e. when passing `true`. Pass `false` to always retain function arguments.
|
674 |
|
675 | - `keep_fnames` (default: `false`) -- Pass `true` to prevent the
|
676 | compressor from discarding function names. Useful for code relying on
|
677 | `Function.prototype.name`. See also: the `keep_fnames` [mangle option](#mangle-options).
|
678 |
|
679 | - `keep_infinity` (default: `false`) -- Pass `true` to prevent `Infinity` from
|
680 | being compressed into `1/0`, which may cause performance issues on Chrome.
|
681 |
|
682 | - `loops` (default: `true`) -- optimizations for `do`, `while` and `for` loops
|
683 | when we can statically determine the condition.
|
684 |
|
685 | - `negate_iife` (default: `true`) -- negate "Immediately-Called Function Expressions"
|
686 | where the return value is discarded, to avoid the parens that the
|
687 | code generator would insert.
|
688 |
|
689 | - `objects` (default: `true`) -- compact duplicate keys in object literals.
|
690 |
|
691 | - `passes` (default: `1`) -- The maximum number of times to run compress.
|
692 | In some cases more than one pass leads to further compressed code. Keep in
|
693 | mind more passes will take more time.
|
694 |
|
695 | - `properties` (default: `true`) -- rewrite property access using the dot notation, for
|
696 | example `foo["bar"] → foo.bar`
|
697 |
|
698 | - `pure_funcs` (default: `null`) -- You can pass an array of names and
|
699 | UglifyJS will assume that those functions do not produce side
|
700 | effects. DANGER: will not check if the name is redefined in scope.
|
701 | An example case here, for instance `var q = Math.floor(a/b)`. If
|
702 | variable `q` is not used elsewhere, UglifyJS will drop it, but will
|
703 | still keep the `Math.floor(a/b)`, not knowing what it does. You can
|
704 | pass `pure_funcs: [ 'Math.floor' ]` to let it know that this
|
705 | function won't produce any side effect, in which case the whole
|
706 | statement would get discarded. The current implementation adds some
|
707 | overhead (compression will be slower). Make sure symbols under `pure_funcs`
|
708 | are also under `mangle.reserved` to avoid mangling.
|
709 |
|
710 | - `pure_getters` (default: `"strict"`) -- If you pass `true` for
|
711 | this, UglifyJS will assume that object property access
|
712 | (e.g. `foo.bar` or `foo["bar"]`) doesn't have any side effects.
|
713 | Specify `"strict"` to treat `foo.bar` as side-effect-free only when
|
714 | `foo` is certain to not throw, i.e. not `null` or `undefined`.
|
715 |
|
716 | - `reduce_funcs` (default: `true`) -- Allows single-use functions to be
|
717 | inlined as function expressions when permissible allowing further
|
718 | optimization. Enabled by default. Option depends on `reduce_vars`
|
719 | being enabled. Some code runs faster in the Chrome V8 engine if this
|
720 | option is disabled. Does not negatively impact other major browsers.
|
721 |
|
722 | - `reduce_vars` (default: `true`) -- Improve optimization on variables assigned with and
|
723 | used as constant values.
|
724 |
|
725 | - `sequences` (default: `true`) -- join consecutive simple statements using the
|
726 | comma operator. May be set to a positive integer to specify the maximum number
|
727 | of consecutive comma sequences that will be generated. If this option is set to
|
728 | `true` then the default `sequences` limit is `200`. Set option to `false` or `0`
|
729 | to disable. The smallest `sequences` length is `2`. A `sequences` value of `1`
|
730 | is grandfathered to be equivalent to `true` and as such means `200`. On rare
|
731 | occasions the default sequences limit leads to very slow compress times in which
|
732 | case a value of `20` or less is recommended.
|
733 |
|
734 | - `side_effects` (default: `true`) -- Pass `false` to disable potentially dropping
|
735 | functions marked as "pure". A function call is marked as "pure" if a comment
|
736 | annotation `/*@__PURE__*/` or `/*#__PURE__*/` immediately precedes the call. For
|
737 | example: `/*@__PURE__*/foo();`
|
738 |
|
739 | - `switches` (default: `true`) -- de-duplicate and remove unreachable `switch` branches
|
740 |
|
741 | - `toplevel` (default: `false`) -- drop unreferenced functions (`"funcs"`) and/or
|
742 | variables (`"vars"`) in the top level scope (`false` by default, `true` to drop
|
743 | both unreferenced functions and variables)
|
744 |
|
745 | - `top_retain` (default: `null`) -- prevent specific toplevel functions and
|
746 | variables from `unused` removal (can be array, comma-separated, RegExp or
|
747 | function. Implies `toplevel`)
|
748 |
|
749 | - `typeofs` (default: `true`) -- Transforms `typeof foo == "undefined"` into
|
750 | `foo === void 0`. Note: recommend to set this value to `false` for IE10 and
|
751 | earlier versions due to known issues.
|
752 |
|
753 | - `unsafe` (default: `false`) -- apply "unsafe" transformations (discussion below)
|
754 |
|
755 | - `unsafe_comps` (default: `false`) -- compress expressions like `a <= b` assuming
|
756 | none of the operands can be (coerced to) `NaN`.
|
757 |
|
758 | - `unsafe_Function` (default: `false`) -- compress and mangle `Function(args, code)`
|
759 | when both `args` and `code` are string literals.
|
760 |
|
761 | - `unsafe_math` (default: `false`) -- optimize numerical expressions like
|
762 | `2 * x * 3` into `6 * x`, which may give imprecise floating point results.
|
763 |
|
764 | - `unsafe_proto` (default: `false`) -- optimize expressions like
|
765 | `Array.prototype.slice.call(a)` into `[].slice.call(a)`
|
766 |
|
767 | - `unsafe_regexp` (default: `false`) -- enable substitutions of variables with
|
768 | `RegExp` values the same way as if they are constants.
|
769 |
|
770 | - `unsafe_undefined` (default: `false`) -- substitute `void 0` if there is a
|
771 | variable named `undefined` in scope (variable name will be mangled, typically
|
772 | reduced to a single character)
|
773 |
|
774 | - `unused` (default: `true`) -- drop unreferenced functions and variables (simple
|
775 | direct variable assignments do not count as references unless set to `"keep_assign"`)
|
776 |
|
777 | ## Mangle options
|
778 |
|
779 | - `eval` (default `false`) -- Pass `true` to mangle names visible in scopes
|
780 | where `eval` or `with` are used.
|
781 |
|
782 | - `keep_fnames` (default `false`) -- Pass `true` to not mangle function names.
|
783 | Useful for code relying on `Function.prototype.name`. See also: the `keep_fnames`
|
784 | [compress option](#compress-options).
|
785 |
|
786 | - `reserved` (default `[]`) -- Pass an array of identifiers that should be
|
787 | excluded from mangling. Example: `["foo", "bar"]`.
|
788 |
|
789 | - `toplevel` (default `false`) -- Pass `true` to mangle names declared in the
|
790 | top level scope.
|
791 |
|
792 | Examples:
|
793 |
|
794 | ```javascript
|
795 | // test.js
|
796 | var globalVar;
|
797 | function funcName(firstLongName, anotherLongName) {
|
798 | var myVariable = firstLongName + anotherLongName;
|
799 | }
|
800 | ```
|
801 | ```javascript
|
802 | var code = fs.readFileSync("test.js", "utf8");
|
803 |
|
804 | UglifyJS.minify(code).code;
|
805 | // 'function funcName(a,n){}var globalVar;'
|
806 |
|
807 | UglifyJS.minify(code, { mangle: { reserved: ['firstLongName'] } }).code;
|
808 | // 'function funcName(firstLongName,a){}var globalVar;'
|
809 |
|
810 | UglifyJS.minify(code, { mangle: { toplevel: true } }).code;
|
811 | // 'function n(n,a){}var a;'
|
812 | ```
|
813 |
|
814 | ### Mangle properties options
|
815 |
|
816 | - `builtins` (default: `false`) -- Use `true` to allow the mangling of builtin
|
817 | DOM properties. Not recommended to override this setting.
|
818 |
|
819 | - `debug` (default: `false`) -— Mangle names with the original name still present.
|
820 | Pass an empty string `""` to enable, or a non-empty string to set the debug suffix.
|
821 |
|
822 | - `keep_quoted` (default: `false`) -— Only mangle unquoted property names.
|
823 |
|
824 | - `regex` (default: `null`) -— Pass a RegExp literal to only mangle property
|
825 | names matching the regular expression.
|
826 |
|
827 | - `reserved` (default: `[]`) -- Do not mangle property names listed in the
|
828 | `reserved` array.
|
829 |
|
830 | ## Output options
|
831 |
|
832 | The code generator tries to output shortest code possible by default. In
|
833 | case you want beautified output, pass `--beautify` (`-b`). Optionally you
|
834 | can pass additional arguments that control the code output:
|
835 |
|
836 | - `ascii_only` (default `false`) -- escape Unicode characters in strings and
|
837 | regexps (affects directives with non-ascii characters becoming invalid)
|
838 |
|
839 | - `beautify` (default `true`) -- whether to actually beautify the output.
|
840 | Passing `-b` will set this to true, but you might need to pass `-b` even
|
841 | when you want to generate minified code, in order to specify additional
|
842 | arguments, so you can use `-b beautify=false` to override it.
|
843 |
|
844 | - `braces` (default `false`) -- always insert braces in `if`, `for`,
|
845 | `do`, `while` or `with` statements, even if their body is a single
|
846 | statement.
|
847 |
|
848 | - `comments` (default `false`) -- pass `true` or `"all"` to preserve all
|
849 | comments, `"some"` to preserve some comments, a regular expression string
|
850 | (e.g. `/^!/`) or a function.
|
851 |
|
852 | - `indent_level` (default `4`)
|
853 |
|
854 | - `indent_start` (default `0`) -- prefix all lines by that many spaces
|
855 |
|
856 | - `inline_script` (default `true`) -- escape HTML comments and the slash in
|
857 | occurrences of `</script>` in strings
|
858 |
|
859 | - `keep_quoted_props` (default `false`) -- when turned on, prevents stripping
|
860 | quotes from property names in object literals.
|
861 |
|
862 | - `max_line_len` (default `false`) -- maximum line length (for uglified code)
|
863 |
|
864 | - `preamble` (default `null`) -- when passed it must be a string and
|
865 | it will be prepended to the output literally. The source map will
|
866 | adjust for this text. Can be used to insert a comment containing
|
867 | licensing information, for example.
|
868 |
|
869 | - `preserve_line` (default `false`) -- pass `true` to retain line numbering on
|
870 | a best effort basis.
|
871 |
|
872 | - `quote_keys` (default `false`) -- pass `true` to quote all keys in literal
|
873 | objects
|
874 |
|
875 | - `quote_style` (default `0`) -- preferred quote style for strings (affects
|
876 | quoted property names and directives as well):
|
877 | - `0` -- prefers double quotes, switches to single quotes when there are
|
878 | more double quotes in the string itself. `0` is best for gzip size.
|
879 | - `1` -- always use single quotes
|
880 | - `2` -- always use double quotes
|
881 | - `3` -- always use the original quotes
|
882 |
|
883 | - `semicolons` (default `true`) -- separate statements with semicolons. If
|
884 | you pass `false` then whenever possible we will use a newline instead of a
|
885 | semicolon, leading to more readable output of uglified code (size before
|
886 | gzip could be smaller; size after gzip insignificantly larger).
|
887 |
|
888 | - `shebang` (default `true`) -- preserve shebang `#!` in preamble (bash scripts)
|
889 |
|
890 | - `webkit` (default `false`) -- enable workarounds for WebKit bugs.
|
891 | PhantomJS users should set this option to `true`.
|
892 |
|
893 | - `width` (default `80`) -- only takes effect when beautification is on, this
|
894 | specifies an (orientative) line width that the beautifier will try to
|
895 | obey. It refers to the width of the line text (excluding indentation).
|
896 | It doesn't work very well currently, but it does make the code generated
|
897 | by UglifyJS more readable.
|
898 |
|
899 | - `wrap_iife` (default `false`) -- pass `true` to wrap immediately invoked
|
900 | function expressions. See
|
901 | [#640](https://github.com/mishoo/UglifyJS2/issues/640) for more details.
|
902 |
|
903 | # Miscellaneous
|
904 |
|
905 | ### Keeping copyright notices or other comments
|
906 |
|
907 | You can pass `--comments` to retain certain comments in the output. By
|
908 | default it will keep JSDoc-style comments that contain "@preserve",
|
909 | "@license" or "@cc_on" (conditional compilation for IE). You can pass
|
910 | `--comments all` to keep all the comments, or a valid JavaScript regexp to
|
911 | keep only comments that match this regexp. For example `--comments /^!/`
|
912 | will keep comments like `/*! Copyright Notice */`.
|
913 |
|
914 | Note, however, that there might be situations where comments are lost. For
|
915 | example:
|
916 | ```javascript
|
917 | function f() {
|
918 | /** @preserve Foo Bar */
|
919 | function g() {
|
920 | // this function is never called
|
921 | }
|
922 | return something();
|
923 | }
|
924 | ```
|
925 |
|
926 | Even though it has "@preserve", the comment will be lost because the inner
|
927 | function `g` (which is the AST node to which the comment is attached to) is
|
928 | discarded by the compressor as not referenced.
|
929 |
|
930 | The safest comments where to place copyright information (or other info that
|
931 | needs to be kept in the output) are comments attached to toplevel nodes.
|
932 |
|
933 | ### The `unsafe` `compress` option
|
934 |
|
935 | It enables some transformations that *might* break code logic in certain
|
936 | contrived cases, but should be fine for most code. You might want to try it
|
937 | on your own code, it should reduce the minified size. Here's what happens
|
938 | when this flag is on:
|
939 |
|
940 | - `new Array(1, 2, 3)` or `Array(1, 2, 3)` → `[ 1, 2, 3 ]`
|
941 | - `new Object()` → `{}`
|
942 | - `String(exp)` or `exp.toString()` → `"" + exp`
|
943 | - `new Object/RegExp/Function/Error/Array (...)` → we discard the `new`
|
944 |
|
945 | ### Conditional compilation
|
946 |
|
947 | You can use the `--define` (`-d`) switch in order to declare global
|
948 | variables that UglifyJS will assume to be constants (unless defined in
|
949 | scope). For example if you pass `--define DEBUG=false` then, coupled with
|
950 | dead code removal UglifyJS will discard the following from the output:
|
951 | ```javascript
|
952 | if (DEBUG) {
|
953 | console.log("debug stuff");
|
954 | }
|
955 | ```
|
956 |
|
957 | You can specify nested constants in the form of `--define env.DEBUG=false`.
|
958 |
|
959 | UglifyJS will warn about the condition being always false and about dropping
|
960 | unreachable code; for now there is no option to turn off only this specific
|
961 | warning, you can pass `warnings=false` to turn off *all* warnings.
|
962 |
|
963 | Another way of doing that is to declare your globals as constants in a
|
964 | separate file and include it into the build. For example you can have a
|
965 | `build/defines.js` file with the following:
|
966 | ```javascript
|
967 | var DEBUG = false;
|
968 | var PRODUCTION = true;
|
969 | // etc.
|
970 | ```
|
971 |
|
972 | and build your code like this:
|
973 |
|
974 | uglifyjs build/defines.js js/foo.js js/bar.js... -c
|
975 |
|
976 | UglifyJS will notice the constants and, since they cannot be altered, it
|
977 | will evaluate references to them to the value itself and drop unreachable
|
978 | code as usual. The build will contain the `const` declarations if you use
|
979 | them. If you are targeting < ES6 environments which does not support `const`,
|
980 | using `var` with `reduce_vars` (enabled by default) should suffice.
|
981 |
|
982 | ### Conditional compilation API
|
983 |
|
984 | You can also use conditional compilation via the programmatic API. With the difference that the
|
985 | property name is `global_defs` and is a compressor property:
|
986 |
|
987 | ```javascript
|
988 | var result = UglifyJS.minify(fs.readFileSync("input.js", "utf8"), {
|
989 | compress: {
|
990 | dead_code: true,
|
991 | global_defs: {
|
992 | DEBUG: false
|
993 | }
|
994 | }
|
995 | });
|
996 | ```
|
997 |
|
998 | To replace an identifier with an arbitrary non-constant expression it is
|
999 | necessary to prefix the `global_defs` key with `"@"` to instruct UglifyJS
|
1000 | to parse the value as an expression:
|
1001 | ```javascript
|
1002 | UglifyJS.minify("alert('hello');", {
|
1003 | compress: {
|
1004 | global_defs: {
|
1005 | "@alert": "console.log"
|
1006 | }
|
1007 | }
|
1008 | }).code;
|
1009 | // returns: 'console.log("hello");'
|
1010 | ```
|
1011 |
|
1012 | Otherwise it would be replaced as string literal:
|
1013 | ```javascript
|
1014 | UglifyJS.minify("alert('hello');", {
|
1015 | compress: {
|
1016 | global_defs: {
|
1017 | "alert": "console.log"
|
1018 | }
|
1019 | }
|
1020 | }).code;
|
1021 | // returns: '"console.log"("hello");'
|
1022 | ```
|
1023 |
|
1024 | ### Using native Uglify AST with `minify()`
|
1025 | ```javascript
|
1026 | // example: parse only, produce native Uglify AST
|
1027 |
|
1028 | var result = UglifyJS.minify(code, {
|
1029 | parse: {},
|
1030 | compress: false,
|
1031 | mangle: false,
|
1032 | output: {
|
1033 | ast: true,
|
1034 | code: false // optional - faster if false
|
1035 | }
|
1036 | });
|
1037 |
|
1038 | // result.ast contains native Uglify AST
|
1039 | ```
|
1040 | ```javascript
|
1041 | // example: accept native Uglify AST input and then compress and mangle
|
1042 | // to produce both code and native AST.
|
1043 |
|
1044 | var result = UglifyJS.minify(ast, {
|
1045 | compress: {},
|
1046 | mangle: {},
|
1047 | output: {
|
1048 | ast: true,
|
1049 | code: true // optional - faster if false
|
1050 | }
|
1051 | });
|
1052 |
|
1053 | // result.ast contains native Uglify AST
|
1054 | // result.code contains the minified code in string form.
|
1055 | ```
|
1056 |
|
1057 | ### Working with Uglify AST
|
1058 |
|
1059 | Transversal and transformation of the native AST can be performed through
|
1060 | [`TreeWalker`](https://github.com/mishoo/UglifyJS2/blob/master/lib/ast.js) and
|
1061 | [`TreeTransformer`](https://github.com/mishoo/UglifyJS2/blob/master/lib/transform.js)
|
1062 | respectively.
|
1063 |
|
1064 | ### ESTree / SpiderMonkey AST
|
1065 |
|
1066 | UglifyJS has its own abstract syntax tree format; for
|
1067 | [practical reasons](http://lisperator.net/blog/uglifyjs-why-not-switching-to-spidermonkey-ast/)
|
1068 | we can't easily change to using the SpiderMonkey AST internally. However,
|
1069 | UglifyJS now has a converter which can import a SpiderMonkey AST.
|
1070 |
|
1071 | For example [Acorn][acorn] is a super-fast parser that produces a
|
1072 | SpiderMonkey AST. It has a small CLI utility that parses one file and dumps
|
1073 | the AST in JSON on the standard output. To use UglifyJS to mangle and
|
1074 | compress that:
|
1075 |
|
1076 | acorn file.js | uglifyjs -p spidermonkey -m -c
|
1077 |
|
1078 | The `-p spidermonkey` option tells UglifyJS that all input files are not
|
1079 | JavaScript, but JS code described in SpiderMonkey AST in JSON. Therefore we
|
1080 | don't use our own parser in this case, but just transform that AST into our
|
1081 | internal AST.
|
1082 |
|
1083 | ### Use Acorn for parsing
|
1084 |
|
1085 | More for fun, I added the `-p acorn` option which will use Acorn to do all
|
1086 | the parsing. If you pass this option, UglifyJS will `require("acorn")`.
|
1087 |
|
1088 | Acorn is really fast (e.g. 250ms instead of 380ms on some 650K code), but
|
1089 | converting the SpiderMonkey tree that Acorn produces takes another 150ms so
|
1090 | in total it's a bit more than just using UglifyJS's own parser.
|
1091 |
|
1092 | [acorn]: https://github.com/ternjs/acorn
|
1093 | [sm-spec]: https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k
|
1094 |
|
1095 | ### Uglify Fast Minify Mode
|
1096 |
|
1097 | It's not well known, but whitespace removal and symbol mangling accounts
|
1098 | for 95% of the size reduction in minified code for most JavaScript - not
|
1099 | elaborate code transforms. One can simply disable `compress` to speed up
|
1100 | Uglify builds by 3 to 4 times. In this fast `mangle`-only mode Uglify has
|
1101 | comparable minify speeds and gzip sizes to
|
1102 | [`butternut`](https://www.npmjs.com/package/butternut):
|
1103 |
|
1104 | | d3.js | minify size | gzip size | minify time (seconds) |
|
1105 | | --- | ---: | ---: | ---: |
|
1106 | | original | 451,131 | 108,733 | - |
|
1107 | | uglify-js@3.0.24 mangle=false, compress=false | 316,600 | 85,245 | 0.70 |
|
1108 | | uglify-js@3.0.24 mangle=true, compress=false | 220,216 | 72,730 | 1.13 |
|
1109 | | butternut@0.4.6 | 217,568 | 72,738 | 1.41 |
|
1110 | | uglify-js@3.0.24 mangle=true, compress=true | 212,511 | 71,560 | 3.36 |
|
1111 | | babili@0.1.4 | 210,713 | 72,140 | 12.64 |
|
1112 |
|
1113 | To enable fast minify mode from the CLI use:
|
1114 | ```
|
1115 | uglifyjs file.js -m
|
1116 | ```
|
1117 | To enable fast minify mode with the API use:
|
1118 | ```js
|
1119 | UglifyJS.minify(code, { compress: false, mangle: true });
|
1120 | ```
|
1121 |
|
1122 | #### Source maps and debugging
|
1123 |
|
1124 | Various `compress` transforms that simplify, rearrange, inline and remove code
|
1125 | are known to have an adverse effect on debugging with source maps. This is
|
1126 | expected as code is optimized and mappings are often simply not possible as
|
1127 | some code no longer exists. For highest fidelity in source map debugging
|
1128 | disable the Uglify `compress` option and just use `mangle`.
|
1129 |
|
1130 | ### Compiler assumptions
|
1131 |
|
1132 | To allow for better optimizations, the compiler makes various assumptions:
|
1133 |
|
1134 | - `.toString()` and `.valueOf()` don't have side effects, and for built-in
|
1135 | objects they have not been overridden.
|
1136 | - `undefined`, `NaN` and `Infinity` have not been externally redefined.
|
1137 | - `arguments.callee`, `arguments.caller` and `Function.prototype.caller` are not used.
|
1138 | - The code doesn't expect the contents of `Function.prototype.toString()` or
|
1139 | `Error.prototype.stack` to be anything in particular.
|
1140 | - Getting and setting properties on a plain object does not cause other side effects
|
1141 | (using `.watch()` or `Proxy`).
|
1142 | - Object properties can be added, removed and modified (not prevented with
|
1143 | `Object.defineProperty()`, `Object.defineProperties()`, `Object.freeze()`,
|
1144 | `Object.preventExtensions()` or `Object.seal()`).
|
1145 |
|
\ | No newline at end of file |