@@ -356,7 +356,6 @@ function invalidPackageTarget(
356356
357357const invalidSegmentRegEx = / ( ^ | \\ | \/ ) ( ( \. | % 2 e ) ( \. | % 2 e ) ? | ( n | % 6 e | % 4 e ) ( o | % 6 f | % 4 f ) ( d | % 6 4 | % 4 4 ) ( e | % 6 5 | % 4 5 ) ( _ | % 5 f ) ( m | % 6 d | % 4 d ) ( o | % 6 f | % 4 f ) ( d | % 6 4 | % 4 4 ) ( u | % 7 5 | % 5 5 ) ( l | % 6 c | % 4 c ) ( e | % 6 5 | % 4 5 ) ( s | % 7 3 | % 5 3 ) ) ? ( \\ | \/ | $ ) / i;
358358const deprecatedInvalidSegmentRegEx = / ( ^ | \\ | \/ ) ( ( \. | % 2 e ) ( \. | % 2 e ) ? | ( n | % 6 e | % 4 e ) ( o | % 6 f | % 4 f ) ( d | % 6 4 | % 4 4 ) ( e | % 6 5 | % 4 5 ) ( _ | % 5 f ) ( m | % 6 d | % 4 d ) ( o | % 6 f | % 4 f ) ( d | % 6 4 | % 4 4 ) ( u | % 7 5 | % 5 5 ) ( l | % 6 c | % 4 c ) ( e | % 6 5 | % 4 5 ) ( s | % 7 3 | % 5 3 ) ) ( \\ | \/ | $ ) / i;
359- const invalidPackageNameRegEx = / ^ \. | % | \\ / ;
360359const patternRegEx = / \* / g;
361360
362361/**
@@ -752,44 +751,6 @@ function packageImportsResolve(name, base, conditions) {
752751 throw importNotDefined ( name , packageJSONUrl , base ) ;
753752}
754753
755- /**
756- * Parse a package name from a specifier.
757- * @param {string } specifier - The import specifier.
758- * @param {string | URL | undefined } base - The parent URL.
759- */
760- function parsePackageName ( specifier , base ) {
761- let separatorIndex = StringPrototypeIndexOf ( specifier , '/' ) ;
762- let validPackageName = true ;
763- let isScoped = false ;
764- if ( specifier [ 0 ] === '@' ) {
765- isScoped = true ;
766- if ( separatorIndex === - 1 || specifier . length === 0 ) {
767- validPackageName = false ;
768- } else {
769- separatorIndex = StringPrototypeIndexOf (
770- specifier , '/' , separatorIndex + 1 ) ;
771- }
772- }
773-
774- const packageName = separatorIndex === - 1 ?
775- specifier : StringPrototypeSlice ( specifier , 0 , separatorIndex ) ;
776-
777- // Package name cannot have leading . and cannot have percent-encoding or
778- // \\ separators.
779- if ( RegExpPrototypeExec ( invalidPackageNameRegEx , packageName ) !== null ) {
780- validPackageName = false ;
781- }
782-
783- if ( ! validPackageName ) {
784- throw new ERR_INVALID_MODULE_SPECIFIER (
785- specifier , 'is not a valid package name' , fileURLToPath ( base ) ) ;
786- }
787-
788- const packageSubpath = '.' + ( separatorIndex === - 1 ? '' :
789- StringPrototypeSlice ( specifier , separatorIndex ) ) ;
790-
791- return { packageName, packageSubpath, isScoped } ;
792- }
793754
794755/**
795756 * Resolves a package specifier to a URL.
@@ -804,57 +765,24 @@ function packageResolve(specifier, base, conditions) {
804765 return new URL ( 'node:' + specifier ) ;
805766 }
806767
807- const { packageName, packageSubpath, isScoped } =
808- parsePackageName ( specifier , base ) ;
768+ const { packageJSONUrl, packageJSONPath, packageSubpath } = packageJsonReader . getPackageJSONURL ( specifier , base ) ;
809769
810- // ResolveSelf
811- const packageConfig = packageJsonReader . getPackageScopeConfig ( base ) ;
812- if ( packageConfig . exists ) {
813- if ( packageConfig . exports != null && packageConfig . name === packageName ) {
814- const packageJSONUrl = pathToFileURL ( packageConfig . pjsonPath ) ;
815- return packageExportsResolve (
816- packageJSONUrl , packageSubpath , packageConfig , base , conditions ) ;
817- }
818- }
770+ const packageConfig = packageJsonReader . read ( packageJSONPath , { __proto__ : null , specifier, base, isESM : true } ) ;
819771
820- let packageJSONUrl =
821- new URL ( './node_modules/' + packageName + '/package.json' , base ) ;
822- let packageJSONPath = fileURLToPath ( packageJSONUrl ) ;
823- let lastPath ;
824- do {
825- const stat = internalFsBinding . internalModuleStat (
826- internalFsBinding ,
827- StringPrototypeSlice ( packageJSONPath , 0 , packageJSONPath . length - 13 ) ,
772+ // Package match.
773+ if ( packageConfig . exports != null ) {
774+ return packageExportsResolve (
775+ packageJSONUrl , packageSubpath , packageConfig , base , conditions ) ;
776+ }
777+ if ( packageSubpath === '.' ) {
778+ return legacyMainResolve (
779+ packageJSONUrl ,
780+ packageConfig ,
781+ base ,
828782 ) ;
829- // Check for !stat.isDirectory()
830- if ( stat !== 1 ) {
831- lastPath = packageJSONPath ;
832- packageJSONUrl = new URL ( ( isScoped ?
833- '../../../../node_modules/' : '../../../node_modules/' ) +
834- packageName + '/package.json' , packageJSONUrl ) ;
835- packageJSONPath = fileURLToPath ( packageJSONUrl ) ;
836- continue ;
837- }
838-
839- // Package match.
840- const packageConfig = packageJsonReader . read ( packageJSONPath , { __proto__ : null , specifier, base, isESM : true } ) ;
841- if ( packageConfig . exports != null ) {
842- return packageExportsResolve (
843- packageJSONUrl , packageSubpath , packageConfig , base , conditions ) ;
844- }
845- if ( packageSubpath === '.' ) {
846- return legacyMainResolve (
847- packageJSONUrl ,
848- packageConfig ,
849- base ,
850- ) ;
851- }
852-
853- return new URL ( packageSubpath , packageJSONUrl ) ;
854- // Cross-platform root check.
855- } while ( packageJSONPath . length !== lastPath . length ) ;
783+ }
856784
857- throw new ERR_MODULE_NOT_FOUND ( packageName , fileURLToPath ( base ) , null ) ;
785+ return new URL ( packageSubpath , packageJSONUrl ) ;
858786}
859787
860788/**
@@ -1105,10 +1033,11 @@ module.exports = {
11051033 decorateErrorWithCommonJSHints,
11061034 defaultResolve,
11071035 encodedSepRegEx,
1036+ legacyMainResolve,
11081037 packageExportsResolve,
11091038 packageImportsResolve,
1039+ packageResolve,
11101040 throwIfInvalidParentURL,
1111- legacyMainResolve,
11121041} ;
11131042
11141043// cycle
0 commit comments