@@ -473,6 +473,23 @@ const patternRegEx = /\*/g;
473
473
474
474
function resolvePackageTargetString (
475
475
target , subpath , match , packageJSONUrl , base , pattern , internal , conditions ) {
476
+
477
+ const composeResult = ( resolved ) => {
478
+ let format ;
479
+ try {
480
+ format = getPackageType ( resolved ) ;
481
+ } catch ( err ) {
482
+ if ( err . code === 'ERR_INVALID_FILE_URL_PATH' ) {
483
+ const invalidModuleErr = new ERR_INVALID_MODULE_SPECIFIER (
484
+ resolved , 'must not include encoded "/" or "\\" characters' , base ) ;
485
+ invalidModuleErr . cause = err ;
486
+ throw invalidModuleErr ;
487
+ }
488
+ throw err ;
489
+ }
490
+ return { resolved, ...( format !== 'none' ) && { format } } ;
491
+ } ;
492
+
476
493
if ( subpath !== '' && ! pattern && target [ target . length - 1 ] !== '/' )
477
494
throwInvalidPackageTarget ( match , target , packageJSONUrl , internal , base ) ;
478
495
@@ -488,7 +505,8 @@ function resolvePackageTargetString(
488
505
const exportTarget = pattern ?
489
506
RegExpPrototypeSymbolReplace ( patternRegEx , target , ( ) => subpath ) :
490
507
target + subpath ;
491
- return packageResolve ( exportTarget , packageJSONUrl , conditions ) ;
508
+ return packageResolve (
509
+ exportTarget , packageJSONUrl , conditions ) ;
492
510
}
493
511
}
494
512
throwInvalidPackageTarget ( match , target , packageJSONUrl , internal , base ) ;
@@ -504,15 +522,18 @@ function resolvePackageTargetString(
504
522
if ( ! StringPrototypeStartsWith ( resolvedPath , packagePath ) )
505
523
throwInvalidPackageTarget ( match , target , packageJSONUrl , internal , base ) ;
506
524
507
- if ( subpath === '' ) return resolved ;
525
+ if ( subpath === '' ) return composeResult ( resolved ) ;
508
526
509
527
if ( RegExpPrototypeTest ( invalidSegmentRegEx , subpath ) )
510
528
throwInvalidSubpath ( match + subpath , packageJSONUrl , internal , base ) ;
511
529
512
- if ( pattern )
513
- return new URL ( RegExpPrototypeSymbolReplace ( patternRegEx , resolved . href ,
514
- ( ) => subpath ) ) ;
515
- return new URL ( subpath , resolved ) ;
530
+ if ( pattern ) {
531
+ return composeResult ( new URL ( RegExpPrototypeSymbolReplace ( patternRegEx ,
532
+ resolved . href ,
533
+ ( ) => subpath ) ) ) ;
534
+ }
535
+
536
+ return composeResult ( new URL ( subpath , resolved ) ) ;
516
537
}
517
538
518
539
/**
@@ -538,9 +559,9 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath,
538
559
let lastException ;
539
560
for ( let i = 0 ; i < target . length ; i ++ ) {
540
561
const targetItem = target [ i ] ;
541
- let resolved ;
562
+ let resolveResult ;
542
563
try {
543
- resolved = resolvePackageTarget (
564
+ resolveResult = resolvePackageTarget (
544
565
packageJSONUrl , targetItem , subpath , packageSubpath , base , pattern ,
545
566
internal , conditions ) ;
546
567
} catch ( e ) {
@@ -549,13 +570,13 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath,
549
570
continue ;
550
571
throw e ;
551
572
}
552
- if ( resolved === undefined )
573
+ if ( resolveResult === undefined )
553
574
continue ;
554
- if ( resolved === null ) {
575
+ if ( resolveResult === null ) {
555
576
lastException = null ;
556
577
continue ;
557
578
}
558
- return resolved ;
579
+ return resolveResult ;
559
580
}
560
581
if ( lastException === undefined || lastException === null )
561
582
return lastException ;
@@ -574,12 +595,12 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath,
574
595
const key = keys [ i ] ;
575
596
if ( key === 'default' || conditions . has ( key ) ) {
576
597
const conditionalTarget = target [ key ] ;
577
- const resolved = resolvePackageTarget (
598
+ const resolveResult = resolvePackageTarget (
578
599
packageJSONUrl , conditionalTarget , subpath , packageSubpath , base ,
579
600
pattern , internal , conditions ) ;
580
- if ( resolved === undefined )
601
+ if ( resolveResult === undefined )
581
602
continue ;
582
- return resolved ;
603
+ return resolveResult ;
583
604
}
584
605
}
585
606
return undefined ;
@@ -638,12 +659,15 @@ function packageExportsResolve(
638
659
! StringPrototypeIncludes ( packageSubpath , '*' ) &&
639
660
! StringPrototypeEndsWith ( packageSubpath , '/' ) ) {
640
661
const target = exports [ packageSubpath ] ;
641
- const resolved = resolvePackageTarget (
662
+ const resolveResult = resolvePackageTarget (
642
663
packageJSONUrl , target , '' , packageSubpath , base , false , false , conditions
643
664
) ;
644
- if ( resolved === null || resolved === undefined )
665
+
666
+ if ( resolveResult == null ) {
645
667
throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
646
- return { resolved, exact : true } ;
668
+ }
669
+
670
+ return { ...resolveResult , exact : true } ;
647
671
}
648
672
649
673
let bestMatch = '' ;
@@ -679,14 +703,25 @@ function packageExportsResolve(
679
703
if ( bestMatch ) {
680
704
const target = exports [ bestMatch ] ;
681
705
const pattern = StringPrototypeIncludes ( bestMatch , '*' ) ;
682
- const resolved = resolvePackageTarget ( packageJSONUrl , target ,
683
- bestMatchSubpath , bestMatch , base ,
684
- pattern , false , conditions ) ;
685
- if ( resolved === null || resolved === undefined )
706
+ const resolveResult = resolvePackageTarget (
707
+ packageJSONUrl ,
708
+ target ,
709
+ bestMatchSubpath ,
710
+ bestMatch ,
711
+ base ,
712
+ pattern ,
713
+ false ,
714
+ conditions ) ;
715
+
716
+ if ( resolveResult == null ) {
686
717
throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
687
- if ( ! pattern )
718
+ }
719
+
720
+ if ( ! pattern ) {
688
721
emitFolderMapDeprecation ( bestMatch , packageJSONUrl , true , base ) ;
689
- return { resolved, exact : pattern } ;
722
+ }
723
+
724
+ return { ...resolveResult , exact : pattern } ;
690
725
}
691
726
692
727
throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
@@ -726,11 +761,12 @@ function packageImportsResolve(name, base, conditions) {
726
761
if ( ObjectPrototypeHasOwnProperty ( imports , name ) &&
727
762
! StringPrototypeIncludes ( name , '*' ) &&
728
763
! StringPrototypeEndsWith ( name , '/' ) ) {
729
- const resolved = resolvePackageTarget (
764
+ const resolveResult = resolvePackageTarget (
730
765
packageJSONUrl , imports [ name ] , '' , name , base , false , true , conditions
731
766
) ;
732
- if ( resolved !== null )
733
- return { resolved, exact : true } ;
767
+ if ( resolveResult != null ) {
768
+ return { resolved : resolveResult . resolved , exact : true } ;
769
+ }
734
770
} else {
735
771
let bestMatch = '' ;
736
772
let bestMatchSubpath ;
@@ -762,14 +798,15 @@ function packageImportsResolve(name, base, conditions) {
762
798
if ( bestMatch ) {
763
799
const target = imports [ bestMatch ] ;
764
800
const pattern = StringPrototypeIncludes ( bestMatch , '*' ) ;
765
- const resolved = resolvePackageTarget ( packageJSONUrl , target ,
766
- bestMatchSubpath , bestMatch ,
767
- base , pattern , true ,
768
- conditions ) ;
769
- if ( resolved !== null ) {
801
+ const resolveResult = resolvePackageTarget (
802
+ packageJSONUrl , target ,
803
+ bestMatchSubpath , bestMatch ,
804
+ base , pattern , true ,
805
+ conditions ) ;
806
+ if ( resolveResult !== null ) {
770
807
if ( ! pattern )
771
808
emitFolderMapDeprecation ( bestMatch , packageJSONUrl , false , base ) ;
772
- return { resolved, exact : pattern } ;
809
+ return { resolved : resolveResult . resolved , exact : pattern } ;
773
810
}
774
811
}
775
812
}
@@ -833,7 +870,7 @@ function parsePackageName(specifier, base) {
833
870
* @param {string } specifier
834
871
* @param {string | URL | undefined } base
835
872
* @param {Set<string> } conditions
836
- * @returns {URL }
873
+ * @returns {resolved: URL, format? : string }
837
874
*/
838
875
function packageResolve ( specifier , base , conditions ) {
839
876
const { packageName, packageSubpath, isScoped } =
@@ -846,8 +883,7 @@ function packageResolve(specifier, base, conditions) {
846
883
if ( packageConfig . name === packageName &&
847
884
packageConfig . exports !== undefined && packageConfig . exports !== null ) {
848
885
return packageExportsResolve (
849
- packageJSONUrl , packageSubpath , packageConfig , base , conditions
850
- ) . resolved ;
886
+ packageJSONUrl , packageSubpath , packageConfig , base , conditions ) ;
851
887
}
852
888
}
853
889
@@ -869,13 +905,26 @@ function packageResolve(specifier, base, conditions) {
869
905
870
906
// Package match.
871
907
const packageConfig = getPackageConfig ( packageJSONPath , specifier , base ) ;
872
- if ( packageConfig . exports !== undefined && packageConfig . exports !== null )
908
+ if ( packageConfig . exports !== undefined && packageConfig . exports !== null ) {
873
909
return packageExportsResolve (
874
- packageJSONUrl , packageSubpath , packageConfig , base , conditions
875
- ) . resolved ;
876
- if ( packageSubpath === '.' )
877
- return legacyMainResolve ( packageJSONUrl , packageConfig , base ) ;
878
- return new URL ( packageSubpath , packageJSONUrl ) ;
910
+ packageJSONUrl , packageSubpath , packageConfig , base , conditions ) ;
911
+ }
912
+
913
+ if ( packageSubpath === '.' ) {
914
+ return {
915
+ resolved : legacyMainResolve (
916
+ packageJSONUrl ,
917
+ packageConfig ,
918
+ base ) ,
919
+ ...( packageConfig . type !== 'none' ) && { format : packageConfig . type }
920
+ } ;
921
+ }
922
+
923
+ return {
924
+ resolved : new URL ( packageSubpath , packageJSONUrl ) ,
925
+ ...( packageConfig . type !== 'none' ) && { format : packageConfig . type }
926
+ } ;
927
+
879
928
// Cross-platform root check.
880
929
} while ( packageJSONPath . length !== lastPath . length ) ;
881
930
@@ -912,12 +961,13 @@ function shouldBeTreatedAsRelativeOrAbsolutePath(specifier) {
912
961
* @param {string } specifier
913
962
* @param {string | URL | undefined } base
914
963
* @param {Set<string> } conditions
915
- * @returns {URL }
964
+ * @returns {url: URL, format?: string }
916
965
*/
917
966
function moduleResolve ( specifier , base , conditions ) {
918
967
// Order swapped from spec for minor perf gain.
919
968
// Ok since relative URLs cannot parse as URLs.
920
969
let resolved ;
970
+ let format ;
921
971
if ( shouldBeTreatedAsRelativeOrAbsolutePath ( specifier ) ) {
922
972
resolved = new URL ( specifier , base ) ;
923
973
} else if ( specifier [ 0 ] === '#' ) {
@@ -926,10 +976,13 @@ function moduleResolve(specifier, base, conditions) {
926
976
try {
927
977
resolved = new URL ( specifier ) ;
928
978
} catch {
929
- resolved = packageResolve ( specifier , base , conditions ) ;
979
+ ( { resolved, format } = packageResolve ( specifier , base , conditions ) ) ;
930
980
}
931
981
}
932
- return finalizeResolution ( resolved , base ) ;
982
+ return {
983
+ url : finalizeResolution ( resolved , base ) ,
984
+ ...( format != null ) && { format }
985
+ } ;
933
986
}
934
987
935
988
/**
@@ -1040,8 +1093,14 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {
1040
1093
1041
1094
conditions = getConditionsSet ( conditions ) ;
1042
1095
let url ;
1096
+ let format ;
1043
1097
try {
1044
- url = moduleResolve ( specifier , parentURL , conditions ) ;
1098
+ ( { url, format } =
1099
+ moduleResolve (
1100
+ specifier ,
1101
+ parentURL ,
1102
+ conditions
1103
+ ) ) ;
1045
1104
} catch ( error ) {
1046
1105
// Try to give the user a hint of what would have been the
1047
1106
// resolved CommonJS module
@@ -1077,7 +1136,10 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {
1077
1136
url . hash = old . hash ;
1078
1137
}
1079
1138
1080
- return { url : `${ url } ` } ;
1139
+ return {
1140
+ url : `${ url } ` ,
1141
+ ...( format != null ) && { format }
1142
+ } ;
1081
1143
}
1082
1144
1083
1145
module . exports = {
0 commit comments