Skip to content

Commit 89812e4

Browse files
committed
fix: move diagnostics to buildInfo.diagnostics and persist parse-time bailouts
1 parent b441ab1 commit 89812e4

20 files changed

Lines changed: 181 additions & 123 deletions

‎.changeset/021-cache-optimization-bailouts.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-
Persist parse-time optimization bailouts in `buildInfo.optimizationBailout`.
5+
Move diagnostics to `buildInfo.diagnostics`, persisting parse-time bailouts.

‎lib/Compilation.js‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const LazySet = require("./util/LazySet");
5353
const { getOrInsert } = require("./util/MapHelpers");
5454
const WeakTupleMap = require("./util/WeakTupleMap");
5555
const asyncLib = require("./util/async");
56+
const { replayOptimizationBailouts } = require("./util/buildDiagnostics");
5657
const { cachedCleverMerge } = require("./util/cleverMerge");
5758
const {
5859
compareErrors,
@@ -68,9 +69,6 @@ const {
6869
soonFrozenObjectDeprecation
6970
} = require("./util/deprecation");
7071
const memoize = require("./util/memoize");
71-
const {
72-
addRecordedOptimizationBailouts
73-
} = require("./util/optimizationBailout");
7472
const processAsyncTree = require("./util/processAsyncTree");
7573
const { getRuntimeKey } = require("./util/runtime");
7674
const { isSourceEqual } = require("./util/source");
@@ -1829,7 +1827,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
18291827
if (currentProfile !== undefined) {
18301828
currentProfile.markBuildingEnd();
18311829
}
1832-
addRecordedOptimizationBailouts(this.moduleGraph, module);
1830+
replayOptimizationBailouts(this.moduleGraph, module);
18331831
this.hooks.stillValidModule.call(module);
18341832
return callback();
18351833
}
@@ -1850,7 +1848,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
18501848
this.hooks.failedModule.call(module, err);
18511849
return callback(err);
18521850
}
1853-
addRecordedOptimizationBailouts(this.moduleGraph, module);
1851+
replayOptimizationBailouts(this.moduleGraph, module);
18541852
if (currentProfile !== undefined) {
18551853
currentProfile.markStoringStart();
18561854
}
@@ -6383,8 +6381,12 @@ This prevents using hashes of each other and should be avoided.`);
63836381
const buildInfo = /** @type {BuildInfo} */ (module.buildInfo);
63846382
if (buildInfo.cacheable === false) {
63856383
cacheable = false;
6386-
if (buildInfo.notCacheableReasons) {
6387-
for (const reason of buildInfo.notCacheableReasons) {
6384+
const moduleDiagnostics = buildInfo.diagnostics;
6385+
if (
6386+
moduleDiagnostics !== undefined &&
6387+
moduleDiagnostics.notCacheableReasons
6388+
) {
6389+
for (const reason of moduleDiagnostics.notCacheableReasons) {
63886390
if (!notCacheableReasons.includes(reason)) {
63896391
notCacheableReasons.push(reason);
63906392
}

‎lib/DefinePlugin.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const {
2020
evaluateToString,
2121
toConstantDependency
2222
} = require("./javascript/JavascriptParserHelpers");
23+
const { getBuildDiagnostics } = require("./util/buildDiagnostics");
2324
const createHash = require("./util/createHash");
2425
const createHooksRegistry = require("./util/createHooksRegistry");
2526

@@ -92,8 +93,10 @@ class RuntimeValue {
9293
const buildInfo = /** @type {BuildInfo} */ (parser.state.module.buildInfo);
9394
if (this.options === true) {
9495
buildInfo.cacheable = false;
96+
const diagnostics = getBuildDiagnostics(buildInfo);
9597
const reasons =
96-
buildInfo.notCacheableReasons || (buildInfo.notCacheableReasons = []);
98+
diagnostics.notCacheableReasons ||
99+
(diagnostics.notCacheableReasons = []);
97100
const reason = `DefinePlugin runtime value ${key}`;
98101
if (!reasons.includes(reason)) reasons.push(reason);
99102
} else {

‎lib/JavascriptMetaInfoPlugin.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const {
1111
JAVASCRIPT_MODULE_TYPE_ESM
1212
} = require("./ModuleTypeConstants");
1313
const { getInnerGraphUtils } = require("./optimize/InnerGraph");
14-
const { recordOptimizationBailout } = require("./util/optimizationBailout");
14+
const { recordOptimizationBailout } = require("./util/buildDiagnostics");
1515

1616
/** @import Compiler from "./Compiler" */
1717
/** @import { BuildInfo } from "./Module" */

‎lib/Module.js‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,30 +187,30 @@ const makeSerializable = require("./util/makeSerializable");
187187
*/
188188

189189
/**
190-
* What a build recorded for the module graph to report as optimization bailouts, kept on the build info so a module restored from the cache still has it.
191-
* @typedef {object} OptimizationBailoutRecord
192-
* @property {string[]} reasons the bailouts as reported by the module graph
193-
* @property {string=} sideEffect the statement whose side effect keeps the module in the bundle, as `type at location`
190+
* What a build recorded for stats and the performance hints to report, kept on the build info so a module restored from the cache still has it.
191+
* @typedef {object} BuildDiagnostics
192+
* @property {string[]=} optimizationBailouts what parser plugins reported as optimization bailouts, replayed into the module graph of every compilation that reuses the module
193+
* @property {string=} sideEffectStatement the statement whose side effect keeps the module in the bundle, as `type at location`
194+
* @property {number=} ineffectivePureAnnotations how many `#__PURE__` annotations sit where the parser does not read them
195+
* @property {number=} topLevelThis how many times the module reads `this` at its top level
196+
* @property {boolean=} usesEval true when the module calls `eval` directly
197+
* @property {string[]=} notCacheableReasons reasons why the module is not cacheable (e.g. paths of loaders that marked it)
194198
*/
195199

196200
/**
197201
* Defines the build info properties common to all module types.
198202
* Module type specific properties live in the `Known*BuildInfo` typedef of the dedicated module class.
199203
* @typedef {object} KnownBuildInfo
200204
* @property {boolean=} cacheable
201-
* @property {string[]=} notCacheableReasons reasons why the module is not cacheable (e.g. paths of loaders that marked it)
202205
* @property {boolean=} strict
203206
* @property {string=} moduleArgument
204207
* @property {string=} exportsArgument
205208
* @property {Record<string, Source>=} assets assets added by loaders or plugins
206209
* @property {Map<string, AssetInfo | undefined>=} assetsInfo
207210
* @property {Set<string>=} topLevelDeclarations top level declaration names
208211
* @property {boolean=} isCircular true when the module is part of a circular dependency chain
209-
* @property {boolean=} usesEval true when the module calls `eval` directly
210-
* @property {number=} ineffectivePureAnnotations how many `#__PURE__` annotations sit where the parser does not read them
211-
* @property {number=} topLevelThis how many times the module reads `this` at its top level
212212
* @property {boolean=} usesTopLevelAwaitForOf module uses top-level `for await…of` or `await using`, which can't be lowered to a generator
213-
* @property {OptimizationBailoutRecord=} optimizationBailout what the build recorded for the module graph to report as optimization bailouts, replayed into every compilation that reuses the module
213+
* @property {BuildDiagnostics=} diagnostics what the build recorded for stats and the performance hints to report
214214
*/
215215

216216
/** @typedef {string | Set<string>} ValueCacheVersion */

‎lib/NormalModule.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const {
3333
const LazySet = require("./util/LazySet");
3434
const { isSubset } = require("./util/SetHelpers");
3535
const { getScheme } = require("./util/URLAbsoluteSpecifier");
36+
const { getBuildDiagnostics } = require("./util/buildDiagnostics");
3637
const {
3738
concatComparators,
3839
keepOriginalOrder,
@@ -1524,7 +1525,8 @@ class NormalModule extends Module {
15241525
}
15251526
buildInfo.cacheable = buildInfo.cacheable && result.cacheable;
15261527
if (result.notCacheableReasons.length > 0) {
1527-
buildInfo.notCacheableReasons = result.notCacheableReasons;
1528+
getBuildDiagnostics(buildInfo).notCacheableReasons =
1529+
result.notCacheableReasons;
15281530
}
15291531
processResult(err, result.result);
15301532
}
@@ -1632,7 +1634,6 @@ class NormalModule extends Module {
16321634
};
16331635
this.buildInfo = {
16341636
cacheable: false,
1635-
notCacheableReasons: undefined,
16361637
parsed: true,
16371638
fileDependencies: undefined,
16381639
contextDependencies: undefined,
@@ -1651,7 +1652,7 @@ class NormalModule extends Module {
16511652
inlineExports: undefined,
16521653
moduleConcatenationBailout: undefined,
16531654
moduleConcatenationRootBailout: undefined,
1654-
optimizationBailout: undefined,
1655+
diagnostics: undefined,
16551656
needCreateRequire: undefined,
16561657
usesTopLevelUsingDeclaration: undefined
16571658
};

‎lib/dependencies/AMDDefineDependencyParserPlugin.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"use strict";
77

88
const RuntimeGlobals = require("../RuntimeGlobals");
9-
const { recordOptimizationBailout } = require("../util/optimizationBailout");
9+
const { recordOptimizationBailout } = require("../util/buildDiagnostics");
1010
const AMDDefineDependency = require("./AMDDefineDependency");
1111
const AMDRequireArrayDependency = require("./AMDRequireArrayDependency");
1212
const AMDRequireContextDependency = require("./AMDRequireContextDependency");

‎lib/dependencies/CommonJsExportsParserPlugin.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
const RuntimeGlobals = require("../RuntimeGlobals");
99
const { evaluateToString } = require("../javascript/JavascriptParserHelpers");
10+
const { recordOptimizationBailout } = require("../util/buildDiagnostics");
1011
const formatLocation = require("../util/formatLocation");
11-
const { recordOptimizationBailout } = require("../util/optimizationBailout");
1212
const { propertyAccess } = require("../util/property");
1313
const CommonJsExportRequireDependency = require("./CommonJsExportRequireDependency");
1414
const CommonJsExportsDependency = require("./CommonJsExportsDependency");

‎lib/optimize/ConcatenatedModule.js‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const {
4040
} = require("../runtime/MakeDeferredNamespaceObjectRuntime");
4141
const { equals } = require("../util/ArrayHelpers");
4242
const LazySet = require("../util/LazySet");
43+
const { getBuildDiagnostics } = require("../util/buildDiagnostics");
4344
const {
4445
compareModulesByFullName,
4546
concatComparators
@@ -1185,15 +1186,17 @@ class ConcatenatedModule extends Module {
11851186

11861187
for (const m of this._modules) {
11871188
// populate cacheable
1188-
const { cacheable, notCacheableReasons } = /** @type {BuildInfo} */ (
1189-
m.buildInfo
1190-
);
1189+
const { cacheable, diagnostics } = /** @type {BuildInfo} */ (m.buildInfo);
11911190
if (!cacheable) {
11921191
this.buildInfo.cacheable = false;
1192+
const notCacheableReasons =
1193+
diagnostics === undefined
1194+
? undefined
1195+
: diagnostics.notCacheableReasons;
11931196
if (notCacheableReasons) {
1197+
const own = getBuildDiagnostics(this.buildInfo);
11941198
const reasons =
1195-
this.buildInfo.notCacheableReasons ||
1196-
(this.buildInfo.notCacheableReasons = []);
1199+
own.notCacheableReasons || (own.notCacheableReasons = []);
11971200
for (const reason of notCacheableReasons) {
11981201
if (!reasons.includes(reason)) reasons.push(reason);
11991202
}

‎lib/optimize/SideEffectsFlagPlugin.js‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ const HarmonyExportImportedSpecifierDependency = require("../dependencies/Harmon
1515
const HarmonyImportDependency = require("../dependencies/HarmonyImportDependency");
1616
const HarmonyImportSpecifierDependency = require("../dependencies/HarmonyImportSpecifierDependency");
1717
const { ImportPhaseUtils } = require("../dependencies/ImportPhase");
18+
const {
19+
getBuildDiagnostics,
20+
recordOptimizationBailout
21+
} = require("../util/buildDiagnostics");
1822
const formatLocation = require("../util/formatLocation");
1923
const { dirname, join, readJson, relative } = require("../util/fs");
2024
const { getGlobToRegExpSource } = require("../util/globUtils");
@@ -24,7 +28,6 @@ const {
2428
relativePathToRequest
2529
} = require("../util/identifier");
2630
const { CompilerHintNotationRegExp } = require("../util/magicComment");
27-
const { recordOptimizationBailout } = require("../util/optimizationBailout");
2831

2932
/**
3033
* @import {
@@ -549,12 +552,16 @@ class SideEffectsFlagPlugin {
549552
const loc = parser.getLocation(sideEffectsStatement);
550553
const at = formatLocation(loc);
551554

552-
const record = recordOptimizationBailout(
553-
/** @type {BuildInfo} */ (parser.state.module.buildInfo),
555+
const buildInfo = /** @type {BuildInfo} */ (
556+
parser.state.module.buildInfo
557+
);
558+
recordOptimizationBailout(
559+
buildInfo,
554560
`Statement (${type}) with side effects in source code at ${at}`
555561
);
556562
// `performance.unusedModules` names the statement, so it is kept typed too.
557-
record.sideEffect = `${type} at ${at}`;
563+
getBuildDiagnostics(buildInfo).sideEffectStatement =
564+
`${type} at ${at}`;
558565
}
559566
});
560567
};

0 commit comments

Comments
 (0)