Skip to content

Commit 76fb5d6

Browse files
committed
lib: improve esm resolve performance
1 parent ecd385e commit 76fb5d6

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

lib/internal/modules/esm/resolve.js

+14-17
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ const internalFS = require('internal/fs/utils');
2727
const { BuiltinModule } = require('internal/bootstrap/loaders');
2828
const {
2929
realpathSync,
30-
statSync,
31-
Stats,
3230
} = require('fs');
3331
const { getOptionValue } = require('internal/options');
3432
// Do not eagerly grab .manifest, it may be in TDZ
@@ -41,7 +39,7 @@ const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
4139
const experimentalNetworkImports =
4240
getOptionValue('--experimental-network-imports');
4341
const typeFlag = getOptionValue('--input-type');
44-
const { URL, pathToFileURL, fileURLToPath } = require('internal/url');
42+
const { URL, pathToFileURL, fileURLToPath, toPathIfFileURL } = require('internal/url');
4543
const {
4644
ERR_INPUT_TYPE_NOT_ALLOWED,
4745
ERR_INVALID_MODULE_SPECIFIER,
@@ -59,6 +57,7 @@ const {
5957
const { Module: CJSModule } = require('internal/modules/cjs/loader');
6058
const { getPackageConfig, getPackageScopeConfig } = require('internal/modules/esm/package_config');
6159
const { getConditionsSet } = require('internal/modules/esm/utils');
60+
const { internalModuleStat } = internalBinding('fs');
6261

6362
/**
6463
* @typedef {import('internal/modules/esm/package_config.js').PackageConfig} PackageConfig
@@ -135,19 +134,12 @@ function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) {
135134

136135
const realpathCache = new SafeMap();
137136

138-
/**
139-
* @param {string | URL} path
140-
* @returns {import('fs').Stats}
141-
*/
142-
const tryStatSync =
143-
(path) => statSync(path, { throwIfNoEntry: false }) ?? new Stats();
144-
145137
/**
146138
* @param {string | URL} url
147139
* @returns {boolean}
148140
*/
149141
function fileExists(url) {
150-
return statSync(url, { throwIfNoEntry: false })?.isFile() ?? false;
142+
return internalModuleStat(toPathIfFileURL(url)) === 0;
151143
}
152144

153145
/**
@@ -218,13 +210,16 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
218210

219211
const path = fileURLToPath(resolved);
220212

221-
const stats = tryStatSync(StringPrototypeEndsWith(path, '/') ?
213+
const stats = internalModuleStat(StringPrototypeEndsWith(path, '/') ?
222214
StringPrototypeSlice(path, -1) : path);
223-
if (stats.isDirectory()) {
215+
216+
// Check for stats.isDirectory()
217+
if (stats === 1) {
224218
const err = new ERR_UNSUPPORTED_DIR_IMPORT(path, fileURLToPath(base));
225219
err.url = String(resolved);
226220
throw err;
227-
} else if (!stats.isFile()) {
221+
} else if (stats !== 0) {
222+
// Check for !stats.isFile()
228223
if (process.env.WATCH_REPORT_DEPENDENCIES && process.send) {
229224
process.send({ 'watch:require': [path || resolved.pathname] });
230225
}
@@ -760,9 +755,11 @@ function packageResolve(specifier, base, conditions) {
760755
let packageJSONPath = fileURLToPath(packageJSONUrl);
761756
let lastPath;
762757
do {
763-
const stat = tryStatSync(StringPrototypeSlice(packageJSONPath, 0,
764-
packageJSONPath.length - 13));
765-
if (!stat.isDirectory()) {
758+
const stat = internalModuleStat(StringPrototypeSlice(packageJSONPath, 0,
759+
packageJSONPath.length - 13));
760+
761+
// Check for !stat.isDirectory
762+
if (stat !== 1) {
766763
lastPath = packageJSONPath;
767764
packageJSONUrl = new URL((isScoped ?
768765
'../../../../node_modules/' : '../../../node_modules/') +

0 commit comments

Comments
 (0)