terser 4.8.0 → 5.0.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # Changelog
2
2
 
3
+ ## v5.0.0
4
+
5
+ - `in` operator now taken into account during property mangle.
6
+ - Fixed infinite loop in face of a reference loop in some situations.
7
+ - Kept exports and imports around even if there's something which will throw before them.
8
+ - The main exported bundle for commonjs, dist/bundle.min.js is no longer minified.
9
+
10
+ ## v5.0.0-beta.0
11
+
12
+ - BREAKING: `minify()` is now async and rejects a promise instead of returning an error.
13
+ - BREAKING: Internal AST is no longer exposed, so that it can be improved without releasing breaking changes.
14
+ - BREAKING: Lowest supported node version is 10
15
+ - Module is now distributed as a dual package - You can `import` and `require()` too.
16
+ - Inline improvements were made
17
+
3
18
  ## v4.8.0
4
19
 
5
20
  - Support for numeric separators (`million = 1_000_000`) was added.
package/README.md CHANGED
@@ -99,7 +99,7 @@ a double dash to prevent input files being used as option arguments:
99
99
  being automatically reserved.
100
100
  `regex` Only mangle matched property names.
101
101
  `reserved` List of names that should not be mangled.
102
- -b, --beautify [options] Specify output options:
102
+ -f, --format [options] Specify format options.
103
103
  `preamble` Preamble to prepend to the output. You
104
104
  can use this to insert a comment, for
105
105
  example for licensing information.
@@ -137,7 +137,7 @@ a double dash to prevent input files being used as option arguments:
137
137
  arguments and values.
138
138
  --ie8 Support non-standard Internet Explorer 8.
139
139
  Equivalent to setting `ie8: true` in `minify()`
140
- for `compress`, `mangle` and `output` options.
140
+ for `compress`, `mangle` and `format` options.
141
141
  By default Terser will not try to be IE-proof.
142
142
  --keep-classnames Do not mangle/drop class names.
143
143
  --keep-fnames Do not mangle/drop function names. Useful for
@@ -147,7 +147,7 @@ a double dash to prevent input files being used as option arguments:
147
147
  --name-cache <file> File to hold mangled name mappings.
148
148
  --safari10 Support non-standard Safari 10/11.
149
149
  Equivalent to setting `safari10: true` in `minify()`
150
- for `mangle` and `output` options.
150
+ for `mangle` and `format` options.
151
151
  By default `terser` will not work around
152
152
  Safari 10/11 bugs.
153
153
  --source-map [options] Enable source map/specify source map options:
@@ -166,8 +166,6 @@ a double dash to prevent input files being used as option arguments:
166
166
  `//# sourceMappingURL`.
167
167
  --timings Display operations run time on STDERR.
168
168
  --toplevel Compress and/or mangle variables in top level scope.
169
- --verbose Print diagnostic messages.
170
- --warn Print warning messages.
171
169
  --wrap <name> Embed everything in a big function, making the
172
170
  “exports” and “global” variables available. You
173
171
  need to pass an argument to this option to
@@ -394,24 +392,32 @@ identify mistakes like writing mangled keys to storage.
394
392
 
395
393
  Assuming installation via NPM, you can load Terser in your application
396
394
  like this:
395
+
396
+ ```javascript
397
+ const { minify } = require("terser");
398
+ ```
399
+
400
+ Or,
401
+
397
402
  ```javascript
398
- var Terser = require("terser");
403
+ import { minify } from "terser";
399
404
  ```
405
+
400
406
  Browser loading is also supported:
401
407
  ```html
402
408
  <script src="node_modules/source-map/dist/source-map.min.js"></script>
403
409
  <script src="dist/bundle.min.js"></script>
404
410
  ```
405
411
 
406
- There is a single high level function, **`minify(code, options)`**,
412
+ There is a single async high level function, **`async minify(code, options)`**,
407
413
  which will perform all minification [phases](#minify-options) in a configurable
408
414
  manner. By default `minify()` will enable the options [`compress`](#compress-options)
409
415
  and [`mangle`](#mangle-options). Example:
410
416
  ```javascript
411
417
  var code = "function add(first, second) { return first + second; }";
412
- var result = Terser.minify(code);
413
- console.log(result.error); // runtime error, or `undefined` if no error
418
+ var result = await minify(code, { sourceMap: true });
414
419
  console.log(result.code); // minified output: function add(n,d){return n+d}
420
+ console.log(result.map); // source map
415
421
  ```
416
422
 
417
423
  You can `minify` more than one JavaScript file at a time by using an object
@@ -422,7 +428,7 @@ var code = {
422
428
  "file1.js": "function add(first, second) { return first + second; }",
423
429
  "file2.js": "console.log(add(1 + 2, 3 + 4));"
424
430
  };
425
- var result = Terser.minify(code);
431
+ var result = await minify(code);
426
432
  console.log(result.code);
427
433
  // function add(d,n){return d+n}console.log(add(3,7));
428
434
  ```
@@ -434,7 +440,7 @@ var code = {
434
440
  "file2.js": "console.log(add(1 + 2, 3 + 4));"
435
441
  };
436
442
  var options = { toplevel: true };
437
- var result = Terser.minify(code, options);
443
+ var result = await minify(code, options);
438
444
  console.log(result.code);
439
445
  // console.log(3+7);
440
446
  ```
@@ -447,10 +453,10 @@ var options = {
447
453
  },
448
454
  nameCache: {}
449
455
  };
450
- var result1 = Terser.minify({
456
+ var result1 = await minify({
451
457
  "file1.js": "function add(first, second) { return first + second; }"
452
458
  }, options);
453
- var result2 = Terser.minify({
459
+ var result2 = await minify({
454
460
  "file2.js": "console.log(add(1 + 2, 3 + 4));"
455
461
  }, options);
456
462
  console.log(result1.code);
@@ -468,11 +474,11 @@ var options = {
468
474
  },
469
475
  nameCache: JSON.parse(fs.readFileSync(cacheFileName, "utf8"))
470
476
  };
471
- fs.writeFileSync("part1.js", Terser.minify({
477
+ fs.writeFileSync("part1.js", await minify({
472
478
  "file1.js": fs.readFileSync("file1.js", "utf8"),
473
479
  "file2.js": fs.readFileSync("file2.js", "utf8")
474
480
  }, options).code, "utf8");
475
- fs.writeFileSync("part2.js", Terser.minify({
481
+ fs.writeFileSync("part2.js", await minify({
476
482
  "file3.js": fs.readFileSync("file3.js", "utf8"),
477
483
  "file4.js": fs.readFileSync("file4.js", "utf8")
478
484
  }, options).code, "utf8");
@@ -493,47 +499,31 @@ var options = {
493
499
  },
494
500
  passes: 2
495
501
  },
496
- output: {
497
- beautify: false,
502
+ format: {
498
503
  preamble: "/* minified */"
499
504
  }
500
505
  };
501
- var result = Terser.minify(code, options);
506
+ var result = await minify(code, options);
502
507
  console.log(result.code);
503
508
  // /* minified */
504
509
  // alert(10);"
505
510
  ```
506
511
 
507
- To produce warnings:
508
- ```javascript
509
- var code = "function f(){ var u; return 2 + 3; }";
510
- var options = { warnings: true };
511
- var result = Terser.minify(code, options);
512
- console.log(result.error); // runtime error, `undefined` in this case
513
- console.log(result.warnings); // [ 'Dropping unused variable u [0:1,18]' ]
514
- console.log(result.code); // function f(){return 5}
515
- ```
516
-
517
512
  An error example:
518
513
  ```javascript
519
- var result = Terser.minify({"foo.js" : "if (0) else console.log(1);"});
520
- console.log(JSON.stringify(result.error));
521
- // {"message":"Unexpected token: keyword (else)","filename":"foo.js","line":1,"col":7,"pos":7}
522
- ```
523
- Note: unlike `uglify-js@2.x`, the Terser API does not throw errors.
524
- To achieve a similar effect one could do the following:
525
- ```javascript
526
- var result = Terser.minify(code, options);
527
- if (result.error) throw result.error;
514
+ try {
515
+ const result = await minify({"foo.js" : "if (0) else console.log(1);"});
516
+ // Do something with result
517
+ } catch (error) {
518
+ const { message, filename, line, col, pos } = error;
519
+ // Do something with error
520
+ }
528
521
  ```
529
522
 
530
523
  ## Minify options
531
524
 
532
525
  - `ecma` (default `undefined`) - pass `5`, `2015`, `2016`, etc to override `parse`,
533
- `compress` and `output`'s `ecma` options.
534
-
535
- - `warnings` (default `false`) — pass `true` to return compressor warnings
536
- in `result.warnings`. Use the value `"verbose"` for more detailed warnings.
526
+ `compress` and `format`'s `ecma` options.
537
527
 
538
528
  - `parse` (default `{}`) — pass an object if you wish to specify some
539
529
  additional [parse options](#parse-options).
@@ -551,8 +541,8 @@ if (result.error) throw result.error;
551
541
  is implied and names can be mangled on the top scope. If `compress` or
552
542
  `mangle` is enabled then the `toplevel` option will be enabled.
553
543
 
554
- - `output` (default `null`) — pass an object if you wish to specify
555
- additional [output options](#output-options). The defaults are optimized
544
+ - `format` or `output` (default `null`) — pass an object if you wish to specify
545
+ additional [format options](#format-options). The defaults are optimized
556
546
  for best compression.
557
547
 
558
548
  - `sourceMap` (default `false`) - pass an object if you wish to specify
@@ -581,7 +571,7 @@ if (result.error) throw result.error;
581
571
 
582
572
  - `safari10` (default: `false`) - pass `true` to work around Safari 10/11 bugs in
583
573
  loop scoping and `await`. See `safari10` options in [`mangle`](#mangle-options)
584
- and [`output`](#output-options) for details.
574
+ and [`format`](#format-options) for details.
585
575
 
586
576
  ## Minify options structure
587
577
 
@@ -600,8 +590,8 @@ if (result.error) throw result.error;
600
590
  // mangle property options
601
591
  }
602
592
  },
603
- output: {
604
- // output options
593
+ format: {
594
+ // format options (can also use `output` for backwards compatibility)
605
595
  },
606
596
  sourceMap: {
607
597
  // source map options
@@ -614,7 +604,6 @@ if (result.error) throw result.error;
614
604
  nameCache: null, // or specify a name cache object
615
605
  safari10: false,
616
606
  toplevel: false,
617
- warnings: false,
618
607
  }
619
608
  ```
620
609
 
@@ -622,7 +611,7 @@ if (result.error) throw result.error;
622
611
 
623
612
  To generate a source map:
624
613
  ```javascript
625
- var result = Terser.minify({"file1.js": "var a = function() {};"}, {
614
+ var result = await minify({"file1.js": "var a = function() {};"}, {
626
615
  sourceMap: {
627
616
  filename: "out.js",
628
617
  url: "out.js.map"
@@ -643,7 +632,7 @@ be appended to code.
643
632
 
644
633
  You can also specify sourceRoot property to be included in source map:
645
634
  ```javascript
646
- var result = Terser.minify({"file1.js": "var a = function() {};"}, {
635
+ var result = await minify({"file1.js": "var a = function() {};"}, {
647
636
  sourceMap: {
648
637
  root: "http://example.com/src",
649
638
  url: "out.js.map"
@@ -654,7 +643,7 @@ var result = Terser.minify({"file1.js": "var a = function() {};"}, {
654
643
  If you're compressing compiled JavaScript and have a source map for it, you
655
644
  can use `sourceMap.content`:
656
645
  ```javascript
657
- var result = Terser.minify({"compiled.js": "compiled code"}, {
646
+ var result = await minify({"compiled.js": "compiled code"}, {
658
647
  sourceMap: {
659
648
  content: "content from compiled.js.map",
660
649
  url: "minified.js.map"
@@ -819,10 +808,8 @@ If you happen to need the source map as a raw object, set `sourceMap.asObject` t
819
808
  occasions the default sequences limit leads to very slow compress times in which
820
809
  case a value of `20` or less is recommended.
821
810
 
822
- - `side_effects` (default: `true`) -- Pass `false` to disable potentially dropping
823
- function calls marked as "pure". A function call is marked as "pure" if a comment
824
- annotation `/*@__PURE__*/` or `/*#__PURE__*/` immediately precedes the call. For
825
- example: `/*@__PURE__*/foo();`
811
+ - `side_effects` (default: `true`) -- Remove expressions which have no side effects
812
+ and whose results aren't used.
826
813
 
827
814
  - `switches` (default: `true`) -- de-duplicate and remove unreachable `switch` branches
828
815
 
@@ -883,9 +870,6 @@ If you happen to need the source map as a raw object, set `sourceMap.asObject` t
883
870
  - `unused` (default: `true`) -- drop unreferenced functions and variables (simple
884
871
  direct variable assignments do not count as references unless set to `"keep_assign"`)
885
872
 
886
- - `warnings` (default: `false`) -- display warnings when dropping unreachable
887
- code or unused declarations etc.
888
-
889
873
  ## Mangle options
890
874
 
891
875
  - `eval` (default `false`) -- Pass `true` to mangle names visible in scopes
@@ -912,7 +896,7 @@ If you happen to need the source map as a raw object, set `sourceMap.asObject` t
912
896
  - `safari10` (default `false`) -- Pass `true` to work around the Safari 10 loop
913
897
  iterator [bug](https://bugs.webkit.org/show_bug.cgi?id=171041)
914
898
  "Cannot declare a let variable twice".
915
- See also: the `safari10` [output option](#output-options).
899
+ See also: the `safari10` [format option](#format-options).
916
900
 
917
901
  Examples:
918
902
 
@@ -926,13 +910,13 @@ function funcName(firstLongName, anotherLongName) {
926
910
  ```javascript
927
911
  var code = fs.readFileSync("test.js", "utf8");
928
912
 
929
- Terser.minify(code).code;
913
+ await minify(code).code;
930
914
  // 'function funcName(a,n){}var globalVar;'
931
915
 
932
- Terser.minify(code, { mangle: { reserved: ['firstLongName'] } }).code;
916
+ await minify(code, { mangle: { reserved: ['firstLongName'] } }).code;
933
917
  // 'function funcName(firstLongName,a){}var globalVar;'
934
918
 
935
- Terser.minify(code, { mangle: { toplevel: true } }).code;
919
+ await minify(code, { mangle: { toplevel: true } }).code;
936
920
  // 'function n(n,a){}var a;'
937
921
  ```
938
922
 
@@ -960,16 +944,16 @@ Terser.minify(code, { mangle: { toplevel: true } }).code;
960
944
  found in input code. May be useful when only minifying parts of a project.
961
945
  See [#397](https://github.com/terser/terser/issues/397) for more details.
962
946
 
963
- ## Output options
964
947
 
965
- The code generator tries to output shortest code possible by default. In
966
- case you want beautified output, pass `--beautify` (`-b`). Optionally you
967
- can pass additional arguments that control the code output:
948
+ ## Format options
949
+
950
+ These options control the format of Terser's output code. Previously known
951
+ as "output options".
968
952
 
969
953
  - `ascii_only` (default `false`) -- escape Unicode characters in strings and
970
954
  regexps (affects directives with non-ascii characters becoming invalid)
971
955
 
972
- - `beautify` (default `true`) -- whether to actually beautify the output.
956
+ - `beautify` (default `false`) -- whether to actually beautify the output.
973
957
  Passing `-b` will set this to true, but you might need to pass `-b` even
974
958
  when you want to generate minified code, in order to specify additional
975
959
  arguments, so you can use `-b beautify=false` to override it.
@@ -983,12 +967,12 @@ can pass additional arguments that control the code output:
983
967
  comments, `false` to omit comments in the output, a regular expression string
984
968
  (e.g. `/^!/`) or a function.
985
969
 
986
- - `ecma` (default `5`) -- set output printing mode. Set `ecma` to `2015` or
987
- greater to emit shorthand object properties - i.e.: `{a}` instead of `{a: a}`.
988
- The `ecma` option will only change the output in direct control of the
989
- beautifier. Non-compatible features in the abstract syntax tree will still
990
- be output as is. For example: an `ecma` setting of `5` will **not** convert
991
- ES6+ code to ES5.
970
+ - `ecma` (default `5`) -- set desired EcmaScript standard version for output.
971
+ Set `ecma` to `2015` or greater to emit shorthand object properties - i.e.:
972
+ `{a}` instead of `{a: a}`. The `ecma` option will only change the output in
973
+ direct control of the beautifier. Non-compatible features in your input will
974
+ still be output as is. For example: an `ecma` setting of `5` will **not**
975
+ convert modern code to ES5.
992
976
 
993
977
  - `indent_level` (default `4`)
994
978
 
@@ -1103,10 +1087,6 @@ if (DEBUG) {
1103
1087
 
1104
1088
  You can specify nested constants in the form of `--define env.DEBUG=false`.
1105
1089
 
1106
- Terser will warn about the condition being always false and about dropping
1107
- unreachable code; for now there is no option to turn off only this specific
1108
- warning, you can pass `warnings=false` to turn off *all* warnings.
1109
-
1110
1090
  Another way of doing that is to declare your globals as constants in a
1111
1091
  separate file and include it into the build. For example you can have a
1112
1092
  `build/defines.js` file with the following:
@@ -1132,7 +1112,7 @@ You can also use conditional compilation via the programmatic API. With the diff
1132
1112
  property name is `global_defs` and is a compressor property:
1133
1113
 
1134
1114
  ```javascript
1135
- var result = Terser.minify(fs.readFileSync("input.js", "utf8"), {
1115
+ var result = await minify(fs.readFileSync("input.js", "utf8"), {
1136
1116
  compress: {
1137
1117
  dead_code: true,
1138
1118
  global_defs: {
@@ -1146,7 +1126,7 @@ To replace an identifier with an arbitrary non-constant expression it is
1146
1126
  necessary to prefix the `global_defs` key with `"@"` to instruct Terser
1147
1127
  to parse the value as an expression:
1148
1128
  ```javascript
1149
- Terser.minify("alert('hello');", {
1129
+ await minify("alert('hello');", {
1150
1130
  compress: {
1151
1131
  global_defs: {
1152
1132
  "@alert": "console.log"
@@ -1158,7 +1138,7 @@ Terser.minify("alert('hello');", {
1158
1138
 
1159
1139
  Otherwise it would be replaced as string literal:
1160
1140
  ```javascript
1161
- Terser.minify("alert('hello');", {
1141
+ await minify("alert('hello');", {
1162
1142
  compress: {
1163
1143
  global_defs: {
1164
1144
  "alert": "console.log"
@@ -1168,40 +1148,6 @@ Terser.minify("alert('hello');", {
1168
1148
  // returns: '"console.log"("hello");'
1169
1149
  ```
1170
1150
 
1171
- ### Using native Terser AST with `minify()`
1172
- ```javascript
1173
- // example: parse only, produce native Terser AST
1174
-
1175
- var result = Terser.minify(code, {
1176
- parse: {},
1177
- compress: false,
1178
- mangle: false,
1179
- output: {
1180
- ast: true,
1181
- code: false // optional - faster if false
1182
- }
1183
- });
1184
-
1185
- // result.ast contains native Terser AST
1186
- ```
1187
- ```javascript
1188
- // example: accept native Terser AST input and then compress and mangle
1189
- // to produce both code and native AST.
1190
-
1191
- var result = Terser.minify(ast, {
1192
- compress: {},
1193
- mangle: {},
1194
- output: {
1195
- ast: true,
1196
- code: true // optional - faster if false
1197
- }
1198
- });
1199
-
1200
- // result.ast contains native Terser AST
1201
- // result.code contains the minified code in string form.
1202
- ```
1203
-
1204
-
1205
1151
  ### Annotations
1206
1152
 
1207
1153
  Annotations in Terser are a way to tell it to treat a certain function call differently. The following annotations are available:
@@ -1224,18 +1170,6 @@ function_cant_be_inlined_into_here()
1224
1170
  const x = /*#__PURE__*/i_am_dropped_if_x_is_not_used()
1225
1171
  ```
1226
1172
 
1227
-
1228
- ### Working with Terser AST
1229
-
1230
- Traversal and transformation of the native AST can be performed through
1231
- [`TreeWalker`](https://github.com/fabiosantoscode/terser/blob/master/lib/ast.js) and
1232
- [`TreeTransformer`](https://github.com/fabiosantoscode/terser/blob/master/lib/transform.js)
1233
- respectively.
1234
-
1235
- Largely compatible native AST examples can be found in the original UglifyJS
1236
- documentation. See: [tree walker](http://lisperator.net/uglifyjs/walk) and
1237
- [tree transform](http://lisperator.net/uglifyjs/transform).
1238
-
1239
1173
  ### ESTree / SpiderMonkey AST
1240
1174
 
1241
1175
  Terser has its own abstract syntax tree format; for
@@ -1290,7 +1224,7 @@ terser file.js -m
1290
1224
  ```
1291
1225
  To enable fast minify mode with the API use:
1292
1226
  ```js
1293
- Terser.minify(code, { compress: false, mangle: true });
1227
+ await minify(code, { compress: false, mangle: true });
1294
1228
  ```
1295
1229
 
1296
1230
  #### Source maps and debugging
@@ -1348,7 +1282,7 @@ $ yarn
1348
1282
 
1349
1283
  # Reporting issues
1350
1284
 
1351
- 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 make good issues.
1285
+ 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.
1352
1286
 
1353
1287
  # README.md Patrons:
1354
1288
 
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "bin",
3
+ "private": true,
4
+ "version": "1.0.0",
5
+ "main": "terser",
6
+ "type": "commonjs",
7
+ "author": "",
8
+ "license": "BSD-2-Clause",
9
+ "description": "A package to hold the Terser bin bundle as commonjs while keeping the rest of it ESM."
10
+ }