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/lib/size.js ADDED
@@ -0,0 +1,448 @@
1
+ import {
2
+ AST_Accessor,
3
+ AST_Array,
4
+ AST_Arrow,
5
+ AST_Await,
6
+ AST_BigInt,
7
+ AST_Binary,
8
+ AST_Block,
9
+ AST_Break,
10
+ AST_Call,
11
+ AST_Case,
12
+ AST_Class,
13
+ AST_ClassProperty,
14
+ AST_ConciseMethod,
15
+ AST_Conditional,
16
+ AST_Const,
17
+ AST_Continue,
18
+ AST_Debugger,
19
+ AST_Default,
20
+ AST_Defun,
21
+ AST_Destructuring,
22
+ AST_Directive,
23
+ AST_Do,
24
+ AST_Dot,
25
+ AST_EmptyStatement,
26
+ AST_Expansion,
27
+ AST_Export,
28
+ AST_False,
29
+ AST_For,
30
+ AST_ForIn,
31
+ AST_Function,
32
+ AST_Hole,
33
+ AST_If,
34
+ AST_Import,
35
+ AST_Infinity,
36
+ AST_LabeledStatement,
37
+ AST_Let,
38
+ AST_NameMapping,
39
+ AST_NaN,
40
+ AST_New,
41
+ AST_NewTarget,
42
+ AST_Node,
43
+ AST_Null,
44
+ AST_Number,
45
+ AST_Object,
46
+ AST_ObjectKeyVal,
47
+ AST_ObjectGetter,
48
+ AST_ObjectSetter,
49
+ AST_RegExp,
50
+ AST_Return,
51
+ AST_Sequence,
52
+ AST_String,
53
+ AST_Sub,
54
+ AST_Super,
55
+ AST_Switch,
56
+ AST_Symbol,
57
+ AST_SymbolClassProperty,
58
+ AST_SymbolExportForeign,
59
+ AST_SymbolImportForeign,
60
+ AST_SymbolRef,
61
+ AST_SymbolDeclaration,
62
+ AST_TemplateSegment,
63
+ AST_TemplateString,
64
+ AST_This,
65
+ AST_Throw,
66
+ AST_Toplevel,
67
+ AST_True,
68
+ AST_Try,
69
+ AST_Catch,
70
+ AST_Finally,
71
+ AST_Unary,
72
+ AST_Undefined,
73
+ AST_Var,
74
+ AST_VarDef,
75
+ AST_While,
76
+ AST_With,
77
+ AST_Yield,
78
+ walk_parent
79
+ } from "./ast.js";
80
+ import { first_in_statement } from "./utils/first_in_statement.js";
81
+
82
+ let mangle_options = undefined;
83
+ AST_Node.prototype.size = function (compressor, stack) {
84
+ mangle_options = undefined; // TODO get mangle_options somehow
85
+
86
+ let size = 0;
87
+ walk_parent(this, (node, info) => {
88
+ size += node._size(info);
89
+ }, stack || (compressor && compressor.stack));
90
+
91
+ // just to save a bit of memory
92
+ mangle_options = undefined;
93
+
94
+ return size;
95
+ };
96
+
97
+ AST_Node.prototype._size = () => 0;
98
+
99
+ AST_Debugger.prototype._size = () => 8;
100
+
101
+ AST_Directive.prototype._size = function () {
102
+ // TODO string encoding stuff
103
+ return 2 + this.value.length;
104
+ };
105
+
106
+ const list_overhead = (array) => array.length && array.length - 1;
107
+
108
+ AST_Block.prototype._size = function () {
109
+ return 2 + list_overhead(this.body);
110
+ };
111
+
112
+ AST_Toplevel.prototype._size = function() {
113
+ return list_overhead(this.body);
114
+ };
115
+
116
+ AST_EmptyStatement.prototype._size = () => 1;
117
+
118
+ AST_LabeledStatement.prototype._size = () => 2; // x:
119
+
120
+ AST_Do.prototype._size = () => 9;
121
+
122
+ AST_While.prototype._size = () => 7;
123
+
124
+ AST_For.prototype._size = () => 8;
125
+
126
+ AST_ForIn.prototype._size = () => 8;
127
+ // AST_ForOf inherits ^
128
+
129
+ AST_With.prototype._size = () => 6;
130
+
131
+ AST_Expansion.prototype._size = () => 3;
132
+
133
+ /*#__INLINE__*/
134
+ const lambda_modifiers = func =>
135
+ (func.is_generator ? 1 : 0) + (func.async ? 6 : 0);
136
+
137
+ AST_Accessor.prototype._size = function () {
138
+ return lambda_modifiers(this) + 4 + list_overhead(this.argnames) + list_overhead(this.body);
139
+ };
140
+
141
+ AST_Function.prototype._size = function (info) {
142
+ const first = !!first_in_statement(info);
143
+ return (first * 2) + lambda_modifiers(this) + 12 + list_overhead(this.argnames) + list_overhead(this.body);
144
+ };
145
+
146
+ AST_Defun.prototype._size = function () {
147
+ return lambda_modifiers(this) + 13 + list_overhead(this.argnames) + list_overhead(this.body);
148
+ };
149
+
150
+ AST_Arrow.prototype._size = function () {
151
+ let args_and_arrow = 2 + list_overhead(this.argnames);
152
+
153
+ if (
154
+ !(
155
+ this.argnames.length === 1
156
+ && this.argnames[0] instanceof AST_Symbol
157
+ )
158
+ ) {
159
+ args_and_arrow += 2;
160
+ }
161
+
162
+ return lambda_modifiers(this) + args_and_arrow + (Array.isArray(this.body) ? list_overhead(this.body) : this.body._size());
163
+ };
164
+
165
+ AST_Destructuring.prototype._size = () => 2;
166
+
167
+ AST_TemplateString.prototype._size = function () {
168
+ return 2 + (Math.floor(this.segments.length / 2) * 3); /* "${}" */
169
+ };
170
+
171
+ AST_TemplateSegment.prototype._size = function () {
172
+ return this.value.length;
173
+ };
174
+
175
+ AST_Return.prototype._size = function () {
176
+ return this.value ? 7 : 6;
177
+ };
178
+
179
+ AST_Throw.prototype._size = () => 6;
180
+
181
+ AST_Break.prototype._size = function () {
182
+ return this.label ? 6 : 5;
183
+ };
184
+
185
+ AST_Continue.prototype._size = function () {
186
+ return this.label ? 9 : 8;
187
+ };
188
+
189
+ AST_If.prototype._size = () => 4;
190
+
191
+ AST_Switch.prototype._size = function () {
192
+ return 8 + list_overhead(this.body);
193
+ };
194
+
195
+ AST_Case.prototype._size = function () {
196
+ return 5 + list_overhead(this.body);
197
+ };
198
+
199
+ AST_Default.prototype._size = function () {
200
+ return 8 + list_overhead(this.body);
201
+ };
202
+
203
+ AST_Try.prototype._size = function () {
204
+ return 3 + list_overhead(this.body);
205
+ };
206
+
207
+ AST_Catch.prototype._size = function () {
208
+ let size = 7 + list_overhead(this.body);
209
+ if (this.argname) {
210
+ size += 2;
211
+ }
212
+ return size;
213
+ };
214
+
215
+ AST_Finally.prototype._size = function () {
216
+ return 7 + list_overhead(this.body);
217
+ };
218
+
219
+ /*#__INLINE__*/
220
+ const def_size = (size, def) => size + list_overhead(def.definitions);
221
+
222
+ AST_Var.prototype._size = function () {
223
+ return def_size(4, this);
224
+ };
225
+
226
+ AST_Let.prototype._size = function () {
227
+ return def_size(4, this);
228
+ };
229
+
230
+ AST_Const.prototype._size = function () {
231
+ return def_size(6, this);
232
+ };
233
+
234
+ AST_VarDef.prototype._size = function () {
235
+ return this.value ? 1 : 0;
236
+ };
237
+
238
+ AST_NameMapping.prototype._size = function () {
239
+ // foreign name isn't mangled
240
+ return this.name ? 4 : 0;
241
+ };
242
+
243
+ AST_Import.prototype._size = function () {
244
+ // import
245
+ let size = 6;
246
+
247
+ if (this.imported_name) size += 1;
248
+
249
+ // from
250
+ if (this.imported_name || this.imported_names) size += 5;
251
+
252
+ // braces, and the commas
253
+ if (this.imported_names) {
254
+ size += 2 + list_overhead(this.imported_names);
255
+ }
256
+
257
+ return size;
258
+ };
259
+
260
+ AST_Export.prototype._size = function () {
261
+ let size = 7 + (this.is_default ? 8 : 0);
262
+
263
+ if (this.exported_value) {
264
+ size += this.exported_value._size();
265
+ }
266
+
267
+ if (this.exported_names) {
268
+ // Braces and commas
269
+ size += 2 + list_overhead(this.exported_names);
270
+ }
271
+
272
+ if (this.module_name) {
273
+ // "from "
274
+ size += 5;
275
+ }
276
+
277
+ return size;
278
+ };
279
+
280
+ AST_Call.prototype._size = function () {
281
+ return 2 + list_overhead(this.args);
282
+ };
283
+
284
+ AST_New.prototype._size = function () {
285
+ return 6 + list_overhead(this.args);
286
+ };
287
+
288
+ AST_Sequence.prototype._size = function () {
289
+ return list_overhead(this.expressions);
290
+ };
291
+
292
+ AST_Dot.prototype._size = function () {
293
+ return this.property.length + 1;
294
+ };
295
+
296
+ AST_Sub.prototype._size = () => 2;
297
+
298
+ AST_Unary.prototype._size = function () {
299
+ if (this.operator === "typeof") return 7;
300
+ if (this.operator === "void") return 5;
301
+ return this.operator.length;
302
+ };
303
+
304
+ AST_Binary.prototype._size = function (info) {
305
+ if (this.operator === "in") return 4;
306
+
307
+ let size = this.operator.length;
308
+
309
+ if (
310
+ (this.operator === "+" || this.operator === "-")
311
+ && this.right instanceof AST_Unary && this.right.operator === this.operator
312
+ ) {
313
+ // 1+ +a > needs space between the +
314
+ size += 1;
315
+ }
316
+
317
+ if (this.needs_parens(info)) {
318
+ size += 2;
319
+ }
320
+
321
+ return size;
322
+ };
323
+
324
+ AST_Conditional.prototype._size = () => 3;
325
+
326
+ AST_Array.prototype._size = function () {
327
+ return 2 + list_overhead(this.elements);
328
+ };
329
+
330
+ AST_Object.prototype._size = function (info) {
331
+ let base = 2;
332
+ if (first_in_statement(info)) {
333
+ base += 2; // parens
334
+ }
335
+ return base + list_overhead(this.properties);
336
+ };
337
+
338
+ /*#__INLINE__*/
339
+ const key_size = key =>
340
+ typeof key === "string" ? key.length : 0;
341
+
342
+ AST_ObjectKeyVal.prototype._size = function () {
343
+ return key_size(this.key) + 1;
344
+ };
345
+
346
+ /*#__INLINE__*/
347
+ const static_size = is_static => is_static ? 7 : 0;
348
+
349
+ AST_ObjectGetter.prototype._size = function () {
350
+ return 5 + static_size(this.static) + key_size(this.key);
351
+ };
352
+
353
+ AST_ObjectSetter.prototype._size = function () {
354
+ return 5 + static_size(this.static) + key_size(this.key);
355
+ };
356
+
357
+ AST_ConciseMethod.prototype._size = function () {
358
+ return static_size(this.static) + key_size(this.key) + lambda_modifiers(this);
359
+ };
360
+
361
+ AST_Class.prototype._size = function () {
362
+ return (
363
+ (this.name ? 8 : 7)
364
+ + (this.extends ? 8 : 0)
365
+ );
366
+ };
367
+
368
+ AST_ClassProperty.prototype._size = function () {
369
+ return (
370
+ static_size(this.static)
371
+ + (typeof this.key === "string" ? this.key.length + 2 : 0)
372
+ + (this.value ? 1 : 0)
373
+ );
374
+ };
375
+
376
+ AST_Symbol.prototype._size = function () {
377
+ return !mangle_options || this.definition().unmangleable(mangle_options)
378
+ ? this.name.length
379
+ : 2;
380
+ };
381
+
382
+ // TODO take propmangle into account
383
+ AST_SymbolClassProperty.prototype._size = function () {
384
+ return this.name.length;
385
+ };
386
+
387
+ AST_SymbolRef.prototype._size = AST_SymbolDeclaration.prototype._size = function () {
388
+ const { name, thedef } = this;
389
+
390
+ if (thedef && thedef.global) return name.length;
391
+
392
+ if (name === "arguments") return 9;
393
+
394
+ return 2;
395
+ };
396
+
397
+ AST_NewTarget.prototype._size = () => 10;
398
+
399
+ AST_SymbolImportForeign.prototype._size = function () {
400
+ return this.name.length;
401
+ };
402
+
403
+ AST_SymbolExportForeign.prototype._size = function () {
404
+ return this.name.length;
405
+ };
406
+
407
+ AST_This.prototype._size = () => 4;
408
+
409
+ AST_Super.prototype._size = () => 5;
410
+
411
+ AST_String.prototype._size = function () {
412
+ return this.value.length + 2;
413
+ };
414
+
415
+ AST_Number.prototype._size = function () {
416
+ const { value } = this;
417
+ if (value === 0) return 1;
418
+ if (value > 0 && Math.floor(value) === value) {
419
+ return Math.floor(Math.log10(value) + 1);
420
+ }
421
+ return value.toString().length;
422
+ };
423
+
424
+ AST_BigInt.prototype._size = function () {
425
+ return this.value.length;
426
+ };
427
+
428
+ AST_RegExp.prototype._size = function () {
429
+ return this.value.toString().length;
430
+ };
431
+
432
+ AST_Null.prototype._size = () => 4;
433
+
434
+ AST_NaN.prototype._size = () => 3;
435
+
436
+ AST_Undefined.prototype._size = () => 6; // "void 0"
437
+
438
+ AST_Hole.prototype._size = () => 0; // comma is taken into account
439
+
440
+ AST_Infinity.prototype._size = () => 8;
441
+
442
+ AST_True.prototype._size = () => 4;
443
+
444
+ AST_False.prototype._size = () => 5;
445
+
446
+ AST_Await.prototype._size = () => 6;
447
+
448
+ AST_Yield.prototype._size = () => 6;
@@ -0,0 +1,106 @@
1
+ /***********************************************************************
2
+
3
+ A JavaScript tokenizer / parser / beautifier / compressor.
4
+ https://github.com/mishoo/UglifyJS2
5
+
6
+ -------------------------------- (C) ---------------------------------
7
+
8
+ Author: Mihai Bazon
9
+ <mihai.bazon@gmail.com>
10
+ http://mihai.bazon.net/blog
11
+
12
+ Distributed under the BSD license:
13
+
14
+ Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
15
+
16
+ Redistribution and use in source and binary forms, with or without
17
+ modification, are permitted provided that the following conditions
18
+ are met:
19
+
20
+ * Redistributions of source code must retain the above
21
+ copyright notice, this list of conditions and the following
22
+ disclaimer.
23
+
24
+ * Redistributions in binary form must reproduce the above
25
+ copyright notice, this list of conditions and the following
26
+ disclaimer in the documentation and/or other materials
27
+ provided with the distribution.
28
+
29
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
30
+ EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
33
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
34
+ OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
38
+ TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
39
+ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40
+ SUCH DAMAGE.
41
+
42
+ ***********************************************************************/
43
+
44
+ "use strict";
45
+
46
+ import MOZ_SourceMap from "source-map";
47
+ import {
48
+ defaults,
49
+ } from "./utils/index.js";
50
+
51
+ // a small wrapper around fitzgen's source-map library
52
+ function SourceMap(options) {
53
+ options = defaults(options, {
54
+ file : null,
55
+ root : null,
56
+ orig : null,
57
+
58
+ orig_line_diff : 0,
59
+ dest_line_diff : 0,
60
+ });
61
+ var generator = new MOZ_SourceMap.SourceMapGenerator({
62
+ file : options.file,
63
+ sourceRoot : options.root
64
+ });
65
+ var orig_map = options.orig && new MOZ_SourceMap.SourceMapConsumer(options.orig);
66
+
67
+ if (orig_map) {
68
+ orig_map.sources.forEach(function(source) {
69
+ var sourceContent = orig_map.sourceContentFor(source, true);
70
+ if (sourceContent) {
71
+ generator.setSourceContent(source, sourceContent);
72
+ }
73
+ });
74
+ }
75
+
76
+ function add(source, gen_line, gen_col, orig_line, orig_col, name) {
77
+ if (orig_map) {
78
+ var info = orig_map.originalPositionFor({
79
+ line: orig_line,
80
+ column: orig_col
81
+ });
82
+ if (info.source === null) {
83
+ return;
84
+ }
85
+ source = info.source;
86
+ orig_line = info.line;
87
+ orig_col = info.column;
88
+ name = info.name || name;
89
+ }
90
+ generator.addMapping({
91
+ generated : { line: gen_line + options.dest_line_diff, column: gen_col },
92
+ original : { line: orig_line + options.orig_line_diff, column: orig_col },
93
+ source : source,
94
+ name : name
95
+ });
96
+ }
97
+ return {
98
+ add : add,
99
+ get : function() { return generator; },
100
+ toString : function() { return JSON.stringify(generator.toJSON()); }
101
+ };
102
+ }
103
+
104
+ export {
105
+ SourceMap,
106
+ };