Skip to content

Commit 3a73621

Browse files
authored
fix: use RuntimeKey instead of Runtime for export generation (#20346)
1 parent a596b26 commit 3a73621

18 files changed

Lines changed: 110 additions & 67 deletions

File tree

.changeset/olive-pans-tell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Fix missing export generation when concatenated modules in different chunks share the same runtime in module library bundles.

.changeset/tender-pillows-build.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-
Preserve star exports for dependencies in ESMA module output.
5+
Preserve star exports for dependencies in ECMA module output.

lib/Module.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ const makeSerializable = require("./util/makeSerializable");
137137
* @property {boolean=} sideEffectFree
138138
* @property {boolean=} isCSSModule
139139
* @property {Record<string, string>=} jsIncompatibleExports
140-
* @property {Map<RuntimeSpec, Record<string, string>>=} exportsFinalNameByRuntime
141-
* @property {Map<RuntimeSpec, string>=} exportsSourceByRuntime
140+
* @property {Map<string, Record<string, string>>=} exportsFinalNameByRuntime
141+
* @property {Map<string, string>=} exportsSourceByRuntime
142142
*/
143143

144144
/**

lib/library/ModuleLibraryPlugin.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const Template = require("../Template");
1313
const HarmonyExportImportedSpecifierDependency = require("../dependencies/HarmonyExportImportedSpecifierDependency");
1414
const ConcatenatedModule = require("../optimize/ConcatenatedModule");
1515
const propertyAccess = require("../util/propertyAccess");
16-
const { getEntryRuntime } = require("../util/runtime");
16+
const { getEntryRuntime, getRuntimeKey } = require("../util/runtime");
1717
const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
1818

1919
/** @typedef {import("webpack-sources").Source} Source */
@@ -92,8 +92,9 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
9292
(buildMeta.exportsFinalNameByRuntime = new Map());
9393

9494
for (const runtime of runtimes) {
95-
exportsSourceByRuntime.set(runtime, source);
96-
exportsFinalNameByRuntime.set(runtime, finalName);
95+
const key = getRuntimeKey(runtime);
96+
exportsSourceByRuntime.set(key, source);
97+
exportsFinalNameByRuntime.set(key, finalName);
9798
}
9899

99100
return true;
@@ -277,7 +278,9 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
277278
const exportsFinalNameByRuntime =
278279
(module.buildMeta &&
279280
module.buildMeta.exportsFinalNameByRuntime &&
280-
module.buildMeta.exportsFinalNameByRuntime.get(chunk.runtime)) ||
281+
module.buildMeta.exportsFinalNameByRuntime.get(
282+
getRuntimeKey(chunk.runtime)
283+
)) ||
281284
{};
282285

283286
const definitions =
@@ -421,9 +424,10 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
421424
const exportsSource =
422425
module.buildMeta &&
423426
module.buildMeta.exportsSourceByRuntime &&
424-
module.buildMeta.exportsSourceByRuntime.get(chunk.runtime);
427+
module.buildMeta.exportsSourceByRuntime.get(getRuntimeKey(chunk.runtime));
425428

426-
// Re-add the module's exports source when rendered in factory or as an inlined startup module wrapped in an IIFE
429+
// Re-add the module's exports source when rendered in factory
430+
// or as an inlined startup module wrapped in an IIFE
427431
if ((inlinedInIIFE || factory) && exportsSource) {
428432
return new ConcatSource(exportsSource, source);
429433
}

test/ConfigTestCases.template.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require("./helpers/warmup-webpack");
44

55
/** @typedef {Record<string, EXPECTED_ANY>} Env */
6-
/** @typedef {{ testPath: string } TestOptions */
6+
/** @typedef {{ testPath: string }} TestOptions */
77

88
const path = require("path");
99
const fs = require("graceful-fs");
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
it("should provide and consume the shared module correctly when output ESM library / 1", async () => {
2+
expect(await import("lib")).toEqual(
3+
expect.objectContaining({
4+
default: "foo~bar~index.js"
5+
})
6+
);
7+
8+
expect(await import("lib/esm")).toEqual(
9+
expect.objectContaining({
10+
default: "foo~bar~index.esm.js"
11+
})
12+
);
13+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
it("should provide and consume the shared module correctly when output ESM library / 2", async () => {
2+
expect(await import("lib")).toEqual(
3+
expect.objectContaining({
4+
default: "foo~bar~index.js"
5+
})
6+
);
7+
8+
expect(await import("lib/esm")).toEqual(
9+
expect.objectContaining({
10+
default: "foo~bar~index.esm.js"
11+
})
12+
);
13+
});

test/configCases/sharing/output-esm-library/node_modules/lib/common.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/configCases/sharing/output-esm-library/node_modules/lib/index.esm.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/configCases/sharing/output-esm-library/node_modules/lib/index.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)