perf: avoid extra jobs for build dependencies (#19905)

This commit is contained in:
Alexander Akait 2025-09-19 04:17:25 +03:00 committed by GitHub
parent a796d25769
commit ab9f78b8f6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 24 additions and 29 deletions

View File

@ -45,8 +45,8 @@ let FS_ACCURACY = 2000;
const EMPTY_SET = new Set();
const RBDT_RESOLVE_CJS = 0;
const RBDT_RESOLVE_ESM = 1;
const RBDT_RESOLVE_INITIAL = 0;
const RBDT_RESOLVE_FILE = 1;
const RBDT_RESOLVE_DIRECTORY = 2;
const RBDT_RESOLVE_CJS_FILE = 3;
const RBDT_RESOLVE_CJS_FILE_AS_CHILD = 4;
@ -56,7 +56,7 @@ const RBDT_FILE = 7;
const RBDT_DIRECTORY_DEPENDENCIES = 8;
const RBDT_FILE_DEPENDENCIES = 9;
/** @typedef {RBDT_RESOLVE_CJS | RBDT_RESOLVE_ESM | RBDT_RESOLVE_DIRECTORY | RBDT_RESOLVE_CJS_FILE | RBDT_RESOLVE_CJS_FILE_AS_CHILD | RBDT_RESOLVE_ESM_FILE | RBDT_DIRECTORY | RBDT_FILE | RBDT_DIRECTORY_DEPENDENCIES | RBDT_FILE_DEPENDENCIES} JobType */
/** @typedef {RBDT_RESOLVE_INITIAL | RBDT_RESOLVE_FILE | RBDT_RESOLVE_DIRECTORY | RBDT_RESOLVE_CJS_FILE | RBDT_RESOLVE_CJS_FILE_AS_CHILD | RBDT_RESOLVE_ESM_FILE | RBDT_DIRECTORY | RBDT_FILE | RBDT_DIRECTORY_DEPENDENCIES | RBDT_FILE_DEPENDENCIES} JobType */
const INVALID = Symbol("invalid");
@ -1623,16 +1623,12 @@ class FileSystemInfo {
/**
* @param {Job} job job
* @returns {`resolve commonjs file ${string}${string}`|`resolve esm file ${string}${string}`|`resolve esm ${string}${string}`|`resolve directory ${string}`|`file ${string}`|`unknown ${string} ${string}`|`resolve commonjs ${string}${string}`|`directory ${string}`|`file dependencies ${string}`|`directory dependencies ${string}`} result
* @returns {string} result
*/
const jobToString = (job) => {
switch (job.type) {
case RBDT_RESOLVE_CJS:
return `resolve commonjs ${job.path}${expectedToString(
job.expected
)}`;
case RBDT_RESOLVE_ESM:
return `resolve esm ${job.path}${expectedToString(job.expected)}`;
case RBDT_RESOLVE_FILE:
return `resolve file ${job.path}${expectedToString(job.expected)}`;
case RBDT_RESOLVE_DIRECTORY:
return `resolve directory ${job.path}`;
case RBDT_RESOLVE_CJS_FILE:
@ -1674,7 +1670,7 @@ class FileSystemInfo {
deps,
(dep) =>
/** @type {Job} */ ({
type: RBDT_RESOLVE_CJS,
type: RBDT_RESOLVE_INITIAL,
context,
path: dep,
expected: undefined,
@ -1778,27 +1774,23 @@ class FileSystemInfo {
}
);
};
switch (type) {
case RBDT_RESOLVE_CJS: {
const isDirectory = /[\\/]$/.test(path);
if (isDirectory) {
resolveDirectory(path.slice(0, -1));
} else {
resolveFile(path, "f", resolveCjs);
}
break;
}
case RBDT_RESOLVE_ESM: {
const isDirectory = /[\\/]$/.test(path);
if (isDirectory) {
resolveDirectory(path.slice(0, -1));
} else {
resolveFile(path);
}
const resolvedType =
type === RBDT_RESOLVE_INITIAL
? /[\\/]$/.test(path)
? RBDT_RESOLVE_DIRECTORY
: RBDT_RESOLVE_FILE
: type;
switch (resolvedType) {
case RBDT_RESOLVE_FILE: {
resolveFile(
path,
"f",
/\.mjs$/.test(path) ? resolveEsm : resolveCjs
);
break;
}
case RBDT_RESOLVE_DIRECTORY: {
resolveDirectory(path);
resolveDirectory(RBDT_RESOLVE_INITIAL ? path.slice(0, -1) : path);
break;
}
case RBDT_RESOLVE_CJS_FILE: {
@ -1963,6 +1955,7 @@ class FileSystemInfo {
const context = dirname(this.fs, path);
const source = /** @type {Buffer} */ (content).toString();
const [imports] = lexer.parse(source);
const added = new Set();
for (const imp of imports) {
try {
let dependency;
@ -1985,6 +1978,8 @@ class FileSystemInfo {
// We should not track Node.js build dependencies
if (dependency.startsWith("node:")) continue;
if (builtinModules.has(dependency)) continue;
// Avoid extra jobs for identical imports
if (added.has(dependency)) continue;
push({
type: RBDT_RESOLVE_ESM_FILE,