Skip to content

Commit 178f141

Browse files
feat(javascript): fold optional calls of a nullish const in the minifier (#22653)
* feat(javascript): fold optional calls of a nullish const in the minifier terser reads a variable as nullish only when its fixed value is stored as a node, so a `const d = void 0` read more than once keeps every `d?.(…)`. Refs terser/terser#1400 * docs(changeset): keep the minifier entry within 80 characters * fix(javascript): keep an optional read of an exposed nullish const in the minifier An exposed variable stays declared, so folding its `?.` read to `void 0` saves no bytes and can cost gzip.
1 parent ff064c2 commit 178f141

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

‎.changeset/047-javascript-minifier-unwrap-iife.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Unwrap IIFEs, fold built-in calls, drop unused built-ins and write long string arrays as one split string when minifying JS.
5+
Shrink minified JS: unwrap IIFEs, fold built-ins and calls, split string arrays.

‎lib/javascript/syntax-printer.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38021,7 +38021,17 @@ const createCompressHelpers = (modules) => {
3802138021
const undefinedValue = isUndefined(node, compressor);
3802238022
if (undefinedValue) return undefinedValue;
3802338023
if (!isSymbolRefNode(node)) return false;
38024-
const fixed = node.definition.fixed;
38024+
let fixed = node.definition.fixed;
38025+
// terser reads only a value stored as a node, not one read lazily; an
38026+
// exposed variable stays declared, so `void 0` saves nothing there.
38027+
if (
38028+
!isSyntaxNode(fixed) &&
38029+
modules.improvements !== undefined &&
38030+
modules.improvements.enabled &&
38031+
!compressor.exposed(node.definition)
38032+
) {
38033+
fixed = ast.fixedValue(node);
38034+
}
3802538035
return (
3802638036
isSyntaxNode(fixed) && isNullish(/** @type {Node} */ (fixed), compressor)
3802738037
);

‎test/unitCases/syntax-printer.unittest.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ const IMPROVED_CASES = [
263263
{ compress: {}, mangle: false },
264264
"console.log(1,function(t){return t.y}({y:2}),function(t){return t.z+1}({z:3}));"
265265
],
266+
[
267+
"optional calls of a `const` known `undefined`, read too often to inline",
268+
"function f(g) { const d = void 0; d?.(g()); d?.(g()); d?.(g()); return [d?.x.y, d ?? 1]; } console.log(f(() => console.log(\"called\")));",
269+
{ compress: {}, mangle: false },
270+
"function f(g){return[void 0,1]}console.log(f(()=>console.log(\"called\")));"
271+
],
266272
[
267273
"an assignment to a `let` folded into the first read of it",
268274
"function f(a) { let x, y; x = a * 2; y = g(x); return [x, y]; } function g(v) { return v + 1; } console.log(f(3));",
@@ -1189,6 +1195,10 @@ const IMPROVED_CASES = [
11891195
// of its function's own, or the call passes, keeps or constructs something.
11901196
/** @type {[string, string][]} */
11911197
const KEPT_CASES = [
1198+
[
1199+
"optional reads of a global `const` known `undefined`, which stays declared",
1200+
"const obj = void 0; function f(x) { console.log(x); } f(obj?.a); f(obj?.a);"
1201+
],
11921202
[
11931203
"a `RegExp` call where the program assigns the global",
11941204
'RegExp = function (pattern) { return "own " + pattern; }; console.log(RegExp("a"));'

0 commit comments

Comments
 (0)