Skip to content

Commit b54c5e2

Browse files
perf(config): read only the browserslist config for the default target (#22324)
* perf(config): read only the browserslist config for the default target Choosing the default target asks one question — does a browserslist config exist — and answered it by loading browserslist, which pulls caniuse-lite, node-releases, electron-to-chromium and baseline-browser-mapping: 1.65 MB retained for the whole build, of which a build with no such config uses nothing. browserslist's config half is a module of its own, the same instance the queries use, so its caches stay shared. Asking that instead costs 334 KB and loads the query data only where a config is found. Start-up heap on a 956-module development build drops 20,728 KB to 19,385 KB, and the saving holds to peak (58,292 KB to 56,924 KB). * test(config): read the default-target cases off the environment and the cache keys The child inherited BROWSERSLIST and BROWSERSLIST_CONFIG, which browserslist reads as a config of their own, so the no-config case answered browserslist where either was set. It now runs without them. It also matched require.cache keys by substring, which no forward-slash entry does on Windows. Asking require.cache by the request's resolved path reads the key Node wrote, so the configured case can assert the whole list. * refactor(config): drop the fallback for an unresolvable browserslist config half No suite can reach it: browserslist's own entry requires that module, so a packaging change hiding it would already break the handler's own require of browserslist, and the fallback would answer for a build that cannot run.
1 parent 6ae31d7 commit b54c5e2

4 files changed

Lines changed: 97 additions & 0 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+
Read only the browserslist config when choosing the default target.

‎declarations.d.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ declare module "color-name" {
1616
export = colors;
1717
}
1818

19+
// The config half of browserslist, which the default target reads without the
20+
// caniuse data a query needs
21+
declare module "browserslist/node" {
22+
export function loadConfig(opts: {
23+
path?: string;
24+
config?: string;
25+
env?: string;
26+
}): string | string[] | undefined;
27+
}
28+
1929
// Deprecated NodeJS API usages in webpack
2030
declare namespace NodeJS {
2131
interface Process {

‎lib/config/target.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,22 @@ const getBrowserslistTargetHandler = memoize(() =>
1212
require("./browserslistTargetHandler")
1313
);
1414

15+
// The half of browserslist that finds a config, the same module instance the
16+
// queries use, so its caches stay shared and a request for one costs nothing
17+
// twice. browserslist's own entry requires it, so it resolves wherever that does
18+
const getBrowserslistConfigLoader = memoize(() => require("browserslist/node"));
19+
1520
/**
1621
* Gets default target.
1722
* @param {string} context the context directory
1823
* @returns {string} default target
1924
*/
2025
const getDefaultTarget = (context) => {
26+
// WHY: the default only turns on whether a browserslist config exists, and
27+
// the config half answers that without the caniuse data the queries read —
28+
// 1.3 MB that a build with no such config never uses.
29+
const config = getBrowserslistConfigLoader().loadConfig({ path: context });
30+
if (!config) return "web";
2131
const browsers = getBrowserslistTargetHandler().load(undefined, context);
2232
return browsers ? "browserslist" : "web";
2333
};

‎test/unitCases/target-browserslist.unittest.js‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use strict";
22

3+
const { execFileSync } = require("child_process");
34
const path = require("path");
45
const { load, resolve } = require("../../lib/config/browserslistTargetHandler");
56

@@ -167,4 +168,75 @@ describe("browserslist target", () => {
167168
);
168169
});
169170
});
171+
172+
describe("the default target", () => {
173+
const withoutConfig = path.resolve(__dirname, "../fixtures");
174+
const withConfig = path.resolve(__dirname, "../fixtures/browserslist");
175+
176+
// What a browserslist query reads, asked for by request rather than by
177+
// path, so `require.cache` is read with the key Node itself wrote
178+
const QUERY_DATA = [
179+
"browserslist",
180+
"caniuse-lite/dist/unpacker/agents",
181+
"electron-to-chromium/versions",
182+
"node-releases/data/processed/envs.json",
183+
"baseline-browser-mapping"
184+
];
185+
186+
/**
187+
* The environment without the browserslist variables, which decide a
188+
* config of their own and would otherwise reach the child.
189+
* @returns {NodeJS.ProcessEnv} the environment to run a child in
190+
*/
191+
const environmentWithoutBrowserslist = () => {
192+
/** @type {NodeJS.ProcessEnv} */
193+
const environment = {};
194+
for (const [name, value] of Object.entries(process.env)) {
195+
if (!name.startsWith("BROWSERSLIST")) environment[name] = value;
196+
}
197+
return environment;
198+
};
199+
200+
/**
201+
* Resolves the default target in a fresh process, reporting which of the
202+
* things a query reads were loaded to answer it.
203+
* @param {string} context the context directory
204+
* @returns {{ target: string, queryData: string[] }} what it answered
205+
*/
206+
const resolveInChildProcess = (context) => {
207+
const script = `
208+
const { getDefaultTarget } = require(${JSON.stringify(
209+
require.resolve("../../lib/config/target")
210+
)});
211+
const target = getDefaultTarget(${JSON.stringify(context)});
212+
const queryData = ${JSON.stringify(QUERY_DATA)}.filter((request) => {
213+
try {
214+
return require.cache[require.resolve(request)] !== undefined;
215+
} catch (_err) {
216+
return false;
217+
}
218+
});
219+
process.stdout.write(JSON.stringify({ target, queryData }));
220+
`;
221+
return JSON.parse(
222+
execFileSync(process.execPath, ["-e", script], {
223+
encoding: "utf8",
224+
env: environmentWithoutBrowserslist()
225+
})
226+
);
227+
};
228+
229+
it("reads no query data where no browserslist config exists", () => {
230+
const { target, queryData } = resolveInChildProcess(withoutConfig);
231+
expect(target).toBe("web");
232+
expect(queryData).toEqual([]);
233+
});
234+
235+
it("answers browserslist where a config exists", () => {
236+
const { target, queryData } = resolveInChildProcess(withConfig);
237+
expect(target).toBe("browserslist");
238+
// A config means the queries do run, so the data is read after all
239+
expect(queryData).toEqual(QUERY_DATA);
240+
});
241+
});
170242
});

0 commit comments

Comments
 (0)