Skip to content

Commit 473b19e

Browse files
feat(performance): three more hints, and group the options before any ship (#21961)
* feat(performance): report loaders that drop the source map A loader that rewrites code and returns no map leaves webpack mapping positions to the loader's output as though it were the file on disk, so every position below it is wrong and nothing says so. Rollup warns about the same thing for plugins; webpack can also name which loaders ran. * fix(asset): keep a bare import emitting when assets are side-effect-free 'futureDefaults' marks asset modules side-effect-free, which lets an import whose binding nothing reads drop its file. A bare 'import "./x.png"' declares no binding to read, so it dropped too — although emitting the file is the only thing such an import can have been written for. The two are told apart at the declaration, by a subclass rather than a flag, so the shape of the far more common bound form is untouched and no instance grows: both classes carry the same twenty fields. Over a 2000-module build the dependency count is unchanged (6400) and retained heap does not rise. * feat(performance): report assets emitted for an import nothing reads An asset whose binding is never read still ships its file, and no check said so: with every hint enabled the build reported nothing. What made it reportable is that a bare import now has a class of its own, so wanting the file on disk is told apart from having forgotten to use it. Reported only when every incoming connection is an unread harmony import, so a url(), a new URL() or an html src keeps the asset quiet. Inlined assets carry no asset type and are left to the inlinedAssets check. * feat(performance): report modules kept only by a side effect Rollup answers "why is this still in my bundle" with the statement that kept it; webpack computed the same thing and buried it in an optimization bailout, which stats show only when asked and the cache does not restore. The statement is now kept on buildInfo, so a cached build reports it too, and written only when the hint is on: with it off no module carries the field and retained heap is unchanged over a 2000-module build where every module has a side effect (65.07-65.09 MB either way). Opting in costs about 60 bytes per such module, 0.18 MB over those 2000. * refactor(performance): group the hint options before any of them ship None of the checks has been released, so the surface is still free to change. Twenty-seven options where there were thirty-three: - 'unusedConfig' replaces 'unusedAliases', 'unusedDefines', 'unusedExternals' and 'unusedRules'. Four switches for one finding — configuration nothing used — where the fix is the same each time. Each check keeps its own message. - 'sourceMaps' replaces 'embeddedSourceMaps' and 'missingSourceMaps'. - 'unusedModules' absorbs 'unusedReexports': the same module-nothing-uses finding, told apart only by what kept it there, so each names its own cause and neither reports what the other will. - 'duplicateModules' absorbs 'entrypointOverlap', which was a strict subset of it once the bug below was fixed. 'duplicateModules' counted chunks per module, so a module concatenated into two entrypoints looked like it was in none and went unreported; 'entrypointOverlap' saw it only because it expanded concatenations. It now expands them too, and names the entrypoints paying for each copy. 'unusedModules' reported every 'import "./polyfill"', which is deliberate and was already ruled out for 'unusedReexports'. It now asks that some importer bound a name, reusing the bare-import class, and skips modules webpack left out — with no chunk there is no runtime, which is the only place 'hasOnlyUnusedExports' reads usage. * fix: declare the SideEffectsFlagPlugin recordStatement parameter * fix: finish the performance option rename in statsCases * fix: mark the new performance options as added in 5.111.0 * refactor: drop the unreachable bare-import evaluation override * fix: keep the unused-modules fixtures on the node 10 baseline
1 parent cde81a3 commit 473b19e

210 files changed

Lines changed: 1824 additions & 550 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": minor
3+
---
4+
5+
Add source-map, unused-asset and unused-module hints; group config checks under `unusedConfig`.

‎declarations/WebpackOptions.d.ts‎

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3472,16 +3472,6 @@ export interface PerformanceOptions {
34723472
* @since 5.110.0
34733473
*/
34743474
dynamicExports?: boolean;
3475-
/**
3476-
* Report a production build whose 'devtool' writes the source map into the JavaScript, so everyone loading the page downloads it (requires 'hints' to be enabled).
3477-
* @since 5.110.0
3478-
*/
3479-
embeddedSourceMaps?: boolean;
3480-
/**
3481-
* Report modules shipped by more than one entrypoint, which every page that loads them downloads again (requires 'hints' to be enabled).
3482-
* @since 5.110.0
3483-
*/
3484-
entrypointOverlap?: boolean;
34853475
/**
34863476
* Report modules that call 'eval' directly, which stops minification, scope hoisting and tree shaking (requires 'hints' to be enabled).
34873477
* @since 5.110.0
@@ -3549,6 +3539,11 @@ export interface PerformanceOptions {
35493539
* @since 5.110.0
35503540
*/
35513541
scopeHoistingBailouts?: boolean;
3542+
/**
3543+
* Report source maps that cost more than they give: a production 'devtool' that writes the map into the JavaScript, and modules a loader transformed without returning a map, which leaves positions pointing at the loader's output (requires 'hints' to be enabled).
3544+
* @since 5.111.0
3545+
*/
3546+
sourceMaps?: boolean;
35523547
/**
35533548
* Report splits 'optimization.splitChunks' refused because 'maxInitialRequests' or 'maxAsyncRequests' was already reached (requires 'hints' to be enabled).
35543549
* @since 5.110.0
@@ -3570,30 +3565,20 @@ export interface PerformanceOptions {
35703565
*/
35713566
unsplitVendors?: boolean;
35723567
/**
3573-
* Report 'resolve.alias' entries that no request matched.
3574-
* @since 5.110.0
3575-
*/
3576-
unusedAliases?: boolean;
3577-
/**
3578-
* Report keys defined by 'DefinePlugin' that no module ever referenced, which cost a parser hook per module and invalidate the build when their value changes.
3579-
* @since 5.110.0
3580-
*/
3581-
unusedDefines?: boolean;
3582-
/**
3583-
* Report requests listed in 'externals' that no module ever imported, which usually means the request is misspelled and the real one got bundled instead.
3584-
* @since 5.110.0
3568+
* Report asset files emitted for an import whose binding nothing reads, so the bytes ship for nothing (requires 'hints' to be enabled).
3569+
* @since 5.111.0
35853570
*/
3586-
unusedExternals?: boolean;
3571+
unusedAssets?: boolean;
35873572
/**
3588-
* Report modules bundled although nothing uses what they export, pulled in by a re-export (requires 'hints' to be enabled).
3589-
* @since 5.110.0
3573+
* Report configuration that no build used: 'resolve.alias' entries nothing matched, 'DefinePlugin' keys nothing referenced, 'externals' nothing imported, and 'module.rules' that never matched (requires 'hints' to be enabled).
3574+
* @since 5.111.0
35903575
*/
3591-
unusedReexports?: boolean;
3576+
unusedConfig?: boolean;
35923577
/**
3593-
* Report rules in 'module.rules' that never matched a module, which cost condition evaluation on every build. Note that plugins may add rules too, so a reported rule is not necessarily one you wrote.
3594-
* @since 5.110.0
3578+
* Report modules bundled although nothing uses what they export, naming the re-export or the side-effect statement that kept each one (requires 'hints' to be enabled).
3579+
* @since 5.111.0
35953580
*/
3596-
unusedRules?: boolean;
3581+
unusedModules?: boolean;
35973582
}
35983583
/**
35993584
* Options affecting how file system snapshots are created and validated.

‎lib/DefinePlugin.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ const PLUGIN_NAME = "DefinePlugin";
375375
const VALUE_DEP_PREFIX = `webpack/${PLUGIN_NAME} `;
376376
const VALUE_DEP_MAIN = `webpack/${PLUGIN_NAME}_hash`;
377377

378-
// Keys webpack injects itself are not the user's to remove, so `performance.unusedDefines`
378+
// Keys webpack injects itself are not the user's to remove, so `performance.unusedConfig`
379379
// never sees them.
380380
/** @type {WeakSet<DefinePlugin>} */
381381
const INTERNAL_PLUGINS = new WeakSet();
@@ -721,11 +721,11 @@ class DefinePlugin {
721721
(compilation, { normalModuleFactory }) => {
722722
const definitions = this.definitions;
723723
const { performance } = compilation.options;
724-
// Only `performance.unusedDefines` reads these, so a build without it
724+
// Only `performance.unusedConfig` reads these, so a build without it
725725
// collects nothing.
726726
const collectDeclared =
727727
performance !== false &&
728-
performance.unusedDefines === true &&
728+
performance.unusedConfig === true &&
729729
!INTERNAL_PLUGINS.has(this);
730730
/** @type {Set<string> | undefined} */
731731
let declaredKeys;
@@ -1331,7 +1331,7 @@ class DefinePlugin {
13311331
DefinePlugin.getCompilationHooks = createHooksRegistry(createCompilationHooks);
13321332

13331333
/**
1334-
* Marks an instance as webpack's own, so `performance.unusedDefines` never
1334+
* Marks an instance as webpack's own, so `performance.unusedConfig` never
13351335
* reports the keys it declares against the user.
13361336
* @param {DefinePlugin} plugin the instance webpack constructed itself
13371337
* @returns {DefinePlugin} the same instance

‎lib/Module.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ const makeSerializable = require("./util/makeSerializable");
202202
* @property {boolean=} usesEval true when the module calls `eval` directly
203203
* @property {number=} ineffectivePureAnnotations how many `#__PURE__` annotations sit where the parser does not read them
204204
* @property {number=} topLevelThis how many times the module reads `this` at its top level
205+
* @property {string=} sideEffectStatement the statement whose side effect keeps the module in the bundle, as `type at location`
205206
* @property {boolean=} usesTopLevelAwaitForOf module uses top-level `for await…of` or `await using`, which can't be lowered to a generator
206207
*/
207208

‎lib/WebpackOptionsApply.js‎

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,12 @@ class WebpackOptionsApply extends OptionsApply {
713713
const SideEffectsFlagPlugin = require("./optimize/SideEffectsFlagPlugin");
714714

715715
new SideEffectsFlagPlugin(
716-
options.optimization.sideEffects === true
716+
options.optimization.sideEffects === true,
717+
Boolean(
718+
options.performance &&
719+
options.performance.hints &&
720+
options.performance.unusedModules
721+
)
717722
).apply(compiler);
718723
}
719724
if (options.optimization.providedExports) {
@@ -909,7 +914,7 @@ class WebpackOptionsApply extends OptionsApply {
909914
? publicPath
910915
: "/";
911916

912-
// Marked internal: these keys are webpack's, so `performance.unusedDefines`
917+
// Marked internal: these keys are webpack's, so `performance.unusedConfig`
913918
// must not report them against the user.
914919
DefinePlugin.markInternal(
915920
new DefinePlugin({
@@ -969,12 +974,6 @@ class WebpackOptionsApply extends OptionsApply {
969974
new RedundantDynamicImportsPlugin(options.performance).apply(compiler);
970975
}
971976

972-
if (options.performance.entrypointOverlap) {
973-
const EntrypointOverlapPlugin = require("./performance/EntrypointOverlapPlugin");
974-
975-
new EntrypointOverlapPlugin(options.performance).apply(compiler);
976-
}
977-
978977
if (options.performance.largeModules) {
979978
const LargeModulesPlugin = require("./performance/LargeModulesPlugin");
980979

@@ -1047,12 +1046,6 @@ class WebpackOptionsApply extends OptionsApply {
10471046
new DynamicExportsPlugin(options.performance).apply(compiler);
10481047
}
10491048

1050-
if (options.performance.embeddedSourceMaps) {
1051-
const EmbeddedSourceMapsPlugin = require("./performance/EmbeddedSourceMapsPlugin");
1052-
1053-
new EmbeddedSourceMapsPlugin(options.performance).apply(compiler);
1054-
}
1055-
10561049
if (options.performance.inlinedAssets) {
10571050
const InlinedAssetsPlugin = require("./performance/InlinedAssetsPlugin");
10581051

@@ -1071,13 +1064,43 @@ class WebpackOptionsApply extends OptionsApply {
10711064
new ScopeHoistingBailoutsPlugin(options.performance).apply(compiler);
10721065
}
10731066

1074-
if (options.performance.unusedReexports) {
1067+
// One switch for source maps: a devtool that ships the map to everyone,
1068+
// and a loader that loses it, are both about the map being worth its cost.
1069+
if (options.performance.sourceMaps) {
1070+
const EmbeddedSourceMapsPlugin = require("./performance/EmbeddedSourceMapsPlugin");
1071+
const MissingSourceMapsPlugin = require("./performance/MissingSourceMapsPlugin");
1072+
1073+
new EmbeddedSourceMapsPlugin(options.performance).apply(compiler);
1074+
new MissingSourceMapsPlugin(options.performance).apply(compiler);
1075+
}
1076+
1077+
// One switch for configuration that did nothing: the four read different
1078+
// parts of the config, but the finding and the fix are the same.
1079+
if (options.performance.unusedConfig) {
1080+
const UnusedAliasesPlugin = require("./performance/UnusedAliasesPlugin");
1081+
const UnusedDefinesPlugin = require("./performance/UnusedDefinesPlugin");
1082+
const UnusedExternalsPlugin = require("./performance/UnusedExternalsPlugin");
1083+
// Reports even where size hints are off: a dead rule is a configuration
1084+
// mistake either way.
1085+
const UnusedRuleWarningPlugin = require("./performance/UnusedRuleWarningPlugin");
1086+
1087+
new UnusedAliasesPlugin(options.performance).apply(compiler);
1088+
new UnusedDefinesPlugin(options.performance).apply(compiler);
1089+
new UnusedExternalsPlugin(options.performance).apply(compiler);
1090+
new UnusedRuleWarningPlugin(options.performance).apply(compiler);
1091+
}
1092+
1093+
// One switch for a module nothing uses: the two differ only in what kept
1094+
// it there, and each names the cause it knows about.
1095+
if (options.performance.unusedModules) {
1096+
const UnusedModulesPlugin = require("./performance/UnusedModulesPlugin");
10751097
const UnusedReexportsPlugin = require("./performance/UnusedReexportsPlugin");
10761098

1099+
new UnusedModulesPlugin(options.performance).apply(compiler);
10771100
new UnusedReexportsPlugin(options.performance).apply(compiler);
10781101
}
10791102

1080-
// Deliberately not gated on `hints`, like `unusedRules` below: a rule
1103+
// Deliberately not gated on `hints`, like `unusedConfig` below: a rule
10811104
// that only matches on one OS is a configuration mistake, not a size.
10821105
if (options.performance.osDependentRules) {
10831106
const OsDependentRuleWarningPlugin = require("./performance/OsDependentRuleWarningPlugin");
@@ -1087,43 +1110,23 @@ class WebpackOptionsApply extends OptionsApply {
10871110

10881111
// Deliberately not gated on `hints`: an alias nothing matches is a
10891112
// configuration mistake, not a size.
1090-
if (options.performance.unusedAliases) {
1091-
const UnusedAliasesPlugin = require("./performance/UnusedAliasesPlugin");
1092-
1093-
new UnusedAliasesPlugin(options.performance).apply(compiler);
1094-
}
1095-
10961113
// Deliberately not gated on `hints`: a key nothing reads is a
10971114
// configuration mistake, not a size.
1098-
if (options.performance.unusedDefines) {
1099-
const UnusedDefinesPlugin = require("./performance/UnusedDefinesPlugin");
1115+
if (options.performance.unusedAssets) {
1116+
const UnusedAssetsPlugin = require("./performance/UnusedAssetsPlugin");
11001117

1101-
new UnusedDefinesPlugin(options.performance).apply(compiler);
1118+
new UnusedAssetsPlugin(options.performance).apply(compiler);
11021119
}
11031120

11041121
// Deliberately not gated on `hints`: a misspelled external gets bundled
11051122
// instead, which no size limit would explain.
1106-
if (options.performance.unusedExternals) {
1107-
const UnusedExternalsPlugin = require("./performance/UnusedExternalsPlugin");
1108-
1109-
new UnusedExternalsPlugin(options.performance).apply(compiler);
1110-
}
1111-
11121123
// Deliberately not gated on `hints`: asking for both directives on one
11131124
// import is a configuration mistake, not a size.
11141125
if (options.performance.conflictingResourceHints) {
11151126
const ConflictingResourceHintsPlugin = require("./performance/ConflictingResourceHintsPlugin");
11161127

11171128
new ConflictingResourceHintsPlugin(options.performance).apply(compiler);
11181129
}
1119-
1120-
// Deliberately not gated on `hints`: a dead rule is a configuration
1121-
// mistake worth reporting even where size hints are off.
1122-
if (options.performance.unusedRules) {
1123-
const UnusedRuleWarningPlugin = require("./performance/UnusedRuleWarningPlugin");
1124-
1125-
new UnusedRuleWarningPlugin(options.performance).apply(compiler);
1126-
}
11271130
}
11281131

11291132
new TemplatedPathPlugin().apply(compiler);

‎lib/config/defaults.js‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2403,8 +2403,6 @@ const applyPerformanceDefaults = (performance, { production }) => {
24032403
D(performance, "duplicateModules", all);
24042404
D(performance, "duplicatePackages", all);
24052405
D(performance, "dynamicExports", all);
2406-
D(performance, "entrypointOverlap", all);
2407-
D(performance, "embeddedSourceMaps", all);
24082406
D(performance, "evalUsage", all);
24092407
D(performance, "hotspots", all);
24102408
D(performance, "legacyJavascript", all);
@@ -2417,14 +2415,13 @@ const applyPerformanceDefaults = (performance, { production }) => {
24172415
D(performance, "redundantDynamicImports", all);
24182416
D(performance, "scopeHoistingBailouts", all);
24192417
D(performance, "splitChunksCapped", all);
2418+
D(performance, "sourceMaps", all);
24202419
D(performance, "tinyChunks", all);
24212420
D(performance, "topLevelThis", all);
24222421
D(performance, "unsplitVendors", all);
2423-
D(performance, "unusedAliases", all);
2424-
D(performance, "unusedDefines", all);
2425-
D(performance, "unusedExternals", all);
2426-
D(performance, "unusedReexports", all);
2427-
D(performance, "unusedRules", all);
2422+
D(performance, "unusedAssets", all);
2423+
D(performance, "unusedConfig", all);
2424+
D(performance, "unusedModules", all);
24282425
F(performance, "hints", () => (production ? "warning" : false));
24292426
};
24302427

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Alexander Akait @alexander-akait
4+
*/
5+
6+
"use strict";
7+
8+
const { ASSET_TYPE } = require("../ModuleSourceTypeConstants");
9+
const makeSerializable = require("../util/makeSerializable");
10+
const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDependency");
11+
12+
/** @import Module from "../Module" */
13+
/** @import ModuleGraph from "../ModuleGraph" */
14+
/** @import { GetConditionFn } from "../Dependency" */
15+
16+
/**
17+
* Whether a module is emitted as a file of its own, asked of the module so a
18+
* plugin emitting the same way is treated alike. `buildInfo.filename` cannot
19+
* serve here: generation sets it long after a connection has to know whether
20+
* it is active.
21+
* @param {Module} module the module to look up
22+
* @returns {boolean} true when it emits a file
23+
*/
24+
const emitsOwnFile = (module) => module.getSourceTypes().has(ASSET_TYPE);
25+
26+
/**
27+
* An `import "..."` that declares no binding. It is a separate class rather
28+
* than a flag so it costs nothing per instance, and the bound form — much the
29+
* more common one — keeps the base class and its inline caches untouched.
30+
*/
31+
class HarmonyImportBareSideEffectDependency extends HarmonyImportSideEffectDependency {
32+
/**
33+
* Returns function to determine if the connection is active.
34+
* @param {ModuleGraph} moduleGraph module graph
35+
* @returns {null | false | GetConditionFn} function to determine if the connection is active
36+
*/
37+
getCondition(moduleGraph) {
38+
return (connection) => {
39+
const refModule = connection.resolvedModule;
40+
41+
if (!refModule) return true;
42+
// Nothing reads the module, so emitting its file is the only thing the
43+
// import can have been written for.
44+
if (emitsOwnFile(refModule)) return true;
45+
46+
return refModule.getSideEffectsConnectionState(moduleGraph);
47+
};
48+
}
49+
}
50+
51+
makeSerializable(
52+
HarmonyImportBareSideEffectDependency,
53+
"webpack/lib/dependencies/HarmonyImportBareSideEffectDependency"
54+
);
55+
56+
HarmonyImportBareSideEffectDependency.Template =
57+
HarmonyImportSideEffectDependency.Template;
58+
59+
module.exports = HarmonyImportBareSideEffectDependency;

‎lib/dependencies/HarmonyImportDependencyParserPlugin.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const HarmonyAcceptDependency = require("./HarmonyAcceptDependency");
1818
const HarmonyAcceptImportDependency = require("./HarmonyAcceptImportDependency");
1919
const HarmonyEvaluatedImportSpecifierDependency = require("./HarmonyEvaluatedImportSpecifierDependency");
2020
const HarmonyExports = require("./HarmonyExports");
21+
const HarmonyImportBareSideEffectDependency = require("./HarmonyImportBareSideEffectDependency");
2122
const {
2223
ExportPresenceModes,
2324
getNonOptionalPart
@@ -311,7 +312,13 @@ class HarmonyImportDependencyParserPlugin {
311312
parser.state.current.addError(error);
312313
}
313314

314-
const sideEffectDep = new HarmonyImportSideEffectDependency(
315+
// A declaration binding nothing was written for what importing produces,
316+
// which for a module emitting its own file is that file.
317+
const SideEffectDependency =
318+
statement.specifiers.length === 0
319+
? HarmonyImportBareSideEffectDependency
320+
: HarmonyImportSideEffectDependency;
321+
const sideEffectDep = new SideEffectDependency(
315322
/** @type {string} */ (source),
316323
parser.state.lastHarmonyImportOrder,
317324
phase,

‎lib/dependencies/HarmonyModulesPlugin.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const HarmonyExportExpressionDependency = require("./HarmonyExportExpressionDepe
2020
const HarmonyExportHeaderDependency = require("./HarmonyExportHeaderDependency");
2121
const HarmonyExportImportedSpecifierDependency = require("./HarmonyExportImportedSpecifierDependency");
2222
const HarmonyExportSpecifierDependency = require("./HarmonyExportSpecifierDependency");
23+
const HarmonyImportBareSideEffectDependency = require("./HarmonyImportBareSideEffectDependency");
2324
const HarmonyImportDependencyParserPlugin = require("./HarmonyImportDependencyParserPlugin");
2425
const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDependency");
2526
const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDependency");
@@ -81,6 +82,15 @@ class HarmonyModulesPlugin {
8182
new HarmonyImportSideEffectDependency.Template()
8283
);
8384

85+
compilation.dependencyFactories.set(
86+
HarmonyImportBareSideEffectDependency,
87+
normalModuleFactory
88+
);
89+
compilation.dependencyTemplates.set(
90+
HarmonyImportBareSideEffectDependency,
91+
new HarmonyImportBareSideEffectDependency.Template()
92+
);
93+
8494
compilation.dependencyFactories.set(
8595
HarmonyImportSpecifierDependency,
8696
normalModuleFactory

0 commit comments

Comments
 (0)