Skip to content

Commit e04f9eb

Browse files
committed
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.
1 parent ca25018 commit e04f9eb

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 side-effect-free barrel in watch when `sideEffects` changes whether its re-export of a missing module is deferred, instead of keeping or dropping the missing-module throw.

‎lib/Compilation.js‎

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

3032+
/**
3033+
* Returns the requests a lazy barrel still defers, which render nothing
3034+
* where a missing target renders a throw, though neither has a connection.
3035+
* @param {Module} module the module
3036+
* @returns {string | undefined} the deferred requests as one key, if any
3037+
*/
3038+
const getLazyRequests = (module) => {
3039+
const lazyRequests = this._lazyBarrels.getLazyRequests(module);
3040+
return lazyRequests && JSON.stringify([...lazyRequests]);
3041+
};
3042+
30323043
const modulesWithoutCache = new Set(modules);
30333044
for (const [module, cachedMemCache] of moduleMemCacheCache) {
30343045
if (modulesWithoutCache.has(module)) {
30353046
const buildInfo = module.buildInfo;
30363047
if (buildInfo) {
3048+
const lazyRequests = getLazyRequests(module);
30373049
if (cachedMemCache.buildInfo !== buildInfo) {
30383050
// use a new one
30393051
/** @type {MemCache} */
@@ -3042,15 +3054,20 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
30423054
affectedModules.add(module);
30433055
cachedMemCache.buildInfo = buildInfo;
30443056
cachedMemCache.references = computeReferences(module);
3057+
cachedMemCache.lazyRequests = lazyRequests;
30453058
cachedMemCache.memCache = memCache;
30463059
statChanged++;
3047-
} else if (!compareReferences(module, cachedMemCache.references)) {
3060+
} else if (
3061+
!compareReferences(module, cachedMemCache.references) ||
3062+
cachedMemCache.lazyRequests !== lazyRequests
3063+
) {
30483064
// use a new one
30493065
/** @type {MemCache} */
30503066
const memCache = new WeakTupleMap();
30513067
moduleMemCaches.set(module, memCache);
30523068
affectedModules.add(module);
30533069
cachedMemCache.references = computeReferences(module);
3070+
cachedMemCache.lazyRequests = lazyRequests;
30543071
cachedMemCache.memCache = memCache;
30553072
statReferencesChanged++;
30563073
} else {
@@ -3077,6 +3094,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
30773094
moduleMemCacheCache.set(module, {
30783095
buildInfo,
30793096
references: computeReferences(module),
3097+
lazyRequests: getLazyRequests(module),
30803098
memCache
30813099
});
30823100
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)