Skip to content

Commit b19519a

Browse files
authored
fix(cjs): recompute a re-export whose required module was missing (#22631)
`exports.x = require("./m").y` and `module.exports = require("./m")` gave no exports while `./m` was missing, which flagged the module cacheable. Once `./m` came back the module had not changed, so its exports were restored without the re-export and an `export *` of it lost the name. Report them uncacheable, as #22432 did for ESM.
1 parent bf1e489 commit b19519a

21 files changed

Lines changed: 86 additions & 2 deletions

‎lib/dependencies/commonjs/CommonJsExportRequireDependency.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,9 @@ class CommonJsExportRequireDependency extends ModuleDependency {
245245
const ids = this.getIds(moduleGraph);
246246
const name = this.names[0];
247247
const from = moduleGraph.getConnection(this);
248-
if (!from) return;
248+
// a missing target may come back without this module changing, so
249+
// what it exports now must not be cached
250+
if (!from) return { exports: [], dependencies: [] };
249251
const exportChain = esmUnwrap
250252
? [ESM_MODULE_EXPORTS_NAME, ...ids]
251253
: ids.length === 0
@@ -297,7 +299,7 @@ class CommonJsExportRequireDependency extends ModuleDependency {
297299
};
298300
}
299301
const from = moduleGraph.getConnection(this);
300-
if (!from) return;
302+
if (!from) return { exports: [], dependencies: [] };
301303
if (esmUnwrap) {
302304
// In `module.exports = require("./esm")` of a module carrying a
303305
// `"module.exports"` export, this module's own exports become the
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict";
2+
3+
const error = [/Module not found/, /Can't resolve '\.\/later-dep'/];
4+
5+
module.exports = [error, error];
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const missing = WATCH_STEP === "1";
2+
3+
it("should provide a CommonJS re-export again once its target is restored", () => {
4+
if (missing) {
5+
expect(() => require("./name-barrel")).toThrow(/Cannot find module/);
6+
expect(() => require("./whole-barrel")).toThrow(/Cannot find module/);
7+
return;
8+
}
9+
const name = require("./name-barrel");
10+
expect(Object.keys(name).sort()).toEqual(["b", "r"]);
11+
expect(name.r).toBe(2);
12+
const whole = require("./whole-barrel");
13+
expect(Object.keys(whole)).toEqual(["a"]);
14+
expect(whole.a).toBe(2);
15+
});
16+
17+
it("should provide a CommonJS re-export once its target appears", () => {
18+
if (!missing) {
19+
expect(() => require("./later-barrel")).toThrow(/Cannot find module/);
20+
return;
21+
}
22+
const later = require("./later-barrel");
23+
expect(Object.keys(later).sort()).toEqual(["b", "r"]);
24+
expect(later.r).toBe(2);
25+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./later";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exports.r = require("./later-dep").a;
2+
exports.b = 1;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./name";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const a = 2;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exports.r = require("./name-dep").a;
2+
exports.b = 1;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./whole";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const a = 2;

0 commit comments

Comments
 (0)