terser 5.11.0 → 5.12.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,10 @@
1
1
  # Changelog
2
2
 
3
+ ## v5.12.0
4
+
5
+ - `TERSER_DEBUG_DIR` environment variable
6
+ - @copyright comments are now preserved with the comments="some" option (#1153)
7
+
3
8
  ## v5.11.0
4
9
 
5
10
  - Unicode code point escapes (`\u{abcde}`) are not emitted inside RegExp literals anymore (#1147)
package/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  [![Travis Build][travis-image]][travis-url]
6
6
  [![Opencollective financial contributors][opencollective-contributors]][opencollective-url]
7
7
 
8
- A JavaScript parser and mangler/compressor toolkit for ES6+.
8
+ A JavaScript mangler/compressor toolkit for ES6+.
9
9
 
10
10
  *note*: You can support this project on patreon: <a target="_blank" rel="nofollow" href="https://www.patreon.com/fabiosantoscode"><img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" alt="patron" width="100px" height="auto"></a>. Check out [PATRONS.md](https://github.com/terser/terser/blob/master/PATRONS.md) for our first-tier patrons.
11
11
 
@@ -127,8 +127,8 @@ a double dash to prevent input files being used as option arguments:
127
127
  as JSON to STDOUT respectively.
128
128
  --comments [filter] Preserve copyright comments in the output. By
129
129
  default this works like Google Closure, keeping
130
- JSDoc-style comments that contain "@license" or
131
- "@preserve". You can optionally pass one of the
130
+ JSDoc-style comments that contain e.g. "@license",
131
+ or start with "!". You can optionally pass one of the
132
132
  following arguments to this flag:
133
133
  - "all" to keep all comments
134
134
  - `false` to omit comments in the output
@@ -989,8 +989,8 @@ as "output options".
989
989
  statement.
990
990
 
991
991
  - `comments` (default `"some"`) -- by default it keeps JSDoc-style comments
992
- that contain "@license", "@preserve" or start with `!`, pass `true` or
993
- `"all"` to preserve all comments, `false` to omit comments in the output,
992
+ that contain "@license", "@copyright", "@preserve" or start with `!`, pass `true`
993
+ or `"all"` to preserve all comments, `false` to omit comments in the output,
994
994
  a regular expression string (e.g. `/^!/`) or a function.
995
995
 
996
996
  - `ecma` (default `5`) -- set desired EcmaScript standard version for output.
@@ -1062,9 +1062,9 @@ as "output options".
1062
1062
  ### Keeping copyright notices or other comments
1063
1063
 
1064
1064
  You can pass `--comments` to retain certain comments in the output. By
1065
- default it will keep JSDoc-style comments that contain "@preserve",
1066
- "@license" or "@cc_on" (conditional compilation for IE). You can pass
1067
- `--comments all` to keep all the comments, or a valid JavaScript regexp to
1065
+ default it will keep comments starting with "!" and JSDoc-style comments that
1066
+ contain "@preserve", "@copyright", "@license" or "@cc_on" (conditional compilation for IE).
1067
+ You can pass `--comments all` to keep all the comments, or a valid JavaScript regexp to
1068
1068
  keep only comments that match this regexp. For example `--comments /^!/`
1069
1069
  will keep comments like `/*! Copyright Notice */`.
1070
1070
 
@@ -1318,6 +1318,22 @@ $ yarn
1318
1318
 
1319
1319
  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.
1320
1320
 
1321
+ ## Obtaining the source code given to Terser
1322
+
1323
+ 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. If you're using a bundler or a project that includes a bundler and are not sure what went wrong with your code, pass that variable like so:
1324
+
1325
+ ```
1326
+ $ TERSER_DEBUG_DIR=/path/to/logs command-that-uses-terser
1327
+ $ ls /path/to/logs
1328
+ terser-debug-123456.log
1329
+ ```
1330
+
1331
+ 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:
1332
+
1333
+ ```
1334
+ > npx cross-env TERSER_DEBUG_DIR=/path/to/logs command-that-uses-terser
1335
+ ```
1336
+
1321
1337
  # README.md Patrons:
1322
1338
 
1323
1339
  *note*: You can support this project on patreon: <a target="_blank" rel="nofollow" href="https://www.patreon.com/fabiosantoscode"><img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" alt="patron" width="100px" height="auto"></a>. Check out [PATRONS.md](https://github.com/terser/terser/blob/master/PATRONS.md) for our first-tier patrons.
@@ -6975,7 +6975,7 @@ function is_some_comments(comment) {
6975
6975
  // multiline comment
6976
6976
  return (
6977
6977
  (comment.type === "comment2" || comment.type === "comment1")
6978
- && /@preserve|@lic|@cc_on|^\**!/i.test(comment.value)
6978
+ && /@preserve|@copyright|@lic|@cc_on|^\**!/i.test(comment.value)
6979
6979
  );
6980
6980
  }
6981
6981
 
@@ -27555,7 +27555,54 @@ function cache_to_json(cache) {
27555
27555
  };
27556
27556
  }
27557
27557
 
27558
- async function minify(files, options) {
27558
+ function log_input(files, options, fs, debug_folder) {
27559
+ if (!(fs && fs.writeFileSync && fs.mkdirSync)) {
27560
+ return;
27561
+ }
27562
+
27563
+ try {
27564
+ fs.mkdirSync(debug_folder);
27565
+ } catch (e) {
27566
+ if (e.code !== "EEXIST") throw e;
27567
+ }
27568
+
27569
+ const log_path = `${debug_folder}/terser-debug-${(Math.random() * 9999999) | 0}.log`;
27570
+
27571
+ options = options || {};
27572
+
27573
+ const options_str = JSON.stringify(options, (_key, thing) => {
27574
+ if (typeof thing === "function") return "[Function " + thing.toString() + "]";
27575
+ if (thing instanceof RegExp) return "[RegExp " + thing.toString() + "]";
27576
+ return thing;
27577
+ }, 4);
27578
+
27579
+ const files_str = (file) => {
27580
+ if (typeof file === "object" && options.parse && options.parse.spidermonkey) {
27581
+ return JSON.stringify(file, null, 2);
27582
+ } else if (typeof file === "object") {
27583
+ return Object.keys(file)
27584
+ .map((key) => key + ": " + files_str(file[key]))
27585
+ .join("\n\n");
27586
+ } else if (typeof file === "string") {
27587
+ return "```\n" + file + "\n```";
27588
+ } else {
27589
+ return file; // What do?
27590
+ }
27591
+ };
27592
+
27593
+ fs.writeFileSync(log_path, "Options: \n" + options_str + "\n\nInput files:\n\n" + files_str(files) + "\n");
27594
+ }
27595
+
27596
+ async function minify(files, options, _fs_module) {
27597
+ if (
27598
+ _fs_module
27599
+ && typeof process === "object"
27600
+ && process.env
27601
+ && typeof process.env.TERSER_DEBUG_DIR === "string"
27602
+ ) {
27603
+ log_input(files, options, _fs_module, process.env.TERSER_DEBUG_DIR);
27604
+ }
27605
+
27559
27606
  options = defaults(options, {
27560
27607
  compress: {},
27561
27608
  ecma: undefined,
@@ -27578,6 +27625,7 @@ async function minify(files, options) {
27578
27625
  warnings: false,
27579
27626
  wrap: false,
27580
27627
  }, true);
27628
+
27581
27629
  var timings = options.timings && {
27582
27630
  start: Date.now()
27583
27631
  };
@@ -27979,7 +28027,7 @@ async function run_cli({ program, packageJson, fs, path }) {
27979
28027
 
27980
28028
  let result;
27981
28029
  try {
27982
- result = await minify(files, options);
28030
+ result = await minify(files, options, fs);
27983
28031
  } catch (ex) {
27984
28032
  if (ex.name == "SyntaxError") {
27985
28033
  print_error("Parse error at " + ex.filename + ":" + ex.line + "," + ex.col);
@@ -28042,14 +28090,18 @@ async function run_cli({ program, packageJson, fs, path }) {
28042
28090
  }, 2));
28043
28091
  } else if (program.output == "spidermonkey") {
28044
28092
  try {
28045
- const minified = await minify(result.code, {
28046
- compress: false,
28047
- mangle: false,
28048
- format: {
28049
- ast: true,
28050
- code: false
28051
- }
28052
- });
28093
+ const minified = await minify(
28094
+ result.code,
28095
+ {
28096
+ compress: false,
28097
+ mangle: false,
28098
+ format: {
28099
+ ast: true,
28100
+ code: false
28101
+ }
28102
+ },
28103
+ fs
28104
+ );
28053
28105
  console.log(JSON.stringify(minified.ast.to_mozilla_ast(), null, 2));
28054
28106
  } catch (ex) {
28055
28107
  fatal(ex);
package/lib/cli.js CHANGED
@@ -224,7 +224,7 @@ export async function run_cli({ program, packageJson, fs, path }) {
224
224
 
225
225
  let result;
226
226
  try {
227
- result = await minify(files, options);
227
+ result = await minify(files, options, fs);
228
228
  } catch (ex) {
229
229
  if (ex.name == "SyntaxError") {
230
230
  print_error("Parse error at " + ex.filename + ":" + ex.line + "," + ex.col);
@@ -287,14 +287,18 @@ export async function run_cli({ program, packageJson, fs, path }) {
287
287
  }, 2));
288
288
  } else if (program.output == "spidermonkey") {
289
289
  try {
290
- const minified = await minify(result.code, {
291
- compress: false,
292
- mangle: false,
293
- format: {
294
- ast: true,
295
- code: false
296
- }
297
- });
290
+ const minified = await minify(
291
+ result.code,
292
+ {
293
+ compress: false,
294
+ mangle: false,
295
+ format: {
296
+ ast: true,
297
+ code: false
298
+ }
299
+ },
300
+ fs
301
+ );
298
302
  console.log(JSON.stringify(minified.ast.to_mozilla_ast(), null, 2));
299
303
  } catch (ex) {
300
304
  fatal(ex);
package/lib/minify.js CHANGED
@@ -61,7 +61,54 @@ function cache_to_json(cache) {
61
61
  };
62
62
  }
63
63
 
64
- async function minify(files, options) {
64
+ function log_input(files, options, fs, debug_folder) {
65
+ if (!(fs && fs.writeFileSync && fs.mkdirSync)) {
66
+ return;
67
+ }
68
+
69
+ try {
70
+ fs.mkdirSync(debug_folder);
71
+ } catch (e) {
72
+ if (e.code !== "EEXIST") throw e;
73
+ }
74
+
75
+ const log_path = `${debug_folder}/terser-debug-${(Math.random() * 9999999) | 0}.log`;
76
+
77
+ options = options || {};
78
+
79
+ const options_str = JSON.stringify(options, (_key, thing) => {
80
+ if (typeof thing === "function") return "[Function " + thing.toString() + "]";
81
+ if (thing instanceof RegExp) return "[RegExp " + thing.toString() + "]";
82
+ return thing;
83
+ }, 4);
84
+
85
+ const files_str = (file) => {
86
+ if (typeof file === "object" && options.parse && options.parse.spidermonkey) {
87
+ return JSON.stringify(file, null, 2);
88
+ } else if (typeof file === "object") {
89
+ return Object.keys(file)
90
+ .map((key) => key + ": " + files_str(file[key]))
91
+ .join("\n\n");
92
+ } else if (typeof file === "string") {
93
+ return "```\n" + file + "\n```";
94
+ } else {
95
+ return file; // What do?
96
+ }
97
+ };
98
+
99
+ fs.writeFileSync(log_path, "Options: \n" + options_str + "\n\nInput files:\n\n" + files_str(files) + "\n");
100
+ }
101
+
102
+ async function minify(files, options, _fs_module) {
103
+ if (
104
+ _fs_module
105
+ && typeof process === "object"
106
+ && process.env
107
+ && typeof process.env.TERSER_DEBUG_DIR === "string"
108
+ ) {
109
+ log_input(files, options, _fs_module, process.env.TERSER_DEBUG_DIR);
110
+ }
111
+
65
112
  options = defaults(options, {
66
113
  compress: {},
67
114
  ecma: undefined,
@@ -84,6 +131,7 @@ async function minify(files, options) {
84
131
  warnings: false,
85
132
  wrap: false,
86
133
  }, true);
134
+
87
135
  var timings = options.timings && {
88
136
  start: Date.now()
89
137
  };
package/lib/output.js CHANGED
@@ -172,7 +172,7 @@ function is_some_comments(comment) {
172
172
  // multiline comment
173
173
  return (
174
174
  (comment.type === "comment2" || comment.type === "comment1")
175
- && /@preserve|@lic|@cc_on|^\**!/i.test(comment.value)
175
+ && /@preserve|@copyright|@lic|@cc_on|^\**!/i.test(comment.value)
176
176
  );
177
177
  }
178
178
 
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "homepage": "https://terser.org",
5
5
  "author": "Mihai Bazon <mihai.bazon@gmail.com> (http://lisperator.net/)",
6
6
  "license": "BSD-2-Clause",
7
- "version": "5.11.0",
7
+ "version": "5.12.0",
8
8
  "engines": {
9
9
  "node": ">=10"
10
10
  },
@@ -104,6 +104,8 @@
104
104
  "describe": false,
105
105
  "it": false,
106
106
  "require": false,
107
+ "before": false,
108
+ "after": false,
107
109
  "global": false,
108
110
  "process": false
109
111
  },