@@ -22,7 +22,6 @@ const {
22
22
StringPrototypeSlice,
23
23
StringPrototypeSplit,
24
24
StringPrototypeStartsWith,
25
- StringPrototypeSubstr,
26
25
} = primordials ;
27
26
const internalFS = require ( 'internal/fs/utils' ) ;
28
27
const { NativeModule } = require ( 'internal/bootstrap/loaders' ) ;
@@ -724,38 +723,49 @@ function packageImportsResolve(name, base, conditions) {
724
723
packageJSONUrl = pathToFileURL ( packageConfig . pjsonPath ) ;
725
724
const imports = packageConfig . imports ;
726
725
if ( imports ) {
727
- if ( ObjectPrototypeHasOwnProperty ( imports , name ) ) {
726
+ if ( ObjectPrototypeHasOwnProperty ( imports , name ) &&
727
+ ! StringPrototypeIncludes ( name , '*' ) &&
728
+ ! StringPrototypeEndsWith ( name , '/' ) ) {
728
729
const resolved = resolvePackageTarget (
729
730
packageJSONUrl , imports [ name ] , '' , name , base , false , true , conditions
730
731
) ;
731
732
if ( resolved !== null )
732
733
return { resolved, exact : true } ;
733
734
} else {
734
735
let bestMatch = '' ;
736
+ let bestMatchSubpath ;
735
737
const keys = ObjectGetOwnPropertyNames ( imports ) ;
736
738
for ( let i = 0 ; i < keys . length ; i ++ ) {
737
739
const key = keys [ i ] ;
738
- if ( key [ key . length - 1 ] === '*' &&
740
+ const patternIndex = StringPrototypeIndexOf ( key , '*' ) ;
741
+ if ( patternIndex !== - 1 &&
739
742
StringPrototypeStartsWith ( name ,
740
- StringPrototypeSlice ( key , 0 , - 1 ) ) &&
741
- name . length >= key . length &&
742
- key . length > bestMatch . length ) {
743
- bestMatch = key ;
743
+ StringPrototypeSlice ( key , 0 ,
744
+ patternIndex ) ) ) {
745
+ const patternTrailer = StringPrototypeSlice ( key , patternIndex + 1 ) ;
746
+ if ( name . length >= key . length &&
747
+ StringPrototypeEndsWith ( name , patternTrailer ) &&
748
+ patternKeyCompare ( bestMatch , key ) === 1 &&
749
+ StringPrototypeLastIndexOf ( key , '*' ) === patternIndex ) {
750
+ bestMatch = key ;
751
+ bestMatchSubpath = StringPrototypeSlice (
752
+ name , patternIndex , name . length - patternTrailer . length ) ;
753
+ }
744
754
} else if ( key [ key . length - 1 ] === '/' &&
745
755
StringPrototypeStartsWith ( name , key ) &&
746
- key . length > bestMatch . length ) {
756
+ patternKeyCompare ( bestMatch , key ) === 1 ) {
747
757
bestMatch = key ;
758
+ bestMatchSubpath = StringPrototypeSlice ( name , key . length ) ;
748
759
}
749
760
}
750
761
751
762
if ( bestMatch ) {
752
763
const target = imports [ bestMatch ] ;
753
- const pattern = bestMatch [ bestMatch . length - 1 ] === '*' ;
754
- const subpath = StringPrototypeSubstr ( name , bestMatch . length -
755
- ( pattern ? 1 : 0 ) ) ;
756
- const resolved = resolvePackageTarget (
757
- packageJSONUrl , target , subpath , bestMatch , base , pattern , true ,
758
- conditions ) ;
764
+ const pattern = StringPrototypeIncludes ( bestMatch , '*' ) ;
765
+ const resolved = resolvePackageTarget ( packageJSONUrl , target ,
766
+ bestMatchSubpath , bestMatch ,
767
+ base , pattern , true ,
768
+ conditions ) ;
759
769
if ( resolved !== null ) {
760
770
if ( ! pattern )
761
771
emitFolderMapDeprecation ( bestMatch , packageJSONUrl , false , base ) ;
0 commit comments