Skip to content

Commit 26946e2

Browse files
fix(javascript): keep what a commented moved function reads when its call goes
A function moved to its one call stays where it is when a comment is on it, so a dead call no longer discounts its body's reads: `var a` read only by `/*! keep */ function f(){return a}` stays declared. The changeset sentence is shortened to 80 characters. Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC
1 parent 359232e commit 26946e2

3 files changed

Lines changed: 10 additions & 3 deletions

File tree

‎.changeset/065-minify-remaining-gaps.md‎

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

5-
Fold known `switch`es and literals, and drop more dead code, unread classes among it, when minifying.
5+
Fold known `switch`es and literals, and drop unread classes and more dead code.

‎lib/javascript/syntax-printer.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11571,9 +11571,11 @@ const createNodePrinter = (modules) => {
1157111571
if (definition) {
1157211572
droppedReads.set(definition, (droppedReads.get(definition) || 0) + 1);
1157311573
}
11574-
// A function moved to a read that goes prints nowhere, nor what it reads.
11574+
// A function moved to a read that goes prints nowhere, nor what it reads,
11575+
// unless a comment on it keeps it where it is.
1157511576
if (moveReads.size !== 0 && moveReads.has(node)) {
11576-
findNode(/** @type {number} */ (movedCallees.get(node)), discountRead);
11577+
const moved = /** @type {number} */ (movedCallees.get(node));
11578+
if (!hasEdgeComments(moved)) findNode(moved, discountRead);
1157711579
}
1157811580
};
1157911581

‎test/unitCases/javascript/printer.unittest.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,11 @@ describe("JavaScript minifier printing a SyntaxTree", () => {
14191419
"(function(){var a; function f(){return a} function u(){return f()} g()})()",
14201420
"g();"
14211421
],
1422+
// One a comment keeps where it is keeps what it reads.
1423+
[
1424+
"(function(){var a={}; /*! keep */ function f(){return a} function u(){return f()} g()})()",
1425+
"!function(){var a={};/*! keep */function f(){return a}g()}();"
1426+
],
14221427
[
14231428
"(function(){var o={m(){return p}}, p={m(){return o}}; g(p)})()",
14241429
"!function(){var o={m(){return p}},p={m(){return o}};g(p)}();"

0 commit comments

Comments
 (0)