@@ -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' ) ;
@@ -582,38 +581,49 @@ function packageImportsResolve(name, base, conditions) {
582
581
packageJSONUrl = pathToFileURL ( packageConfig . pjsonPath ) ;
583
582
const imports = packageConfig . imports ;
584
583
if ( imports ) {
585
- if ( ObjectPrototypeHasOwnProperty ( imports , name ) ) {
584
+ if ( ObjectPrototypeHasOwnProperty ( imports , name ) &&
585
+ ! StringPrototypeIncludes ( name , '*' ) &&
586
+ ! StringPrototypeEndsWith ( name , '/' ) ) {
586
587
const resolved = resolvePackageTarget (
587
588
packageJSONUrl , imports [ name ] , '' , name , base , false , true , conditions
588
589
) ;
589
590
if ( resolved !== null )
590
591
return { resolved, exact : true } ;
591
592
} else {
592
593
let bestMatch = '' ;
594
+ let bestMatchSubpath ;
593
595
const keys = ObjectGetOwnPropertyNames ( imports ) ;
594
596
for ( let i = 0 ; i < keys . length ; i ++ ) {
595
597
const key = keys [ i ] ;
596
- if ( key [ key . length - 1 ] === '*' &&
598
+ const patternIndex = StringPrototypeIndexOf ( key , '*' ) ;
599
+ if ( patternIndex !== - 1 &&
597
600
StringPrototypeStartsWith ( name ,
598
- StringPrototypeSlice ( key , 0 , - 1 ) ) &&
599
- name . length >= key . length &&
600
- key . length > bestMatch . length ) {
601
- bestMatch = key ;
601
+ StringPrototypeSlice ( key , 0 ,
602
+ patternIndex ) ) ) {
603
+ const patternTrailer = StringPrototypeSlice ( key , patternIndex + 1 ) ;
604
+ if ( name . length >= key . length &&
605
+ StringPrototypeEndsWith ( name , patternTrailer ) &&
606
+ patternKeyCompare ( bestMatch , key ) === 1 &&
607
+ StringPrototypeLastIndexOf ( key , '*' ) === patternIndex ) {
608
+ bestMatch = key ;
609
+ bestMatchSubpath = StringPrototypeSlice (
610
+ name , patternIndex , name . length - patternTrailer . length ) ;
611
+ }
602
612
} else if ( key [ key . length - 1 ] === '/' &&
603
613
StringPrototypeStartsWith ( name , key ) &&
604
- key . length > bestMatch . length ) {
614
+ patternKeyCompare ( bestMatch , key ) === 1 ) {
605
615
bestMatch = key ;
616
+ bestMatchSubpath = StringPrototypeSlice ( name , key . length ) ;
606
617
}
607
618
}
608
619
609
620
if ( bestMatch ) {
610
621
const target = imports [ bestMatch ] ;
611
- const pattern = bestMatch [ bestMatch . length - 1 ] === '*' ;
612
- const subpath = StringPrototypeSubstr ( name , bestMatch . length -
613
- ( pattern ? 1 : 0 ) ) ;
614
- const resolved = resolvePackageTarget (
615
- packageJSONUrl , target , subpath , bestMatch , base , pattern , true ,
616
- conditions ) ;
622
+ const pattern = StringPrototypeIncludes ( bestMatch , '*' ) ;
623
+ const resolved = resolvePackageTarget ( packageJSONUrl , target ,
624
+ bestMatchSubpath , bestMatch ,
625
+ base , pattern , true ,
626
+ conditions ) ;
617
627
if ( resolved !== null )
618
628
return { resolved, exact : pattern } ;
619
629
}
0 commit comments