terser 5.22.0 → 5.23.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,9 @@
1
1
  # Changelog
2
2
 
3
+ ## v5.23.0
4
+ - When top_retain will keep a variable assignment around, inline the assignee when it's shorter than the name (#1434)
5
+ - Remove empty class `static {}` blocks.
6
+
3
7
  ## v5.22.0
4
8
  - Do not `unsafe`ly shorten expressions like a?.toString() when they're conditional.
5
9
  - Avoid running drop_unused in nodes that aren't scopes. Fixes a rare crash.
@@ -2,7 +2,7 @@
2
2
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@jridgewell/source-map')) :
3
3
  typeof define === 'function' && define.amd ? define(['exports', '@jridgewell/source-map'], factory) :
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Terser = {}, global.sourceMap));
5
- }(this, (function (exports, sourceMap) { 'use strict';
5
+ })(this, (function (exports, sourceMap) { 'use strict';
6
6
 
7
7
  /***********************************************************************
8
8
 
@@ -17696,18 +17696,43 @@ function scope_encloses_variables_in_this_scope(scope, pulled_scope) {
17696
17696
  return false;
17697
17697
  }
17698
17698
 
17699
+ /**
17700
+ * An extra check function for `top_retain` option, compare the length of const identifier
17701
+ * and init value length and return true if init value is longer than identifier. for example:
17702
+ * ```
17703
+ * // top_retain: ["example"]
17704
+ * const example = 100
17705
+ * ```
17706
+ * it will return false because length of "100" is short than identifier "example".
17707
+ */
17708
+ function is_const_symbol_short_than_init_value(def, fixed_value) {
17709
+ if (def.orig.length === 1 && fixed_value) {
17710
+ const init_value_length = fixed_value.size();
17711
+ const identifer_length = def.name.length;
17712
+ return init_value_length > identifer_length;
17713
+ }
17714
+ return true;
17715
+ }
17716
+
17699
17717
  function inline_into_symbolref(self, compressor) {
17700
17718
  const parent = compressor.parent();
17701
-
17702
17719
  const def = self.definition();
17703
17720
  const nearest_scope = compressor.find_scope();
17704
- if (compressor.top_retain && def.global && compressor.top_retain(def)) {
17721
+ let fixed = self.fixed_value();
17722
+ if (
17723
+ compressor.top_retain &&
17724
+ def.global &&
17725
+ compressor.top_retain(def) &&
17726
+ // when identifier is in top_retain option dose not mean we can always inline it.
17727
+ // if identifier name is longer then init value, we can replace it.
17728
+ is_const_symbol_short_than_init_value(def, fixed)
17729
+ ) {
17730
+ // keep it
17705
17731
  def.fixed = false;
17706
17732
  def.single_use = false;
17707
17733
  return self;
17708
17734
  }
17709
17735
 
17710
- let fixed = self.fixed_value();
17711
17736
  let single_use = def.single_use
17712
17737
  && !(parent instanceof AST_Call
17713
17738
  && (parent.is_callee_pure(compressor))
@@ -21643,8 +21668,14 @@ def_optimize(AST_Function, function(self, compressor) {
21643
21668
  });
21644
21669
 
21645
21670
  def_optimize(AST_Class, function(self) {
21646
- // HACK to avoid compress failure.
21647
- // AST_Class is not really an AST_Scope/AST_Block as it lacks a body.
21671
+ for (let i = 0; i < self.properties.length; i++) {
21672
+ const prop = self.properties[i];
21673
+ if (prop instanceof AST_ClassStaticBlock && prop.body.length == 0) {
21674
+ self.properties.splice(i, 1);
21675
+ i--;
21676
+ }
21677
+ }
21678
+
21648
21679
  return self;
21649
21680
  });
21650
21681
 
@@ -31264,4 +31295,4 @@ exports._default_options = _default_options;
31264
31295
  exports._run_cli = run_cli;
31265
31296
  exports.minify = minify;
31266
31297
 
31267
- })));
31298
+ }));
@@ -3549,8 +3549,14 @@ def_optimize(AST_Function, function(self, compressor) {
3549
3549
  });
3550
3550
 
3551
3551
  def_optimize(AST_Class, function(self) {
3552
- // HACK to avoid compress failure.
3553
- // AST_Class is not really an AST_Scope/AST_Block as it lacks a body.
3552
+ for (let i = 0; i < self.properties.length; i++) {
3553
+ const prop = self.properties[i];
3554
+ if (prop instanceof AST_ClassStaticBlock && prop.body.length == 0) {
3555
+ self.properties.splice(i, 1);
3556
+ i--;
3557
+ }
3558
+ }
3559
+
3554
3560
  return self;
3555
3561
  });
3556
3562
 
@@ -84,7 +84,7 @@ import {
84
84
 
85
85
  _INLINE,
86
86
  _NOINLINE,
87
- _PURE
87
+ _PURE,
88
88
  } from "../ast.js";
89
89
  import { make_node, has_annotation } from "../utils/index.js";
90
90
  import "../size.js";
@@ -148,18 +148,43 @@ function scope_encloses_variables_in_this_scope(scope, pulled_scope) {
148
148
  return false;
149
149
  }
150
150
 
151
+ /**
152
+ * An extra check function for `top_retain` option, compare the length of const identifier
153
+ * and init value length and return true if init value is longer than identifier. for example:
154
+ * ```
155
+ * // top_retain: ["example"]
156
+ * const example = 100
157
+ * ```
158
+ * it will return false because length of "100" is short than identifier "example".
159
+ */
160
+ function is_const_symbol_short_than_init_value(def, fixed_value) {
161
+ if (def.orig.length === 1 && fixed_value) {
162
+ const init_value_length = fixed_value.size();
163
+ const identifer_length = def.name.length;
164
+ return init_value_length > identifer_length;
165
+ }
166
+ return true;
167
+ }
168
+
151
169
  export function inline_into_symbolref(self, compressor) {
152
170
  const parent = compressor.parent();
153
-
154
171
  const def = self.definition();
155
172
  const nearest_scope = compressor.find_scope();
156
- if (compressor.top_retain && def.global && compressor.top_retain(def)) {
173
+ let fixed = self.fixed_value();
174
+ if (
175
+ compressor.top_retain &&
176
+ def.global &&
177
+ compressor.top_retain(def) &&
178
+ // when identifier is in top_retain option dose not mean we can always inline it.
179
+ // if identifier name is longer then init value, we can replace it.
180
+ is_const_symbol_short_than_init_value(def, fixed)
181
+ ) {
182
+ // keep it
157
183
  def.fixed = false;
158
184
  def.single_use = false;
159
185
  return self;
160
186
  }
161
187
 
162
- let fixed = self.fixed_value();
163
188
  let single_use = def.single_use
164
189
  && !(parent instanceof AST_Call
165
190
  && (parent.is_callee_pure(compressor))
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.22.0",
7
+ "version": "5.23.0",
8
8
  "engines": {
9
9
  "node": ">=10"
10
10
  },