Skip to content

Commit 9dc25ca

Browse files
authored
fix: module library export definitions when multiple runtimes (#20179)
1 parent bfa9cc5 commit 9dc25ca

17 files changed

Lines changed: 145 additions & 37 deletions

File tree

lib/Compilation.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3744,6 +3744,7 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
37443744
dependencyTemplates,
37453745
runtimeTemplate,
37463746
runtime,
3747+
runtimes,
37473748
codeGenerationResults: results,
37483749
compilation: this
37493750
});

lib/Module.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ const makeSerializable = require("./util/makeSerializable");
8787
* @property {ModuleGraph} moduleGraph the module graph
8888
* @property {ChunkGraph} chunkGraph the chunk graph
8989
* @property {RuntimeSpec} runtime the runtimes code should be generated for
90+
* @property {RuntimeSpec[]} runtimes all runtimes code should be generated for
9091
* @property {ConcatenationScope=} concatenationScope when in concatenated module, information about other concatenated modules
9192
* @property {CodeGenerationResults | undefined} codeGenerationResults code generation results of other modules (need to have a codeGenerationDependency to use that)
9293
* @property {Compilation=} compilation the compilation
@@ -133,8 +134,8 @@ const makeSerializable = require("./util/makeSerializable");
133134
* @property {boolean=} sideEffectFree
134135
* @property {boolean=} isCSSModule
135136
* @property {Record<string, string>=} jsIncompatibleExports
136-
* @property {Record<string, string>=} exportsFinalName
137-
* @property {string=} factoryExportsBinding
137+
* @property {Map<RuntimeSpec, Record<string, string>>=} exportsFinalNameByRuntime
138+
* @property {Map<RuntimeSpec, string>=} exportsSourceByRuntime
138139
*/
139140

140141
/** @typedef {LazySet<string>} FileSystemDependencies */
@@ -965,6 +966,7 @@ class Module extends DependenciesBlock {
965966
moduleGraph: chunkGraph.moduleGraph,
966967
chunkGraph,
967968
runtime: undefined,
969+
runtimes: [],
968970
codeGenerationResults: undefined
969971
};
970972
const sources = this.codeGeneration(codeGenContext).sources;

lib/Template.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ class Template {
373373
moduleGraph: renderContext.moduleGraph,
374374
runtimeTemplate: renderContext.runtimeTemplate,
375375
runtime: renderContext.chunk.runtime,
376+
runtimes: [renderContext.chunk.runtime],
376377
codeGenerationResults
377378
});
378379
if (!codeGenResult) continue;

lib/library/ModuleLibraryPlugin.js

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
2121
/** @typedef {import("../Chunk")} Chunk */
2222
/** @typedef {import("../Compiler")} Compiler */
2323
/** @typedef {import("../Module")} Module */
24+
/** @typedef {import("../Module").BuildMeta} BuildMeta */
2425
/** @typedef {import("../Module").RuntimeRequirements} RuntimeRequirements */
2526
/** @typedef {import("../javascript/JavascriptModulesPlugin").StartupRenderContext} StartupRenderContext */
2627
/** @typedef {import("../javascript/JavascriptModulesPlugin").ModuleRenderContext} ModuleRenderContext */
@@ -69,7 +70,28 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
6970
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
7071
const { onDemandExportsGeneration } =
7172
ConcatenatedModule.getCompilationHooks(compilation);
72-
onDemandExportsGeneration.tap(PLUGIN_NAME, (_module) => true);
73+
onDemandExportsGeneration.tap(
74+
PLUGIN_NAME,
75+
(module, runtimes, source, finalName) => {
76+
/** @type {BuildMeta} */
77+
const buildMeta = module.buildMeta || (module.buildMeta = {});
78+
79+
const exportsSourceByRuntime =
80+
buildMeta.exportsSourceByRuntime ||
81+
(buildMeta.exportsSourceByRuntime = new Map());
82+
83+
const exportsFinalNameByRuntime =
84+
buildMeta.exportsFinalNameByRuntime ||
85+
(buildMeta.exportsFinalNameByRuntime = new Map());
86+
87+
for (const runtime of runtimes) {
88+
exportsSourceByRuntime.set(runtime, source);
89+
exportsFinalNameByRuntime.set(runtime, finalName);
90+
}
91+
92+
return true;
93+
}
94+
);
7395
});
7496
}
7597

@@ -152,10 +174,16 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
152174
)
153175
]
154176
: moduleGraph.getExportsInfo(module).orderedExports;
177+
178+
const exportsFinalNameByRuntime =
179+
(module.buildMeta &&
180+
module.buildMeta.exportsFinalNameByRuntime &&
181+
module.buildMeta.exportsFinalNameByRuntime.get(chunk.runtime)) ||
182+
{};
183+
155184
const definitions =
156-
inlined && !inlinedInIIFE
157-
? (module.buildMeta && module.buildMeta.exportsFinalName) || {}
158-
: {};
185+
inlined && !inlinedInIIFE ? exportsFinalNameByRuntime : {};
186+
159187
/** @type {string[]} */
160188
const shortHandedExports = [];
161189
/** @type {[string, string][]} */
@@ -274,17 +302,17 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
274302
renderModuleContent(
275303
source,
276304
module,
277-
{ factory, inlinedInIIFE },
305+
{ factory, inlinedInIIFE, chunk },
278306
libraryContext
279307
) {
280-
// Re-add `factoryExportsBinding` to the source
281-
// when the module is rendered as a factory or treated as an inlined (startup) module but wrapped in an IIFE
282-
if (
283-
(inlinedInIIFE || factory) &&
308+
const exportsSource =
284309
module.buildMeta &&
285-
module.buildMeta.factoryExportsBinding
286-
) {
287-
return new ConcatSource(module.buildMeta.factoryExportsBinding, source);
310+
module.buildMeta.exportsSourceByRuntime &&
311+
module.buildMeta.exportsSourceByRuntime.get(chunk.runtime);
312+
313+
// Re-add the module's exports source when rendered in factory or as an inlined startup module wrapped in an IIFE
314+
if ((inlinedInIIFE || factory) && exportsSource) {
315+
return new ConcatSource(exportsSource, source);
288316
}
289317
return source;
290318
}

lib/optimize/ConcatenatedModule.js

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ const getFinalName = (
687687

688688
/**
689689
* @typedef {object} ConcatenateModuleHooks
690-
* @property {SyncBailHook<[ConcatenatedModule], boolean>} onDemandExportsGeneration
690+
* @property {SyncBailHook<[ConcatenatedModule, RuntimeSpec[], string, Record<string,string>], boolean>} onDemandExportsGeneration
691691
* @property {SyncBailHook<[Partial<ConcatenatedModuleInfo>, ConcatenatedModuleInfo], boolean | void>} concatenatedModuleInfo
692692
*/
693693

@@ -737,7 +737,12 @@ class ConcatenatedModule extends Module {
737737
let hooks = compilationHooksMap.get(compilation);
738738
if (hooks === undefined) {
739739
hooks = {
740-
onDemandExportsGeneration: new SyncBailHook(["module"]),
740+
onDemandExportsGeneration: new SyncBailHook([
741+
"module",
742+
"runtimes",
743+
"exportsFinalName",
744+
"exportsSource"
745+
]),
741746
concatenatedModuleInfo: new SyncBailHook([
742747
"updatedInfo",
743748
"concatenatedModuleInfo"
@@ -1257,6 +1262,7 @@ class ConcatenatedModule extends Module {
12571262
moduleGraph,
12581263
chunkGraph,
12591264
runtime: generationRuntime,
1265+
runtimes,
12601266
codeGenerationResults
12611267
}) {
12621268
const { concatenatedModuleInfo } = ConcatenatedModule.getCompilationHooks(
@@ -1292,6 +1298,7 @@ class ConcatenatedModule extends Module {
12921298
moduleGraph,
12931299
chunkGraph,
12941300
runtime,
1301+
runtimes,
12951302
/** @type {CodeGenerationResults} */
12961303
(codeGenerationResults),
12971304
allUsedNames
@@ -1775,9 +1782,6 @@ class ConcatenatedModule extends Module {
17751782
);
17761783
}
17771784

1778-
const { onDemandExportsGeneration } =
1779-
ConcatenatedModule.getCompilationHooks(this.compilation);
1780-
17811785
runtimeRequirements.add(RuntimeGlobals.exports);
17821786
runtimeRequirements.add(RuntimeGlobals.definePropertyGetters);
17831787

@@ -1791,21 +1795,24 @@ class ConcatenatedModule extends Module {
17911795
);
17921796
}
17931797

1794-
if (onDemandExportsGeneration.call(this)) {
1795-
/** @type {BuildMeta} */ (this.buildMeta).factoryExportsBinding =
1796-
"\n// EXPORTS\n" +
1797-
`${RuntimeGlobals.definePropertyGetters}(${
1798-
this.exportsArgument
1799-
}, {${definitions.join(",")}\n});\n`;
1800-
/** @type {BuildMeta} */ (this.buildMeta).exportsFinalName =
1801-
exportsFinalName;
1802-
} else {
1803-
result.add("\n// EXPORTS\n");
1804-
result.add(
1805-
`${RuntimeGlobals.definePropertyGetters}(${
1806-
this.exportsArgument
1807-
}, {${definitions.join(",")}\n});\n`
1808-
);
1798+
const exportsSource =
1799+
"\n// EXPORTS\n" +
1800+
`${RuntimeGlobals.definePropertyGetters}(${this.exportsArgument}, {${definitions.join(
1801+
","
1802+
)}\n});\n`;
1803+
1804+
const { onDemandExportsGeneration } =
1805+
ConcatenatedModule.getCompilationHooks(this.compilation);
1806+
1807+
if (
1808+
!onDemandExportsGeneration.call(
1809+
this,
1810+
runtimes,
1811+
exportsSource,
1812+
exportsFinalName
1813+
)
1814+
) {
1815+
result.add(exportsSource);
18091816
}
18101817
}
18111818

@@ -2027,6 +2034,7 @@ ${defineGetters}`
20272034
* @param {ModuleGraph} moduleGraph moduleGraph
20282035
* @param {ChunkGraph} chunkGraph chunkGraph
20292036
* @param {RuntimeSpec} runtime runtime
2037+
* @param {RuntimeSpec[]} runtimes runtimes
20302038
* @param {CodeGenerationResults} codeGenerationResults codeGenerationResults
20312039
* @param {Set<string>} usedNames used names
20322040
*/
@@ -2038,6 +2046,7 @@ ${defineGetters}`
20382046
moduleGraph,
20392047
chunkGraph,
20402048
runtime,
2049+
runtimes,
20412050
codeGenerationResults,
20422051
usedNames
20432052
) {
@@ -2058,6 +2067,7 @@ ${defineGetters}`
20582067
moduleGraph,
20592068
chunkGraph,
20602069
runtime,
2070+
runtimes,
20612071
concatenationScope,
20622072
codeGenerationResults,
20632073
sourceTypes: JAVASCRIPT_TYPES
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./baz"
2+
3+
export const bar = "bar"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const baz = "baz"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("./concat");
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import concat2 from "./concat2";
2+
3+
export const baz = "baz";
4+
export const foo = "foo";
5+
export const bar = "bar";
6+
export const concat = "concat~" + concat2;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "concat2";

0 commit comments

Comments
 (0)