Skip to content

Commit 8bc561c

Browse files
committed
review
1 parent a905883 commit 8bc561c

6 files changed

Lines changed: 46 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()` edges. A `require()` target and its whole transitive dependency closure are wrapped together, so nothing a lazily evaluated member depends on is scope-hoisted ahead of it. Set `optimization.concatenateModules: { commonjs: false }` to opt out of both.
5+
Wrap concatenated modules in lazy `__webpack_require__.cw` accessors and inline `require()`.

‎lib/optimize/ConcatenatedModule.js‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,20 +2773,21 @@ ${defineGetters}`
27732773
case "concatenated": {
27742774
name = info.namespaceObjectName;
27752775
// wrapped bodies are emitted above, evaluated at their call sites
2776-
if (!info.wrapped) {
2777-
result.add(
2778-
`\n;// ${info.module.readableIdentifier(requestShortener)}\n`
2779-
);
2780-
result.add(/** @type {ReplaceSource} */ (info.source));
2781-
} else if (info.eager) {
2776+
if (info.wrapped && info.eager) {
27822777
// This slot is the module's place in evaluation order; calling from
27832778
// the importing body would run it too late.
27842779
result.add(
27852780
`\n;// ${info.module.readableIdentifier(
27862781
requestShortener
27872782
)}\n${info.namespaceFnName}();\n`
27882783
);
2784+
} else if (!info.wrapped) {
2785+
result.add(
2786+
`\n;// ${info.module.readableIdentifier(requestShortener)}\n`
2787+
);
2788+
result.add(/** @type {ReplaceSource} */ (info.source));
27892789
}
2790+
27902791
if (info.chunkInitFragments) {
27912792
for (const f of info.chunkInitFragments) chunkInitFragments.push(f);
27922793
}
@@ -2842,6 +2843,12 @@ ${defineGetters}`
28422843
// module must come after its import variable is defined.
28432844
const escapeSource = escapeNamespaceObjectSources.get(info);
28442845
if (escapeSource) result.add(escapeSource);
2846+
} else if (info.wrapped && info.eager) {
2847+
result.add(
2848+
`\n;// EXTERNAL MODULE: ${info.module.readableIdentifier(
2849+
requestShortener
2850+
)}\n${info.namespaceFnName}();\n`
2851+
);
28452852
}
28462853
// If a module is deferred in other places, but used as non-deferred here,
28472854
// the module itself will be emitted as mod_deferred (in the case "external"),
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
3+
// require.resolve can't be concatenated, so it keeps "external-side-effect"
4+
// outside the concatenation; the lazy require() below still wraps it
5+
exports.resolved = typeof require.resolve("./external-side-effect");
6+
7+
global.__externalOrder = (global.__externalOrder || []).concat(
8+
"external-consumer"
9+
);
10+
11+
exports.read = function read() {
12+
return require("./external-side-effect").tag;
13+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict";
2+
3+
global.__externalOrder = (global.__externalOrder || []).concat("external");
4+
5+
exports.tag = "external";

‎test/configCases/concatenate-modules/commonjs-wrapped-import-order/index.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import { v } from "./esm-second";
77
import { label } from "./nested-outer";
88
import "./nested-after";
99
import "./nested-consumer";
10+
// a wrapped member kept out of the concatenation is only reachable through its
11+
// accessor, so a side-effect import has to call it at this slot
12+
import "./external-side-effect";
13+
import { read } from "./external-consumer";
1014

1115
it("should evaluate imports in source order across wrapped and hoisted members", () => {
1216
expect(v).toBe("esm-second");
@@ -24,3 +28,11 @@ it("should evaluate a nested wrapped chain in source order", () => {
2428
delete global.__importOrder;
2529
delete global.__nestedOrder;
2630
});
31+
32+
it("should evaluate a wrapped non-concatenated member at its import slot", () => {
33+
// already run by the side-effect import; read() only sees the memoized result
34+
expect(global.__externalOrder).toEqual(["external", "external-consumer"]);
35+
expect(read()).toBe("external");
36+
expect(global.__externalOrder).toEqual(["external", "external-consumer"]);
37+
delete global.__externalOrder;
38+
});

‎types.d.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8483,12 +8483,12 @@ declare interface ExternalModuleInfo {
84838483
type: "external";
84848484

84858485
/**
8486-
* the body renders inside the lazy CJS wrapper with real module/exports instead of being scope-hoisted into the shared scope
8486+
* the module's `require()` call is deferred behind an accessor instead of running at its own slot; unlike a wrapped concatenated member there is no body and no CJS wrapper, so any external may be wrapped
84878487
*/
84888488
wrapped: boolean;
84898489

84908490
/**
8491-
* a wrapped body reached by an eager import edge, so its accessor is called at its own slot in evaluation order
8491+
* a wrapped module reached by an eager import edge, so its accessor is called at its own slot in evaluation order
84928492
*/
84938493
eager: boolean;
84948494
module: Module;

0 commit comments

Comments
 (0)