@@ -312,10 +312,11 @@ function throwInvalidPackageTarget(
312312}
313313
314314const invalidSegmentRegEx = / ( ^ | \\ | \/ ) ( \. \. ? | n o d e _ m o d u l e s ) ( \\ | \/ | $ ) / ;
315+ const patternRegEx = / \* / g;
315316
316317function resolvePackageTargetString (
317- target , subpath , match , packageJSONUrl , base , internal , conditions ) {
318- if ( subpath !== '' && target [ target . length - 1 ] !== '/' )
318+ target , subpath , match , packageJSONUrl , base , pattern , internal , conditions ) {
319+ if ( subpath !== '' && ! pattern && target [ target . length - 1 ] !== '/' )
319320 throwInvalidPackageTarget ( match , target , packageJSONUrl , internal , base ) ;
320321
321322 if ( ! StringPrototypeStartsWith ( target , './' ) ) {
@@ -326,8 +327,12 @@ function resolvePackageTargetString(
326327 new URL ( target ) ;
327328 isURL = true ;
328329 } catch { }
329- if ( ! isURL )
330- return packageResolve ( target + subpath , packageJSONUrl , conditions ) ;
330+ if ( ! isURL ) {
331+ const exportTarget = pattern ?
332+ StringPrototypeReplace ( target , patternRegEx , subpath ) :
333+ target + subpath ;
334+ return packageResolve ( exportTarget , packageJSONUrl , conditions ) ;
335+ }
331336 }
332337 throwInvalidPackageTarget ( match , target , packageJSONUrl , internal , base ) ;
333338 }
@@ -347,6 +352,9 @@ function resolvePackageTargetString(
347352 if ( RegExpPrototypeTest ( invalidSegmentRegEx , subpath ) )
348353 throwInvalidSubpath ( match + subpath , packageJSONUrl , internal , base ) ;
349354
355+ if ( pattern )
356+ return new URL ( StringPrototypeReplace ( resolved . href , patternRegEx ,
357+ subpath ) ) ;
350358 return new URL ( subpath , resolved ) ;
351359}
352360
@@ -361,10 +369,10 @@ function isArrayIndex(key) {
361369}
362370
363371function resolvePackageTarget ( packageJSONUrl , target , subpath , packageSubpath ,
364- base , internal , conditions ) {
372+ base , pattern , internal , conditions ) {
365373 if ( typeof target === 'string' ) {
366374 return resolvePackageTargetString (
367- target , subpath , packageSubpath , packageJSONUrl , base , internal ,
375+ target , subpath , packageSubpath , packageJSONUrl , base , pattern , internal ,
368376 conditions ) ;
369377 } else if ( ArrayIsArray ( target ) ) {
370378 if ( target . length === 0 )
@@ -376,8 +384,8 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath,
376384 let resolved ;
377385 try {
378386 resolved = resolvePackageTarget (
379- packageJSONUrl , targetItem , subpath , packageSubpath , base , internal ,
380- conditions ) ;
387+ packageJSONUrl , targetItem , subpath , packageSubpath , base , pattern ,
388+ internal , conditions ) ;
381389 } catch ( e ) {
382390 lastException = e ;
383391 if ( e . code === 'ERR_INVALID_PACKAGE_TARGET' )
@@ -411,7 +419,7 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath,
411419 const conditionalTarget = target [ key ] ;
412420 const resolved = resolvePackageTarget (
413421 packageJSONUrl , conditionalTarget , subpath , packageSubpath , base ,
414- internal , conditions ) ;
422+ pattern , internal , conditions ) ;
415423 if ( resolved === undefined )
416424 continue ;
417425 return resolved ;
@@ -465,7 +473,7 @@ function packageExportsResolve(
465473 if ( ObjectPrototypeHasOwnProperty ( exports , packageSubpath ) ) {
466474 const target = exports [ packageSubpath ] ;
467475 const resolved = resolvePackageTarget (
468- packageJSONUrl , target , '' , packageSubpath , base , false , conditions
476+ packageJSONUrl , target , '' , packageSubpath , base , false , false , conditions
469477 ) ;
470478 if ( resolved === null || resolved === undefined )
471479 throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
@@ -476,7 +484,13 @@ function packageExportsResolve(
476484 const keys = ObjectGetOwnPropertyNames ( exports ) ;
477485 for ( let i = 0 ; i < keys . length ; i ++ ) {
478486 const key = keys [ i ] ;
479- if ( key [ key . length - 1 ] === '/' &&
487+ if ( key [ key . length - 1 ] === '*' &&
488+ StringPrototypeStartsWith ( packageSubpath ,
489+ StringPrototypeSlice ( key , 0 , - 1 ) ) &&
490+ packageSubpath . length >= key . length &&
491+ key . length > bestMatch . length ) {
492+ bestMatch = key ;
493+ } else if ( key [ key . length - 1 ] === '/' &&
480494 StringPrototypeStartsWith ( packageSubpath , key ) &&
481495 key . length > bestMatch . length ) {
482496 bestMatch = key ;
@@ -485,12 +499,15 @@ function packageExportsResolve(
485499
486500 if ( bestMatch ) {
487501 const target = exports [ bestMatch ] ;
488- const subpath = StringPrototypeSubstr ( packageSubpath , bestMatch . length ) ;
502+ const pattern = bestMatch [ bestMatch . length - 1 ] === '*' ;
503+ const subpath = StringPrototypeSubstr ( packageSubpath , bestMatch . length -
504+ ( pattern ? 1 : 0 ) ) ;
489505 const resolved = resolvePackageTarget ( packageJSONUrl , target , subpath ,
490- bestMatch , base , false , conditions ) ;
506+ bestMatch , base , pattern , false ,
507+ conditions ) ;
491508 if ( resolved === null || resolved === undefined )
492509 throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
493- return { resolved, exact : false } ;
510+ return { resolved, exact : pattern } ;
494511 }
495512
496513 throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
@@ -509,7 +526,7 @@ function packageImportsResolve(name, base, conditions) {
509526 if ( imports ) {
510527 if ( ObjectPrototypeHasOwnProperty ( imports , name ) ) {
511528 const resolved = resolvePackageTarget (
512- packageJSONUrl , imports [ name ] , '' , name , base , true , conditions
529+ packageJSONUrl , imports [ name ] , '' , name , base , false , true , conditions
513530 ) ;
514531 if ( resolved !== null )
515532 return { resolved, exact : true } ;
@@ -518,7 +535,13 @@ function packageImportsResolve(name, base, conditions) {
518535 const keys = ObjectGetOwnPropertyNames ( imports ) ;
519536 for ( let i = 0 ; i < keys . length ; i ++ ) {
520537 const key = keys [ i ] ;
521- if ( key [ key . length - 1 ] === '/' &&
538+ if ( key [ key . length - 1 ] === '*' &&
539+ StringPrototypeStartsWith ( name ,
540+ StringPrototypeSlice ( key , 0 , - 1 ) ) &&
541+ name . length >= key . length &&
542+ key . length > bestMatch . length ) {
543+ bestMatch = key ;
544+ } else if ( key [ key . length - 1 ] === '/' &&
522545 StringPrototypeStartsWith ( name , key ) &&
523546 key . length > bestMatch . length ) {
524547 bestMatch = key ;
@@ -527,11 +550,14 @@ function packageImportsResolve(name, base, conditions) {
527550
528551 if ( bestMatch ) {
529552 const target = imports [ bestMatch ] ;
530- const subpath = StringPrototypeSubstr ( name , bestMatch . length ) ;
553+ const pattern = bestMatch [ bestMatch . length - 1 ] === '*' ;
554+ const subpath = StringPrototypeSubstr ( name , bestMatch . length -
555+ ( pattern ? 1 : 0 ) ) ;
531556 const resolved = resolvePackageTarget (
532- packageJSONUrl , target , subpath , bestMatch , base , true , conditions ) ;
557+ packageJSONUrl , target , subpath , bestMatch , base , pattern , true ,
558+ conditions ) ;
533559 if ( resolved !== null )
534- return { resolved, exact : false } ;
560+ return { resolved, exact : pattern } ;
535561 }
536562 }
537563 }
0 commit comments