[6a3a178] | 1 | <h1><img src="https://terser.org/img/terser-banner-logo.png" alt="Terser" width="400"></h1>
|
---|
| 2 |
|
---|
| 3 | [![NPM Version][npm-image]][npm-url]
|
---|
| 4 | [![NPM Downloads][downloads-image]][downloads-url]
|
---|
| 5 | [![Travis Build][travis-image]][travis-url]
|
---|
| 6 | [![Opencollective financial contributors][opencollective-contributors]][opencollective-url]
|
---|
| 7 |
|
---|
| 8 | A JavaScript parser and mangler/compressor toolkit for ES6+.
|
---|
| 9 |
|
---|
| 10 | *note*: You can support this project on patreon: <a target="_blank" rel="nofollow" href="https://www.patreon.com/fabiosantoscode"><img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" alt="patron" width="100px" height="auto"></a>. Check out [PATRONS.md](https://github.com/terser/terser/blob/master/PATRONS.md) for our first-tier patrons.
|
---|
| 11 |
|
---|
| 12 | Terser recommends you use RollupJS to bundle your modules, as that produces smaller code overall.
|
---|
| 13 |
|
---|
| 14 | *Beautification* has been undocumented and is *being removed* from terser, we recommend you use [prettier](https://npmjs.com/package/prettier).
|
---|
| 15 |
|
---|
| 16 | Find the changelog in [CHANGELOG.md](https://github.com/terser/terser/blob/master/CHANGELOG.md)
|
---|
| 17 |
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 | [npm-image]: https://img.shields.io/npm/v/terser.svg
|
---|
| 21 | [npm-url]: https://npmjs.org/package/terser
|
---|
| 22 | [downloads-image]: https://img.shields.io/npm/dm/terser.svg
|
---|
| 23 | [downloads-url]: https://npmjs.org/package/terser
|
---|
| 24 | [travis-image]: https://travis-ci.com/terser/terser.svg?branch=master
|
---|
| 25 | [travis-url]: https://travis-ci.com/terser/terser
|
---|
| 26 | [opencollective-contributors]: https://opencollective.com/terser/tiers/badge.svg
|
---|
| 27 | [opencollective-url]: https://opencollective.com/terser
|
---|
| 28 |
|
---|
| 29 | Why choose terser?
|
---|
| 30 | ------------------
|
---|
| 31 |
|
---|
| 32 | `uglify-es` is [no longer maintained](https://github.com/mishoo/UglifyJS2/issues/3156#issuecomment-392943058) and `uglify-js` does not support ES6+.
|
---|
| 33 |
|
---|
| 34 | **`terser`** is a fork of `uglify-es` that mostly retains API and CLI compatibility
|
---|
| 35 | with `uglify-es` and `uglify-js@3`.
|
---|
| 36 |
|
---|
| 37 | Install
|
---|
| 38 | -------
|
---|
| 39 |
|
---|
| 40 | First make sure you have installed the latest version of [node.js](http://nodejs.org/)
|
---|
| 41 | (You may need to restart your computer after this step).
|
---|
| 42 |
|
---|
| 43 | From NPM for use as a command line app:
|
---|
| 44 |
|
---|
| 45 | npm install terser -g
|
---|
| 46 |
|
---|
| 47 | From NPM for programmatic use:
|
---|
| 48 |
|
---|
| 49 | npm install terser
|
---|
| 50 |
|
---|
| 51 | # Command line usage
|
---|
| 52 |
|
---|
| 53 | <!-- CLI_USAGE:START -->
|
---|
| 54 |
|
---|
| 55 | terser [input files] [options]
|
---|
| 56 |
|
---|
| 57 | Terser can take multiple input files. It's recommended that you pass the
|
---|
| 58 | input files first, then pass the options. Terser will parse input files
|
---|
| 59 | in sequence and apply any compression options. The files are parsed in the
|
---|
| 60 | same global scope, that is, a reference from a file to some
|
---|
| 61 | variable/function declared in another file will be matched properly.
|
---|
| 62 |
|
---|
| 63 | If no input file is specified, Terser will read from STDIN.
|
---|
| 64 |
|
---|
| 65 | If you wish to pass your options before the input files, separate the two with
|
---|
| 66 | a double dash to prevent input files being used as option arguments:
|
---|
| 67 |
|
---|
| 68 | terser --compress --mangle -- input.js
|
---|
| 69 |
|
---|
| 70 | ### Command line options
|
---|
| 71 |
|
---|
| 72 | ```
|
---|
| 73 | -h, --help Print usage information.
|
---|
| 74 | `--help options` for details on available options.
|
---|
| 75 | -V, --version Print version number.
|
---|
| 76 | -p, --parse <options> Specify parser options:
|
---|
| 77 | `acorn` Use Acorn for parsing.
|
---|
| 78 | `bare_returns` Allow return outside of functions.
|
---|
| 79 | Useful when minifying CommonJS
|
---|
| 80 | modules and Userscripts that may
|
---|
| 81 | be anonymous function wrapped (IIFE)
|
---|
| 82 | by the .user.js engine `caller`.
|
---|
| 83 | `expression` Parse a single expression, rather than
|
---|
| 84 | a program (for parsing JSON).
|
---|
| 85 | `spidermonkey` Assume input files are SpiderMonkey
|
---|
| 86 | AST format (as JSON).
|
---|
| 87 | -c, --compress [options] Enable compressor/specify compressor options:
|
---|
| 88 | `pure_funcs` List of functions that can be safely
|
---|
| 89 | removed when their return values are
|
---|
| 90 | not used.
|
---|
| 91 | -m, --mangle [options] Mangle names/specify mangler options:
|
---|
| 92 | `reserved` List of names that should not be mangled.
|
---|
| 93 | --mangle-props [options] Mangle properties/specify mangler options:
|
---|
| 94 | `builtins` Mangle property names that overlaps
|
---|
| 95 | with standard JavaScript globals and DOM
|
---|
| 96 | API props.
|
---|
| 97 | `debug` Add debug prefix and suffix.
|
---|
| 98 | `keep_quoted` Only mangle unquoted properties, quoted
|
---|
| 99 | properties are automatically reserved.
|
---|
| 100 | `strict` disables quoted properties
|
---|
| 101 | being automatically reserved.
|
---|
| 102 | `regex` Only mangle matched property names.
|
---|
| 103 | `reserved` List of names that should not be mangled.
|
---|
| 104 | -f, --format [options] Specify format options.
|
---|
| 105 | `preamble` Preamble to prepend to the output. You
|
---|
| 106 | can use this to insert a comment, for
|
---|
| 107 | example for licensing information.
|
---|
| 108 | This will not be parsed, but the source
|
---|
| 109 | map will adjust for its presence.
|
---|
| 110 | `quote_style` Quote style:
|
---|
| 111 | 0 - auto
|
---|
| 112 | 1 - single
|
---|
| 113 | 2 - double
|
---|
| 114 | 3 - original
|
---|
| 115 | `wrap_iife` Wrap IIFEs in parenthesis. Note: you may
|
---|
| 116 | want to disable `negate_iife` under
|
---|
| 117 | compressor options.
|
---|
| 118 | `wrap_func_args` Wrap function arguments in parenthesis.
|
---|
| 119 | -o, --output <file> Output file path (default STDOUT). Specify `ast` or
|
---|
| 120 | `spidermonkey` to write Terser or SpiderMonkey AST
|
---|
| 121 | as JSON to STDOUT respectively.
|
---|
| 122 | --comments [filter] Preserve copyright comments in the output. By
|
---|
| 123 | default this works like Google Closure, keeping
|
---|
| 124 | JSDoc-style comments that contain "@license" or
|
---|
| 125 | "@preserve". You can optionally pass one of the
|
---|
| 126 | following arguments to this flag:
|
---|
| 127 | - "all" to keep all comments
|
---|
| 128 | - `false` to omit comments in the output
|
---|
| 129 | - a valid JS RegExp like `/foo/` or `/^!/` to
|
---|
| 130 | keep only matching comments.
|
---|
| 131 | Note that currently not *all* comments can be
|
---|
| 132 | kept when compression is on, because of dead
|
---|
| 133 | code removal or cascading statements into
|
---|
| 134 | sequences.
|
---|
| 135 | --config-file <file> Read `minify()` options from JSON file.
|
---|
| 136 | -d, --define <expr>[=value] Global definitions.
|
---|
| 137 | --ecma <version> Specify ECMAScript release: 5, 2015, 2016, etc.
|
---|
| 138 | -e, --enclose [arg[:value]] Embed output in a big function with configurable
|
---|
| 139 | arguments and values.
|
---|
| 140 | --ie8 Support non-standard Internet Explorer 8.
|
---|
| 141 | Equivalent to setting `ie8: true` in `minify()`
|
---|
| 142 | for `compress`, `mangle` and `format` options.
|
---|
| 143 | By default Terser will not try to be IE-proof.
|
---|
| 144 | --keep-classnames Do not mangle/drop class names.
|
---|
| 145 | --keep-fnames Do not mangle/drop function names. Useful for
|
---|
| 146 | code relying on Function.prototype.name.
|
---|
| 147 | --module Input is an ES6 module. If `compress` or `mangle` is
|
---|
| 148 | enabled then the `toplevel` option will be enabled.
|
---|
| 149 | --name-cache <file> File to hold mangled name mappings.
|
---|
| 150 | --safari10 Support non-standard Safari 10/11.
|
---|
| 151 | Equivalent to setting `safari10: true` in `minify()`
|
---|
| 152 | for `mangle` and `format` options.
|
---|
| 153 | By default `terser` will not work around
|
---|
| 154 | Safari 10/11 bugs.
|
---|
| 155 | --source-map [options] Enable source map/specify source map options:
|
---|
| 156 | `base` Path to compute relative paths from input files.
|
---|
| 157 | `content` Input source map, useful if you're compressing
|
---|
| 158 | JS that was generated from some other original
|
---|
| 159 | code. Specify "inline" if the source map is
|
---|
| 160 | included within the sources.
|
---|
| 161 | `filename` Name and/or location of the output source.
|
---|
| 162 | `includeSources` Pass this flag if you want to include
|
---|
| 163 | the content of source files in the
|
---|
| 164 | source map as sourcesContent property.
|
---|
| 165 | `root` Path to the original source to be included in
|
---|
| 166 | the source map.
|
---|
| 167 | `url` If specified, path to the source map to append in
|
---|
| 168 | `//# sourceMappingURL`.
|
---|
| 169 | --timings Display operations run time on STDERR.
|
---|
| 170 | --toplevel Compress and/or mangle variables in top level scope.
|
---|
| 171 | --wrap <name> Embed everything in a big function, making the
|
---|
| 172 | “exports” and “global” variables available. You
|
---|
| 173 | need to pass an argument to this option to
|
---|
| 174 | specify the name that your module will take
|
---|
| 175 | when included in, say, a browser.
|
---|
| 176 | ```
|
---|
| 177 |
|
---|
| 178 | Specify `--output` (`-o`) to declare the output file. Otherwise the output
|
---|
| 179 | goes to STDOUT.
|
---|
| 180 |
|
---|
| 181 | ## CLI source map options
|
---|
| 182 |
|
---|
| 183 | Terser can generate a source map file, which is highly useful for
|
---|
| 184 | debugging your compressed JavaScript. To get a source map, pass
|
---|
| 185 | `--source-map --output output.js` (source map will be written out to
|
---|
| 186 | `output.js.map`).
|
---|
| 187 |
|
---|
| 188 | Additional options:
|
---|
| 189 |
|
---|
| 190 | - `--source-map "filename='<NAME>'"` to specify the name of the source map.
|
---|
| 191 |
|
---|
| 192 | - `--source-map "root='<URL>'"` to pass the URL where the original files can be found.
|
---|
| 193 |
|
---|
| 194 | - `--source-map "url='<URL>'"` to specify the URL where the source map can be found.
|
---|
| 195 | Otherwise Terser assumes HTTP `X-SourceMap` is being used and will omit the
|
---|
| 196 | `//# sourceMappingURL=` directive.
|
---|
| 197 |
|
---|
| 198 | For example:
|
---|
| 199 |
|
---|
| 200 | terser js/file1.js js/file2.js \
|
---|
| 201 | -o foo.min.js -c -m \
|
---|
| 202 | --source-map "root='http://foo.com/src',url='foo.min.js.map'"
|
---|
| 203 |
|
---|
| 204 | The above will compress and mangle `file1.js` and `file2.js`, will drop the
|
---|
| 205 | output in `foo.min.js` and the source map in `foo.min.js.map`. The source
|
---|
| 206 | mapping will refer to `http://foo.com/src/js/file1.js` and
|
---|
| 207 | `http://foo.com/src/js/file2.js` (in fact it will list `http://foo.com/src`
|
---|
| 208 | as the source map root, and the original files as `js/file1.js` and
|
---|
| 209 | `js/file2.js`).
|
---|
| 210 |
|
---|
| 211 | ### Composed source map
|
---|
| 212 |
|
---|
| 213 | When you're compressing JS code that was output by a compiler such as
|
---|
| 214 | CoffeeScript, mapping to the JS code won't be too helpful. Instead, you'd
|
---|
| 215 | like to map back to the original code (i.e. CoffeeScript). Terser has an
|
---|
| 216 | option to take an input source map. Assuming you have a mapping from
|
---|
| 217 | CoffeeScript → compiled JS, Terser can generate a map from CoffeeScript →
|
---|
| 218 | compressed JS by mapping every token in the compiled JS to its original
|
---|
| 219 | location.
|
---|
| 220 |
|
---|
| 221 | To use this feature pass `--source-map "content='/path/to/input/source.map'"`
|
---|
| 222 | or `--source-map "content=inline"` if the source map is included inline with
|
---|
| 223 | the sources.
|
---|
| 224 |
|
---|
| 225 | ## CLI compress options
|
---|
| 226 |
|
---|
| 227 | You need to pass `--compress` (`-c`) to enable the compressor. Optionally
|
---|
| 228 | you can pass a comma-separated list of [compress options](#compress-options).
|
---|
| 229 |
|
---|
| 230 | Options are in the form `foo=bar`, or just `foo` (the latter implies
|
---|
| 231 | a boolean option that you want to set `true`; it's effectively a
|
---|
| 232 | shortcut for `foo=true`).
|
---|
| 233 |
|
---|
| 234 | Example:
|
---|
| 235 |
|
---|
| 236 | terser file.js -c toplevel,sequences=false
|
---|
| 237 |
|
---|
| 238 | ## CLI mangle options
|
---|
| 239 |
|
---|
| 240 | To enable the mangler you need to pass `--mangle` (`-m`). The following
|
---|
| 241 | (comma-separated) options are supported:
|
---|
| 242 |
|
---|
| 243 | - `toplevel` (default `false`) -- mangle names declared in the top level scope.
|
---|
| 244 |
|
---|
| 245 | - `eval` (default `false`) -- mangle names visible in scopes where `eval` or `with` are used.
|
---|
| 246 |
|
---|
| 247 | When mangling is enabled but you want to prevent certain names from being
|
---|
| 248 | mangled, you can declare those names with `--mangle reserved` — pass a
|
---|
| 249 | comma-separated list of names. For example:
|
---|
| 250 |
|
---|
| 251 | terser ... -m reserved=['$','require','exports']
|
---|
| 252 |
|
---|
| 253 | to prevent the `require`, `exports` and `$` names from being changed.
|
---|
| 254 |
|
---|
| 255 | ### CLI mangling property names (`--mangle-props`)
|
---|
| 256 |
|
---|
| 257 | **Note:** THIS **WILL** BREAK YOUR CODE. A good rule of thumb is not to use this unless you know exactly what you're doing and how this works and read this section until the end.
|
---|
| 258 |
|
---|
| 259 | Mangling property names is a separate step, different from variable name mangling. Pass
|
---|
| 260 | `--mangle-props` to enable it. The least dangerous
|
---|
| 261 | way to use this is to use the `regex` option like so:
|
---|
| 262 |
|
---|
| 263 | ```
|
---|
| 264 | terser example.js -c -m --mangle-props regex=/_$/
|
---|
| 265 | ```
|
---|
| 266 |
|
---|
| 267 | This will mangle all properties that end with an
|
---|
| 268 | underscore. So you can use it to mangle internal methods.
|
---|
| 269 |
|
---|
| 270 | By default, it will mangle all properties in the
|
---|
| 271 | input code with the exception of built in DOM properties and properties
|
---|
| 272 | in core JavaScript classes, which is what will break your code if you don't:
|
---|
| 273 |
|
---|
| 274 | 1. Control all the code you're mangling
|
---|
| 275 | 2. Avoid using a module bundler, as they usually will call Terser on each file individually, making it impossible to pass mangled objects between modules.
|
---|
| 276 | 3. Avoid calling functions like `defineProperty` or `hasOwnProperty`, because they refer to object properties using strings and will break your code if you don't know what you are doing.
|
---|
| 277 |
|
---|
| 278 | An example:
|
---|
| 279 |
|
---|
| 280 | ```javascript
|
---|
| 281 | // example.js
|
---|
| 282 | var x = {
|
---|
| 283 | baz_: 0,
|
---|
| 284 | foo_: 1,
|
---|
| 285 | calc: function() {
|
---|
| 286 | return this.foo_ + this.baz_;
|
---|
| 287 | }
|
---|
| 288 | };
|
---|
| 289 | x.bar_ = 2;
|
---|
| 290 | x["baz_"] = 3;
|
---|
| 291 | console.log(x.calc());
|
---|
| 292 | ```
|
---|
| 293 | Mangle all properties (except for JavaScript `builtins`) (**very** unsafe):
|
---|
| 294 | ```bash
|
---|
| 295 | $ terser example.js -c passes=2 -m --mangle-props
|
---|
| 296 | ```
|
---|
| 297 | ```javascript
|
---|
| 298 | var x={o:3,t:1,i:function(){return this.t+this.o},s:2};console.log(x.i());
|
---|
| 299 | ```
|
---|
| 300 | Mangle all properties except for `reserved` properties (still very unsafe):
|
---|
| 301 | ```bash
|
---|
| 302 | $ terser example.js -c passes=2 -m --mangle-props reserved=[foo_,bar_]
|
---|
| 303 | ```
|
---|
| 304 | ```javascript
|
---|
| 305 | var x={o:3,foo_:1,t:function(){return this.foo_+this.o},bar_:2};console.log(x.t());
|
---|
| 306 | ```
|
---|
| 307 | Mangle all properties matching a `regex` (not as unsafe but still unsafe):
|
---|
| 308 | ```bash
|
---|
| 309 | $ terser example.js -c passes=2 -m --mangle-props regex=/_$/
|
---|
| 310 | ```
|
---|
| 311 | ```javascript
|
---|
| 312 | var x={o:3,t:1,calc:function(){return this.t+this.o},i:2};console.log(x.calc());
|
---|
| 313 | ```
|
---|
| 314 |
|
---|
| 315 | Combining mangle properties options:
|
---|
| 316 | ```bash
|
---|
| 317 | $ terser example.js -c passes=2 -m --mangle-props regex=/_$/,reserved=[bar_]
|
---|
| 318 | ```
|
---|
| 319 | ```javascript
|
---|
| 320 | var x={o:3,t:1,calc:function(){return this.t+this.o},bar_:2};console.log(x.calc());
|
---|
| 321 | ```
|
---|
| 322 |
|
---|
| 323 | In order for this to be of any use, we avoid mangling standard JS names and DOM
|
---|
| 324 | API properties by default (`--mangle-props builtins` to override).
|
---|
| 325 |
|
---|
| 326 | A regular expression can be used to define which property names should be
|
---|
| 327 | mangled. For example, `--mangle-props regex=/^_/` will only mangle property
|
---|
| 328 | names that start with an underscore.
|
---|
| 329 |
|
---|
| 330 | When you compress multiple files using this option, in order for them to
|
---|
| 331 | work together in the end we need to ensure somehow that one property gets
|
---|
| 332 | mangled to the same name in all of them. For this, pass `--name-cache filename.json`
|
---|
| 333 | and Terser will maintain these mappings in a file which can then be reused.
|
---|
| 334 | It should be initially empty. Example:
|
---|
| 335 |
|
---|
| 336 | ```bash
|
---|
| 337 | $ rm -f /tmp/cache.json # start fresh
|
---|
| 338 | $ terser file1.js file2.js --mangle-props --name-cache /tmp/cache.json -o part1.js
|
---|
| 339 | $ terser file3.js file4.js --mangle-props --name-cache /tmp/cache.json -o part2.js
|
---|
| 340 | ```
|
---|
| 341 |
|
---|
| 342 | Now, `part1.js` and `part2.js` will be consistent with each other in terms
|
---|
| 343 | of mangled property names.
|
---|
| 344 |
|
---|
| 345 | Using the name cache is not necessary if you compress all your files in a
|
---|
| 346 | single call to Terser.
|
---|
| 347 |
|
---|
| 348 | ### Mangling unquoted names (`--mangle-props keep_quoted`)
|
---|
| 349 |
|
---|
| 350 | Using quoted property name (`o["foo"]`) reserves the property name (`foo`)
|
---|
| 351 | so that it is not mangled throughout the entire script even when used in an
|
---|
| 352 | unquoted style (`o.foo`). Example:
|
---|
| 353 |
|
---|
| 354 | ```javascript
|
---|
| 355 | // stuff.js
|
---|
| 356 | var o = {
|
---|
| 357 | "foo": 1,
|
---|
| 358 | bar: 3
|
---|
| 359 | };
|
---|
| 360 | o.foo += o.bar;
|
---|
| 361 | console.log(o.foo);
|
---|
| 362 | ```
|
---|
| 363 | ```bash
|
---|
| 364 | $ terser stuff.js --mangle-props keep_quoted -c -m
|
---|
| 365 | ```
|
---|
| 366 | ```javascript
|
---|
| 367 | var o={foo:1,o:3};o.foo+=o.o,console.log(o.foo);
|
---|
| 368 | ```
|
---|
| 369 |
|
---|
| 370 | ### Debugging property name mangling
|
---|
| 371 |
|
---|
| 372 | You can also pass `--mangle-props debug` in order to mangle property names
|
---|
| 373 | without completely obscuring them. For example the property `o.foo`
|
---|
| 374 | would mangle to `o._$foo$_` with this option. This allows property mangling
|
---|
| 375 | of a large codebase while still being able to debug the code and identify
|
---|
| 376 | where mangling is breaking things.
|
---|
| 377 |
|
---|
| 378 | ```bash
|
---|
| 379 | $ terser stuff.js --mangle-props debug -c -m
|
---|
| 380 | ```
|
---|
| 381 | ```javascript
|
---|
| 382 | var o={_$foo$_:1,_$bar$_:3};o._$foo$_+=o._$bar$_,console.log(o._$foo$_);
|
---|
| 383 | ```
|
---|
| 384 |
|
---|
| 385 | You can also pass a custom suffix using `--mangle-props debug=XYZ`. This would then
|
---|
| 386 | mangle `o.foo` to `o._$foo$XYZ_`. You can change this each time you compile a
|
---|
| 387 | script to identify how a property got mangled. One technique is to pass a
|
---|
| 388 | random number on every compile to simulate mangling changing with different
|
---|
| 389 | inputs (e.g. as you update the input script with new properties), and to help
|
---|
| 390 | identify mistakes like writing mangled keys to storage.
|
---|
| 391 |
|
---|
| 392 | <!-- CLI_USAGE:END -->
|
---|
| 393 |
|
---|
| 394 | # API Reference
|
---|
| 395 |
|
---|
| 396 | <!-- API_REFERENCE:START -->
|
---|
| 397 |
|
---|
| 398 | Assuming installation via NPM, you can load Terser in your application
|
---|
| 399 | like this:
|
---|
| 400 |
|
---|
| 401 | ```javascript
|
---|
| 402 | const { minify } = require("terser");
|
---|
| 403 | ```
|
---|
| 404 |
|
---|
| 405 | Or,
|
---|
| 406 |
|
---|
| 407 | ```javascript
|
---|
| 408 | import { minify } from "terser";
|
---|
| 409 | ```
|
---|
| 410 |
|
---|
| 411 | Browser loading is also supported:
|
---|
| 412 | ```html
|
---|
| 413 | <script src="https://cdn.jsdelivr.net/npm/source-map@0.7.3/dist/source-map.js"></script>
|
---|
| 414 | <script src="https://cdn.jsdelivr.net/npm/terser/dist/bundle.min.js"></script>
|
---|
| 415 | ```
|
---|
| 416 |
|
---|
| 417 | There is a single async high level function, **`async minify(code, options)`**,
|
---|
| 418 | which will perform all minification [phases](#minify-options) in a configurable
|
---|
| 419 | manner. By default `minify()` will enable [`compress`](#compress-options)
|
---|
| 420 | and [`mangle`](#mangle-options). Example:
|
---|
| 421 | ```javascript
|
---|
| 422 | var code = "function add(first, second) { return first + second; }";
|
---|
| 423 | var result = await minify(code, { sourceMap: true });
|
---|
| 424 | console.log(result.code); // minified output: function add(n,d){return n+d}
|
---|
| 425 | console.log(result.map); // source map
|
---|
| 426 | ```
|
---|
| 427 |
|
---|
| 428 | You can `minify` more than one JavaScript file at a time by using an object
|
---|
| 429 | for the first argument where the keys are file names and the values are source
|
---|
| 430 | code:
|
---|
| 431 | ```javascript
|
---|
| 432 | var code = {
|
---|
| 433 | "file1.js": "function add(first, second) { return first + second; }",
|
---|
| 434 | "file2.js": "console.log(add(1 + 2, 3 + 4));"
|
---|
| 435 | };
|
---|
| 436 | var result = await minify(code);
|
---|
| 437 | console.log(result.code);
|
---|
| 438 | // function add(d,n){return d+n}console.log(add(3,7));
|
---|
| 439 | ```
|
---|
| 440 |
|
---|
| 441 | The `toplevel` option:
|
---|
| 442 | ```javascript
|
---|
| 443 | var code = {
|
---|
| 444 | "file1.js": "function add(first, second) { return first + second; }",
|
---|
| 445 | "file2.js": "console.log(add(1 + 2, 3 + 4));"
|
---|
| 446 | };
|
---|
| 447 | var options = { toplevel: true };
|
---|
| 448 | var result = await minify(code, options);
|
---|
| 449 | console.log(result.code);
|
---|
| 450 | // console.log(3+7);
|
---|
| 451 | ```
|
---|
| 452 |
|
---|
| 453 | The `nameCache` option:
|
---|
| 454 | ```javascript
|
---|
| 455 | var options = {
|
---|
| 456 | mangle: {
|
---|
| 457 | toplevel: true,
|
---|
| 458 | },
|
---|
| 459 | nameCache: {}
|
---|
| 460 | };
|
---|
| 461 | var result1 = await minify({
|
---|
| 462 | "file1.js": "function add(first, second) { return first + second; }"
|
---|
| 463 | }, options);
|
---|
| 464 | var result2 = await minify({
|
---|
| 465 | "file2.js": "console.log(add(1 + 2, 3 + 4));"
|
---|
| 466 | }, options);
|
---|
| 467 | console.log(result1.code);
|
---|
| 468 | // function n(n,r){return n+r}
|
---|
| 469 | console.log(result2.code);
|
---|
| 470 | // console.log(n(3,7));
|
---|
| 471 | ```
|
---|
| 472 |
|
---|
| 473 | You may persist the name cache to the file system in the following way:
|
---|
| 474 | ```javascript
|
---|
| 475 | var cacheFileName = "/tmp/cache.json";
|
---|
| 476 | var options = {
|
---|
| 477 | mangle: {
|
---|
| 478 | properties: true,
|
---|
| 479 | },
|
---|
| 480 | nameCache: JSON.parse(fs.readFileSync(cacheFileName, "utf8"))
|
---|
| 481 | };
|
---|
| 482 | fs.writeFileSync("part1.js", await minify({
|
---|
| 483 | "file1.js": fs.readFileSync("file1.js", "utf8"),
|
---|
| 484 | "file2.js": fs.readFileSync("file2.js", "utf8")
|
---|
| 485 | }, options).code, "utf8");
|
---|
| 486 | fs.writeFileSync("part2.js", await minify({
|
---|
| 487 | "file3.js": fs.readFileSync("file3.js", "utf8"),
|
---|
| 488 | "file4.js": fs.readFileSync("file4.js", "utf8")
|
---|
| 489 | }, options).code, "utf8");
|
---|
| 490 | fs.writeFileSync(cacheFileName, JSON.stringify(options.nameCache), "utf8");
|
---|
| 491 | ```
|
---|
| 492 |
|
---|
| 493 | An example of a combination of `minify()` options:
|
---|
| 494 | ```javascript
|
---|
| 495 | var code = {
|
---|
| 496 | "file1.js": "function add(first, second) { return first + second; }",
|
---|
| 497 | "file2.js": "console.log(add(1 + 2, 3 + 4));"
|
---|
| 498 | };
|
---|
| 499 | var options = {
|
---|
| 500 | toplevel: true,
|
---|
| 501 | compress: {
|
---|
| 502 | global_defs: {
|
---|
| 503 | "@console.log": "alert"
|
---|
| 504 | },
|
---|
| 505 | passes: 2
|
---|
| 506 | },
|
---|
| 507 | format: {
|
---|
| 508 | preamble: "/* minified */"
|
---|
| 509 | }
|
---|
| 510 | };
|
---|
| 511 | var result = await minify(code, options);
|
---|
| 512 | console.log(result.code);
|
---|
| 513 | // /* minified */
|
---|
| 514 | // alert(10);"
|
---|
| 515 | ```
|
---|
| 516 |
|
---|
| 517 | An error example:
|
---|
| 518 | ```javascript
|
---|
| 519 | try {
|
---|
| 520 | const result = await minify({"foo.js" : "if (0) else console.log(1);"});
|
---|
| 521 | // Do something with result
|
---|
| 522 | } catch (error) {
|
---|
| 523 | const { message, filename, line, col, pos } = error;
|
---|
| 524 | // Do something with error
|
---|
| 525 | }
|
---|
| 526 | ```
|
---|
| 527 |
|
---|
| 528 | ## Minify options
|
---|
| 529 |
|
---|
| 530 | - `ecma` (default `undefined`) - pass `5`, `2015`, `2016`, etc to override
|
---|
| 531 | `compress` and `format`'s `ecma` options.
|
---|
| 532 |
|
---|
| 533 | - `parse` (default `{}`) — pass an object if you wish to specify some
|
---|
| 534 | additional [parse options](#parse-options).
|
---|
| 535 |
|
---|
| 536 | - `compress` (default `{}`) — pass `false` to skip compressing entirely.
|
---|
| 537 | Pass an object to specify custom [compress options](#compress-options).
|
---|
| 538 |
|
---|
| 539 | - `mangle` (default `true`) — pass `false` to skip mangling names, or pass
|
---|
| 540 | an object to specify [mangle options](#mangle-options) (see below).
|
---|
| 541 |
|
---|
| 542 | - `mangle.properties` (default `false`) — a subcategory of the mangle option.
|
---|
| 543 | Pass an object to specify custom [mangle property options](#mangle-properties-options).
|
---|
| 544 |
|
---|
| 545 | - `module` (default `false`) — Use when minifying an ES6 module. "use strict"
|
---|
| 546 | is implied and names can be mangled on the top scope. If `compress` or
|
---|
| 547 | `mangle` is enabled then the `toplevel` option will be enabled.
|
---|
| 548 |
|
---|
| 549 | - `format` or `output` (default `null`) — pass an object if you wish to specify
|
---|
| 550 | additional [format options](#format-options). The defaults are optimized
|
---|
| 551 | for best compression.
|
---|
| 552 |
|
---|
| 553 | - `sourceMap` (default `false`) - pass an object if you wish to specify
|
---|
| 554 | [source map options](#source-map-options).
|
---|
| 555 |
|
---|
| 556 | - `toplevel` (default `false`) - set to `true` if you wish to enable top level
|
---|
| 557 | variable and function name mangling and to drop unused variables and functions.
|
---|
| 558 |
|
---|
| 559 | - `nameCache` (default `null`) - pass an empty object `{}` or a previously
|
---|
| 560 | used `nameCache` object if you wish to cache mangled variable and
|
---|
| 561 | property names across multiple invocations of `minify()`. Note: this is
|
---|
| 562 | a read/write property. `minify()` will read the name cache state of this
|
---|
| 563 | object and update it during minification so that it may be
|
---|
| 564 | reused or externally persisted by the user.
|
---|
| 565 |
|
---|
| 566 | - `ie8` (default `false`) - set to `true` to support IE8.
|
---|
| 567 |
|
---|
| 568 | - `keep_classnames` (default: `undefined`) - pass `true` to prevent discarding or mangling
|
---|
| 569 | of class names. Pass a regular expression to only keep class names matching that regex.
|
---|
| 570 |
|
---|
| 571 | - `keep_fnames` (default: `false`) - pass `true` to prevent discarding or mangling
|
---|
| 572 | of function names. Pass a regular expression to only keep class names matching that regex.
|
---|
| 573 | Useful for code relying on `Function.prototype.name`. If the top level minify option
|
---|
| 574 | `keep_classnames` is `undefined` it will be overridden with the value of the top level
|
---|
| 575 | minify option `keep_fnames`.
|
---|
| 576 |
|
---|
| 577 | - `safari10` (default: `false`) - pass `true` to work around Safari 10/11 bugs in
|
---|
| 578 | loop scoping and `await`. See `safari10` options in [`mangle`](#mangle-options)
|
---|
| 579 | and [`format`](#format-options) for details.
|
---|
| 580 |
|
---|
| 581 | ## Minify options structure
|
---|
| 582 |
|
---|
| 583 | ```javascript
|
---|
| 584 | {
|
---|
| 585 | parse: {
|
---|
| 586 | // parse options
|
---|
| 587 | },
|
---|
| 588 | compress: {
|
---|
| 589 | // compress options
|
---|
| 590 | },
|
---|
| 591 | mangle: {
|
---|
| 592 | // mangle options
|
---|
| 593 |
|
---|
| 594 | properties: {
|
---|
| 595 | // mangle property options
|
---|
| 596 | }
|
---|
| 597 | },
|
---|
| 598 | format: {
|
---|
| 599 | // format options (can also use `output` for backwards compatibility)
|
---|
| 600 | },
|
---|
| 601 | sourceMap: {
|
---|
| 602 | // source map options
|
---|
| 603 | },
|
---|
| 604 | ecma: 5, // specify one of: 5, 2015, 2016, etc.
|
---|
| 605 | keep_classnames: false,
|
---|
| 606 | keep_fnames: false,
|
---|
| 607 | ie8: false,
|
---|
| 608 | module: false,
|
---|
| 609 | nameCache: null, // or specify a name cache object
|
---|
| 610 | safari10: false,
|
---|
| 611 | toplevel: false
|
---|
| 612 | }
|
---|
| 613 | ```
|
---|
| 614 |
|
---|
| 615 | ### Source map options
|
---|
| 616 |
|
---|
| 617 | To generate a source map:
|
---|
| 618 | ```javascript
|
---|
| 619 | var result = await minify({"file1.js": "var a = function() {};"}, {
|
---|
| 620 | sourceMap: {
|
---|
| 621 | filename: "out.js",
|
---|
| 622 | url: "out.js.map"
|
---|
| 623 | }
|
---|
| 624 | });
|
---|
| 625 | console.log(result.code); // minified output
|
---|
| 626 | console.log(result.map); // source map
|
---|
| 627 | ```
|
---|
| 628 |
|
---|
| 629 | Note that the source map is not saved in a file, it's just returned in
|
---|
| 630 | `result.map`. The value passed for `sourceMap.url` is only used to set
|
---|
| 631 | `//# sourceMappingURL=out.js.map` in `result.code`. The value of
|
---|
| 632 | `filename` is only used to set `file` attribute (see [the spec][sm-spec])
|
---|
| 633 | in source map file.
|
---|
| 634 |
|
---|
| 635 | You can set option `sourceMap.url` to be `"inline"` and source map will
|
---|
| 636 | be appended to code.
|
---|
| 637 |
|
---|
| 638 | You can also specify sourceRoot property to be included in source map:
|
---|
| 639 | ```javascript
|
---|
| 640 | var result = await minify({"file1.js": "var a = function() {};"}, {
|
---|
| 641 | sourceMap: {
|
---|
| 642 | root: "http://example.com/src",
|
---|
| 643 | url: "out.js.map"
|
---|
| 644 | }
|
---|
| 645 | });
|
---|
| 646 | ```
|
---|
| 647 |
|
---|
| 648 | If you're compressing compiled JavaScript and have a source map for it, you
|
---|
| 649 | can use `sourceMap.content`:
|
---|
| 650 | ```javascript
|
---|
| 651 | var result = await minify({"compiled.js": "compiled code"}, {
|
---|
| 652 | sourceMap: {
|
---|
| 653 | content: "content from compiled.js.map",
|
---|
| 654 | url: "minified.js.map"
|
---|
| 655 | }
|
---|
| 656 | });
|
---|
| 657 | // same as before, it returns `code` and `map`
|
---|
| 658 | ```
|
---|
| 659 |
|
---|
| 660 | If you're using the `X-SourceMap` header instead, you can just omit `sourceMap.url`.
|
---|
| 661 |
|
---|
| 662 | If you happen to need the source map as a raw object, set `sourceMap.asObject` to `true`.
|
---|
| 663 |
|
---|
| 664 | ## Parse options
|
---|
| 665 |
|
---|
| 666 | - `bare_returns` (default `false`) -- support top level `return` statements
|
---|
| 667 |
|
---|
| 668 | - `html5_comments` (default `true`)
|
---|
| 669 |
|
---|
| 670 | - `shebang` (default `true`) -- support `#!command` as the first line
|
---|
| 671 |
|
---|
| 672 | - `spidermonkey` (default `false`) -- accept a Spidermonkey (Mozilla) AST
|
---|
| 673 |
|
---|
| 674 | ## Compress options
|
---|
| 675 |
|
---|
| 676 | - `defaults` (default: `true`) -- Pass `false` to disable most default
|
---|
| 677 | enabled `compress` transforms. Useful when you only want to enable a few
|
---|
| 678 | `compress` options while disabling the rest.
|
---|
| 679 |
|
---|
| 680 | - `arrows` (default: `true`) -- Class and object literal methods are converted
|
---|
| 681 | will also be converted to arrow expressions if the resultant code is shorter:
|
---|
| 682 | `m(){return x}` becomes `m:()=>x`. To do this to regular ES5 functions which
|
---|
| 683 | don't use `this` or `arguments`, see `unsafe_arrows`.
|
---|
| 684 |
|
---|
| 685 | - `arguments` (default: `false`) -- replace `arguments[index]` with function
|
---|
| 686 | parameter name whenever possible.
|
---|
| 687 |
|
---|
| 688 | - `booleans` (default: `true`) -- various optimizations for boolean context,
|
---|
| 689 | for example `!!a ? b : c → a ? b : c`
|
---|
| 690 |
|
---|
| 691 | - `booleans_as_integers` (default: `false`) -- Turn booleans into 0 and 1, also
|
---|
| 692 | makes comparisons with booleans use `==` and `!=` instead of `===` and `!==`.
|
---|
| 693 |
|
---|
| 694 | - `collapse_vars` (default: `true`) -- Collapse single-use non-constant variables,
|
---|
| 695 | side effects permitting.
|
---|
| 696 |
|
---|
| 697 | - `comparisons` (default: `true`) -- apply certain optimizations to binary nodes,
|
---|
| 698 | e.g. `!(a <= b) → a > b` (only when `unsafe_comps`), attempts to negate binary
|
---|
| 699 | nodes, e.g. `a = !b && !c && !d && !e → a=!(b||c||d||e)` etc.
|
---|
| 700 |
|
---|
| 701 | - `computed_props` (default: `true`) -- Transforms constant computed properties
|
---|
| 702 | into regular ones: `{["computed"]: 1}` is converted to `{computed: 1}`.
|
---|
| 703 |
|
---|
| 704 | - `conditionals` (default: `true`) -- apply optimizations for `if`-s and conditional
|
---|
| 705 | expressions
|
---|
| 706 |
|
---|
| 707 | - `dead_code` (default: `true`) -- remove unreachable code
|
---|
| 708 |
|
---|
| 709 | - `directives` (default: `true`) -- remove redundant or non-standard directives
|
---|
| 710 |
|
---|
| 711 | - `drop_console` (default: `false`) -- Pass `true` to discard calls to
|
---|
| 712 | `console.*` functions. If you wish to drop a specific function call
|
---|
| 713 | such as `console.info` and/or retain side effects from function arguments
|
---|
| 714 | after dropping the function call then use `pure_funcs` instead.
|
---|
| 715 |
|
---|
| 716 | - `drop_debugger` (default: `true`) -- remove `debugger;` statements
|
---|
| 717 |
|
---|
| 718 | - `ecma` (default: `5`) -- Pass `2015` or greater to enable `compress` options that
|
---|
| 719 | will transform ES5 code into smaller ES6+ equivalent forms.
|
---|
| 720 |
|
---|
| 721 | - `evaluate` (default: `true`) -- attempt to evaluate constant expressions
|
---|
| 722 |
|
---|
| 723 | - `expression` (default: `false`) -- Pass `true` to preserve completion values
|
---|
| 724 | from terminal statements without `return`, e.g. in bookmarklets.
|
---|
| 725 |
|
---|
| 726 | - `global_defs` (default: `{}`) -- see [conditional compilation](#conditional-compilation)
|
---|
| 727 |
|
---|
| 728 | - `hoist_funs` (default: `false`) -- hoist function declarations
|
---|
| 729 |
|
---|
| 730 | - `hoist_props` (default: `true`) -- hoist properties from constant object and
|
---|
| 731 | array literals into regular variables subject to a set of constraints. For example:
|
---|
| 732 | `var o={p:1, q:2}; f(o.p, o.q);` is converted to `f(1, 2);`. Note: `hoist_props`
|
---|
| 733 | works best with `mangle` enabled, the `compress` option `passes` set to `2` or higher,
|
---|
| 734 | and the `compress` option `toplevel` enabled.
|
---|
| 735 |
|
---|
| 736 | - `hoist_vars` (default: `false`) -- hoist `var` declarations (this is `false`
|
---|
| 737 | by default because it seems to increase the size of the output in general)
|
---|
| 738 |
|
---|
| 739 | - `if_return` (default: `true`) -- optimizations for if/return and if/continue
|
---|
| 740 |
|
---|
| 741 | - `inline` (default: `true`) -- inline calls to function with simple/`return` statement:
|
---|
| 742 | - `false` -- same as `0`
|
---|
| 743 | - `0` -- disabled inlining
|
---|
| 744 | - `1` -- inline simple functions
|
---|
| 745 | - `2` -- inline functions with arguments
|
---|
| 746 | - `3` -- inline functions with arguments and variables
|
---|
| 747 | - `true` -- same as `3`
|
---|
| 748 |
|
---|
| 749 | - `join_vars` (default: `true`) -- join consecutive `var` statements
|
---|
| 750 |
|
---|
| 751 | - `keep_classnames` (default: `false`) -- Pass `true` to prevent the compressor from
|
---|
| 752 | discarding class names. Pass a regular expression to only keep class names matching
|
---|
| 753 | that regex. See also: the `keep_classnames` [mangle option](#mangle-options).
|
---|
| 754 |
|
---|
| 755 | - `keep_fargs` (default: `true`) -- Prevents the compressor from discarding unused
|
---|
| 756 | function arguments. You need this for code which relies on `Function.length`.
|
---|
| 757 |
|
---|
| 758 | - `keep_fnames` (default: `false`) -- Pass `true` to prevent the
|
---|
| 759 | compressor from discarding function names. Pass a regular expression to only keep
|
---|
| 760 | function names matching that regex. Useful for code relying on `Function.prototype.name`.
|
---|
| 761 | See also: the `keep_fnames` [mangle option](#mangle-options).
|
---|
| 762 |
|
---|
| 763 | - `keep_infinity` (default: `false`) -- Pass `true` to prevent `Infinity` from
|
---|
| 764 | being compressed into `1/0`, which may cause performance issues on Chrome.
|
---|
| 765 |
|
---|
| 766 | - `loops` (default: `true`) -- optimizations for `do`, `while` and `for` loops
|
---|
| 767 | when we can statically determine the condition.
|
---|
| 768 |
|
---|
| 769 | - `module` (default `false`) -- Pass `true` when compressing an ES6 module. Strict
|
---|
| 770 | mode is implied and the `toplevel` option as well.
|
---|
| 771 |
|
---|
| 772 | - `negate_iife` (default: `true`) -- negate "Immediately-Called Function Expressions"
|
---|
| 773 | where the return value is discarded, to avoid the parens that the
|
---|
| 774 | code generator would insert.
|
---|
| 775 |
|
---|
| 776 | - `passes` (default: `1`) -- The maximum number of times to run compress.
|
---|
| 777 | In some cases more than one pass leads to further compressed code. Keep in
|
---|
| 778 | mind more passes will take more time.
|
---|
| 779 |
|
---|
| 780 | - `properties` (default: `true`) -- rewrite property access using the dot notation, for
|
---|
| 781 | example `foo["bar"] → foo.bar`
|
---|
| 782 |
|
---|
| 783 | - `pure_funcs` (default: `null`) -- You can pass an array of names and
|
---|
| 784 | Terser will assume that those functions do not produce side
|
---|
| 785 | effects. DANGER: will not check if the name is redefined in scope.
|
---|
| 786 | An example case here, for instance `var q = Math.floor(a/b)`. If
|
---|
| 787 | variable `q` is not used elsewhere, Terser will drop it, but will
|
---|
| 788 | still keep the `Math.floor(a/b)`, not knowing what it does. You can
|
---|
| 789 | pass `pure_funcs: [ 'Math.floor' ]` to let it know that this
|
---|
| 790 | function won't produce any side effect, in which case the whole
|
---|
| 791 | statement would get discarded. The current implementation adds some
|
---|
| 792 | overhead (compression will be slower).
|
---|
| 793 |
|
---|
| 794 | - `pure_getters` (default: `"strict"`) -- If you pass `true` for
|
---|
| 795 | this, Terser will assume that object property access
|
---|
| 796 | (e.g. `foo.bar` or `foo["bar"]`) doesn't have any side effects.
|
---|
| 797 | Specify `"strict"` to treat `foo.bar` as side-effect-free only when
|
---|
| 798 | `foo` is certain to not throw, i.e. not `null` or `undefined`.
|
---|
| 799 |
|
---|
| 800 | - `reduce_vars` (default: `true`) -- Improve optimization on variables assigned with and
|
---|
| 801 | used as constant values.
|
---|
| 802 |
|
---|
| 803 | - `reduce_funcs` (default: `true`) -- Inline single-use functions when
|
---|
| 804 | possible. Depends on `reduce_vars` being enabled. Disabling this option
|
---|
| 805 | sometimes improves performance of the output code.
|
---|
| 806 |
|
---|
| 807 | - `sequences` (default: `true`) -- join consecutive simple statements using the
|
---|
| 808 | comma operator. May be set to a positive integer to specify the maximum number
|
---|
| 809 | of consecutive comma sequences that will be generated. If this option is set to
|
---|
| 810 | `true` then the default `sequences` limit is `200`. Set option to `false` or `0`
|
---|
| 811 | to disable. The smallest `sequences` length is `2`. A `sequences` value of `1`
|
---|
| 812 | is grandfathered to be equivalent to `true` and as such means `200`. On rare
|
---|
| 813 | occasions the default sequences limit leads to very slow compress times in which
|
---|
| 814 | case a value of `20` or less is recommended.
|
---|
| 815 |
|
---|
| 816 | - `side_effects` (default: `true`) -- Remove expressions which have no side effects
|
---|
| 817 | and whose results aren't used.
|
---|
| 818 |
|
---|
| 819 | - `switches` (default: `true`) -- de-duplicate and remove unreachable `switch` branches
|
---|
| 820 |
|
---|
| 821 | - `toplevel` (default: `false`) -- drop unreferenced functions (`"funcs"`) and/or
|
---|
| 822 | variables (`"vars"`) in the top level scope (`false` by default, `true` to drop
|
---|
| 823 | both unreferenced functions and variables)
|
---|
| 824 |
|
---|
| 825 | - `top_retain` (default: `null`) -- prevent specific toplevel functions and
|
---|
| 826 | variables from `unused` removal (can be array, comma-separated, RegExp or
|
---|
| 827 | function. Implies `toplevel`)
|
---|
| 828 |
|
---|
| 829 | - `typeofs` (default: `true`) -- Transforms `typeof foo == "undefined"` into
|
---|
| 830 | `foo === void 0`. Note: recommend to set this value to `false` for IE10 and
|
---|
| 831 | earlier versions due to known issues.
|
---|
| 832 |
|
---|
| 833 | - `unsafe` (default: `false`) -- apply "unsafe" transformations
|
---|
| 834 | ([details](#the-unsafe-compress-option)).
|
---|
| 835 |
|
---|
| 836 | - `unsafe_arrows` (default: `false`) -- Convert ES5 style anonymous function
|
---|
| 837 | expressions to arrow functions if the function body does not reference `this`.
|
---|
| 838 | Note: it is not always safe to perform this conversion if code relies on the
|
---|
| 839 | the function having a `prototype`, which arrow functions lack.
|
---|
| 840 | This transform requires that the `ecma` compress option is set to `2015` or greater.
|
---|
| 841 |
|
---|
| 842 | - `unsafe_comps` (default: `false`) -- Reverse `<` and `<=` to `>` and `>=` to
|
---|
| 843 | allow improved compression. This might be unsafe when an at least one of two
|
---|
| 844 | operands is an object with computed values due the use of methods like `get`,
|
---|
| 845 | or `valueOf`. This could cause change in execution order after operands in the
|
---|
| 846 | comparison are switching. Compression only works if both `comparisons` and
|
---|
| 847 | `unsafe_comps` are both set to true.
|
---|
| 848 |
|
---|
| 849 | - `unsafe_Function` (default: `false`) -- compress and mangle `Function(args, code)`
|
---|
| 850 | when both `args` and `code` are string literals.
|
---|
| 851 |
|
---|
| 852 | - `unsafe_math` (default: `false`) -- optimize numerical expressions like
|
---|
| 853 | `2 * x * 3` into `6 * x`, which may give imprecise floating point results.
|
---|
| 854 |
|
---|
| 855 | - `unsafe_symbols` (default: `false`) -- removes keys from native Symbol
|
---|
| 856 | declarations, e.g `Symbol("kDog")` becomes `Symbol()`.
|
---|
| 857 |
|
---|
| 858 | - `unsafe_methods` (default: false) -- Converts `{ m: function(){} }` to
|
---|
| 859 | `{ m(){} }`. `ecma` must be set to `6` or greater to enable this transform.
|
---|
| 860 | If `unsafe_methods` is a RegExp then key/value pairs with keys matching the
|
---|
| 861 | RegExp will be converted to concise methods.
|
---|
| 862 | Note: if enabled there is a risk of getting a "`<method name>` is not a
|
---|
| 863 | constructor" TypeError should any code try to `new` the former function.
|
---|
| 864 |
|
---|
| 865 | - `unsafe_proto` (default: `false`) -- optimize expressions like
|
---|
| 866 | `Array.prototype.slice.call(a)` into `[].slice.call(a)`
|
---|
| 867 |
|
---|
| 868 | - `unsafe_regexp` (default: `false`) -- enable substitutions of variables with
|
---|
| 869 | `RegExp` values the same way as if they are constants.
|
---|
| 870 |
|
---|
| 871 | - `unsafe_undefined` (default: `false`) -- substitute `void 0` if there is a
|
---|
| 872 | variable named `undefined` in scope (variable name will be mangled, typically
|
---|
| 873 | reduced to a single character)
|
---|
| 874 |
|
---|
| 875 | - `unused` (default: `true`) -- drop unreferenced functions and variables (simple
|
---|
| 876 | direct variable assignments do not count as references unless set to `"keep_assign"`)
|
---|
| 877 |
|
---|
| 878 | ## Mangle options
|
---|
| 879 |
|
---|
| 880 | - `eval` (default `false`) -- Pass `true` to mangle names visible in scopes
|
---|
| 881 | where `eval` or `with` are used.
|
---|
| 882 |
|
---|
| 883 | - `keep_classnames` (default `false`) -- Pass `true` to not mangle class names.
|
---|
| 884 | Pass a regular expression to only keep class names matching that regex.
|
---|
| 885 | See also: the `keep_classnames` [compress option](#compress-options).
|
---|
| 886 |
|
---|
| 887 | - `keep_fnames` (default `false`) -- Pass `true` to not mangle function names.
|
---|
| 888 | Pass a regular expression to only keep class names matching that regex.
|
---|
| 889 | Useful for code relying on `Function.prototype.name`. See also: the `keep_fnames`
|
---|
| 890 | [compress option](#compress-options).
|
---|
| 891 |
|
---|
| 892 | - `module` (default `false`) -- Pass `true` an ES6 modules, where the toplevel
|
---|
| 893 | scope is not the global scope. Implies `toplevel`.
|
---|
| 894 |
|
---|
| 895 | - `reserved` (default `[]`) -- Pass an array of identifiers that should be
|
---|
| 896 | excluded from mangling. Example: `["foo", "bar"]`.
|
---|
| 897 |
|
---|
| 898 | - `toplevel` (default `false`) -- Pass `true` to mangle names declared in the
|
---|
| 899 | top level scope.
|
---|
| 900 |
|
---|
| 901 | - `safari10` (default `false`) -- Pass `true` to work around the Safari 10 loop
|
---|
| 902 | iterator [bug](https://bugs.webkit.org/show_bug.cgi?id=171041)
|
---|
| 903 | "Cannot declare a let variable twice".
|
---|
| 904 | See also: the `safari10` [format option](#format-options).
|
---|
| 905 |
|
---|
| 906 | Examples:
|
---|
| 907 |
|
---|
| 908 | ```javascript
|
---|
| 909 | // test.js
|
---|
| 910 | var globalVar;
|
---|
| 911 | function funcName(firstLongName, anotherLongName) {
|
---|
| 912 | var myVariable = firstLongName + anotherLongName;
|
---|
| 913 | }
|
---|
| 914 | ```
|
---|
| 915 | ```javascript
|
---|
| 916 | var code = fs.readFileSync("test.js", "utf8");
|
---|
| 917 |
|
---|
| 918 | await minify(code).code;
|
---|
| 919 | // 'function funcName(a,n){}var globalVar;'
|
---|
| 920 |
|
---|
| 921 | await minify(code, { mangle: { reserved: ['firstLongName'] } }).code;
|
---|
| 922 | // 'function funcName(firstLongName,a){}var globalVar;'
|
---|
| 923 |
|
---|
| 924 | await minify(code, { mangle: { toplevel: true } }).code;
|
---|
| 925 | // 'function n(n,a){}var a;'
|
---|
| 926 | ```
|
---|
| 927 |
|
---|
| 928 | ### Mangle properties options
|
---|
| 929 |
|
---|
| 930 | - `builtins` (default: `false`) — Use `true` to allow the mangling of builtin
|
---|
| 931 | DOM properties. Not recommended to override this setting.
|
---|
| 932 |
|
---|
| 933 | - `debug` (default: `false`) — Mangle names with the original name still present.
|
---|
| 934 | Pass an empty string `""` to enable, or a non-empty string to set the debug suffix.
|
---|
| 935 |
|
---|
| 936 | - `keep_quoted` (default: `false`) — How quoting properties (`{"prop": ...}` and `obj["prop"]`) controls what gets mangled.
|
---|
| 937 | - `"strict"` (recommended) -- `obj.prop` is mangled.
|
---|
| 938 | - `false` -- `obj["prop"]` is mangled.
|
---|
| 939 | - `true` -- `obj.prop` is mangled unless there is `obj["prop"]` elsewhere in the code.
|
---|
| 940 |
|
---|
| 941 | - `regex` (default: `null`) — Pass a [RegExp literal or pattern string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) to only mangle property matching the regular expression.
|
---|
| 942 |
|
---|
| 943 | - `reserved` (default: `[]`) — Do not mangle property names listed in the
|
---|
| 944 | `reserved` array.
|
---|
| 945 |
|
---|
| 946 | - `undeclared` (default: `false`) - Mangle those names when they are accessed
|
---|
| 947 | as properties of known top level variables but their declarations are never
|
---|
| 948 | found in input code. May be useful when only minifying parts of a project.
|
---|
| 949 | See [#397](https://github.com/terser/terser/issues/397) for more details.
|
---|
| 950 |
|
---|
| 951 |
|
---|
| 952 | ## Format options
|
---|
| 953 |
|
---|
| 954 | These options control the format of Terser's output code. Previously known
|
---|
| 955 | as "output options".
|
---|
| 956 |
|
---|
| 957 | - `ascii_only` (default `false`) -- escape Unicode characters in strings and
|
---|
| 958 | regexps (affects directives with non-ascii characters becoming invalid)
|
---|
| 959 |
|
---|
| 960 | - `beautify` (default `false`) -- (DEPRECATED) whether to beautify the output.
|
---|
| 961 | When using the legacy `-b` CLI flag, this is set to true by default.
|
---|
| 962 |
|
---|
| 963 | - `braces` (default `false`) -- always insert braces in `if`, `for`,
|
---|
| 964 | `do`, `while` or `with` statements, even if their body is a single
|
---|
| 965 | statement.
|
---|
| 966 |
|
---|
| 967 | - `comments` (default `"some"`) -- by default it keeps JSDoc-style comments
|
---|
| 968 | that contain "@license", "@preserve" or start with `!`, pass `true` or
|
---|
| 969 | `"all"` to preserve all comments, `false` to omit comments in the output,
|
---|
| 970 | a regular expression string (e.g. `/^!/`) or a function.
|
---|
| 971 |
|
---|
| 972 | - `ecma` (default `5`) -- set desired EcmaScript standard version for output.
|
---|
| 973 | Set `ecma` to `2015` or greater to emit shorthand object properties - i.e.:
|
---|
| 974 | `{a}` instead of `{a: a}`. The `ecma` option will only change the output in
|
---|
| 975 | direct control of the beautifier. Non-compatible features in your input will
|
---|
| 976 | still be output as is. For example: an `ecma` setting of `5` will **not**
|
---|
| 977 | convert modern code to ES5.
|
---|
| 978 |
|
---|
| 979 | - `indent_level` (default `4`)
|
---|
| 980 |
|
---|
| 981 | - `indent_start` (default `0`) -- prefix all lines by that many spaces
|
---|
| 982 |
|
---|
| 983 | - `inline_script` (default `true`) -- escape HTML comments and the slash in
|
---|
| 984 | occurrences of `</script>` in strings
|
---|
| 985 |
|
---|
| 986 | - `keep_numbers` (default `false`) -- keep number literals as it was in original code
|
---|
| 987 | (disables optimizations like converting `1000000` into `1e6`)
|
---|
| 988 |
|
---|
| 989 | - `keep_quoted_props` (default `false`) -- when turned on, prevents stripping
|
---|
| 990 | quotes from property names in object literals.
|
---|
| 991 |
|
---|
| 992 | - `max_line_len` (default `false`) -- maximum line length (for minified code)
|
---|
| 993 |
|
---|
| 994 | - `preamble` (default `null`) -- when passed it must be a string and
|
---|
| 995 | it will be prepended to the output literally. The source map will
|
---|
| 996 | adjust for this text. Can be used to insert a comment containing
|
---|
| 997 | licensing information, for example.
|
---|
| 998 |
|
---|
| 999 | - `quote_keys` (default `false`) -- pass `true` to quote all keys in literal
|
---|
| 1000 | objects
|
---|
| 1001 |
|
---|
| 1002 | - `quote_style` (default `0`) -- preferred quote style for strings (affects
|
---|
| 1003 | quoted property names and directives as well):
|
---|
| 1004 | - `0` -- prefers double quotes, switches to single quotes when there are
|
---|
| 1005 | more double quotes in the string itself. `0` is best for gzip size.
|
---|
| 1006 | - `1` -- always use single quotes
|
---|
| 1007 | - `2` -- always use double quotes
|
---|
| 1008 | - `3` -- always use the original quotes
|
---|
| 1009 |
|
---|
| 1010 | - `preserve_annotations` -- (default `false`) -- Preserve [Terser annotations](#annotations) in the output.
|
---|
| 1011 |
|
---|
| 1012 | - `safari10` (default `false`) -- set this option to `true` to work around
|
---|
| 1013 | the [Safari 10/11 await bug](https://bugs.webkit.org/show_bug.cgi?id=176685).
|
---|
| 1014 | See also: the `safari10` [mangle option](#mangle-options).
|
---|
| 1015 |
|
---|
| 1016 | - `semicolons` (default `true`) -- separate statements with semicolons. If
|
---|
| 1017 | you pass `false` then whenever possible we will use a newline instead of a
|
---|
| 1018 | semicolon, leading to more readable output of minified code (size before
|
---|
| 1019 | gzip could be smaller; size after gzip insignificantly larger).
|
---|
| 1020 |
|
---|
| 1021 | - `shebang` (default `true`) -- preserve shebang `#!` in preamble (bash scripts)
|
---|
| 1022 |
|
---|
| 1023 | - `spidermonkey` (default `false`) -- produce a Spidermonkey (Mozilla) AST
|
---|
| 1024 |
|
---|
| 1025 | - `webkit` (default `false`) -- enable workarounds for WebKit bugs.
|
---|
| 1026 | PhantomJS users should set this option to `true`.
|
---|
| 1027 |
|
---|
| 1028 | - `wrap_iife` (default `false`) -- pass `true` to wrap immediately invoked
|
---|
| 1029 | function expressions. See
|
---|
| 1030 | [#640](https://github.com/mishoo/UglifyJS2/issues/640) for more details.
|
---|
| 1031 |
|
---|
| 1032 | - `wrap_func_args` (default `true`) -- pass `false` if you do not want to wrap
|
---|
| 1033 | function expressions that are passed as arguments, in parenthesis. See
|
---|
| 1034 | [OptimizeJS](https://github.com/nolanlawson/optimize-js) for more details.
|
---|
| 1035 |
|
---|
| 1036 | # Miscellaneous
|
---|
| 1037 |
|
---|
| 1038 | ### Keeping copyright notices or other comments
|
---|
| 1039 |
|
---|
| 1040 | You can pass `--comments` to retain certain comments in the output. By
|
---|
| 1041 | default it will keep JSDoc-style comments that contain "@preserve",
|
---|
| 1042 | "@license" or "@cc_on" (conditional compilation for IE). You can pass
|
---|
| 1043 | `--comments all` to keep all the comments, or a valid JavaScript regexp to
|
---|
| 1044 | keep only comments that match this regexp. For example `--comments /^!/`
|
---|
| 1045 | will keep comments like `/*! Copyright Notice */`.
|
---|
| 1046 |
|
---|
| 1047 | Note, however, that there might be situations where comments are lost. For
|
---|
| 1048 | example:
|
---|
| 1049 | ```javascript
|
---|
| 1050 | function f() {
|
---|
| 1051 | /** @preserve Foo Bar */
|
---|
| 1052 | function g() {
|
---|
| 1053 | // this function is never called
|
---|
| 1054 | }
|
---|
| 1055 | return something();
|
---|
| 1056 | }
|
---|
| 1057 | ```
|
---|
| 1058 |
|
---|
| 1059 | Even though it has "@preserve", the comment will be lost because the inner
|
---|
| 1060 | function `g` (which is the AST node to which the comment is attached to) is
|
---|
| 1061 | discarded by the compressor as not referenced.
|
---|
| 1062 |
|
---|
| 1063 | The safest comments where to place copyright information (or other info that
|
---|
| 1064 | needs to be kept in the output) are comments attached to toplevel nodes.
|
---|
| 1065 |
|
---|
| 1066 | ### The `unsafe` `compress` option
|
---|
| 1067 |
|
---|
| 1068 | It enables some transformations that *might* break code logic in certain
|
---|
| 1069 | contrived cases, but should be fine for most code. It assumes that standard
|
---|
| 1070 | built-in ECMAScript functions and classes have not been altered or replaced.
|
---|
| 1071 | You might want to try it on your own code; it should reduce the minified size.
|
---|
| 1072 | Some examples of the optimizations made when this option is enabled:
|
---|
| 1073 |
|
---|
| 1074 | - `new Array(1, 2, 3)` or `Array(1, 2, 3)` → `[ 1, 2, 3 ]`
|
---|
| 1075 | - `new Object()` → `{}`
|
---|
| 1076 | - `String(exp)` or `exp.toString()` → `"" + exp`
|
---|
| 1077 | - `new Object/RegExp/Function/Error/Array (...)` → we discard the `new`
|
---|
| 1078 | - `"foo bar".substr(4)` → `"bar"`
|
---|
| 1079 |
|
---|
| 1080 | ### Conditional compilation
|
---|
| 1081 |
|
---|
| 1082 | You can use the `--define` (`-d`) switch in order to declare global
|
---|
| 1083 | variables that Terser will assume to be constants (unless defined in
|
---|
| 1084 | scope). For example if you pass `--define DEBUG=false` then, coupled with
|
---|
| 1085 | dead code removal Terser will discard the following from the output:
|
---|
| 1086 | ```javascript
|
---|
| 1087 | if (DEBUG) {
|
---|
| 1088 | console.log("debug stuff");
|
---|
| 1089 | }
|
---|
| 1090 | ```
|
---|
| 1091 |
|
---|
| 1092 | You can specify nested constants in the form of `--define env.DEBUG=false`.
|
---|
| 1093 |
|
---|
| 1094 | Another way of doing that is to declare your globals as constants in a
|
---|
| 1095 | separate file and include it into the build. For example you can have a
|
---|
| 1096 | `build/defines.js` file with the following:
|
---|
| 1097 | ```javascript
|
---|
| 1098 | var DEBUG = false;
|
---|
| 1099 | var PRODUCTION = true;
|
---|
| 1100 | // etc.
|
---|
| 1101 | ```
|
---|
| 1102 |
|
---|
| 1103 | and build your code like this:
|
---|
| 1104 |
|
---|
| 1105 | terser build/defines.js js/foo.js js/bar.js... -c
|
---|
| 1106 |
|
---|
| 1107 | Terser will notice the constants and, since they cannot be altered, it
|
---|
| 1108 | will evaluate references to them to the value itself and drop unreachable
|
---|
| 1109 | code as usual. The build will contain the `const` declarations if you use
|
---|
| 1110 | them. If you are targeting < ES6 environments which does not support `const`,
|
---|
| 1111 | using `var` with `reduce_vars` (enabled by default) should suffice.
|
---|
| 1112 |
|
---|
| 1113 | ### Conditional compilation API
|
---|
| 1114 |
|
---|
| 1115 | You can also use conditional compilation via the programmatic API. With the difference that the
|
---|
| 1116 | property name is `global_defs` and is a compressor property:
|
---|
| 1117 |
|
---|
| 1118 | ```javascript
|
---|
| 1119 | var result = await minify(fs.readFileSync("input.js", "utf8"), {
|
---|
| 1120 | compress: {
|
---|
| 1121 | dead_code: true,
|
---|
| 1122 | global_defs: {
|
---|
| 1123 | DEBUG: false
|
---|
| 1124 | }
|
---|
| 1125 | }
|
---|
| 1126 | });
|
---|
| 1127 | ```
|
---|
| 1128 |
|
---|
| 1129 | To replace an identifier with an arbitrary non-constant expression it is
|
---|
| 1130 | necessary to prefix the `global_defs` key with `"@"` to instruct Terser
|
---|
| 1131 | to parse the value as an expression:
|
---|
| 1132 | ```javascript
|
---|
| 1133 | await minify("alert('hello');", {
|
---|
| 1134 | compress: {
|
---|
| 1135 | global_defs: {
|
---|
| 1136 | "@alert": "console.log"
|
---|
| 1137 | }
|
---|
| 1138 | }
|
---|
| 1139 | }).code;
|
---|
| 1140 | // returns: 'console.log("hello");'
|
---|
| 1141 | ```
|
---|
| 1142 |
|
---|
| 1143 | Otherwise it would be replaced as string literal:
|
---|
| 1144 | ```javascript
|
---|
| 1145 | await minify("alert('hello');", {
|
---|
| 1146 | compress: {
|
---|
| 1147 | global_defs: {
|
---|
| 1148 | "alert": "console.log"
|
---|
| 1149 | }
|
---|
| 1150 | }
|
---|
| 1151 | }).code;
|
---|
| 1152 | // returns: '"console.log"("hello");'
|
---|
| 1153 | ```
|
---|
| 1154 |
|
---|
| 1155 | ### Annotations
|
---|
| 1156 |
|
---|
| 1157 | Annotations in Terser are a way to tell it to treat a certain function call differently. The following annotations are available:
|
---|
| 1158 |
|
---|
| 1159 | * `/*@__INLINE__*/` - forces a function to be inlined somewhere.
|
---|
| 1160 | * `/*@__NOINLINE__*/` - Makes sure the called function is not inlined into the call site.
|
---|
| 1161 | * `/*@__PURE__*/` - Marks a function call as pure. That means, it can safely be dropped.
|
---|
| 1162 |
|
---|
| 1163 | You can use either a `@` sign at the start, or a `#`.
|
---|
| 1164 |
|
---|
| 1165 | Here are some examples on how to use them:
|
---|
| 1166 |
|
---|
| 1167 | ```javascript
|
---|
| 1168 | /*@__INLINE__*/
|
---|
| 1169 | function_always_inlined_here()
|
---|
| 1170 |
|
---|
| 1171 | /*#__NOINLINE__*/
|
---|
| 1172 | function_cant_be_inlined_into_here()
|
---|
| 1173 |
|
---|
| 1174 | const x = /*#__PURE__*/i_am_dropped_if_x_is_not_used()
|
---|
| 1175 | ```
|
---|
| 1176 |
|
---|
| 1177 | ### ESTree / SpiderMonkey AST
|
---|
| 1178 |
|
---|
| 1179 | Terser has its own abstract syntax tree format; for
|
---|
| 1180 | [practical reasons](http://lisperator.net/blog/uglifyjs-why-not-switching-to-spidermonkey-ast/)
|
---|
| 1181 | we can't easily change to using the SpiderMonkey AST internally. However,
|
---|
| 1182 | Terser now has a converter which can import a SpiderMonkey AST.
|
---|
| 1183 |
|
---|
| 1184 | For example [Acorn][acorn] is a super-fast parser that produces a
|
---|
| 1185 | SpiderMonkey AST. It has a small CLI utility that parses one file and dumps
|
---|
| 1186 | the AST in JSON on the standard output. To use Terser to mangle and
|
---|
| 1187 | compress that:
|
---|
| 1188 |
|
---|
| 1189 | acorn file.js | terser -p spidermonkey -m -c
|
---|
| 1190 |
|
---|
| 1191 | The `-p spidermonkey` option tells Terser that all input files are not
|
---|
| 1192 | JavaScript, but JS code described in SpiderMonkey AST in JSON. Therefore we
|
---|
| 1193 | don't use our own parser in this case, but just transform that AST into our
|
---|
| 1194 | internal AST.
|
---|
| 1195 |
|
---|
| 1196 | `spidermonkey` is also available in `minify` as `parse` and `format` options to
|
---|
| 1197 | accept and/or produce a spidermonkey AST.
|
---|
| 1198 |
|
---|
| 1199 | ### Use Acorn for parsing
|
---|
| 1200 |
|
---|
| 1201 | More for fun, I added the `-p acorn` option which will use Acorn to do all
|
---|
| 1202 | the parsing. If you pass this option, Terser will `require("acorn")`.
|
---|
| 1203 |
|
---|
| 1204 | Acorn is really fast (e.g. 250ms instead of 380ms on some 650K code), but
|
---|
| 1205 | converting the SpiderMonkey tree that Acorn produces takes another 150ms so
|
---|
| 1206 | in total it's a bit more than just using Terser's own parser.
|
---|
| 1207 |
|
---|
| 1208 | [acorn]: https://github.com/ternjs/acorn
|
---|
| 1209 | [sm-spec]: https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k
|
---|
| 1210 |
|
---|
| 1211 | ### Terser Fast Minify Mode
|
---|
| 1212 |
|
---|
| 1213 | It's not well known, but whitespace removal and symbol mangling accounts
|
---|
| 1214 | for 95% of the size reduction in minified code for most JavaScript - not
|
---|
| 1215 | elaborate code transforms. One can simply disable `compress` to speed up
|
---|
| 1216 | Terser builds by 3 to 4 times.
|
---|
| 1217 |
|
---|
| 1218 | | d3.js | size | gzip size | time (s) |
|
---|
| 1219 | | --- | ---: | ---: | ---: |
|
---|
| 1220 | | original | 451,131 | 108,733 | - |
|
---|
| 1221 | | terser@3.7.5 mangle=false, compress=false | 316,600 | 85,245 | 0.82 |
|
---|
| 1222 | | terser@3.7.5 mangle=true, compress=false | 220,216 | 72,730 | 1.45 |
|
---|
| 1223 | | terser@3.7.5 mangle=true, compress=true | 212,046 | 70,954 | 5.87 |
|
---|
| 1224 | | babili@0.1.4 | 210,713 | 72,140 | 12.64 |
|
---|
| 1225 | | babel-minify@0.4.3 | 210,321 | 72,242 | 48.67 |
|
---|
| 1226 | | babel-minify@0.5.0-alpha.01eac1c3 | 210,421 | 72,238 | 14.17 |
|
---|
| 1227 |
|
---|
| 1228 | To enable fast minify mode from the CLI use:
|
---|
| 1229 | ```
|
---|
| 1230 | terser file.js -m
|
---|
| 1231 | ```
|
---|
| 1232 | To enable fast minify mode with the API use:
|
---|
| 1233 | ```js
|
---|
| 1234 | await minify(code, { compress: false, mangle: true });
|
---|
| 1235 | ```
|
---|
| 1236 |
|
---|
| 1237 | #### Source maps and debugging
|
---|
| 1238 |
|
---|
| 1239 | Various `compress` transforms that simplify, rearrange, inline and remove code
|
---|
| 1240 | are known to have an adverse effect on debugging with source maps. This is
|
---|
| 1241 | expected as code is optimized and mappings are often simply not possible as
|
---|
| 1242 | some code no longer exists. For highest fidelity in source map debugging
|
---|
| 1243 | disable the `compress` option and just use `mangle`.
|
---|
| 1244 |
|
---|
| 1245 | ### Compiler assumptions
|
---|
| 1246 |
|
---|
| 1247 | To allow for better optimizations, the compiler makes various assumptions:
|
---|
| 1248 |
|
---|
| 1249 | - `.toString()` and `.valueOf()` don't have side effects, and for built-in
|
---|
| 1250 | objects they have not been overridden.
|
---|
| 1251 | - `undefined`, `NaN` and `Infinity` have not been externally redefined.
|
---|
| 1252 | - `arguments.callee`, `arguments.caller` and `Function.prototype.caller` are not used.
|
---|
| 1253 | - The code doesn't expect the contents of `Function.prototype.toString()` or
|
---|
| 1254 | `Error.prototype.stack` to be anything in particular.
|
---|
| 1255 | - Getting and setting properties on a plain object does not cause other side effects
|
---|
| 1256 | (using `.watch()` or `Proxy`).
|
---|
| 1257 | - Object properties can be added, removed and modified (not prevented with
|
---|
| 1258 | `Object.defineProperty()`, `Object.defineProperties()`, `Object.freeze()`,
|
---|
| 1259 | `Object.preventExtensions()` or `Object.seal()`).
|
---|
| 1260 | - `document.all` is not `== null`
|
---|
| 1261 | - Assigning properties to a class doesn't have side effects and does not throw.
|
---|
| 1262 |
|
---|
| 1263 | ### Build Tools and Adaptors using Terser
|
---|
| 1264 |
|
---|
| 1265 | https://www.npmjs.com/browse/depended/terser
|
---|
| 1266 |
|
---|
| 1267 | ### Replacing `uglify-es` with `terser` in a project using `yarn`
|
---|
| 1268 |
|
---|
| 1269 | A number of JS bundlers and uglify wrappers are still using buggy versions
|
---|
| 1270 | of `uglify-es` and have not yet upgraded to `terser`. If you are using `yarn`
|
---|
| 1271 | you can add the following alias to your project's `package.json` file:
|
---|
| 1272 |
|
---|
| 1273 | ```js
|
---|
| 1274 | "resolutions": {
|
---|
| 1275 | "uglify-es": "npm:terser"
|
---|
| 1276 | }
|
---|
| 1277 | ```
|
---|
| 1278 |
|
---|
| 1279 | to use `terser` instead of `uglify-es` in all deeply nested dependencies
|
---|
| 1280 | without changing any code.
|
---|
| 1281 |
|
---|
| 1282 | Note: for this change to take effect you must run the following commands
|
---|
| 1283 | to remove the existing `yarn` lock file and reinstall all packages:
|
---|
| 1284 |
|
---|
| 1285 | ```
|
---|
| 1286 | $ rm -rf node_modules yarn.lock
|
---|
| 1287 | $ yarn
|
---|
| 1288 | ```
|
---|
| 1289 |
|
---|
| 1290 | <!-- API_REFERENCE:END -->
|
---|
| 1291 |
|
---|
| 1292 | # Reporting issues
|
---|
| 1293 |
|
---|
| 1294 | In the terser CLI we use [source-map-support](https://npmjs.com/source-map-support) to produce good error stacks. In your own app, you're expected to enable source-map-support (read their docs) to have nice stack traces that will help you write good issues.
|
---|
| 1295 |
|
---|
| 1296 | # README.md Patrons:
|
---|
| 1297 |
|
---|
| 1298 | *note*: You can support this project on patreon: <a target="_blank" rel="nofollow" href="https://www.patreon.com/fabiosantoscode"><img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" alt="patron" width="100px" height="auto"></a>. Check out [PATRONS.md](https://github.com/terser/terser/blob/master/PATRONS.md) for our first-tier patrons.
|
---|
| 1299 |
|
---|
| 1300 | These are the second-tier patrons. Great thanks for your support!
|
---|
| 1301 |
|
---|
| 1302 | * CKEditor ![](https://c10.patreonusercontent.com/3/eyJoIjoxMDAsInciOjEwMH0%3D/patreon-media/p/user/15452278/f8548dcf48d740619071e8d614459280/1?token-time=2145916800&token-hash=SIQ54PhIPHv3M7CVz9LxS8_8v4sOw4H304HaXsXj8MM%3D)
|
---|
| 1303 | * 38elements ![](https://c10.patreonusercontent.com/3/eyJ3IjoyMDB9/patreon-media/p/user/12501844/88e7fc5dd62d45c6a5626533bbd48cfb/1?token-time=2145916800&token-hash=c3AsQ5T0IQWic0zKxFHu-bGGQJkXQFvafvJ4bPerFR4%3D)
|
---|
| 1304 |
|
---|
| 1305 | ## Contributors
|
---|
| 1306 |
|
---|
| 1307 | ### Code Contributors
|
---|
| 1308 |
|
---|
| 1309 | This project exists thanks to all the people who contribute. [[Contribute](CONTRIBUTING.md)].
|
---|
| 1310 | <a href="https://github.com/terser/terser/graphs/contributors"><img src="https://opencollective.com/terser/contributors.svg?width=890&button=false" /></a>
|
---|
| 1311 |
|
---|
| 1312 | ### Financial Contributors
|
---|
| 1313 |
|
---|
| 1314 | Become a financial contributor and help us sustain our community. [[Contribute](https://opencollective.com/terser/contribute)]
|
---|
| 1315 |
|
---|
| 1316 | #### Individuals
|
---|
| 1317 |
|
---|
| 1318 | <a href="https://opencollective.com/terser"><img src="https://opencollective.com/terser/individuals.svg?width=890"></a>
|
---|
| 1319 |
|
---|
| 1320 | #### Organizations
|
---|
| 1321 |
|
---|
| 1322 | Support this project with your organization. Your logo will show up here with a link to your website. [[Contribute](https://opencollective.com/terser/contribute)]
|
---|
| 1323 |
|
---|
| 1324 | <a href="https://opencollective.com/terser/organization/0/website"><img src="https://opencollective.com/terser/organization/0/avatar.svg"></a>
|
---|
| 1325 | <a href="https://opencollective.com/terser/organization/1/website"><img src="https://opencollective.com/terser/organization/1/avatar.svg"></a>
|
---|
| 1326 | <a href="https://opencollective.com/terser/organization/2/website"><img src="https://opencollective.com/terser/organization/2/avatar.svg"></a>
|
---|
| 1327 | <a href="https://opencollective.com/terser/organization/3/website"><img src="https://opencollective.com/terser/organization/3/avatar.svg"></a>
|
---|
| 1328 | <a href="https://opencollective.com/terser/organization/4/website"><img src="https://opencollective.com/terser/organization/4/avatar.svg"></a>
|
---|
| 1329 | <a href="https://opencollective.com/terser/organization/5/website"><img src="https://opencollective.com/terser/organization/5/avatar.svg"></a>
|
---|
| 1330 | <a href="https://opencollective.com/terser/organization/6/website"><img src="https://opencollective.com/terser/organization/6/avatar.svg"></a>
|
---|
| 1331 | <a href="https://opencollective.com/terser/organization/7/website"><img src="https://opencollective.com/terser/organization/7/avatar.svg"></a>
|
---|
| 1332 | <a href="https://opencollective.com/terser/organization/8/website"><img src="https://opencollective.com/terser/organization/8/avatar.svg"></a>
|
---|
| 1333 | <a href="https://opencollective.com/terser/organization/9/website"><img src="https://opencollective.com/terser/organization/9/avatar.svg"></a>
|
---|