@@ -307,10 +307,11 @@ function throwInvalidPackageTarget(
307307}
308308
309309const invalidSegmentRegEx = / ( ^ | \\ | \/ ) ( \. \. ? | n o d e _ m o d u l e s ) ( \\ | \/ | $ ) / ;
310+ const patternRegEx = / \* / g;
310311
311312function resolvePackageTargetString (
312- target , subpath , match , packageJSONUrl , base , internal , conditions ) {
313- if ( subpath !== '' && target [ target . length - 1 ] !== '/' )
313+ target , subpath , match , packageJSONUrl , base , pattern , internal , conditions ) {
314+ if ( subpath !== '' && ! pattern && target [ target . length - 1 ] !== '/' )
314315 throwInvalidPackageTarget ( match , target , packageJSONUrl , internal , base ) ;
315316
316317 if ( ! StringPrototypeStartsWith ( target , './' ) ) {
@@ -321,8 +322,12 @@ function resolvePackageTargetString(
321322 new URL ( target ) ;
322323 isURL = true ;
323324 } catch { }
324- if ( ! isURL )
325- return packageResolve ( target + subpath , packageJSONUrl , conditions ) ;
325+ if ( ! isURL ) {
326+ const exportTarget = pattern ?
327+ StringPrototypeReplace ( target , patternRegEx , subpath ) :
328+ target + subpath ;
329+ return packageResolve ( exportTarget , packageJSONUrl , conditions ) ;
330+ }
326331 }
327332 throwInvalidPackageTarget ( match , target , packageJSONUrl , internal , base ) ;
328333 }
@@ -342,6 +347,9 @@ function resolvePackageTargetString(
342347 if ( RegExpPrototypeTest ( invalidSegmentRegEx , subpath ) )
343348 throwInvalidSubpath ( match + subpath , packageJSONUrl , internal , base ) ;
344349
350+ if ( pattern )
351+ return new URL ( StringPrototypeReplace ( resolved . href , patternRegEx ,
352+ subpath ) ) ;
345353 return new URL ( subpath , resolved ) ;
346354}
347355
@@ -356,10 +364,10 @@ function isArrayIndex(key) {
356364}
357365
358366function resolvePackageTarget ( packageJSONUrl , target , subpath , packageSubpath ,
359- base , internal , conditions ) {
367+ base , pattern , internal , conditions ) {
360368 if ( typeof target === 'string' ) {
361369 return resolvePackageTargetString (
362- target , subpath , packageSubpath , packageJSONUrl , base , internal ,
370+ target , subpath , packageSubpath , packageJSONUrl , base , pattern , internal ,
363371 conditions ) ;
364372 } else if ( ArrayIsArray ( target ) ) {
365373 if ( target . length === 0 )
@@ -371,8 +379,8 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath,
371379 let resolved ;
372380 try {
373381 resolved = resolvePackageTarget (
374- packageJSONUrl , targetItem , subpath , packageSubpath , base , internal ,
375- conditions ) ;
382+ packageJSONUrl , targetItem , subpath , packageSubpath , base , pattern ,
383+ internal , conditions ) ;
376384 } catch ( e ) {
377385 lastException = e ;
378386 if ( e . code === 'ERR_INVALID_PACKAGE_TARGET' )
@@ -406,7 +414,7 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath,
406414 const conditionalTarget = target [ key ] ;
407415 const resolved = resolvePackageTarget (
408416 packageJSONUrl , conditionalTarget , subpath , packageSubpath , base ,
409- internal , conditions ) ;
417+ pattern , internal , conditions ) ;
410418 if ( resolved === undefined )
411419 continue ;
412420 return resolved ;
@@ -460,7 +468,7 @@ function packageExportsResolve(
460468 if ( ObjectPrototypeHasOwnProperty ( exports , packageSubpath ) ) {
461469 const target = exports [ packageSubpath ] ;
462470 const resolved = resolvePackageTarget (
463- packageJSONUrl , target , '' , packageSubpath , base , false , conditions
471+ packageJSONUrl , target , '' , packageSubpath , base , false , false , conditions
464472 ) ;
465473 if ( resolved === null || resolved === undefined )
466474 throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
@@ -471,7 +479,13 @@ function packageExportsResolve(
471479 const keys = ObjectGetOwnPropertyNames ( exports ) ;
472480 for ( let i = 0 ; i < keys . length ; i ++ ) {
473481 const key = keys [ i ] ;
474- if ( key [ key . length - 1 ] === '/' &&
482+ if ( key [ key . length - 1 ] === '*' &&
483+ StringPrototypeStartsWith ( packageSubpath ,
484+ StringPrototypeSlice ( key , 0 , - 1 ) ) &&
485+ packageSubpath . length >= key . length &&
486+ key . length > bestMatch . length ) {
487+ bestMatch = key ;
488+ } else if ( key [ key . length - 1 ] === '/' &&
475489 StringPrototypeStartsWith ( packageSubpath , key ) &&
476490 key . length > bestMatch . length ) {
477491 bestMatch = key ;
@@ -480,12 +494,15 @@ function packageExportsResolve(
480494
481495 if ( bestMatch ) {
482496 const target = exports [ bestMatch ] ;
483- const subpath = StringPrototypeSubstr ( packageSubpath , bestMatch . length ) ;
497+ const pattern = bestMatch [ bestMatch . length - 1 ] === '*' ;
498+ const subpath = StringPrototypeSubstr ( packageSubpath , bestMatch . length -
499+ ( pattern ? 1 : 0 ) ) ;
484500 const resolved = resolvePackageTarget ( packageJSONUrl , target , subpath ,
485- bestMatch , base , false , conditions ) ;
501+ bestMatch , base , pattern , false ,
502+ conditions ) ;
486503 if ( resolved === null || resolved === undefined )
487504 throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
488- return { resolved, exact : false } ;
505+ return { resolved, exact : pattern } ;
489506 }
490507
491508 throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
@@ -504,7 +521,7 @@ function packageImportsResolve(name, base, conditions) {
504521 if ( imports ) {
505522 if ( ObjectPrototypeHasOwnProperty ( imports , name ) ) {
506523 const resolved = resolvePackageTarget (
507- packageJSONUrl , imports [ name ] , '' , name , base , true , conditions
524+ packageJSONUrl , imports [ name ] , '' , name , base , false , true , conditions
508525 ) ;
509526 if ( resolved !== null )
510527 return { resolved, exact : true } ;
@@ -513,7 +530,13 @@ function packageImportsResolve(name, base, conditions) {
513530 const keys = ObjectGetOwnPropertyNames ( imports ) ;
514531 for ( let i = 0 ; i < keys . length ; i ++ ) {
515532 const key = keys [ i ] ;
516- if ( key [ key . length - 1 ] === '/' &&
533+ if ( key [ key . length - 1 ] === '*' &&
534+ StringPrototypeStartsWith ( name ,
535+ StringPrototypeSlice ( key , 0 , - 1 ) ) &&
536+ name . length >= key . length &&
537+ key . length > bestMatch . length ) {
538+ bestMatch = key ;
539+ } else if ( key [ key . length - 1 ] === '/' &&
517540 StringPrototypeStartsWith ( name , key ) &&
518541 key . length > bestMatch . length ) {
519542 bestMatch = key ;
@@ -522,11 +545,14 @@ function packageImportsResolve(name, base, conditions) {
522545
523546 if ( bestMatch ) {
524547 const target = imports [ bestMatch ] ;
525- const subpath = StringPrototypeSubstr ( name , bestMatch . length ) ;
548+ const pattern = bestMatch [ bestMatch . length - 1 ] === '*' ;
549+ const subpath = StringPrototypeSubstr ( name , bestMatch . length -
550+ ( pattern ? 1 : 0 ) ) ;
526551 const resolved = resolvePackageTarget (
527- packageJSONUrl , target , subpath , bestMatch , base , true , conditions ) ;
552+ packageJSONUrl , target , subpath , bestMatch , base , pattern , true ,
553+ conditions ) ;
528554 if ( resolved !== null )
529- return { resolved, exact : false } ;
555+ return { resolved, exact : pattern } ;
530556 }
531557 }
532558 }
0 commit comments