Skip to content

Commit a4b0878

Browse files
authored
fix(cache): recheck a lazy barrel's warnings when its deferred requests change (#22724)
* fix(cache): recheck a lazy barrel's warnings when its deferred requests change A re-export leaving a lazy barrel reports without any rebuild, while the report memo only renews for rebuilt modules, so watch lost its "export not found" warning. Record the deferred requests a module was found clean with, and check it again when they differ. * test: cover every deferred shape, a named request and exportsPresence error
1 parent 4d15e69 commit a4b0878

23 files changed

Lines changed: 169 additions & 19 deletions

‎.changeset/050-lazy-barrel-deferred-hash.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-
Rebuild a lazy barrel in watch when `sideEffects` changes what it defers.
5+
Rebuild and recheck a lazy barrel in watch when what it defers changes.

‎lib/Compilation.js‎

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,9 @@ const unsafeCacheData = new WeakMap();
633633
const dependencyReportCacheTokens = new WeakMap();
634634

635635
/**
636-
* Modules known to have no dependency errors/warnings, per cache token.
637-
* @type {WeakMap<DependencyReportCacheToken, WeakSet<Module>>}
636+
* Modules known to have no dependency errors/warnings, per cache token, with
637+
* the deferred requests they were checked with (`true` for none).
638+
* @type {WeakMap<DependencyReportCacheToken, WeakMap<Module, string | true>>}
638639
*/
639640
const modulesWithoutProblems = new WeakMap();
640641

@@ -2969,6 +2970,18 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
29692970
});
29702971
}
29712972

2973+
/**
2974+
* Returns the requests a lazy barrel still defers, which neither render nor
2975+
* report what they would once requested, though no connection changes.
2976+
* @private
2977+
* @param {Module} module the module
2978+
* @returns {string | undefined} the deferred requests as one key, if any
2979+
*/
2980+
_getLazyRequests(module) {
2981+
const lazyRequests = this._lazyBarrels.getLazyRequests(module);
2982+
return lazyRequests && JSON.stringify([...lazyRequests]);
2983+
}
2984+
29722985
/**
29732986
* Compute affected modules.
29742987
* @private
@@ -3028,23 +3041,12 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
30283041
return true;
30293042
};
30303043

3031-
/**
3032-
* Returns the requests a lazy barrel still defers, which render nothing
3033-
* where a missing target renders a throw, though neither has a connection.
3034-
* @param {Module} module the module
3035-
* @returns {string | undefined} the deferred requests as one key, if any
3036-
*/
3037-
const getLazyRequests = (module) => {
3038-
const lazyRequests = this._lazyBarrels.getLazyRequests(module);
3039-
return lazyRequests && JSON.stringify([...lazyRequests]);
3040-
};
3041-
30423044
const modulesWithoutCache = new Set(modules);
30433045
for (const [module, cachedMemCache] of moduleMemCacheCache) {
30443046
if (modulesWithoutCache.has(module)) {
30453047
const buildInfo = module.buildInfo;
30463048
if (buildInfo) {
3047-
const lazyRequests = getLazyRequests(module);
3049+
const lazyRequests = this._getLazyRequests(module);
30483050
if (cachedMemCache.buildInfo !== buildInfo) {
30493051
// use a new one
30503052
/** @type {MemCache} */
@@ -3093,7 +3095,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
30933095
moduleMemCacheCache.set(module, {
30943096
buildInfo,
30953097
references: computeReferences(module),
3096-
lazyRequests: getLazyRequests(module),
3098+
lazyRequests: this._getLazyRequests(module),
30973099
memCache
30983100
});
30993101
moduleMemCaches.set(module, memCache);
@@ -3632,15 +3634,20 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
36323634
continue;
36333635
}
36343636
let token;
3637+
/** @type {string | true} */
3638+
let clean = true;
36353639
if (useReportCacheTokens) {
3640+
// a request leaving a lazy barrel reports without any rebuild
3641+
const lazyRequests = this._getLazyRequests(module);
3642+
if (lazyRequests !== undefined) clean = lazyRequests;
36363643
token = dependencyReportCacheTokens.get(module);
36373644
const cleanModules = modulesWithoutProblems.get(
36383645
/** @type {DependencyReportCacheToken} */ (token)
36393646
);
36403647
if (
36413648
!unresolved &&
36423649
cleanModules !== undefined &&
3643-
cleanModules.has(module)
3650+
cleanModules.get(module) === clean
36443651
) {
36453652
continue;
36463653
}
@@ -3677,10 +3684,10 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
36773684
if (cleanModules === undefined) {
36783685
modulesWithoutProblems.set(
36793686
/** @type {DependencyReportCacheToken} */ (token),
3680-
(cleanModules = new WeakSet())
3687+
(cleanModules = new WeakMap())
36813688
);
36823689
}
3683-
cleanModules.add(module);
3690+
cleanModules.set(module, clean);
36843691
}
36853692
}
36863693
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { a } from "./pkg/barrel";
2+
import { a as named } from "./pkg/named";
3+
import { a as defaultBarrel } from "./pkg/default";
4+
import { a as imported } from "./pkg/imported";
5+
import { a as direct } from "./pkg/target";
6+
7+
it("should import only names the barrels do not defer", () => {
8+
expect([a, named, defaultBarrel, imported, direct]).toEqual([
9+
"barrel",
10+
"named",
11+
"default",
12+
"other",
13+
"target"
14+
]);
15+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { b as r0 } from "./target";
2+
export const a = "barrel";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { default } from "./target";
2+
export const a = "default";
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { b } from "./target";
2+
3+
export { b };
4+
export { a } from "./other";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { b } from "./target";
2+
export const a = "named";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const a = "other";
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const a = "target";

0 commit comments

Comments
 (0)