@@ -16,6 +16,7 @@ const {
16
16
String,
17
17
StringPrototypeEndsWith,
18
18
StringPrototypeIndexOf,
19
+ StringPrototypeLastIndexOf,
19
20
StringPrototypeReplace,
20
21
StringPrototypeSlice,
21
22
StringPrototypeSplit,
@@ -114,7 +115,7 @@ function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) {
114
115
`Package ${ pkgPath } has a "main" field set to ${ JSONStringify ( main ) } , ` +
115
116
`excluding the full filename and extension to the resolved file at "${
116
117
StringPrototypeSlice ( path , pkgPath . length ) } ", imported from ${
117
- basePath } .\n Automatic extension resolution of the "main" field is` +
118
+ basePath } .\n Automatic extension resolution of the "main" field is ` +
118
119
'deprecated for ES modules.' ,
119
120
'DeprecationWarning' ,
120
121
'DEP0151'
@@ -607,7 +608,9 @@ function packageExportsResolve(
607
608
if ( isConditionalExportsMainSugar ( exports , packageJSONUrl , base ) )
608
609
exports = { '.' : exports } ;
609
610
610
- if ( ObjectPrototypeHasOwnProperty ( exports , packageSubpath ) ) {
611
+ if ( ObjectPrototypeHasOwnProperty ( exports , packageSubpath ) &&
612
+ StringPrototypeIndexOf ( packageSubpath , '*' ) === - 1 &&
613
+ ! StringPrototypeEndsWith ( packageSubpath , '/' ) ) {
611
614
const target = exports [ packageSubpath ] ;
612
615
const resolved = resolvePackageTarget (
613
616
packageJSONUrl , target , '' , packageSubpath , base , false , false , conditions
@@ -618,30 +621,39 @@ function packageExportsResolve(
618
621
}
619
622
620
623
let bestMatch = '' ;
624
+ let bestMatchSubpath ;
621
625
const keys = ObjectGetOwnPropertyNames ( exports ) ;
622
626
for ( let i = 0 ; i < keys . length ; i ++ ) {
623
627
const key = keys [ i ] ;
624
- if ( key [ key . length - 1 ] === '*' &&
628
+ const patternIndex = StringPrototypeIndexOf ( key , '*' ) ;
629
+ if ( patternIndex !== - 1 &&
625
630
StringPrototypeStartsWith ( packageSubpath ,
626
- StringPrototypeSlice ( key , 0 , - 1 ) ) &&
627
- packageSubpath . length >= key . length &&
628
- key . length > bestMatch . length ) {
629
- bestMatch = key ;
631
+ StringPrototypeSlice ( key , 0 , patternIndex ) ) ) {
632
+ const patternTrailer = StringPrototypeSlice ( key , patternIndex + 1 ) ;
633
+ if ( packageSubpath . length >= key . length &&
634
+ StringPrototypeEndsWith ( packageSubpath , patternTrailer ) &&
635
+ patternKeyCompare ( bestMatch , key ) === 1 &&
636
+ StringPrototypeLastIndexOf ( key , '*' ) === patternIndex ) {
637
+ bestMatch = key ;
638
+ bestMatchSubpath = StringPrototypeSlice (
639
+ packageSubpath , patternIndex ,
640
+ packageSubpath . length - patternTrailer . length ) ;
641
+ }
630
642
} else if ( key [ key . length - 1 ] === '/' &&
631
643
StringPrototypeStartsWith ( packageSubpath , key ) &&
632
- key . length > bestMatch . length ) {
644
+ packageSubpath . length > key . length &&
645
+ patternKeyCompare ( bestMatch , key ) === 1 ) {
633
646
bestMatch = key ;
647
+ bestMatchSubpath = StringPrototypeSlice ( packageSubpath , key . length ) ;
634
648
}
635
649
}
636
650
637
651
if ( bestMatch ) {
638
652
const target = exports [ bestMatch ] ;
639
- const pattern = bestMatch [ bestMatch . length - 1 ] === '*' ;
640
- const subpath = StringPrototypeSubstr ( packageSubpath , bestMatch . length -
641
- ( pattern ? 1 : 0 ) ) ;
642
- const resolved = resolvePackageTarget ( packageJSONUrl , target , subpath ,
643
- bestMatch , base , pattern , false ,
644
- conditions ) ;
653
+ const pattern = bestMatch . indexOf ( '*' ) !== - 1 ;
654
+ const resolved = resolvePackageTarget ( packageJSONUrl , target ,
655
+ bestMatchSubpath , bestMatch , base ,
656
+ pattern , false , conditions ) ;
645
657
if ( resolved === null || resolved === undefined )
646
658
throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
647
659
if ( ! pattern )
@@ -652,6 +664,20 @@ function packageExportsResolve(
652
664
throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
653
665
}
654
666
667
+ function patternKeyCompare ( a , b ) {
668
+ const aPatternIndex = StringPrototypeIndexOf ( a , '*' ) ;
669
+ const bPatternIndex = StringPrototypeIndexOf ( b , '*' ) ;
670
+ const baseLenA = aPatternIndex === - 1 ? a . length : aPatternIndex + 1 ;
671
+ const baseLenB = bPatternIndex === - 1 ? b . length : bPatternIndex + 1 ;
672
+ if ( baseLenA > baseLenB ) return - 1 ;
673
+ if ( baseLenB > baseLenA ) return 1 ;
674
+ if ( aPatternIndex === - 1 ) return 1 ;
675
+ if ( bPatternIndex === - 1 ) return - 1 ;
676
+ if ( a . length > b . length ) return - 1 ;
677
+ if ( b . length > a . length ) return 1 ;
678
+ return 0 ;
679
+ }
680
+
655
681
/**
656
682
* @param {string } name
657
683
* @param {string | URL | undefined } base
0 commit comments