@@ -15,7 +15,9 @@ const {
1515 SafeSet,
1616 String,
1717 StringPrototypeEndsWith,
18+ StringPrototypeIncludes,
1819 StringPrototypeIndexOf,
20+ StringPrototypeLastIndexOf,
1921 StringPrototypeReplace,
2022 StringPrototypeSlice,
2123 StringPrototypeSplit,
@@ -59,7 +61,6 @@ const userConditions = getOptionValue('--conditions');
5961const DEFAULT_CONDITIONS = ObjectFreeze ( [ 'node' , 'import' , ...userConditions ] ) ;
6062const DEFAULT_CONDITIONS_SET = new SafeSet ( DEFAULT_CONDITIONS ) ;
6163
62-
6364function getConditionsSet ( conditions ) {
6465 if ( conditions !== undefined && conditions !== DEFAULT_CONDITIONS ) {
6566 if ( ! ArrayIsArray ( conditions ) ) {
@@ -479,7 +480,9 @@ function packageExportsResolve(
479480 if ( isConditionalExportsMainSugar ( exports , packageJSONUrl , base ) )
480481 exports = { '.' : exports } ;
481482
482- if ( ObjectPrototypeHasOwnProperty ( exports , packageSubpath ) ) {
483+ if ( ObjectPrototypeHasOwnProperty ( exports , packageSubpath ) &&
484+ ! StringPrototypeIncludes ( packageSubpath , '*' ) &&
485+ ! StringPrototypeEndsWith ( packageSubpath , '/' ) ) {
483486 const target = exports [ packageSubpath ] ;
484487 const resolved = resolvePackageTarget (
485488 packageJSONUrl , target , '' , packageSubpath , base , false , false , conditions
@@ -490,30 +493,38 @@ function packageExportsResolve(
490493 }
491494
492495 let bestMatch = '' ;
496+ let bestMatchSubpath ;
493497 const keys = ObjectGetOwnPropertyNames ( exports ) ;
494498 for ( let i = 0 ; i < keys . length ; i ++ ) {
495499 const key = keys [ i ] ;
496- if ( key [ key . length - 1 ] === '*' &&
500+ const patternIndex = StringPrototypeIndexOf ( key , '*' ) ;
501+ if ( patternIndex !== - 1 &&
497502 StringPrototypeStartsWith ( packageSubpath ,
498- StringPrototypeSlice ( key , 0 , - 1 ) ) &&
499- packageSubpath . length >= key . length &&
500- key . length > bestMatch . length ) {
501- bestMatch = key ;
503+ StringPrototypeSlice ( key , 0 , patternIndex ) ) ) {
504+ const patternTrailer = StringPrototypeSlice ( key , patternIndex + 1 ) ;
505+ if ( packageSubpath . length >= key . length &&
506+ StringPrototypeEndsWith ( packageSubpath , patternTrailer ) &&
507+ patternKeyCompare ( bestMatch , key ) === 1 &&
508+ StringPrototypeLastIndexOf ( key , '*' ) === patternIndex ) {
509+ bestMatch = key ;
510+ bestMatchSubpath = StringPrototypeSlice (
511+ packageSubpath , patternIndex ,
512+ packageSubpath . length - patternTrailer . length ) ;
513+ }
502514 } else if ( key [ key . length - 1 ] === '/' &&
503515 StringPrototypeStartsWith ( packageSubpath , key ) &&
504- key . length > bestMatch . length ) {
516+ patternKeyCompare ( bestMatch , key ) === 1 ) {
505517 bestMatch = key ;
518+ bestMatchSubpath = StringPrototypeSlice ( packageSubpath , key . length ) ;
506519 }
507520 }
508521
509522 if ( bestMatch ) {
510523 const target = exports [ bestMatch ] ;
511- const pattern = bestMatch [ bestMatch . length - 1 ] === '*' ;
512- const subpath = StringPrototypeSubstr ( packageSubpath , bestMatch . length -
513- ( pattern ? 1 : 0 ) ) ;
514- const resolved = resolvePackageTarget ( packageJSONUrl , target , subpath ,
515- bestMatch , base , pattern , false ,
516- conditions ) ;
524+ const pattern = StringPrototypeIncludes ( bestMatch , '*' ) ;
525+ const resolved = resolvePackageTarget ( packageJSONUrl , target ,
526+ bestMatchSubpath , bestMatch , base ,
527+ pattern , false , conditions ) ;
517528 if ( resolved === null || resolved === undefined )
518529 throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
519530 return { resolved, exact : pattern } ;
@@ -522,6 +533,20 @@ function packageExportsResolve(
522533 throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
523534}
524535
536+ function patternKeyCompare ( a , b ) {
537+ const aPatternIndex = StringPrototypeIndexOf ( a , '*' ) ;
538+ const bPatternIndex = StringPrototypeIndexOf ( b , '*' ) ;
539+ const baseLenA = aPatternIndex === - 1 ? a . length : aPatternIndex + 1 ;
540+ const baseLenB = bPatternIndex === - 1 ? b . length : bPatternIndex + 1 ;
541+ if ( baseLenA > baseLenB ) return - 1 ;
542+ if ( baseLenB > baseLenA ) return 1 ;
543+ if ( aPatternIndex === - 1 ) return 1 ;
544+ if ( bPatternIndex === - 1 ) return - 1 ;
545+ if ( a . length > b . length ) return - 1 ;
546+ if ( b . length > a . length ) return 1 ;
547+ return 0 ;
548+ }
549+
525550function packageImportsResolve ( name , base , conditions ) {
526551 if ( name === '#' || StringPrototypeStartsWith ( name , '#/' ) ) {
527552 const reason = 'is not a valid internal imports specifier name' ;
0 commit comments