You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1421 lines
59 KiB

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