Skip to content

Commit 9fea501

Browse files
authored
fix: re-export the default of a module from a wrapped concatenation (#22272)
1 parent 063d146 commit 9fea501

13 files changed

Lines changed: 131 additions & 16 deletions

File tree

‎.changeset/038-concatenation-wrap-nested.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-
Allow wrapping an external concatenated module, now asked of the module itself.
5+
Allow wrapping an external concatenated module, now asked of the module itself, and re-exporting a JSON or asset `default` as a namespace object.

‎lib/css/CssModule.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ class CssModule extends NormalModule {
150150
* @returns {boolean} true, when it may be wrapped
151151
*/
152152
canBeWrappedInConcatenation() {
153-
// CssGenerator has no wrapped path: exports are shared-scope `const`s, and
154-
// an `exportType: "style"` push lands after the concatenation flushed them
153+
// CssGenerator puts each export in the shared scope as a const and never
154+
// writes `module.exports`, so a wrapper would hand out an empty object
155155
return false;
156156
}
157157

‎lib/dependencies/HarmonyExportImportedSpecifierDependency.js‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,16 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
14581458
concatenationScope
14591459
).createModuleReference(importedModule, { asiSafe: true })
14601460
: dep.getImportVar(moduleGraph);
1461+
1462+
const defaultImportVar = inScope
1463+
? /** @type {ConcatenationScope} */ (
1464+
concatenationScope
1465+
).createModuleReference(importedModule, {
1466+
ids: ["default"],
1467+
asiSafe: true
1468+
})
1469+
: dep.getImportVar(moduleGraph);
1470+
14611471
// whole-namespace modes need the original export names, not mangled ones
14621472
const namespaceImportVar = inScope
14631473
? /** @type {ConcatenationScope} */ (
@@ -1520,25 +1530,14 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
15201530
break;
15211531

15221532
case "reexport-dynamic-default": {
1523-
// `${importVar}_default` names the interop variable that the import
1524-
// statement declares. An in-scope target has no import statement, so
1525-
// reference its default export and let the concatenation resolve it.
1526-
const defaultAccess = inScope
1527-
? /** @type {ConcatenationScope} */ (
1528-
concatenationScope
1529-
).createModuleReference(importedModule, {
1530-
ids: ["default"],
1531-
asiSafe: true
1532-
})
1533-
: importVar;
15341533
initFragments.push(
15351534
this.getReexportFragment(
15361535
module,
15371536
"reexport default from dynamic",
15381537
moduleGraph
15391538
.getExportsInfo(module)
15401539
.getUsedName(/** @type {string} */ (mode.name), runtime),
1541-
defaultAccess,
1540+
defaultImportVar,
15421541
inScope ? "" : null,
15431542
runtimeRequirements
15441543
)
@@ -1605,7 +1604,7 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
16051604
moduleGraph
16061605
.getExportsInfo(module)
16071606
.getUsedName(/** @type {string} */ (mode.name), runtime),
1608-
importVar,
1607+
defaultImportVar,
16091608
"",
16101609
runtimeRequirements
16111610
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// reached only through a whole-object require(), so this barrel is wrapped
2+
// and every re-export renders as runtime code over the wrapper accessors
3+
export { bump, count, value as esmValue } from "./esm-member.js";
4+
export * as esmNs from "./esm-member.js";
5+
export * from "./esm-star.js";
6+
export * as dynNs from "./dynamic-cjs.js";
7+
export * from "./dynamic-cjs.js";
8+
export { default as jsonDefault } from "./data.json";
9+
export { default as htmlDefault } from "./page.html";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// read by one name, so the star re-export narrows to a runtime-checked getter
2+
export * from "./dynamic-cjs.js";
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
3+
const barrel = require("./barrel.js");
4+
const checkedDynV = require("./checked-barrel.js").dynV;
5+
6+
exports.barrel = barrel;
7+
exports.checkedDynV = checkedDynV;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "a": 1 }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict";
2+
3+
// using `exports` directly bails out of structured exports, so this module
4+
// keeps the "dynamic" exports type
5+
Object.assign(exports, { dynV: "dynamic" });
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export let count = 0;
2+
export const value = "esm";
3+
export function bump() {
4+
count += 1;
5+
return count;
6+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export let starA = "star-a";
2+
export let starB = "star-b";

0 commit comments

Comments
 (0)