terser 3.14.0 → 3.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of terser might be problematic. Click here for more details.

package/CHANGELOG.md ADDED
@@ -0,0 +1,28 @@
1
+ # Changelog
2
+
3
+ ## v3.17.0
4
+
5
+ - More DOM properties added to --mangle-properties's DOM property list
6
+ - Closed issue where if 2 functions had the same argument name, Terser would not inline them together properly
7
+ - Fixed issue with `hasOwnProperty.call`
8
+ - You can now list files to minify in a Terser config file
9
+ - Started replacing `new Array(<number>)` with an array literal
10
+ - Started using ES6 capabilities like `Set` and the `includes` method for strings and arrays
11
+
12
+ ## v3.16.1
13
+
14
+ - Fixed issue where Terser being imported with `import` would cause it not to work due to the `__esModule` property. (PR #254 was submitted, which was nice, but since it wasn't a pure commonJS approach I decided to go with my own solution)
15
+
16
+ ## v3.16.0
17
+
18
+ - No longer leaves names like Array or Object or window as a SimpleStatement (statement which is just a single expression).
19
+ - Add support for sections sourcemaps (IndexedSourceMapConsumer)
20
+ - Drops node.js v4 and starts using commonJS
21
+ - Is now built with rollup
22
+
23
+ ## v3.15.0
24
+
25
+ - Inlined spread syntax (`[...[1, 2, 3], 4, 5] => [1, 2, 3, 4, 5]`) in arrays and objects.
26
+ - Fixed typo in compressor warning
27
+ - Fixed inline source map input bug
28
+ - Fixed parsing of template literals with unnecessary escapes (Like `\\a`)
package/PATRONS.md ADDED
@@ -0,0 +1,5 @@
1
+ * 38elements
2
+ * CKEditor
3
+ * Philippe Léger
4
+ * Piotrek Koszuliński
5
+ * Viktor Hubert
package/README.md CHANGED
@@ -5,13 +5,17 @@ terser
5
5
 
6
6
  A JavaScript parser and mangler/compressor toolkit for ES6+.
7
7
 
8
- *note*: You can support this project on patreon: <a target="_blank" rel="nofollow" href="https://www.patreon.com/terser_jscomp_maintainer"><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 for our first-tier patrons.
8
+ *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 for our first-tier patrons.
9
9
 
10
10
  Terser recommends you use RollupJS to bundle your modules, as that produces smaller code overall.
11
11
 
12
12
  *Beautification* has been undocumented and is *being removed* from terser, we recommend you use [prettier](https://npmjs.com/package/prettier).
13
13
 
14
- [![Build Status](https://travis-ci.org/terser-js/terser.svg?branch=master)](https://travis-ci.org/terser-js/terser) [![Coverage Status](https://coveralls.io/repos/github/terser-js/terser/badge.svg?branch=master)](https://coveralls.io/github/terser-js/terser?branch=master)
14
+ [![Build Status](https://travis-ci.org/terser-js/terser.svg?branch=master)](https://travis-ci.org/terser-js/terser)
15
+
16
+ Find the changelog in [CHANGELOG.md](https://github.com/terser-js/terser/blob/master/CHANGELOG.md)
17
+
18
+ A JavaScript parser, mangler/compressor and beautifier toolkit for ES6+.
15
19
 
16
20
 
17
21
  Why choose terser?
@@ -85,6 +89,7 @@ a double dash to prevent input files being used as option arguments:
85
89
  `keep_quoted` Only mangle unquoted properties.
86
90
  `regex` Only mangle matched property names.
87
91
  `reserved` List of names that should not be mangled.
92
+ -b, --beautify [options] Specify output options:
88
93
  `preamble` Preamble to prepend to the output. You
89
94
  can use this to insert a comment, for
90
95
  example for licensing information.
@@ -237,11 +242,28 @@ to prevent the `require`, `exports` and `$` names from being changed.
237
242
 
238
243
  ### CLI mangling property names (`--mangle-props`)
239
244
 
240
- **Note:** THIS WILL PROBABLY BREAK YOUR CODE. Mangling property names
241
- is a separate step, different from variable name mangling. Pass
242
- `--mangle-props` to enable it. It will mangle all properties in the
245
+ **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.
246
+
247
+ Mangling property names is a separate step, different from variable name mangling. Pass
248
+ `--mangle-props` to enable it. The least dangerous
249
+ way to use this is to use the `regex` option like so:
250
+
251
+ ```
252
+ terser example.js -c -m --mangle-props regex=/_$/
253
+ ```
254
+
255
+ This will mangle all properties that start with an
256
+ underscore. So you can use it to mangle internal methods.
257
+
258
+ By default, it will mangle all properties in the
243
259
  input code with the exception of built in DOM properties and properties
244
- in core JavaScript classes. For example:
260
+ in core JavaScript classes, which is what will break your code if you don't:
261
+
262
+ 1. Control all the code you're mangling
263
+ 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.
264
+ 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.
265
+
266
+ An example:
245
267
 
246
268
  ```javascript
247
269
  // example.js
@@ -256,21 +278,21 @@ x.bar_ = 2;
256
278
  x["baz_"] = 3;
257
279
  console.log(x.calc());
258
280
  ```
259
- Mangle all properties (except for JavaScript `builtins`):
281
+ Mangle all properties (except for JavaScript `builtins`) (**very** unsafe):
260
282
  ```bash
261
283
  $ terser example.js -c -m --mangle-props
262
284
  ```
263
285
  ```javascript
264
286
  var x={o:0,_:1,l:function(){return this._+this.o}};x.t=2,x.o=3,console.log(x.l());
265
287
  ```
266
- Mangle all properties except for `reserved` properties:
288
+ Mangle all properties except for `reserved` properties (still very unsafe):
267
289
  ```bash
268
290
  $ terser example.js -c -m --mangle-props reserved=[foo_,bar_]
269
291
  ```
270
292
  ```javascript
271
293
  var x={o:0,foo_:1,_:function(){return this.foo_+this.o}};x.bar_=2,x.o=3,console.log(x._());
272
294
  ```
273
- Mangle all properties matching a `regex`:
295
+ Mangle all properties matching a `regex` (not as unsafe but still unsafe):
274
296
  ```bash
275
297
  $ terser example.js -c -m --mangle-props regex=/_$/
276
298
  ```
@@ -367,6 +389,11 @@ like this:
367
389
  ```javascript
368
390
  var Terser = require("terser");
369
391
  ```
392
+ Browser loading is also supported:
393
+ ```html
394
+ <script src="node_modules/source-map/dist/source-map.min.js"></script>
395
+ <script src="dist/bundle.min.js"></script>
396
+ ```
370
397
 
371
398
  There is a single high level function, **`minify(code, options)`**,
372
399
  which will perform all minification [phases](#minify-options) in a configurable
@@ -459,6 +486,7 @@ var options = {
459
486
  passes: 2
460
487
  },
461
488
  output: {
489
+ beautify: false,
462
490
  preamble: "/* minified */"
463
491
  }
464
492
  };
@@ -916,12 +944,18 @@ Terser.minify(code, { mangle: { toplevel: true } }).code;
916
944
 
917
945
  ## Output options
918
946
 
919
- The code generator tries to output shortest code possible. Optionally you
947
+ The code generator tries to output shortest code possible by default. In
948
+ case you want beautified output, pass `--beautify` (`-b`). Optionally you
920
949
  can pass additional arguments that control the code output:
921
950
 
922
951
  - `ascii_only` (default `false`) -- escape Unicode characters in strings and
923
952
  regexps (affects directives with non-ascii characters becoming invalid)
924
953
 
954
+ - `beautify` (default `true`) -- whether to actually beautify the output.
955
+ Passing `-b` will set this to true, but you might need to pass `-b` even
956
+ when you want to generate minified code, in order to specify additional
957
+ arguments, so you can use `-b beautify=false` to override it.
958
+
925
959
  - `braces` (default `false`) -- always insert braces in `if`, `for`,
926
960
  `do`, `while` or `with` statements, even if their body is a single
927
961
  statement.
@@ -932,7 +966,8 @@ can pass additional arguments that control the code output:
932
966
 
933
967
  - `ecma` (default `5`) -- set output printing mode. Set `ecma` to `6` or
934
968
  greater to emit shorthand object properties - i.e.: `{a}` instead of `{a: a}`.
935
- Non-compatible features in the abstract syntax tree will still
969
+ The `ecma` option will only change the output in direct control of the
970
+ beautifier. Non-compatible features in the abstract syntax tree will still
936
971
  be output as is. For example: an `ecma` setting of `5` will **not** convert
937
972
  ES6+ code to ES5.
938
973
 
@@ -1263,8 +1298,8 @@ In the terser CLI we use [source-map-support](https://npmjs.com/source-map-suppo
1263
1298
 
1264
1299
  # README.md Patrons:
1265
1300
 
1266
- *note*: You can support this project on patreon: <a target="_blank" rel="nofollow" href="https://www.patreon.com/terser_jscomp_maintainer"><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 for our first-tier patrons.
1267
-
1301
+ *note*: You can support this project on patreon: <a target="_blank" rel="nofollow" href="https://www.patreon.com/terser_ecmacomp_maintainer"><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 for our first-tier patrons.
1268
1302
 
1269
- * CKEditor ![CKEditor](https://c10.patreonusercontent.com/3/eyJoIjoxMDAsInciOjEwMH0%3D/patreon-media/p/user/15452278/f8548dcf48d740619071e8d614459280/1?token-time=2145916800&token-hash=SIQ54PhIPHv3M7CVz9LxS8_8v4sOw4H304HaXsXj8MM%3D)
1270
1303
 
1304
+ * CKEditor ![](https://c10.patreonusercontent.com/3/eyJoIjoxMDAsInciOjEwMH0%3D/patreon-media/p/user/15452278/f8548dcf48d740619071e8d614459280/1?token-time=2145916800&token-hash=SIQ54PhIPHv3M7CVz9LxS8_8v4sOw4H304HaXsXj8MM%3D)
1305
+ * 38elements ![](https://c10.patreonusercontent.com/3/eyJ3IjoyMDB9/patreon-media/p/user/12501844/88e7fc5dd62d45c6a5626533bbd48cfb/1?token-time=2145916800&token-hash=c3AsQ5T0IQWic0zKxFHu-bGGQJkXQFvafvJ4bPerFR4%3D)
package/bin/uglifyjs CHANGED
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  // -*- js -*-
3
+ /* eslint-env node */
3
4
 
4
5
  "use strict";
5
6
 
@@ -10,15 +11,13 @@ var info = require("../package.json");
10
11
  var path = require("path");
11
12
  var program = require("commander");
12
13
 
13
- var bundle_path = __dirname + "/../dist/bundle.js";
14
- if (!process.env.TERSER_NO_BUNDLE && fs.existsSync(bundle_path)) {
15
- var UglifyJS = require(bundle_path)
16
- try {
17
- require("source-map-support").install();
18
- } catch (err) {}
19
- } else {
20
- var UglifyJS = require("../tools/node.js");
21
- }
14
+ var bundle_path = __dirname + (process.env.TERSER_NO_BUNDLE ?
15
+ "/../dist/bundle.js" :
16
+ "/../dist/bundle.min.js");
17
+ var UglifyJS = require(bundle_path);
18
+ try {
19
+ require("source-map-support").install();
20
+ } catch (err) {}
22
21
 
23
22
  var skip_keys = [ "cname", "inlined", "parent_scope", "scope", "uses_eval", "uses_with" ];
24
23
  var files = {};
@@ -29,12 +28,12 @@ var options = {
29
28
  program.version(info.name + " " + info.version);
30
29
  program.parseArgv = program.parse;
31
30
  program.parse = undefined;
32
- if (process.argv.indexOf("ast") >= 0) program.helpInformation = describe_ast;
33
- else if (process.argv.indexOf("options") >= 0) program.helpInformation = function() {
31
+ if (process.argv.includes("ast")) program.helpInformation = describe_ast;
32
+ else if (process.argv.includes("options")) program.helpInformation = function() {
34
33
  var text = [];
35
34
  var options = UglifyJS.default_options();
36
35
  for (var option in options) {
37
- text.push("--" + (option == "output" ? "beautify" : option == "sourceMap" ? "source-map" : option) + " options:");
36
+ text.push("--" + (option === "output" ? "beautify" : option === "sourceMap" ? "source-map" : option) + " options:");
38
37
  text.push(format_object(options[option]));
39
38
  text.push("");
40
39
  }
@@ -162,8 +161,18 @@ if (program.verbose) {
162
161
  } else if (program.warn) {
163
162
  options.warnings = true;
164
163
  }
165
- if (program.args.length) {
166
- simple_glob(program.args).forEach(function(name) {
164
+
165
+ let filesList;
166
+ if (options.files && options.files.length) {
167
+ filesList = options.files;
168
+
169
+ delete options.files;
170
+ } else if (program.args.length) {
171
+ filesList = program.args;
172
+ }
173
+
174
+ if (filesList) {
175
+ simple_glob(filesList).forEach(function(name) {
167
176
  files[convert_path(name)] = read_file(name);
168
177
  });
169
178
  run();
@@ -299,7 +308,7 @@ function run() {
299
308
  }
300
309
 
301
310
  function fatal(message) {
302
- if (message instanceof Error) message = message.stack.replace(/^\S*?Error:/, "ERROR:")
311
+ if (message instanceof Error) message = message.stack.replace(/^\S*?Error:/, "ERROR:");
303
312
  print_error(message);
304
313
  process.exit(1);
305
314
  }
@@ -312,7 +321,7 @@ function simple_glob(glob) {
312
321
  if (Array.isArray(glob)) {
313
322
  return [].concat.apply([], glob.map(simple_glob));
314
323
  }
315
- if (glob && glob.match(/\*|\?/)) {
324
+ if (glob && glob.match(/[*?]/)) {
316
325
  var dir = path.dirname(glob);
317
326
  try {
318
327
  var entries = fs.readdirSync(dir);
@@ -392,7 +401,7 @@ function parse_js(flag) {
392
401
  }
393
402
  }
394
403
  return options;
395
- }
404
+ };
396
405
  }
397
406
 
398
407
  function parse_source_map() {
@@ -401,15 +410,14 @@ function parse_source_map() {
401
410
  var hasContent = options && "content" in options;
402
411
  var settings = parse(value, options);
403
412
  if (!hasContent && settings.content && settings.content != "inline") {
404
- print_error("INFO: Using input source map: " + settings.content);
405
413
  settings.content = read_file(settings.content, settings.content);
406
414
  }
407
415
  return settings;
408
- }
416
+ };
409
417
  }
410
418
 
411
419
  function skip_key(key) {
412
- return skip_keys.indexOf(key) >= 0;
420
+ return skip_keys.includes(key);
413
421
  }
414
422
 
415
423
  function symdef(def) {
@@ -444,13 +452,13 @@ function describe_ast() {
444
452
  var out = UglifyJS.OutputStream({ beautify: true });
445
453
  function doitem(ctor) {
446
454
  out.print("AST_" + ctor.TYPE);
447
- var props = ctor.SELF_PROPS.filter(function(prop){
455
+ var props = ctor.SELF_PROPS.filter(function(prop) {
448
456
  return !/^\$/.test(prop);
449
457
  });
450
458
  if (props.length > 0) {
451
459
  out.space();
452
- out.with_parens(function(){
453
- props.forEach(function(prop, i){
460
+ out.with_parens(function() {
461
+ props.forEach(function(prop, i) {
454
462
  if (i) out.space();
455
463
  out.print(prop);
456
464
  });
@@ -462,15 +470,15 @@ function describe_ast() {
462
470
  }
463
471
  if (ctor.SUBCLASSES.length > 0) {
464
472
  out.space();
465
- out.with_block(function(){
466
- ctor.SUBCLASSES.forEach(function(ctor, i){
473
+ out.with_block(function() {
474
+ ctor.SUBCLASSES.forEach(function(ctor, i) {
467
475
  out.indent();
468
476
  doitem(ctor);
469
477
  out.newline();
470
478
  });
471
479
  });
472
480
  }
473
- };
481
+ }
474
482
  doitem(UglifyJS.AST_Node);
475
483
  return out + "\n";
476
484
  }
@@ -1,3 +1,4 @@
1
1
  #!/usr/bin/env node
2
- process.env.TERSER_NO_BUNDLE = '1'
3
- require('./uglifyjs')
2
+ /* eslint-env node */
3
+ process.env.TERSER_NO_BUNDLE = "1";
4
+ require("./uglifyjs");