Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 21ff331

Browse filesBrowse files
anonrigtargos
authored andcommitted
lib: improve esm resolve performance
PR-URL: #46652 Refs: nodejs/performance#39 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Erick Wendel <erick.workspace@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 5a9b71a commit 21ff331
Copy full SHA for 21ff331

File tree

Expand file treeCollapse file tree

1 file changed

+16
-22
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+16
-22
lines changed
Open diff view settings
Collapse file

‎lib/internal/modules/esm/resolve.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/resolve.js
+16-22Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,19 @@ const {
2525
} = primordials;
2626
const internalFS = require('internal/fs/utils');
2727
const { BuiltinModule } = require('internal/bootstrap/realm');
28-
const {
29-
realpathSync,
30-
statSync,
31-
Stats,
32-
} = require('fs');
28+
const { realpathSync } = require('fs');
3329
const { getOptionValue } = require('internal/options');
3430
// Do not eagerly grab .manifest, it may be in TDZ
3531
const policy = getOptionValue('--experimental-policy') ?
3632
require('internal/process/policy') :
3733
null;
38-
const { sep, relative } = require('path');
34+
const { sep, relative, toNamespacedPath } = require('path');
3935
const preserveSymlinks = getOptionValue('--preserve-symlinks');
4036
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
4137
const experimentalNetworkImports =
4238
getOptionValue('--experimental-network-imports');
4339
const typeFlag = getOptionValue('--input-type');
44-
const { URL, pathToFileURL, fileURLToPath, isURL } = require('internal/url');
40+
const { URL, pathToFileURL, fileURLToPath, isURL, toPathIfFileURL } = require('internal/url');
4541
const { canParse: canParseURL } = internalBinding('url');
4642
const {
4743
ERR_INPUT_TYPE_NOT_ALLOWED,
@@ -61,6 +57,7 @@ const {
6157
const { Module: CJSModule } = require('internal/modules/cjs/loader');
6258
const { getPackageConfig, getPackageScopeConfig } = require('internal/modules/esm/package_config');
6359
const { getConditionsSet } = require('internal/modules/esm/utils');
60+
const { internalModuleStat } = internalBinding('fs');
6461

6562
/**
6663
* @typedef {import('internal/modules/esm/package_config.js').PackageConfig} PackageConfig
@@ -137,19 +134,12 @@ function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) {
137134

138135
const realpathCache = new SafeMap();
139136

140-
/**
141-
* @param {string | URL} path
142-
* @returns {import('fs').Stats}
143-
*/
144-
const tryStatSync =
145-
(path) => statSync(path, { throwIfNoEntry: false }) ?? new Stats();
146-
147137
/**
148138
* @param {string | URL} url
149139
* @returns {boolean}
150140
*/
151141
function fileExists(url) {
152-
return statSync(url, { throwIfNoEntry: false })?.isFile() ?? false;
142+
return internalModuleStat(toNamespacedPath(toPathIfFileURL(url))) === 0;
153143
}
154144

155145
/**
@@ -220,13 +210,16 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
220210

221211
const path = fileURLToPath(resolved);
222212

223-
const stats = tryStatSync(StringPrototypeEndsWith(path, '/') ?
224-
StringPrototypeSlice(path, -1) : path);
225-
if (stats.isDirectory()) {
213+
const stats = internalModuleStat(toNamespacedPath(StringPrototypeEndsWith(path, '/') ?
214+
StringPrototypeSlice(path, -1) : path));
215+
216+
// Check for stats.isDirectory()
217+
if (stats === 1) {
226218
const err = new ERR_UNSUPPORTED_DIR_IMPORT(path, fileURLToPath(base));
227219
err.url = String(resolved);
228220
throw err;
229-
} else if (!stats.isFile()) {
221+
} else if (stats !== 0) {
222+
// Check for !stats.isFile()
230223
if (process.env.WATCH_REPORT_DEPENDENCIES && process.send) {
231224
process.send({ 'watch:require': [path || resolved.pathname] });
232225
}
@@ -755,9 +748,10 @@ function packageResolve(specifier, base, conditions) {
755748
let packageJSONPath = fileURLToPath(packageJSONUrl);
756749
let lastPath;
757750
do {
758-
const stat = tryStatSync(StringPrototypeSlice(packageJSONPath, 0,
759-
packageJSONPath.length - 13));
760-
if (!stat.isDirectory()) {
751+
const stat = internalModuleStat(toNamespacedPath(StringPrototypeSlice(packageJSONPath, 0,
752+
packageJSONPath.length - 13)));
753+
// Check for !stat.isDirectory()
754+
if (stat !== 1) {
761755
lastPath = packageJSONPath;
762756
packageJSONUrl = new URL((isScoped ?
763757
'../../../../node_modules/' : '../../../node_modules/') +

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.