Skip to content

Commit b6ea6e5

Browse files
authored
fix(cache): hash whether a lazy barrel defers an import (#22699)
* fix(cache): hash whether a lazy barrel defers an import A deferred re-export and a missing one both have no connection, so toggling `sideEffects` kept the barrel's hash and watch reused code with or without the missing-module throw. Hash whether an import is deferred, and under `cacheUnaffected` treat a module whose deferred requests changed as affected. * chore: use the changeset filename and sentence format
1 parent 7691697 commit b6ea6e5

18 files changed

Lines changed: 125 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+
Rebuild a lazy barrel in watch when `sideEffects` changes what it defers.

‎lib/Compilation.js‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3028,11 +3028,23 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
30283028
return true;
30293029
};
30303030

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+
30313042
const modulesWithoutCache = new Set(modules);
30323043
for (const [module, cachedMemCache] of moduleMemCacheCache) {
30333044
if (modulesWithoutCache.has(module)) {
30343045
const buildInfo = module.buildInfo;
30353046
if (buildInfo) {
3047+
const lazyRequests = getLazyRequests(module);
30363048
if (cachedMemCache.buildInfo !== buildInfo) {
30373049
// use a new one
30383050
/** @type {MemCache} */
@@ -3041,15 +3053,20 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
30413053
affectedModules.add(module);
30423054
cachedMemCache.buildInfo = buildInfo;
30433055
cachedMemCache.references = computeReferences(module);
3056+
cachedMemCache.lazyRequests = lazyRequests;
30443057
cachedMemCache.memCache = memCache;
30453058
statChanged++;
3046-
} else if (!compareReferences(module, cachedMemCache.references)) {
3059+
} else if (
3060+
!compareReferences(module, cachedMemCache.references) ||
3061+
cachedMemCache.lazyRequests !== lazyRequests
3062+
) {
30473063
// use a new one
30483064
/** @type {MemCache} */
30493065
const memCache = new WeakTupleMap();
30503066
moduleMemCaches.set(module, memCache);
30513067
affectedModules.add(module);
30523068
cachedMemCache.references = computeReferences(module);
3069+
cachedMemCache.lazyRequests = lazyRequests;
30533070
cachedMemCache.memCache = memCache;
30543071
statReferencesChanged++;
30553072
} else {
@@ -3076,6 +3093,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
30763093
moduleMemCacheCache.set(module, {
30773094
buildInfo,
30783095
references: computeReferences(module),
3096+
lazyRequests: getLazyRequests(module),
30793097
memCache
30803098
});
30813099
moduleMemCaches.set(module, memCache);

‎lib/Compiler.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ const getConcurrentCompilationError = memoize(() =>
140140

141141
/** @typedef {WeakMap<Dependency, Module>} WeakReferences */
142142
/** @typedef {import("./util/WeakTupleMap")<EXPECTED_ANY[], EXPECTED_ANY>} MemCache */
143-
/** @typedef {{ buildInfo: BuildInfo, references: WeakReferences | undefined, memCache: MemCache }} ModuleMemCachesItem */
143+
/** @typedef {{ buildInfo: BuildInfo, references: WeakReferences | undefined, lazyRequests: string | undefined, memCache: MemCache }} ModuleMemCachesItem */
144144

145145
/**
146146
* Checks whether this object is sorted.

‎lib/dependencies/esm/ESMImportDependency.js‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const ModuleDependency = require("../core/ModuleDependency");
2323
* JavascriptParserOptions
2424
* } from "../../../declarations/WebpackOptions"
2525
*/
26-
/** @import { ReferencedExports, DependencyKinds, LazyRole } from "../../graph/Dependency" */
26+
/** @import { ReferencedExports, DependencyKinds, LazyRole, UpdateHashContext } from "../../graph/Dependency" */
2727
/** @import { DependencyTemplateContext } from "../../template/DependencyTemplate" */
2828
/** @import ExportsInfo from "../../graph/ExportsInfo" */
2929
/** @import Module, { BuildMeta } from "../../module/Module" */
@@ -37,6 +37,7 @@ const ModuleDependency = require("../core/ModuleDependency");
3737
* ObjectSerializerContext
3838
* } from "../../serialization/ObjectMiddleware"
3939
*/
40+
/** @import Hash from "../../util/Hash" */
4041
/** @import { RuntimeSpec } from "../../util/runtime" */
4142

4243
/** @typedef {0 | 1 | 2 | 3} ExportPresenceMode */
@@ -146,6 +147,20 @@ class ESMImportDependency extends ModuleDependency {
146147
return "esm";
147148
}
148149

150+
/**
151+
* Updates the hash with the data contributed by this instance.
152+
* @param {Hash} hash hash to be updated
153+
* @param {UpdateHashContext} context context
154+
* @returns {void}
155+
*/
156+
updateHash(hash, context) {
157+
// neither a deferred nor a missing target has a connection, yet only a
158+
// missing one renders a throw
159+
if (LazyBarrels.isLazy(context.chunkGraph.moduleGraph, this)) {
160+
hash.update("lazy");
161+
}
162+
}
163+
149164
/**
150165
* Returns how this dependency may be deferred when its parent module is side-effect-free (lazy barrel optimization).
151166
* @returns {LazyRole | null} lazy classification, null when it must be processed eagerly
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict";
2+
3+
const errors = [[/Module not found/], [/Module not found/]];
4+
5+
module.exports = [...errors, ...errors];
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const deferred = WATCH_STEP === "1" || WATCH_STEP === "2";
2+
3+
for (const [name, load] of [
4+
["export *", () => require("./load-star")],
5+
["export { b } from", () => require("./load-named")]
6+
]) {
7+
it(`should ${deferred ? "defer" : "render"} the missing ${name} as package.json changes`, () => {
8+
if (deferred) {
9+
expect(load().default).toBe("a");
10+
} else {
11+
expect(load).toThrow(/Cannot find module '\.\/missing'/);
12+
}
13+
});
14+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { a as default } from "./pkg/named";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { a as default } from "./pkg/star";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { b } from "./missing";
2+
export const a = "a";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"sideEffects": true
3+
}

0 commit comments

Comments
 (0)