Skip to content

Commit f3881d8

Browse files
fix(concatenate-modules): keep a wrapped reference's ASI guard outside its parentheses (#22068)
getFinalName re-parenthesized a reference whose rawName already carried an ASI guard, nesting the semicolon inside the new parentheses and emitting JavaScript that does not parse. Claude-Session: https://claude.ai/code/session_01MDVzu1fMFHqzayaaQXZCYE Co-authored-by: Claude Opus 5 <[email protected]>
1 parent e142eea commit f3881d8

22 files changed

Lines changed: 4015 additions & 3 deletions

File tree

‎.changeset/017-concatenate-asi-position.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-
Keep a concatenated CommonJS reference from joining the previous statement.
5+
Keep a concatenated CommonJS reference a separate statement, guarding it once.

‎lib/optimize/ConcatenatedModule.js‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,18 @@ const getUnusedExportExpr = (info, asiSafe) =>
434434
? `${asiSafe === false ? ";" : ""}/* unused export */ (${info.namespaceObjectName}, undefined)`
435435
: "/* unused export */ undefined";
436436

437+
/**
438+
* Parenthesizes a reference that may already carry an ASI guard of its own,
439+
* keeping that guard outside the new parentheses — nested it would not parse.
440+
* @param {string} reference the reference
441+
* @param {boolean | undefined} asiSafe asiSafe
442+
* @returns {string} the parenthesized reference
443+
*/
444+
const parenthesizeReference = (reference, asiSafe) =>
445+
asiSafe === false
446+
? `;(${reference.startsWith(";") ? reference.slice(1) : reference})`
447+
: `(${reference})`;
448+
437449
/**
438450
* @param {ModuleGraph} moduleGraph the module graph
439451
* @param {ModuleInfo} info module info
@@ -1002,12 +1014,12 @@ const getFinalName = (
10021014
? `;(0,${reference})`
10031015
: `/*#__PURE__*/Object(${reference})`;
10041016
} else if (binding.info.wrapped) {
1005-
return asiSafe === false ? `;(${reference})` : `(${reference})`;
1017+
return parenthesizeReference(reference, asiSafe);
10061018
}
10071019
} else if (accessorCall && !asCall) {
10081020
// without parentheses `new X()` would construct the accessor rather
10091021
// than what it returns
1010-
return asiSafe === false ? `;(${reference})` : `(${reference})`;
1022+
return parenthesizeReference(reference, asiSafe);
10111023
}
10121024

10131025
return reference;

0 commit comments

Comments
 (0)