@@ -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' ) ;
@@ -559,38 +558,49 @@ function packageImportsResolve(name, base, conditions) {
559
558
packageJSONUrl = pathToFileURL ( packageConfig . pjsonPath ) ;
560
559
const imports = packageConfig . imports ;
561
560
if ( imports ) {
562
- if ( ObjectPrototypeHasOwnProperty ( imports , name ) ) {
561
+ if ( ObjectPrototypeHasOwnProperty ( imports , name ) &&
562
+ ! StringPrototypeIncludes ( name , '*' ) &&
563
+ ! StringPrototypeEndsWith ( name , '/' ) ) {
563
564
const resolved = resolvePackageTarget (
564
565
packageJSONUrl , imports [ name ] , '' , name , base , false , true , conditions
565
566
) ;
566
567
if ( resolved !== null )
567
568
return { resolved, exact : true } ;
568
569
} else {
569
570
let bestMatch = '' ;
571
+ let bestMatchSubpath ;
570
572
const keys = ObjectGetOwnPropertyNames ( imports ) ;
571
573
for ( let i = 0 ; i < keys . length ; i ++ ) {
572
574
const key = keys [ i ] ;
573
- if ( key [ key . length - 1 ] === '*' &&
575
+ const patternIndex = StringPrototypeIndexOf ( key , '*' ) ;
576
+ if ( patternIndex !== - 1 &&
574
577
StringPrototypeStartsWith ( name ,
575
- StringPrototypeSlice ( key , 0 , - 1 ) ) &&
576
- name . length >= key . length &&
577
- key . length > bestMatch . length ) {
578
- bestMatch = key ;
578
+ StringPrototypeSlice ( key , 0 ,
579
+ patternIndex ) ) ) {
580
+ const patternTrailer = StringPrototypeSlice ( key , patternIndex + 1 ) ;
581
+ if ( name . length >= key . length &&
582
+ StringPrototypeEndsWith ( name , patternTrailer ) &&
583
+ patternKeyCompare ( bestMatch , key ) === 1 &&
584
+ StringPrototypeLastIndexOf ( key , '*' ) === patternIndex ) {
585
+ bestMatch = key ;
586
+ bestMatchSubpath = StringPrototypeSlice (
587
+ name , patternIndex , name . length - patternTrailer . length ) ;
588
+ }
579
589
} else if ( key [ key . length - 1 ] === '/' &&
580
590
StringPrototypeStartsWith ( name , key ) &&
581
- key . length > bestMatch . length ) {
591
+ patternKeyCompare ( bestMatch , key ) === 1 ) {
582
592
bestMatch = key ;
593
+ bestMatchSubpath = StringPrototypeSlice ( name , key . length ) ;
583
594
}
584
595
}
585
596
586
597
if ( bestMatch ) {
587
598
const target = imports [ bestMatch ] ;
588
- const pattern = bestMatch [ bestMatch . length - 1 ] === '*' ;
589
- const subpath = StringPrototypeSubstr ( name , bestMatch . length -
590
- ( pattern ? 1 : 0 ) ) ;
591
- const resolved = resolvePackageTarget (
592
- packageJSONUrl , target , subpath , bestMatch , base , pattern , true ,
593
- conditions ) ;
599
+ const pattern = StringPrototypeIncludes ( bestMatch , '*' ) ;
600
+ const resolved = resolvePackageTarget ( packageJSONUrl , target ,
601
+ bestMatchSubpath , bestMatch ,
602
+ base , pattern , true ,
603
+ conditions ) ;
594
604
if ( resolved !== null )
595
605
return { resolved, exact : pattern } ;
596
606
}
0 commit comments