Skip to content

Commit 373c595

Browse files
authored
fix: use buildInfo.diagnostics and persist parse-time bailouts (#22055)
* fix: keep parse-time optimization bailouts across caches * fix: move diagnostics to buildInfo.diagnostics and persist parse-time bailouts
1 parent eb82194 commit 373c595

24 files changed

Lines changed: 260 additions & 103 deletions
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+
Move diagnostics to `buildInfo.diagnostics`, persisting parse-time bailouts.

‎lib/Compilation.js‎

Lines changed: 9 additions & 2 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,
@@ -1826,6 +1827,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
18261827
if (currentProfile !== undefined) {
18271828
currentProfile.markBuildingEnd();
18281829
}
1830+
replayOptimizationBailouts(this.moduleGraph, module);
18291831
this.hooks.stillValidModule.call(module);
18301832
return callback();
18311833
}
@@ -1846,6 +1848,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
18461848
this.hooks.failedModule.call(module, err);
18471849
return callback(err);
18481850
}
1851+
replayOptimizationBailouts(this.moduleGraph, module);
18491852
if (currentProfile !== undefined) {
18501853
currentProfile.markStoringStart();
18511854
}
@@ -6377,8 +6380,12 @@ This prevents using hashes of each other and should be avoided.`);
63776380
const buildInfo = /** @type {BuildInfo} */ (module.buildInfo);
63786381
if (buildInfo.cacheable === false) {
63796382
cacheable = false;
6380-
if (buildInfo.notCacheableReasons) {
6381-
for (const reason of buildInfo.notCacheableReasons) {
6383+
const moduleDiagnostics = buildInfo.diagnostics;
6384+
if (
6385+
moduleDiagnostics !== undefined &&
6386+
moduleDiagnostics.notCacheableReasons
6387+
) {
6388+
for (const reason of moduleDiagnostics.notCacheableReasons) {
63826389
if (!notCacheableReasons.includes(reason)) {
63836390
notCacheableReasons.push(reason);
63846391
}

‎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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {
1111
JAVASCRIPT_MODULE_TYPE_ESM
1212
} = require("./ModuleTypeConstants");
1313
const { getInnerGraphUtils } = require("./optimize/InnerGraph");
14+
const { recordOptimizationBailout } = require("./util/buildDiagnostics");
1415

1516
/** @import Compiler from "./Compiler" */
1617
/** @import { BuildInfo } from "./Module" */
@@ -73,9 +74,10 @@ class JavascriptMetaInfoPlugin {
7374
const wasEnabled = innerGraph.isEnabled(parser.state);
7475
innerGraph.bailout(parser.state);
7576
if (wasEnabled) {
76-
compilation.moduleGraph
77-
.getOptimizationBailout(parser.state.module)
78-
.push("Inner graph bailout: eval()");
77+
recordOptimizationBailout(
78+
buildInfo,
79+
"Inner graph bailout: eval()"
80+
);
7981
}
8082
}
8183
});

‎lib/Module.js‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,25 +186,32 @@ const makeSerializable = require("./util/makeSerializable");
186186
* @property {Map<string, string>=} exportsBindingSourceByRuntime export definitions `ModuleLibraryPlugin` took over, re-emitted only where the module is wrapped
187187
*/
188188

189+
/**
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)
198+
*/
199+
189200
/**
190201
* Defines the build info properties common to all module types.
191202
* Module type specific properties live in the `Known*BuildInfo` typedef of the dedicated module class.
192203
* @typedef {object} KnownBuildInfo
193204
* @property {boolean=} specNamespaceObject the module hands out a spec Module Namespace Exotic Object
194205
* @property {boolean=} cacheable
195-
* @property {string[]=} notCacheableReasons reasons why the module is not cacheable (e.g. paths of loaders that marked it)
196206
* @property {boolean=} strict
197207
* @property {string=} moduleArgument
198208
* @property {string=} exportsArgument
199209
* @property {Record<string, Source>=} assets assets added by loaders or plugins
200210
* @property {Map<string, AssetInfo | undefined>=} assetsInfo
201211
* @property {Set<string>=} topLevelDeclarations top level declaration names
202212
* @property {boolean=} isCircular true when the module is part of a circular dependency chain
203-
* @property {boolean=} usesEval true when the module calls `eval` directly
204-
* @property {number=} ineffectivePureAnnotations how many `#__PURE__` annotations sit where the parser does not read them
205-
* @property {number=} topLevelThis how many times the module reads `this` at its top level
206-
* @property {string=} sideEffectStatement the statement whose side effect keeps the module in the bundle, as `type at location`
207213
* @property {boolean=} usesTopLevelAwaitForOf module uses top-level `for await…of` or `await using`, which can't be lowered to a generator
214+
* @property {BuildDiagnostics=} diagnostics what the build recorded for stats and the performance hints to report
208215
*/
209216

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

‎lib/NormalModule.js‎

Lines changed: 4 additions & 2 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,6 +1652,7 @@ class NormalModule extends Module {
16511652
inlineExports: undefined,
16521653
moduleConcatenationBailout: undefined,
16531654
moduleConcatenationRootBailout: undefined,
1655+
diagnostics: undefined,
16541656
needCreateRequire: undefined,
16551657
usesTopLevelUsingDeclaration: undefined
16561658
};

‎lib/WebpackOptionsApply.js‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -693,12 +693,7 @@ class WebpackOptionsApply extends OptionsApply {
693693
const SideEffectsFlagPlugin = require("./optimize/SideEffectsFlagPlugin");
694694

695695
new SideEffectsFlagPlugin(
696-
options.optimization.sideEffects === true,
697-
Boolean(
698-
options.performance &&
699-
options.performance.hints &&
700-
options.performance.unusedModules
701-
)
696+
options.optimization.sideEffects === true
702697
).apply(compiler);
703698
}
704699
if (options.optimization.providedExports) {

‎lib/dependencies/AMDDefineDependencyParserPlugin.js‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"use strict";
77

88
const RuntimeGlobals = require("../RuntimeGlobals");
9+
const { recordOptimizationBailout } = require("../util/buildDiagnostics");
910
const AMDDefineDependency = require("./AMDDefineDependency");
1011
const AMDRequireArrayDependency = require("./AMDRequireArrayDependency");
1112
const AMDRequireContextDependency = require("./AMDRequireContextDependency");
@@ -35,6 +36,7 @@ const { addLocalModule, getLocalModule } = require("./LocalModulesHelpers");
3536
* } from "../../declarations/WebpackOptions"
3637
*/
3738
/** @import Dependency from "../Dependency" */
39+
/** @import { BuildInfo } from "../Module" */
3840
/** @import BasicEvaluatedExpression from "../javascript/BasicEvaluatedExpression" */
3941
/**
4042
* @import JavascriptParser, {
@@ -353,9 +355,10 @@ class AMDDefineDependencyParserPlugin {
353355
return;
354356
}
355357
DynamicExports.bailout(parser.state);
356-
parser.state.compilation.moduleGraph
357-
.getOptimizationBailout(parser.state.module)
358-
.push("AMD bailout: define() prevents static exports analysis");
358+
recordOptimizationBailout(
359+
/** @type {BuildInfo} */ (parser.state.module.buildInfo),
360+
"AMD bailout: define() prevents static exports analysis"
361+
);
359362
/** @type {Identifier[] | null} */
360363
let fnParams = null;
361364
let fnParamsOffset = 0;

‎lib/dependencies/CommonJsExportsParserPlugin.js‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
const RuntimeGlobals = require("../RuntimeGlobals");
99
const { evaluateToString } = require("../javascript/JavascriptParserHelpers");
10+
const { recordOptimizationBailout } = require("../util/buildDiagnostics");
1011
const formatLocation = require("../util/formatLocation");
1112
const { propertyAccess } = require("../util/property");
1213
const CommonJsExportRequireDependency = require("./CommonJsExportRequireDependency");
@@ -38,7 +39,7 @@ const FUNCTION_PROPERTY_DROP_HEAD =
3839
* } from "estree"
3940
*/
4041
/** @import { ExportInfoName } from "../Dependency" */
41-
/** @import ModuleGraph from "../ModuleGraph" */
42+
/** @import { BuildInfo } from "../Module" */
4243
/** @import BasicEvaluatedExpression from "../javascript/BasicEvaluatedExpression" */
4344
/**
4445
* @import JavascriptParser, {
@@ -357,15 +358,6 @@ const getObjectExportKind = (property) => {
357358
const PLUGIN_NAME = "CommonJsExportsParserPlugin";
358359

359360
class CommonJsExportsParserPlugin {
360-
/**
361-
* Creates an instance of CommonJsExportsParserPlugin.
362-
* @param {ModuleGraph} moduleGraph module graph
363-
*/
364-
constructor(moduleGraph) {
365-
/** @type {ModuleGraph} */
366-
this.moduleGraph = moduleGraph;
367-
}
368-
369361
/**
370362
* Applies the plugin by registering its hooks on the compiler.
371363
* @param {JavascriptParser} parser the parser
@@ -406,9 +398,10 @@ class CommonJsExportsParserPlugin {
406398
* @param {string} reason reason
407399
*/
408400
const bailoutHint = (reason) => {
409-
this.moduleGraph
410-
.getOptimizationBailout(parser.state.module)
411-
.push(`CommonJS bailout: ${reason}`);
401+
recordOptimizationBailout(
402+
/** @type {BuildInfo} */ (parser.state.module.buildInfo),
403+
`CommonJS bailout: ${reason}`
404+
);
412405
};
413406
/** @type {unknown} */
414407
let keptAllExportsForState;

‎lib/dependencies/CommonJsPlugin.js‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,7 @@ class CommonJsPlugin {
254254
);
255255

256256
new CommonJsImportsParserPlugin(parserOptions).apply(parser);
257-
new CommonJsExportsParserPlugin(compilation.moduleGraph).apply(
258-
parser
259-
);
257+
new CommonJsExportsParserPlugin().apply(parser);
260258
};
261259

262260
normalModuleFactory.hooks.parser

0 commit comments

Comments
 (0)