Skip to content

Commit a102613

Browse files
authored
fix(performance): read an alias's evidence from the request, not the module (#21786)
* fix(performance): read an alias's evidence from the request, not the module `unusedAliases` looked for the request as written on `NormalModule.rawRequest`, which two applied aliases never reach: one resolving to `false` leaves a `RawModule`, and a context module's elements are resolved relative to the directory the alias named, so `@locales/en.js` survives only on the context dependency. Both were reported as never applied — `alias: { fs: false }` is the common spelling of the first, and the advice is to delete an alias the build needs. The fallback pass reads `dependency.request`, and a context dependency's `options.request`, so a request the graph keeps nowhere else is still counted. It runs only while something is unaccounted for, as before: a build whose aliases all match walks no dependencies at all and is unchanged (0.11ms -> 0.15ms over 7 builds, inside the spread). Where one is dead the walk now costs about a millisecond on 600 modules, which is where a wrong answer would have been printed. * perf(performance): ask each distinct request once while walking dependencies One import states its request on several dependencies, and a shared module is requested once per importer, so the walk asked the same question repeatedly. A set of the requests already put to the aliases skips the repeats. The joined path stays undeduped: it is the issuer's context joined with the request, so the same request answers differently per module. Counted, not timed — the machine's spread swamped the hook either way. On 600 modules that all import one shared util: 2403 calls down to 1803. Where every request is distinct it saves nothing (1802 -> 1801), which is the shape it cannot help.
1 parent 21a7551 commit a102613

7 files changed

Lines changed: 85 additions & 11 deletions

File tree

‎lib/performance/UnusedAliasesPlugin.js‎

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const getSourceModules = require("./getSourceModules");
1818
/** @typedef {import("../Compiler")} Compiler */
1919
/** @typedef {import("../Dependency")} Dependency */
2020
/** @typedef {import("../DependenciesBlock")} DependenciesBlock */
21+
/** @typedef {import("../dependencies/ContextDependency").ContextDependencyOptions} ContextDependencyOptions */
2122
/** @typedef {import("../Module")} Module */
2223

2324
/**
@@ -167,18 +168,20 @@ class UnusedAliasesPlugin {
167168
}
168169
}
169170

170-
// An absolute name matches the path the resolver saw: for a relative
171-
// request that is the issuer's context joined with it, never a `rawRequest`.
172-
if (unused > 0 && hasAbsoluteName) {
171+
// Only what is left unaccounted for is worth the walk: a request the
172+
// graph keeps nowhere else is on the dependency that asked for it.
173+
if (unused > 0) {
173174
const fs = compiler.inputFileSystem || undefined;
175+
// One import states its request on several dependencies, and a
176+
// request every module makes states it once per module.
177+
/** @type {Set<string>} */
178+
const seen = new Set();
174179

175180
for (const module of compilation.modules) {
176181
if (unused === 0) break;
177182

178183
const context = module.context;
179184

180-
if (context === null) continue;
181-
182185
/** @type {DependenciesBlock[]} */
183186
const blocks = [module];
184187

@@ -188,15 +191,36 @@ class UnusedAliasesPlugin {
188191
(blocks.pop());
189192

190193
for (const dependency of block.dependencies) {
194+
const typed =
195+
/** @type {Dependency & { request?: string, options?: ContextDependencyOptions }} */
196+
(dependency);
197+
// A `ContextModule` keeps the request in its options, and one
198+
// an alias sent to `false` reaches no `rawRequest` at all.
191199
const request =
192-
/** @type {Dependency & { request?: string }} */
193-
(dependency).request;
194-
195-
if (typeof request !== "string" || !request.startsWith(".")) {
196-
continue;
200+
typeof typed.request === "string"
201+
? typed.request
202+
: typed.options &&
203+
typeof typed.options.request === "string"
204+
? typed.options.request
205+
: undefined;
206+
207+
if (request === undefined) continue;
208+
209+
if (!seen.has(request)) {
210+
seen.add(request);
211+
consume(request);
197212
}
198213

199-
consume(join(fs, context, request), true);
214+
// An absolute name matches the path the resolver saw, which for
215+
// a relative request is the issuer's context joined with it, so
216+
// the same request answers differently per module.
217+
if (
218+
hasAbsoluteName &&
219+
context !== null &&
220+
request.startsWith(".")
221+
) {
222+
consume(join(fs, context, request), true);
223+
}
200224
}
201225

202226
for (const child of block.blocks) blocks.push(child);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
it("should not report an alias a context module resolved through", () => {
2+
const lang = "en";
3+
4+
return import(`@locales/${lang}.js`).then((module) => {
5+
expect(module.default).toBe("en");
6+
});
7+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "de";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "en";
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use strict";
2+
3+
const path = require("path");
4+
5+
// The elements resolve relative to the directory the alias named, so the
6+
// request as written survives only on the context dependency.
7+
/** @type {import("../../../../").Configuration} */
8+
module.exports = {
9+
mode: "development",
10+
performance: {
11+
hints: "warning",
12+
unusedAliases: true
13+
},
14+
resolve: {
15+
alias: {
16+
"@locales": path.resolve(__dirname, "locales")
17+
}
18+
}
19+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import stub from "stub-me";
2+
3+
it("should not report an alias that stubs a request out", () => {
4+
expect(stub).toEqual({});
5+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"use strict";
2+
3+
// `false` is what the request resolves to, so it never reaches a module
4+
// carrying the request as written — the alias applied all the same.
5+
/** @type {import("../../../../").Configuration} */
6+
module.exports = {
7+
mode: "development",
8+
performance: {
9+
hints: "warning",
10+
unusedAliases: true
11+
},
12+
resolve: {
13+
alias: {
14+
"stub-me": false
15+
}
16+
}
17+
};

0 commit comments

Comments
 (0)