Skip to content

Commit 1489f42

Browse files
fix(lazy-barrel): don't report unused imports a nested barrel defers (#22930)
* fix(lazy-barrel): don't report unused imports a nested barrel defers An unused named import is still checked for presence, but lazy barrels never request it. When it reaches a nested barrel through a built `export *`, the deferred name looked missing. Follow built star re-exports when collecting deferred names. Fixes #22929 * fix(lazy-barrel): keep reporting a missing default behind a lazy star `export *` never re-exports `default`, so a deferred star re-export, nested or not, must not hide a missing default.
1 parent 9109b11 commit 1489f42

24 files changed

Lines changed: 222 additions & 6 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+
Keep unused imports of names a nested lazy barrel defers from failing to link.

‎lib/dependencies/esm/ESMImportDependency.js‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,39 @@ const getNonOptionalPart = (members, membersOptionals) => {
101101

102102
/**
103103
* Export names a lazy barrel still defers, so they are missing from its provided
104-
* exports without being absent from the module.
104+
* exports without being absent from the module. Follows built star re-exports into nested barrels.
105105
* @param {ModuleGraph} moduleGraph the module graph
106106
* @param {Module} module the imported module
107-
* @returns {Set<string> | true | undefined} deferred names, true when a deferred star re-export may provide any name, undefined when none
107+
* @param {string} id the requested export name
108+
* @param {Set<Module>=} visited modules already searched, against star re-export cycles
109+
* @returns {Set<string> | true | undefined} deferred names, true when a deferred star re-export may provide `id`, undefined when none
108110
*/
109-
const getDeferredExports = (moduleGraph, module) => {
111+
const getDeferredExports = (moduleGraph, module, id, visited) => {
110112
const factoryMeta = module.factoryMeta;
111113
if (factoryMeta === undefined || !factoryMeta.sideEffectFree) return;
112114
/** @type {Set<string> | undefined} */
113115
let names;
114116
for (const dep of module.dependencies) {
115-
if (!LazyBarrels.isLazy(moduleGraph, dep)) continue;
116117
const role = dep.lazyRole;
117-
if (role === Dependency.LAZY_STAR_REEXPORT) return true;
118+
if (!LazyBarrels.isLazy(moduleGraph, dep)) {
119+
if (role !== Dependency.LAZY_STAR_REEXPORT) continue;
120+
const target = moduleGraph.getModule(dep);
121+
if (target === null) continue;
122+
if (visited === undefined) visited = new Set([module]);
123+
if (visited.has(target)) continue;
124+
visited.add(target);
125+
const nested = getDeferredExports(moduleGraph, target, id, visited);
126+
if (nested === true) return true;
127+
if (nested === undefined) continue;
128+
if (names === undefined) names = new Set();
129+
// `export *` never re-exports a default
130+
for (const name of nested) if (name !== "default") names.add(name);
131+
continue;
132+
}
133+
if (role === Dependency.LAZY_STAR_REEXPORT) {
134+
if (id !== "default") return true;
135+
continue;
136+
}
118137
if (role !== Dependency.LAZY_NAMED_REEXPORT) continue;
119138
if (names === undefined) names = new Set();
120139
names.add(/** @type {string} */ (dep.lazyName));
@@ -317,7 +336,11 @@ class ESMImportDependency extends ModuleDependency {
317336
(exportsType !== "default-with-named" || ids[0] !== "default") &&
318337
moduleGraph.isExportProvided(importedModule, ids) === false
319338
) {
320-
const deferredExports = getDeferredExports(moduleGraph, importedModule);
339+
const deferredExports = getDeferredExports(
340+
moduleGraph,
341+
importedModule,
342+
ids[0]
343+
);
321344

322345
if (
323346
deferredExports !== true &&
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigCacheTestCases lazy-barrel nested-star-unused-specifier nested-star-unused-specifier should compile 1`] = `
4+
"./index.js
5+
├─ ./unused.js
6+
│ ├─ ./ui/index.js [barrel]
7+
│ │ └─ (*) ./ui/theme/index.js [barrel]
8+
│ │ ├─ (colors) ./ui/theme/colors.js
9+
│ │ ├─ (fonts) ./fonts [raw] [lazy]
10+
│ │ ├─ (spacing) ./spacing [raw] [lazy]
11+
│ │ ├─ (default) ./default-theme [raw] [lazy]
12+
│ │ └─ (*) ./ui/index.js [barrel] (deduped)
13+
│ ├─ ./kit/index.js [barrel]
14+
│ │ └─ (*) ./kit/theme/index.js [barrel]
15+
│ │ ├─ (tone) ./kit/theme/tone.js
16+
│ │ └─ (*) ./more [raw] [lazy]
17+
│ └─ ./lib/index.js [barrel]
18+
│ ├─ (default) ./foo [raw] [lazy]
19+
│ └─ (*) ./other [raw] [lazy]
20+
├─ (colors, missing) ./ui/index.js [barrel] (deduped)
21+
├─ (tone) ./kit/index.js [barrel] (deduped)
22+
└─ (local) ./lib/index.js [barrel] (deduped)"
23+
`;
24+
25+
exports[`ConfigCacheTestCases lazy-barrel nested-star-unused-specifier nested-star-unused-specifier should compile 2`] = `
26+
"./index.js
27+
├─ ./unused.js
28+
│ ├─ ./ui/index.js [barrel]
29+
│ │ └─ (*) ./ui/theme/index.js [barrel]
30+
│ │ ├─ (colors) ./ui/theme/colors.js
31+
│ │ ├─ (fonts) ./fonts [raw] [lazy]
32+
│ │ ├─ (spacing) ./spacing [raw] [lazy]
33+
│ │ ├─ (default) ./default-theme [raw] [lazy]
34+
│ │ └─ (*) ./ui/index.js [barrel] (deduped)
35+
│ ├─ ./kit/index.js [barrel]
36+
│ │ └─ (*) ./kit/theme/index.js [barrel]
37+
│ │ ├─ (tone) ./kit/theme/tone.js
38+
│ │ └─ (*) ./more [raw] [lazy]
39+
│ └─ ./lib/index.js [barrel]
40+
│ ├─ (default) ./foo [raw] [lazy]
41+
│ └─ (*) ./other [raw] [lazy]
42+
├─ (colors, missing) ./ui/index.js [barrel] (deduped)
43+
├─ (tone) ./kit/index.js [barrel] (deduped)
44+
└─ (local) ./lib/index.js [barrel] (deduped)"
45+
`;
46+
47+
exports[`ConfigCacheTestCases lazy-barrel nested-star-unused-specifier nested-star-unused-specifier should pre-compile to fill disk cache (1st) 1`] = `
48+
"./index.js
49+
├─ ./unused.js
50+
│ ├─ ./ui/index.js [barrel]
51+
│ │ └─ (*) ./ui/theme/index.js [barrel]
52+
│ │ ├─ (colors) ./ui/theme/colors.js
53+
│ │ ├─ (fonts) ./fonts [raw] [lazy]
54+
│ │ ├─ (spacing) ./spacing [raw] [lazy]
55+
│ │ ├─ (default) ./default-theme [raw] [lazy]
56+
│ │ └─ (*) ./ui/index.js [barrel] (deduped)
57+
│ ├─ ./kit/index.js [barrel]
58+
│ │ └─ (*) ./kit/theme/index.js [barrel]
59+
│ │ ├─ (tone) ./kit/theme/tone.js
60+
│ │ └─ (*) ./more [raw] [lazy]
61+
│ └─ ./lib/index.js [barrel]
62+
│ ├─ (default) ./foo [raw] [lazy]
63+
│ └─ (*) ./other [raw] [lazy]
64+
├─ (colors, missing) ./ui/index.js [barrel] (deduped)
65+
├─ (tone) ./kit/index.js [barrel] (deduped)
66+
└─ (local) ./lib/index.js [barrel] (deduped)"
67+
`;
68+
69+
exports[`ConfigCacheTestCases lazy-barrel nested-star-unused-specifier nested-star-unused-specifier should pre-compile to fill disk cache (2nd) 1`] = `
70+
"./index.js
71+
├─ ./unused.js
72+
│ ├─ ./ui/index.js [barrel]
73+
│ │ └─ (*) ./ui/theme/index.js [barrel]
74+
│ │ ├─ (colors) ./ui/theme/colors.js
75+
│ │ ├─ (fonts) ./fonts [raw] [lazy]
76+
│ │ ├─ (spacing) ./spacing [raw] [lazy]
77+
│ │ ├─ (default) ./default-theme [raw] [lazy]
78+
│ │ └─ (*) ./ui/index.js [barrel] (deduped)
79+
│ ├─ ./kit/index.js [barrel]
80+
│ │ └─ (*) ./kit/theme/index.js [barrel]
81+
│ │ ├─ (tone) ./kit/theme/tone.js
82+
│ │ └─ (*) ./more [raw] [lazy]
83+
│ └─ ./lib/index.js [barrel]
84+
│ ├─ (default) ./foo [raw] [lazy]
85+
│ └─ (*) ./other [raw] [lazy]
86+
├─ (colors, missing) ./ui/index.js [barrel] (deduped)
87+
├─ (tone) ./kit/index.js [barrel] (deduped)
88+
└─ (local) ./lib/index.js [barrel] (deduped)"
89+
`;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigTestCases lazy-barrel nested-star-unused-specifier nested-star-unused-specifier should compile 1`] = `
4+
"./index.js
5+
├─ ./unused.js
6+
│ ├─ ./ui/index.js [barrel]
7+
│ │ └─ (*) ./ui/theme/index.js [barrel]
8+
│ │ ├─ (colors) ./ui/theme/colors.js
9+
│ │ ├─ (fonts) ./fonts [raw] [lazy]
10+
│ │ ├─ (spacing) ./spacing [raw] [lazy]
11+
│ │ ├─ (default) ./default-theme [raw] [lazy]
12+
│ │ └─ (*) ./ui/index.js [barrel] (deduped)
13+
│ ├─ ./kit/index.js [barrel]
14+
│ │ └─ (*) ./kit/theme/index.js [barrel]
15+
│ │ ├─ (tone) ./kit/theme/tone.js
16+
│ │ └─ (*) ./more [raw] [lazy]
17+
│ └─ ./lib/index.js [barrel]
18+
│ ├─ (default) ./foo [raw] [lazy]
19+
│ └─ (*) ./other [raw] [lazy]
20+
├─ (colors, missing) ./ui/index.js [barrel] (deduped)
21+
├─ (tone) ./kit/index.js [barrel] (deduped)
22+
└─ (local) ./lib/index.js [barrel] (deduped)"
23+
`;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict";
2+
3+
module.exports = [
4+
[/export 'missing' \(imported as 'missing'\) was not found in '\.\/ui'/],
5+
[/export 'default' \(imported as 'theme'\) was not found in '\.\/ui'/],
6+
[/export 'absent' \(imported as 'absent'\) was not found in '\.\/ui'/],
7+
[/export 'default' \(imported as 'kitDefault'\) was not found in '\.\/kit'/]
8+
];
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import "./unused";
2+
import { colors, missing } from "./ui";
3+
import { tone } from "./kit";
4+
import { local } from "./lib";
5+
6+
it("should build the target of an unused import through a nested barrel (#22929)", () => {
7+
expect(colors).toBe("colors");
8+
expect(missing).toBe(undefined);
9+
expect(tone).toBe("tone");
10+
expect(local).toBe("local");
11+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./theme";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"sideEffects": false
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { tone } from "./tone";
2+
export * from "./more";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const more = "more";

0 commit comments

Comments
 (0)