Skip to content

Commit 07c80d6

Browse files
committed
fix(cjs): build a CommonJS re-export of an ES module inside a cycle
Since #22276 the re-export declares a nested `__esModule`, so its export owns a nested exports info that redirects to the required module's. In a cycle, merging it through the redirect marked the target as providing `__esModule`, and two such infos made `setHasUseInfo` recurse forever. Merge a spec's names into the nested info it owns, and stop `setHasUseInfo` at a redirect, as `_resetUsedExports` does.
1 parent 05eb6bd commit 07c80d6

14 files changed

Lines changed: 67 additions & 4 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 builds failing when a CommonJS module re-exports an ES module in a cycle.

‎lib/graph/ExportsInfo.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,9 +413,9 @@ class ExportsInfo {
413413
exportInfo.setHasUseInfo();
414414
}
415415
this._sideEffectsOnlyInfo.setHasUseInfo();
416-
if (this._redirectTo !== undefined) {
417-
this._redirectTo.setHasUseInfo();
418-
} else {
416+
// never follow _redirectTo: FlagDependencyUsage initializes the target with
417+
// its own module, and a re-export cycle of redirects would never return
418+
if (this._redirectTo === undefined) {
419419
this._otherExportsInfo.setHasUseInfo();
420420
}
421421
}

‎lib/optimize/FlagDependencyExportsPlugin.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,9 @@ class FlagDependencyExportsPlugin {
357357
inlined = exportNameOrSpec.inlined;
358358
}
359359
}
360-
const exportInfo = exportsInfo.getExportInfo(name);
360+
// A nested exports info may already redirect to the target's;
361+
// following it would mark the name provided by the target module.
362+
const exportInfo = exportsInfo.getOwnExportInfo(name);
361363

362364
if (
363365
exportInfo.provided === false ||
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * as cjs from "./binding-reexport.cjs";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const esm = require("./binding-namespace");
2+
3+
exports.esm = esm;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as bindingNamespace from "./binding-namespace";
2+
import * as namespace from "./namespace";
3+
4+
it("re-exports a CommonJS module that re-exports the namespace back", () => {
5+
expect(Object.keys(namespace)).toEqual(["cjs"]);
6+
expect(namespace.cjs.esm).toBe(namespace);
7+
expect(namespace.cjs.esm.__esModule).toBe(true);
8+
});
9+
10+
it("re-exports a CommonJS module that re-exports the namespace back through a binding", () => {
11+
expect(Object.keys(bindingNamespace)).toEqual(["cjs"]);
12+
expect(bindingNamespace.cjs.esm).toBe(bindingNamespace);
13+
expect(bindingNamespace.cjs.esm.__esModule).toBe(true);
14+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * as cjs from "./reexport.cjs";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.esm = require("./namespace");
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
mode: "production",
6+
optimization: {
7+
minimize: false
8+
}
9+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { readReexport, value } from "./module";
2+
3+
// Exports are analyzed in post-order from `index.js`, the first module by name,
4+
// which puts the CommonJS re-export before the ES module it re-exports.
5+
it("concatenates an ES module that a CommonJS module in its cycle re-exports", () => {
6+
expect(value).toBe(1);
7+
expect(readReexport()).toBe("1:true");
8+
});

0 commit comments

Comments
 (0)