Skip to content

Commit c593dea

Browse files
fix(concatenation): keep wrapped module names and side effects intact (#21593)
* fix(concatenation): keep wrapped module names and side effects intact A wrapped CommonJS body is emitted verbatim and never scope-analysed, so its identifiers were unknown when the concatenation allocated its own top-level names. A local declaration could shadow the wrapper accessor it calls, and a free global could be captured by another member's top-level binding. Claim the body's identifiers instead, in place of the scope analysis that is skipped. An unused export of a wrapped module also collapsed to `undefined`, dropping the accessor call with it — and with it the module's side effects, which a hoisted body would still have run at its own slot. * fix(concatenation): drop the dead identifier cache and untestable branch Coverage showed the WeakMap around the identifier scan never hit: each _analyseModule builds a fresh Source, so the key is always new. It cost an allocation and a lookup per wrapped body for nothing. The unused-export change at the second getFinalBinding site is reverted: no build reaches it, because an unused ESM re-export of a wrapped module is dropped before code generation. The require()-shaped site keeps the fix and is the one a build actually hits. * test: make three cases hermetic against a shared global ConfigTestCases and ConfigCacheTestCases run the same cases, and a worker that shares one global between test files (Bun's --workerThreads) carries their leftovers into the second run. The harness only clears globals whose name contains "webpack", so a plain counter survives: the deferred-evaluation case then saw a non-zero count and the inline-exports case saw its side effects twice. Both now clear their own counter. The amd-runtimeChunk library variants are a timing difference, not a leftover: the `promise` external is still settling when the harness deletes the chunk loading globals, which only survives because Node gives each test file a fresh global. Filtered out under Bun until the harness can await those externals.
1 parent f2462c2 commit c593dea

36 files changed

Lines changed: 413 additions & 9 deletions

File tree

‎.changeset/010-concatenate-module-wrapping.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-
Wrap concatenated modules in lazy `__webpack_require__.cw` accessors and inline `require()`.
5+
Wrap concatenated modules in lazy `__webpack_require__.cw` accessors and inline `require()`, keeping a wrapped body's names and side effects intact.

‎lib/optimize/ConcatenatedModule.js‎

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,34 @@ const { InlinedUsedName } = require("./InlineExports");
128128
const getNormalModule = memoize(() => require("../NormalModule"));
129129
const getExternalModule = memoize(() => require("../ExternalModule"));
130130

131+
const IDENTIFIER_SCAN_REGEXP = /[$A-Za-z_][\w$]*/g;
132+
const IDENTIFIER_SCAN_WHITESPACE = new Set([" ", "\t", "\n", "\r"]);
133+
134+
/**
135+
* Finds every identifier a generated source could resolve against the enclosing
136+
* concatenation scope. Property names are skipped; string and comment contents
137+
* are not, which only over-reserves.
138+
* @param {Source} source generated source
139+
* @returns {ReadonlySet<string>} identifier names
140+
*/
141+
const findIdentifiers = (source) => {
142+
const code = source.source().toString();
143+
/** @type {Set<string>} */
144+
const result = new Set();
145+
IDENTIFIER_SCAN_REGEXP.lastIndex = 0;
146+
let match = IDENTIFIER_SCAN_REGEXP.exec(code);
147+
while (match !== null) {
148+
let i = match.index - 1;
149+
while (i >= 0 && IDENTIFIER_SCAN_WHITESPACE.has(code[i])) i--;
150+
// `a.b` can't bind to the outer scope, but the `b` of `...b` can
151+
if (!(i >= 0 && code[i] === "." && code[i - 1] !== ".")) {
152+
result.add(match[0]);
153+
}
154+
match = IDENTIFIER_SCAN_REGEXP.exec(code);
155+
}
156+
return result;
157+
};
158+
131159
// fix eslint-scope to support class properties correctly
132160
// cspell:word Referencer
133161
const ReferencerClass = Referencer;
@@ -382,6 +410,18 @@ const getExternalModuleExpr = (info) =>
382410
const getInteropExpr = (info, interopName) =>
383411
info.wrapped ? `${interopName}()` : /** @type {string} */ (interopName);
384412

413+
/**
414+
* Expression for an export that is not used. A hoisted body runs at its own
415+
* slot, but a wrapped one only runs when its accessor is called, so dropping
416+
* the reference would drop the module's side effects with it.
417+
* @param {ConcatenatedModuleInfo} info module info
418+
* @returns {string} expression evaluating to undefined
419+
*/
420+
const getUnusedExportExpr = (info) =>
421+
info.wrapped
422+
? `/* unused export */ (${info.namespaceObjectName}, undefined)`
423+
: "/* unused export */ undefined";
424+
385425
/**
386426
* @param {ModuleGraph} moduleGraph the module graph
387427
* @param {ModuleInfo} info module info
@@ -436,7 +476,7 @@ const getFinalBinding = (
436476
if (!usedName) {
437477
return {
438478
info,
439-
rawName: "/* unused export */ undefined",
479+
rawName: getUnusedExportExpr(info),
440480
ids: [],
441481
exportName
442482
};
@@ -1778,6 +1818,14 @@ class ConcatenatedModule extends Module {
17781818
for (const info of modulesWithInfo) {
17791819
if (info.type === "concatenated") {
17801820
if (info.wrapped) {
1821+
// The body is emitted verbatim and never renamed, so every identifier
1822+
// in it is claimed: its declarations must not shadow a name [3]
1823+
// generates, and its free globals must not be captured by one.
1824+
for (const name of findIdentifiers(
1825+
/** @type {Source} */ (info.internalSource)
1826+
)) {
1827+
allUsedNames.add(name);
1828+
}
17811829
// A wrapped body has no scope analysis, so its references are found
17821830
// textually. They still have to be resolved here: binding one is what
17831831
// flags the interop objects it needs, and [3] allocates their names.
@@ -2017,10 +2065,9 @@ class ConcatenatedModule extends Module {
20172065
case "concatenated": {
20182066
if (info.wrapped) {
20192067
// A wrapped module declares exactly one shared-scope name: the
2020-
// lazy wrapper accessor holding its body. The wrapped body is not
2021-
// scope-analyzed, so its locals are unknown here and can't be
2022-
// avoided by findNewName — the reserved `__webpack_` prefix is
2023-
// what keeps a local in that body from shadowing the accessor.
2068+
// lazy wrapper accessor holding its body. [2] claimed every
2069+
// identifier of that body, so findNewName avoids the ones that
2070+
// would shadow the accessor from inside it.
20242071
const namespaceFnName = findNewName(
20252072
"namespaceFn",
20262073
allUsedNames,
@@ -2963,9 +3010,10 @@ ${defineGetters}`
29633010
if (info.type === "concatenated") {
29643011
const m = info.module;
29653012
if (info.wrapped) {
2966-
// the wrapper isolates the body's top-level declarations, so no scope
2967-
// analysis or renaming is needed -- only a concatenation scope, to
2968-
// rewrite in-scope `require()` targets to accessor references
3013+
// the wrapper isolates the body's top-level declarations, so no
3014+
// renaming is needed -- only a concatenation scope, to rewrite in-scope
3015+
// `require()` targets to accessor references. [2] claims its identifiers
3016+
// textually in place of the scope analysis skipped here.
29693017
const concatenationScope = new ConcatenationScope(
29703018
modulesMap,
29713019
info,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
module.exports = { tag: "alias-target" };
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"use strict";
2+
3+
let calls = 0;
4+
5+
module.exports = {
6+
next() {
7+
calls += 1;
8+
return calls;
9+
}
10+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict";
2+
3+
module.exports = function Thing() {
4+
this.kind = "thing";
5+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
module.exports = "one";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
module.exports = "two";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const esmValue = "esm-dep";
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { esmValue } from "./esm-dep.js";
2+
3+
const { first, second } = require("./named-exports.js");
4+
const templateRequire = require(`./named-exports.js`);
5+
const contextName = "one";
6+
const contextRequire = require(`./dir/${contextName}.js`);
7+
const instance = new (require("./ctor.js"))();
8+
// `new require(x)` binds the argument list to `new`, so this is the exports
9+
// value itself -- the trailing `()` in `new require(x)()` would call it
10+
const constructedRequire = new require("./ctor.js");
11+
12+
it("should read a destructured require()", () => {
13+
expect(esmValue).toBe("esm-dep");
14+
expect(first).toBe("first");
15+
expect(second).toBe("second");
16+
});
17+
18+
it("should read a require() written as a template literal", () => {
19+
expect(templateRequire.first).toBe("first");
20+
});
21+
22+
it("should pass the exports object as `this` to a member call", () => {
23+
expect(require("./named-exports.js").readThis()).toBe("this=exports");
24+
});
25+
26+
it("should keep a computed request resolving at runtime", () => {
27+
expect(contextRequire).toBe("one");
28+
// the context module stays out of the concatenation, so the two requires of
29+
// ./named-exports.js must still share its instance
30+
expect(templateRequire).toBe(require("./named-exports.js"));
31+
});
32+
33+
it("should construct through a parenthesized require()", () => {
34+
expect(instance.kind).toBe("thing");
35+
});
36+
37+
it("should give `new require()` the Node.js return-value semantics", () => {
38+
// a function exports value passes through the `new` return-value rule
39+
expect(constructedRequire).toBe(require("./ctor.js"));
40+
// so calling it is a plain call, with no `this` -- exactly as in Node.js
41+
expect(() => new require("./ctor.js")()).toThrow(TypeError);
42+
});
43+
44+
it("should share one instance across every require() of a module", () => {
45+
const a = require("./counter.js");
46+
const b = require("./counter.js");
47+
expect(a).toBe(b);
48+
expect(a.next()).toBe(1);
49+
expect(b.next()).toBe(2);
50+
});
51+
52+
it("should keep a whole-module and a member alias pointing at one instance", () => {
53+
expect(require("./whole-alias.js")).toBe(require("./alias-target.js"));
54+
expect(require("./member-alias.js").tag).toBe("alias-target");
55+
});
56+
57+
it("should concatenate every module with a static request", () => {
58+
const concatenated = new Set();
59+
for (const m of __STATS__.modules) {
60+
if (!m.modules) continue;
61+
for (const inner of m.modules) concatenated.add(inner.name);
62+
}
63+
for (const name of [
64+
"./alias-target.js",
65+
"./counter.js",
66+
"./ctor.js",
67+
"./esm-dep.js",
68+
"./index.js",
69+
"./member-alias.js",
70+
"./named-exports.js",
71+
"./whole-alias.js"
72+
]) {
73+
expect(concatenated).toContain(name);
74+
}
75+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
exports.tag = require("./alias-target.js").tag;

0 commit comments

Comments
 (0)