@@ -770,73 +770,67 @@ impl<C: Cache> ResolverGeneric<C> {
770770
771771 // 1. let DIRS = NODE_MODULES_PATHS(START)
772772 // 2. for each DIR in DIRS:
773- for module_name in & self . options . modules {
774- for cached_path in std:: iter:: successors ( Some ( cached_path) , |p| p. parent ( ) ) {
775- // Skip if /path/to/node_modules does not exist
776- if !self . cache . is_dir ( cached_path, ctx) {
777- continue ;
778- }
779-
780- let Some ( cached_path) = self . get_module_directory ( cached_path, module_name, ctx)
781- else {
782- continue ;
783- } ;
784- // Optimize node_modules lookup by inspecting whether the package exists
785- // From LOAD_PACKAGE_EXPORTS(X, DIR)
786- // 1. Try to interpret X as a combination of NAME and SUBPATH where the name
787- // may have a @scope/ prefix and the subpath begins with a slash (`/`).
788- if !package_name. is_empty ( ) {
789- let cached_path = cached_path. normalize_with ( package_name, self . cache . as_ref ( ) ) ;
790- // Try foo/node_modules/package_name
791- if self . cache . is_dir ( & cached_path, ctx) {
792- // a. LOAD_PACKAGE_EXPORTS(X, DIR)
793- if let Some ( path) =
794- self . load_package_exports ( specifier, subpath, & cached_path, ctx) ?
795- {
796- return Ok ( Some ( path) ) ;
797- }
798- } else {
799- // foo/node_modules/package_name is not a directory, so useless to check inside it
800- if !subpath. is_empty ( ) {
801- continue ;
802- }
803- // Skip if the directory lead to the scope package does not exist
804- // i.e. `foo/node_modules/@scope` is not a directory for `foo/node_modules/@scope/package`
805- if package_name. starts_with ( '@' ) {
806- if let Some ( path) = cached_path. parent ( ) {
807- if !self . cache . is_dir ( path, ctx) {
808- continue ;
809- }
773+ for cached_path in std:: iter:: successors ( Some ( cached_path) , |p| p. parent ( ) ) {
774+ // Skip if /path/to/node_modules does not exist
775+ let cached_path = cached_path. normalize_with ( "node_modules" , self . cache . as_ref ( ) ) ;
776+ if !self . cache . is_dir ( & cached_path, ctx) {
777+ continue ;
778+ }
779+ // Optimize node_modules lookup by inspecting whether the package exists
780+ // From LOAD_PACKAGE_EXPORTS(X, DIR)
781+ // 1. Try to interpret X as a combination of NAME and SUBPATH where the name
782+ // may have a @scope/ prefix and the subpath begins with a slash (`/`).
783+ if !package_name. is_empty ( ) {
784+ let cached_path = cached_path. normalize_with ( package_name, self . cache . as_ref ( ) ) ;
785+ // Try foo/node_modules/package_name
786+ if self . cache . is_dir ( & cached_path, ctx) {
787+ // a. LOAD_PACKAGE_EXPORTS(X, DIR)
788+ if let Some ( path) =
789+ self . load_package_exports ( specifier, subpath, & cached_path, ctx) ?
790+ {
791+ return Ok ( Some ( path) ) ;
792+ }
793+ } else {
794+ // foo/node_modules/package_name is not a directory, so useless to check inside it
795+ if !subpath. is_empty ( ) {
796+ continue ;
797+ }
798+ // Skip if the directory lead to the scope package does not exist
799+ // i.e. `foo/node_modules/@scope` is not a directory for `foo/node_modules/@scope/package`
800+ if package_name. starts_with ( '@' ) {
801+ if let Some ( path) = cached_path. parent ( ) {
802+ if !self . cache . is_dir ( path, ctx) {
803+ continue ;
810804 }
811805 }
812806 }
813807 }
808+ }
814809
815- // Try as file or directory for all other cases
816- // b. LOAD_AS_FILE(DIR/X)
817- // c. LOAD_AS_DIRECTORY(DIR/X)
810+ // Try as file or directory for all other cases
811+ // b. LOAD_AS_FILE(DIR/X)
812+ // c. LOAD_AS_DIRECTORY(DIR/X)
818813
819- let cached_path = cached_path. normalize_with ( specifier, self . cache . as_ref ( ) ) ;
814+ let cached_path = cached_path. normalize_with ( specifier, self . cache . as_ref ( ) ) ;
820815
821- // Perf: try the directory first for package specifiers.
822- if self . options . resolve_to_context {
823- return Ok ( self . cache . is_dir ( & cached_path, ctx) . then ( || cached_path. clone ( ) ) ) ;
824- }
825- if self . cache . is_dir ( & cached_path, ctx) {
826- if let Some ( path) = self . load_browser_field_or_alias ( & cached_path, ctx) ? {
827- return Ok ( Some ( path) ) ;
828- }
829- if let Some ( path) = self . load_as_directory ( & cached_path, ctx) ? {
830- return Ok ( Some ( path) ) ;
831- }
832- }
833- if let Some ( path) = self . load_as_file ( & cached_path, ctx) ? {
816+ // Perf: try the directory first for package specifiers.
817+ if self . options . resolve_to_context {
818+ return Ok ( self . cache . is_dir ( & cached_path, ctx) . then ( || cached_path. clone ( ) ) ) ;
819+ }
820+ if self . cache . is_dir ( & cached_path, ctx) {
821+ if let Some ( path) = self . load_browser_field_or_alias ( & cached_path, ctx) ? {
834822 return Ok ( Some ( path) ) ;
835823 }
836824 if let Some ( path) = self . load_as_directory ( & cached_path, ctx) ? {
837825 return Ok ( Some ( path) ) ;
838826 }
839827 }
828+ if let Some ( path) = self . load_as_file ( & cached_path, ctx) ? {
829+ return Ok ( Some ( path) ) ;
830+ }
831+ if let Some ( path) = self . load_as_directory ( & cached_path, ctx) ? {
832+ return Ok ( Some ( path) ) ;
833+ }
840834 }
841835 Ok ( None )
842836 }
@@ -880,23 +874,6 @@ impl<C: Cache> ResolverGeneric<C> {
880874 }
881875 }
882876
883- fn get_module_directory (
884- & self ,
885- cached_path : & C :: Cp ,
886- module_name : & str ,
887- ctx : & mut Ctx ,
888- ) -> Option < C :: Cp > {
889- if module_name == "node_modules" {
890- cached_path. cached_node_modules ( self . cache . as_ref ( ) , ctx)
891- } else if cached_path. path ( ) . components ( ) . next_back ( )
892- == Some ( Component :: Normal ( OsStr :: new ( module_name) ) )
893- {
894- Some ( cached_path. clone ( ) )
895- } else {
896- cached_path. module_directory ( module_name, self . cache . as_ref ( ) , ctx)
897- }
898- }
899-
900877 fn load_package_exports (
901878 & self ,
902879 specifier : & str ,
@@ -1339,51 +1316,49 @@ impl<C: Cache> ResolverGeneric<C> {
13391316 self . require_core ( package_name) ?;
13401317
13411318 // 11. While parentURL is not the file system root,
1342- for module_name in & self . options . modules {
1343- for cached_path in std:: iter:: successors ( Some ( cached_path) , |p| p. parent ( ) ) {
1344- // 1. Let packageURL be the URL resolution of "node_modules/" concatenated with packageSpecifier, relative to parentURL.
1345- let Some ( cached_path) = self . get_module_directory ( cached_path, module_name, ctx)
1346- else {
1347- continue ;
1348- } ;
1349- // 2. Set parentURL to the parent folder URL of parentURL.
1350- let cached_path = cached_path. normalize_with ( package_name, self . cache . as_ref ( ) ) ;
1351- // 3. If the folder at packageURL does not exist, then
1352- // 1. Continue the next loop iteration.
1353- if self . cache . is_dir ( & cached_path, ctx) {
1354- // 4. Let pjson be the result of READ_PACKAGE_JSON(packageURL).
1355- if let Some ( ( _, package_json) ) =
1356- self . cache . get_package_json ( & cached_path, & self . options , ctx) ?
1357- {
1358- // 5. If pjson is not null and pjson.exports is not null or undefined, then
1359- // 1. Return the result of PACKAGE_EXPORTS_RESOLVE(packageURL, packageSubpath, pjson.exports, defaultConditions).
1360- for exports in package_json. exports_fields ( & self . options . exports_fields ) {
1361- if let Some ( path) = self . package_exports_resolve (
1362- & cached_path,
1363- & format ! ( ".{subpath}" ) ,
1364- & exports,
1365- ctx,
1366- ) ? {
1367- return Ok ( Some ( path) ) ;
1368- }
1319+ for cached_path in std:: iter:: successors ( Some ( cached_path) , |p| p. parent ( ) ) {
1320+ // 1. Let packageURL be the URL resolution of "node_modules/" concatenated with packageSpecifier, relative to parentURL.
1321+ let cached_path = cached_path. normalize_with ( "node_modules" , self . cache . as_ref ( ) ) ;
1322+ if !self . cache . is_dir ( & cached_path, ctx) {
1323+ continue ;
1324+ }
1325+ // 2. Set parentURL to the parent folder URL of parentURL.
1326+ let cached_path = cached_path. normalize_with ( package_name, self . cache . as_ref ( ) ) ;
1327+ // 3. If the folder at packageURL does not exist, then
1328+ // 1. Continue the next loop iteration.
1329+ if self . cache . is_dir ( & cached_path, ctx) {
1330+ // 4. Let pjson be the result of READ_PACKAGE_JSON(packageURL).
1331+ if let Some ( ( _, package_json) ) =
1332+ self . cache . get_package_json ( & cached_path, & self . options , ctx) ?
1333+ {
1334+ // 5. If pjson is not null and pjson.exports is not null or undefined, then
1335+ // 1. Return the result of PACKAGE_EXPORTS_RESOLVE(packageURL, packageSubpath, pjson.exports, defaultConditions).
1336+ for exports in package_json. exports_fields ( & self . options . exports_fields ) {
1337+ if let Some ( path) = self . package_exports_resolve (
1338+ & cached_path,
1339+ & format ! ( ".{subpath}" ) ,
1340+ & exports,
1341+ ctx,
1342+ ) ? {
1343+ return Ok ( Some ( path) ) ;
13691344 }
1370- // 6. Otherwise, if packageSubpath is equal to ".", then
1371- if subpath == "." {
1372- // 1. If pjson.main is a string, then
1373- for main_field in package_json . main_fields ( & self . options . main_fields ) {
1374- // 1. Return the URL resolution of main in packageURL.
1375- let cached_path =
1376- cached_path. normalize_with ( main_field , self . cache . as_ref ( ) ) ;
1377- if self . cache . is_file ( & cached_path , ctx ) {
1378- return Ok ( Some ( cached_path) ) ;
1379- }
1345+ }
1346+ // 6. Otherwise, if packageSubpath is equal to ".", then
1347+ if subpath == "." {
1348+ // 1. If pjson.main is a string, then
1349+ for main_field in package_json . main_fields ( & self . options . main_fields ) {
1350+ // 1. Return the URL resolution of main in packageURL.
1351+ let cached_path =
1352+ cached_path . normalize_with ( main_field , self . cache . as_ref ( ) ) ;
1353+ if self . cache . is_file ( & cached_path, ctx ) {
1354+ return Ok ( Some ( cached_path ) ) ;
13801355 }
13811356 }
13821357 }
1383- let subpath = format ! ( ".{subpath}" ) ;
1384- ctx. with_fully_specified ( false ) ;
1385- return self . require ( & cached_path, & subpath, ctx) . map ( Some ) ;
13861358 }
1359+ let subpath = format ! ( ".{subpath}" ) ;
1360+ ctx. with_fully_specified ( false ) ;
1361+ return self . require ( & cached_path, & subpath, ctx) . map ( Some ) ;
13871362 }
13881363 }
13891364
0 commit comments