Skip to content

Commit 3ca32bc

Browse files
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 2406192 commit 3ca32bc

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37832,11 +37832,13 @@ const createCompressHelpers = (modules) => {
3783237832
if (undefinedValue) return undefinedValue;
3783337833
if (!isSymbolRefNode(node)) return false;
3783437834
let fixed = node.definition.fixed;
37835-
// terser reads only a value stored as a node, not one read lazily.
37835+
// terser reads only a value stored as a node, not one read lazily; an
37836+
// exposed variable stays declared, so `void 0` saves nothing there.
3783637837
if (
3783737838
!isSyntaxNode(fixed) &&
3783837839
modules.improvements !== undefined &&
37839-
modules.improvements.enabled
37840+
modules.improvements.enabled &&
37841+
!compressor.exposed(node.definition)
3784037842
) {
3784137843
fixed = ast.fixedValue(node);
3784237844
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,10 @@ const IMPROVED_CASES = [
11651165
// of its function's own, or the call passes, keeps or constructs something.
11661166
/** @type {[string, string][]} */
11671167
const KEPT_CASES = [
1168+
[
1169+
"optional reads of a global `const` known `undefined`, which stays declared",
1170+
"const obj = void 0; function f(x) { console.log(x); } f(obj?.a); f(obj?.a);"
1171+
],
11681172
[
11691173
"a `RegExp` call where the program assigns the global",
11701174
'RegExp = function (pattern) { return "own " + pattern; }; console.log(RegExp("a"));'

0 commit comments

Comments
 (0)