Skip to content

Commit 9c9c1b9

Browse files
fix(library): keep one default export when merging a CommonJS entry
A CommonJS entry module adds `export default __webpack_exports__` to a `module` library so it stays importable, and that decision read the last entry module alone. With `entryExports: "all"` an earlier ES module can still export a real `default`, so the bundle carried both and no engine would load it. The synthetic one is now emitted only when every merged module is CommonJS — the same condition that already decided whether to drop `default` from the export clause, and unchanged for a single entry.
1 parent 9988277 commit 9c9c1b9

7 files changed

Lines changed: 74 additions & 8 deletions

File tree

‎.changeset/008-library-entry-exports.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-
Add `library.entryExports` to expose the exports of every entry module.
5+
Add `library.entryExports: "all"` to expose the exports of every entry module.

‎lib/library/ModuleLibraryPlugin.js‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -588,12 +588,9 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
588588
// A merged entry is awaited in the bootstrap, so the object here is settled.
589589
const isAsync = !mergedModules && moduleGraph.isAsync(module);
590590

591-
const treatAsCommonJs =
592-
module.buildMeta &&
593-
/** @type {JavascriptModuleBuildMeta} */ (module.buildMeta)
594-
.treatAsCommonJs;
595-
// One ES module among the merged ones still provides a real `default`.
596-
const skipRenderDefaultExport = exportedModules.every((m) =>
591+
// One ES module among the merged ones still provides a real `default`, and
592+
// the synthetic one below would then be a second export of that name.
593+
const treatAsCommonJs = exportedModules.every((m) =>
597594
Boolean(
598595
m.buildMeta &&
599596
/** @type {JavascriptModuleBuildMeta} */ (m.buildMeta).treatAsCommonJs
@@ -625,7 +622,7 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
625622

626623
const originalName = exportInfo.name;
627624
// Skip rendering the default export in some cases
628-
if (skipRenderDefaultExport && originalName === "default") continue;
625+
if (treatAsCommonJs && originalName === "default") continue;
629626

630627
// Try to find all exports from the reexported modules
631628
const target = exportInfo.getTarget(moduleGraph);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const fromA = "a";
2+
export default "default-a";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
fromB: "b"
3+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
fromC: "c"
3+
};
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.noTests = true;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"use strict";
2+
3+
/** @import { Compilation, Compiler } from "../../../../" */
4+
5+
// A CommonJS entry module adds `export default __webpack_exports__` so that the
6+
// library stays importable, which only holds while no merged module exports a
7+
// real `default` — two exports of that name are not a module any engine loads.
8+
/**
9+
* @param {boolean} synthetic whether the synthetic default export is expected
10+
* @returns {(this: Compiler) => void} plugin
11+
*/
12+
const expectOneDefaultExport = (synthetic) =>
13+
function expectOneDefaultExportPlugin() {
14+
/**
15+
* @param {Compilation} compilation compilation
16+
*/
17+
const handler = (compilation) => {
18+
compilation.hooks.afterProcessAssets.tap("testcase", (assets) => {
19+
const source = assets["main.mjs"].source().toString();
20+
const [, names = ""] = /export \{([^}]*)\};/.exec(source) || [];
21+
expect(names).toContain(" as fromB");
22+
if (synthetic) {
23+
expect(source).toContain("export default __webpack_exports__;");
24+
expect(names).not.toContain(" as default");
25+
expect(names).toContain(" as fromC");
26+
} else {
27+
expect(source).not.toContain("export default ");
28+
expect(names.match(/ as default\b/g)).toHaveLength(1);
29+
expect(names).toContain(" as fromA");
30+
}
31+
});
32+
};
33+
this.hooks.compilation.tap("testcase", handler);
34+
};
35+
36+
/**
37+
* @param {string[]} entry entry modules, the last one CommonJS
38+
* @param {boolean} synthetic whether the synthetic default export is expected
39+
* @returns {import("../../../../").Configuration} config
40+
*/
41+
const config = (entry, synthetic) => ({
42+
entry: { main: entry },
43+
target: "node14",
44+
output: {
45+
filename: "[name].mjs",
46+
module: true,
47+
library: { entryExports: "all", type: "module" }
48+
},
49+
experiments: { outputModule: true },
50+
optimization: { concatenateModules: false },
51+
plugins: [expectOneDefaultExport(synthetic)]
52+
});
53+
54+
/** @type {import("../../../../").Configuration[]} */
55+
module.exports = [
56+
config(["./a.js", "./b.js"], false),
57+
config(["./c.js", "./b.js"], true)
58+
];

0 commit comments

Comments
 (0)