Skip to content

Commit 21ff331

Browse 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

1 file changed

Lines changed: 16 additions & 22 deletions

File tree

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

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,19 @@ const {
2525
}=primordials;
2626
constinternalFS=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
constpolicy=getOptionValue('--experimental-policy') ?
3632
require('internal/process/policy') :
3733
null;
38-
const{ sep, relative }=require('path');
34+
const{ sep, relative, toNamespacedPath}=require('path');
3935
constpreserveSymlinks=getOptionValue('--preserve-symlinks');
4036
constpreserveSymlinksMain=getOptionValue('--preserve-symlinks-main');
4137
constexperimentalNetworkImports=
4238
getOptionValue('--experimental-network-imports');
4339
consttypeFlag=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
constrealpathCache=newSafeMap();
139136

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

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

221211
constpath=fileURLToPath(resolved);
222212

223-
conststats=tryStatSync(StringPrototypeEndsWith(path,'/') ?
224-
StringPrototypeSlice(path,-1) : path);
225-
if(stats.isDirectory()){
213+
conststats=internalModuleStat(toNamespacedPath(StringPrototypeEndsWith(path,'/') ?
214+
StringPrototypeSlice(path,-1) : path));
215+
216+
// Check for stats.isDirectory()
217+
if(stats===1){
226218
consterr=newERR_UNSUPPORTED_DIR_IMPORT(path,fileURLToPath(base));
227219
err.url=String(resolved);
228220
throwerr;
229-
}elseif(!stats.isFile()){
221+
}elseif(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
letpackageJSONPath=fileURLToPath(packageJSONUrl);
756749
letlastPath;
757750
do{
758-
conststat=tryStatSync(StringPrototypeSlice(packageJSONPath,0,
759-
packageJSONPath.length-13));
760-
if(!stat.isDirectory()){
751+
conststat=internalModuleStat(toNamespacedPath(StringPrototypeSlice(packageJSONPath,0,
752+
packageJSONPath.length-13)));
753+
// Check for !stat.isDirectory()
754+
if(stat!==1){
761755
lastPath=packageJSONPath;
762756
packageJSONUrl=newURL((isScoped ?
763757
'../../../../node_modules/' : '../../../node_modules/')+

0 commit comments

Comments
 (0)