@@ -307,10 +307,11 @@ function throwInvalidPackageTarget(
307
307
}
308
308
309
309
const invalidSegmentRegEx = / ( ^ | \\ | \/ ) ( \. \. ? | n o d e _ m o d u l e s ) ( \\ | \/ | $ ) / ;
310
+ const patternRegEx = / \* / g;
310
311
311
312
function 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 ] !== '/' )
314
315
throwInvalidPackageTarget ( match , target , packageJSONUrl , internal , base ) ;
315
316
316
317
if ( ! StringPrototypeStartsWith ( target , './' ) ) {
@@ -321,8 +322,12 @@ function resolvePackageTargetString(
321
322
new URL ( target ) ;
322
323
isURL = true ;
323
324
} 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
+ }
326
331
}
327
332
throwInvalidPackageTarget ( match , target , packageJSONUrl , internal , base ) ;
328
333
}
@@ -342,6 +347,9 @@ function resolvePackageTargetString(
342
347
if ( RegExpPrototypeTest ( invalidSegmentRegEx , subpath ) )
343
348
throwInvalidSubpath ( match + subpath , packageJSONUrl , internal , base ) ;
344
349
350
+ if ( pattern )
351
+ return new URL ( StringPrototypeReplace ( resolved . href , patternRegEx ,
352
+ subpath ) ) ;
345
353
return new URL ( subpath , resolved ) ;
346
354
}
347
355
@@ -356,10 +364,10 @@ function isArrayIndex(key) {
356
364
}
357
365
358
366
function resolvePackageTarget ( packageJSONUrl , target , subpath , packageSubpath ,
359
- base , internal , conditions ) {
367
+ base , pattern , internal , conditions ) {
360
368
if ( typeof target === 'string' ) {
361
369
return resolvePackageTargetString (
362
- target , subpath , packageSubpath , packageJSONUrl , base , internal ,
370
+ target , subpath , packageSubpath , packageJSONUrl , base , pattern , internal ,
363
371
conditions ) ;
364
372
} else if ( ArrayIsArray ( target ) ) {
365
373
if ( target . length === 0 )
@@ -371,8 +379,8 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath,
371
379
let resolved ;
372
380
try {
373
381
resolved = resolvePackageTarget (
374
- packageJSONUrl , targetItem , subpath , packageSubpath , base , internal ,
375
- conditions ) ;
382
+ packageJSONUrl , targetItem , subpath , packageSubpath , base , pattern ,
383
+ internal , conditions ) ;
376
384
} catch ( e ) {
377
385
lastException = e ;
378
386
if ( e . code === 'ERR_INVALID_PACKAGE_TARGET' )
@@ -406,7 +414,7 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath,
406
414
const conditionalTarget = target [ key ] ;
407
415
const resolved = resolvePackageTarget (
408
416
packageJSONUrl , conditionalTarget , subpath , packageSubpath , base ,
409
- internal , conditions ) ;
417
+ pattern , internal , conditions ) ;
410
418
if ( resolved === undefined )
411
419
continue ;
412
420
return resolved ;
@@ -460,7 +468,7 @@ function packageExportsResolve(
460
468
if ( ObjectPrototypeHasOwnProperty ( exports , packageSubpath ) ) {
461
469
const target = exports [ packageSubpath ] ;
462
470
const resolved = resolvePackageTarget (
463
- packageJSONUrl , target , '' , packageSubpath , base , false , conditions
471
+ packageJSONUrl , target , '' , packageSubpath , base , false , false , conditions
464
472
) ;
465
473
if ( resolved === null || resolved === undefined )
466
474
throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
@@ -471,7 +479,13 @@ function packageExportsResolve(
471
479
const keys = ObjectGetOwnPropertyNames ( exports ) ;
472
480
for ( let i = 0 ; i < keys . length ; i ++ ) {
473
481
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 ] === '/' &&
475
489
StringPrototypeStartsWith ( packageSubpath , key ) &&
476
490
key . length > bestMatch . length ) {
477
491
bestMatch = key ;
@@ -480,12 +494,15 @@ function packageExportsResolve(
480
494
481
495
if ( bestMatch ) {
482
496
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 ) ) ;
484
500
const resolved = resolvePackageTarget ( packageJSONUrl , target , subpath ,
485
- bestMatch , base , false , conditions ) ;
501
+ bestMatch , base , pattern , false ,
502
+ conditions ) ;
486
503
if ( resolved === null || resolved === undefined )
487
504
throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
488
- return { resolved, exact : false } ;
505
+ return { resolved, exact : pattern } ;
489
506
}
490
507
491
508
throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
@@ -504,7 +521,7 @@ function packageImportsResolve(name, base, conditions) {
504
521
if ( imports ) {
505
522
if ( ObjectPrototypeHasOwnProperty ( imports , name ) ) {
506
523
const resolved = resolvePackageTarget (
507
- packageJSONUrl , imports [ name ] , '' , name , base , true , conditions
524
+ packageJSONUrl , imports [ name ] , '' , name , base , false , true , conditions
508
525
) ;
509
526
if ( resolved !== null )
510
527
return { resolved, exact : true } ;
@@ -513,7 +530,13 @@ function packageImportsResolve(name, base, conditions) {
513
530
const keys = ObjectGetOwnPropertyNames ( imports ) ;
514
531
for ( let i = 0 ; i < keys . length ; i ++ ) {
515
532
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 ] === '/' &&
517
540
StringPrototypeStartsWith ( name , key ) &&
518
541
key . length > bestMatch . length ) {
519
542
bestMatch = key ;
@@ -522,11 +545,14 @@ function packageImportsResolve(name, base, conditions) {
522
545
523
546
if ( bestMatch ) {
524
547
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 ) ) ;
526
551
const resolved = resolvePackageTarget (
527
- packageJSONUrl , target , subpath , bestMatch , base , true , conditions ) ;
552
+ packageJSONUrl , target , subpath , bestMatch , base , pattern , true ,
553
+ conditions ) ;
528
554
if ( resolved !== null )
529
- return { resolved, exact : false } ;
555
+ return { resolved, exact : pattern } ;
530
556
}
531
557
}
532
558
}
0 commit comments