Skip to content

Commit b90cb59

Browse files
fix(cjs): flag exports used through a CommonJS namespace re-export (#22791)
* fix(cjs): flag exports used through a CommonJS namespace re-export A namespace re-export's nested exports info redirects to the required module's exports, so marking an export used through it never queued that module, leaving its self-references unused. Fixes #22788 * test(cjs): cover chained and side-effectful namespace re-exports Both failed with "Self-reference dependency has unused export name" before the fix; run the case with and without module concatenation. * test(cjs): drop the concatenation variant of the namespace re-export case CommonJS modules concatenate only with `concatenateModules.commonjs` from an ESM root, so both variants emitted identical bundles.
1 parent c2052d0 commit b90cb59

12 files changed

Lines changed: 117 additions & 3 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 "Self-reference dependency has unused export name" for CommonJS namespace re-exports.

‎lib/optimize/FlagDependencyUsagePlugin.js‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const TupleQueue = require("./TupleQueue");
1616

1717
/** @import Compiler from "../Compiler" */
1818
/** @import DependenciesBlock from "../graph/DependenciesBlock" */
19-
/** @import { ReferencedExport, ReferencedExports } from "../graph/Dependency" */
19+
/** @import { ExportInfoName, ReferencedExport, ReferencedExports } from "../graph/Dependency" */
2020
/** @import ExportsInfo from "../graph/ExportsInfo" */
2121
/** @import Module from "../module/Module" */
2222
/** @import { RuntimeSpec } from "../util/runtime" */
@@ -77,6 +77,25 @@ class FlagDependencyUsagePlugin {
7777
/** @type {Map<ExportsInfo, Module>} */
7878
const exportInfoToModuleMap = new Map();
7979

80+
/**
81+
* Returns the module owning an export of a nested exports info. A
82+
* nested exports info redirecting to a module's exports owns only
83+
* its own names, the rest belong to the redirect target.
84+
* @param {ExportsInfo} exportsInfo nested exports info
85+
* @param {ExportInfoName} name export name
86+
* @returns {Module | undefined} the module owning the export
87+
*/
88+
const getExportOwnerModule = (exportsInfo, name) => {
89+
/** @type {ExportsInfo | undefined} */
90+
let current = exportsInfo;
91+
while (current !== undefined) {
92+
const owner = exportInfoToModuleMap.get(current);
93+
if (owner !== undefined) return owner;
94+
if (current._exports.has(name)) return;
95+
current = current._redirectTo;
96+
}
97+
};
98+
8099
/** @type {TupleQueue<[Module], RuntimeSpec>} */
81100
const queue = new TupleQueue();
82101

@@ -179,7 +198,10 @@ class FlagDependencyUsagePlugin {
179198
const currentModule =
180199
currentExportsInfo === exportsInfo
181200
? module
182-
: exportInfoToModuleMap.get(currentExportsInfo);
201+
: getExportOwnerModule(
202+
currentExportsInfo,
203+
usedExport[i]
204+
);
183205
if (currentModule) {
184206
queue.enqueue(currentModule, runtime);
185207
}
@@ -198,7 +220,10 @@ class FlagDependencyUsagePlugin {
198220
const currentModule =
199221
currentExportsInfo === exportsInfo
200222
? module
201-
: exportInfoToModuleMap.get(currentExportsInfo);
223+
: getExportOwnerModule(
224+
currentExportsInfo,
225+
usedExport[i]
226+
);
202227
if (currentModule) {
203228
queue.enqueue(currentModule, runtime);
204229
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.Signature = void 0;
4+
exports.Signature = require("./signature.cjs");
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.Inner = void 0;
4+
exports.Inner = require("./inner.cjs");
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.magic = void 0;
4+
exports.wrap = wrap;
5+
const version = require("../version.cjs");
6+
exports.magic = "deep";
7+
function wrap(value) {
8+
return `${value}:${exports.magic}:${version.version}`;
9+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.Signature = void 0;
4+
exports.Signature = require("./signature.cjs");
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.magic = void 0;
4+
exports.wrap = wrap;
5+
const version = require("../version.cjs");
6+
exports.magic = "effectful";
7+
function wrap(value) {
8+
return `${value}:${exports.magic}:${version.version}`;
9+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const lib = require("./lib.cjs");
2+
3+
it("marks the self-references of a module used through a namespace re-export", () => {
4+
expect(lib.Signature.wrap("sig")).toBe("sig:magic:1");
5+
});
6+
7+
it("keeps the re-export's own __esModule marker", () => {
8+
expect(lib.Signature.__esModule).toBe(true);
9+
});
10+
11+
it("follows a namespace re-export of a namespace re-export", () => {
12+
expect(require("./deep/outer.cjs").Inner.Signature.wrap("sig")).toBe(
13+
"sig:deep:1"
14+
);
15+
});
16+
17+
it("marks the self-references of a module with side effects", () => {
18+
expect(require("./effectful/lib.cjs").Signature.wrap("sig")).toBe(
19+
"sig:effectful:1"
20+
);
21+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.Signature = void 0;
4+
exports.Signature = require("./signature.cjs");
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.magic = void 0;
4+
exports.wrap = wrap;
5+
const version = require("./version.cjs");
6+
exports.magic = "magic";
7+
function wrap(value) {
8+
return `${value}:${exports.magic}:${version.version}`;
9+
}

0 commit comments

Comments
 (0)