Skip to content

Commit eb214cd

Browse files
committed
fix: expose exports from all entry modules in library output
When using array entry with output.library, only the last entry's exports were accessible on the library object. Loop over all entry deps in AbstractLibraryPlugin and share the same __webpack_exports__ object across all inlined entry modules so every export is merged into the library. Fixes webpack#15936
1 parent 925ba2d commit eb214cd

8 files changed

Lines changed: 64 additions & 11 deletions

File tree

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 library output only exposing exports from the last entry module when multiple entry files are specified. All entry modules now contribute their exports to the library.

‎lib/javascript/JavascriptModulesPlugin.js‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,9 @@ class JavascriptModulesPlugin {
10191019
}
10201020
const lastInlinedModule = /** @type {Module} */ (last(inlinedModules));
10211021
const startupSource = new ConcatSource();
1022+
const mergeLibraryEntryExports = Boolean(
1023+
compilation.outputOptions.library && inlinedModules.size > 1
1024+
);
10221025

10231026
const avoidEntryIife = compilation.options.optimization.avoidEntryIife;
10241027
/** @type {Map<Module, Source> | false} */
@@ -1099,7 +1102,16 @@ class JavascriptModulesPlugin {
10991102
}
11001103
if (exports) {
11011104
if (m !== lastInlinedModule) {
1102-
startupSource.add(`var ${m.exportsArgument} = {};\n`);
1105+
if (
1106+
mergeLibraryEntryExports &&
1107+
m.exportsArgument !== RuntimeGlobals.exports
1108+
) {
1109+
startupSource.add(
1110+
`var ${m.exportsArgument} = ${RuntimeGlobals.exports};\n`
1111+
);
1112+
} else if (!mergeLibraryEntryExports) {
1113+
startupSource.add(`var ${m.exportsArgument} = {};\n`);
1114+
}
11031115
} else if (m.exportsArgument !== RuntimeGlobals.exports) {
11041116
startupSource.add(
11051117
`var ${m.exportsArgument} = ${RuntimeGlobals.exports};\n`
@@ -1392,6 +1404,10 @@ class JavascriptModulesPlugin {
13921404
}
13931405
}
13941406
let i = jsEntries.length;
1407+
const mergeLibraryEntryExports = Boolean(
1408+
runtimeTemplate.compilation.outputOptions.library &&
1409+
jsEntries.length > 1
1410+
);
13951411
for (const [entryModule, entrypoint] of jsEntries) {
13961412
const chunks =
13971413
/** @type {Entrypoint} */
@@ -1509,7 +1525,10 @@ class JavascriptModulesPlugin {
15091525
requireScopeUsed ||
15101526
entryRuntimeRequirements.has(RuntimeGlobals.exports)
15111527
) {
1512-
const exportsArg = i === 0 ? RuntimeGlobals.exports : "{}";
1528+
const exportsArg =
1529+
i === 0 || mergeLibraryEntryExports
1530+
? RuntimeGlobals.exports
1531+
: "{}";
15131532
args.push("0", exportsArg);
15141533
if (requireScopeUsed) {
15151534
args.push(RuntimeGlobals.require);

‎lib/library/AbstractLibraryPlugin.js‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,16 @@ class AbstractLibraryPlugin {
8080
: compilation.outputOptions.library
8181
);
8282
if (options !== false) {
83-
const dep = deps[deps.length - 1];
84-
if (dep) {
85-
const module = compilation.moduleGraph.getModule(dep);
86-
if (module) {
87-
this.finishEntryModule(module, name, {
88-
options,
89-
compilation,
90-
chunkGraph: compilation.chunkGraph
91-
});
83+
for (const dep of deps) {
84+
if (dep) {
85+
const module = compilation.moduleGraph.getModule(dep);
86+
if (module) {
87+
this.finishEntryModule(module, name, {
88+
options,
89+
compilation,
90+
chunkGraph: compilation.chunkGraph
91+
});
92+
}
9293
}
9394
}
9495
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const a = 1;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const b = 2;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const c = 3;
2+
3+
it("should export from all entry modules", function () {
4+
expect(MultiEntryLib.a).toBe(1);
5+
expect(MultiEntryLib.b).toBe(2);
6+
expect(MultiEntryLib.c).toBe(3);
7+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
3+
module.exports = {
4+
afterExecute() {
5+
delete global.MultiEntryLib;
6+
}
7+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
entry: ["./a.js", "./b.js", "./c.js"],
6+
output: {
7+
library: {
8+
name: "MultiEntryLib",
9+
type: "assign"
10+
}
11+
}
12+
};

0 commit comments

Comments
 (0)