@@ -487,6 +487,23 @@ const patternRegEx = /\*/g;
487
487
488
488
function resolvePackageTargetString (
489
489
target , subpath , match , packageJSONUrl , base , pattern , internal , conditions ) {
490
+
491
+ const composeResult = ( resolved ) => {
492
+ let format ;
493
+ try {
494
+ format = getPackageType ( resolved ) ;
495
+ } catch ( err ) {
496
+ if ( err . code === 'ERR_INVALID_FILE_URL_PATH' ) {
497
+ const invalidModuleErr = new ERR_INVALID_MODULE_SPECIFIER (
498
+ resolved , 'must not include encoded "/" or "\\" characters' , base ) ;
499
+ invalidModuleErr . cause = err ;
500
+ throw invalidModuleErr ;
501
+ }
502
+ throw err ;
503
+ }
504
+ return { resolved, ...( format !== 'none' ) && { format } } ;
505
+ } ;
506
+
490
507
if ( subpath !== '' && ! pattern && target [ target . length - 1 ] !== '/' )
491
508
throwInvalidPackageTarget ( match , target , packageJSONUrl , internal , base ) ;
492
509
@@ -502,7 +519,8 @@ function resolvePackageTargetString(
502
519
const exportTarget = pattern ?
503
520
RegExpPrototypeSymbolReplace ( patternRegEx , target , ( ) => subpath ) :
504
521
target + subpath ;
505
- return packageResolve ( exportTarget , packageJSONUrl , conditions ) ;
522
+ return packageResolve (
523
+ exportTarget , packageJSONUrl , conditions ) ;
506
524
}
507
525
}
508
526
throwInvalidPackageTarget ( match , target , packageJSONUrl , internal , base ) ;
@@ -518,15 +536,18 @@ function resolvePackageTargetString(
518
536
if ( ! StringPrototypeStartsWith ( resolvedPath , packagePath ) )
519
537
throwInvalidPackageTarget ( match , target , packageJSONUrl , internal , base ) ;
520
538
521
- if ( subpath === '' ) return resolved ;
539
+ if ( subpath === '' ) return composeResult ( resolved ) ;
522
540
523
541
if ( RegExpPrototypeTest ( invalidSegmentRegEx , subpath ) )
524
542
throwInvalidSubpath ( match + subpath , packageJSONUrl , internal , base ) ;
525
543
526
- if ( pattern )
527
- return new URL ( RegExpPrototypeSymbolReplace ( patternRegEx , resolved . href ,
528
- ( ) => subpath ) ) ;
529
- return new URL ( subpath , resolved ) ;
544
+ if ( pattern ) {
545
+ return composeResult ( new URL ( RegExpPrototypeSymbolReplace ( patternRegEx ,
546
+ resolved . href ,
547
+ ( ) => subpath ) ) ) ;
548
+ }
549
+
550
+ return composeResult ( new URL ( subpath , resolved ) ) ;
530
551
}
531
552
532
553
/**
@@ -552,9 +573,9 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath,
552
573
let lastException ;
553
574
for ( let i = 0 ; i < target . length ; i ++ ) {
554
575
const targetItem = target [ i ] ;
555
- let resolved ;
576
+ let resolveResult ;
556
577
try {
557
- resolved = resolvePackageTarget (
578
+ resolveResult = resolvePackageTarget (
558
579
packageJSONUrl , targetItem , subpath , packageSubpath , base , pattern ,
559
580
internal , conditions ) ;
560
581
} catch ( e ) {
@@ -563,13 +584,13 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath,
563
584
continue ;
564
585
throw e ;
565
586
}
566
- if ( resolved === undefined )
587
+ if ( resolveResult === undefined )
567
588
continue ;
568
- if ( resolved === null ) {
589
+ if ( resolveResult === null ) {
569
590
lastException = null ;
570
591
continue ;
571
592
}
572
- return resolved ;
593
+ return resolveResult ;
573
594
}
574
595
if ( lastException === undefined || lastException === null )
575
596
return lastException ;
@@ -588,12 +609,12 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath,
588
609
const key = keys [ i ] ;
589
610
if ( key === 'default' || conditions . has ( key ) ) {
590
611
const conditionalTarget = target [ key ] ;
591
- const resolved = resolvePackageTarget (
612
+ const resolveResult = resolvePackageTarget (
592
613
packageJSONUrl , conditionalTarget , subpath , packageSubpath , base ,
593
614
pattern , internal , conditions ) ;
594
- if ( resolved === undefined )
615
+ if ( resolveResult === undefined )
595
616
continue ;
596
- return resolved ;
617
+ return resolveResult ;
597
618
}
598
619
}
599
620
return undefined ;
@@ -652,12 +673,15 @@ function packageExportsResolve(
652
673
! StringPrototypeIncludes ( packageSubpath , '*' ) &&
653
674
! StringPrototypeEndsWith ( packageSubpath , '/' ) ) {
654
675
const target = exports [ packageSubpath ] ;
655
- const resolved = resolvePackageTarget (
676
+ const resolveResult = resolvePackageTarget (
656
677
packageJSONUrl , target , '' , packageSubpath , base , false , false , conditions
657
678
) ;
658
- if ( resolved === null || resolved === undefined )
679
+
680
+ if ( resolveResult == null ) {
659
681
throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
660
- return { resolved, exact : true } ;
682
+ }
683
+
684
+ return { ...resolveResult , exact : true } ;
661
685
}
662
686
663
687
let bestMatch = '' ;
@@ -693,14 +717,25 @@ function packageExportsResolve(
693
717
if ( bestMatch ) {
694
718
const target = exports [ bestMatch ] ;
695
719
const pattern = StringPrototypeIncludes ( bestMatch , '*' ) ;
696
- const resolved = resolvePackageTarget ( packageJSONUrl , target ,
697
- bestMatchSubpath , bestMatch , base ,
698
- pattern , false , conditions ) ;
699
- if ( resolved === null || resolved === undefined )
720
+ const resolveResult = resolvePackageTarget (
721
+ packageJSONUrl ,
722
+ target ,
723
+ bestMatchSubpath ,
724
+ bestMatch ,
725
+ base ,
726
+ pattern ,
727
+ false ,
728
+ conditions ) ;
729
+
730
+ if ( resolveResult == null ) {
700
731
throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
701
- if ( ! pattern )
732
+ }
733
+
734
+ if ( ! pattern ) {
702
735
emitFolderMapDeprecation ( bestMatch , packageJSONUrl , true , base ) ;
703
- return { resolved, exact : pattern } ;
736
+ }
737
+
738
+ return { ...resolveResult , exact : pattern } ;
704
739
}
705
740
706
741
throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
@@ -740,11 +775,12 @@ function packageImportsResolve(name, base, conditions) {
740
775
if ( ObjectPrototypeHasOwnProperty ( imports , name ) &&
741
776
! StringPrototypeIncludes ( name , '*' ) &&
742
777
! StringPrototypeEndsWith ( name , '/' ) ) {
743
- const resolved = resolvePackageTarget (
778
+ const resolveResult = resolvePackageTarget (
744
779
packageJSONUrl , imports [ name ] , '' , name , base , false , true , conditions
745
780
) ;
746
- if ( resolved !== null )
747
- return { resolved, exact : true } ;
781
+ if ( resolveResult != null ) {
782
+ return { resolved : resolveResult . resolved , exact : true } ;
783
+ }
748
784
} else {
749
785
let bestMatch = '' ;
750
786
let bestMatchSubpath ;
@@ -776,14 +812,15 @@ function packageImportsResolve(name, base, conditions) {
776
812
if ( bestMatch ) {
777
813
const target = imports [ bestMatch ] ;
778
814
const pattern = StringPrototypeIncludes ( bestMatch , '*' ) ;
779
- const resolved = resolvePackageTarget ( packageJSONUrl , target ,
780
- bestMatchSubpath , bestMatch ,
781
- base , pattern , true ,
782
- conditions ) ;
783
- if ( resolved !== null ) {
815
+ const resolveResult = resolvePackageTarget (
816
+ packageJSONUrl , target ,
817
+ bestMatchSubpath , bestMatch ,
818
+ base , pattern , true ,
819
+ conditions ) ;
820
+ if ( resolveResult !== null ) {
784
821
if ( ! pattern )
785
822
emitFolderMapDeprecation ( bestMatch , packageJSONUrl , false , base ) ;
786
- return { resolved, exact : pattern } ;
823
+ return { resolved : resolveResult . resolved , exact : pattern } ;
787
824
}
788
825
}
789
826
}
@@ -843,7 +880,7 @@ function parsePackageName(specifier, base) {
843
880
* @param {string } specifier
844
881
* @param {string | URL | undefined } base
845
882
* @param {Set<string> } conditions
846
- * @returns {URL }
883
+ * @returns {resolved: URL, format? : string }
847
884
*/
848
885
function packageResolve ( specifier , base , conditions ) {
849
886
if ( NativeModule . canBeRequiredByUsers ( specifier ) )
@@ -859,8 +896,7 @@ function packageResolve(specifier, base, conditions) {
859
896
if ( packageConfig . name === packageName &&
860
897
packageConfig . exports !== undefined && packageConfig . exports !== null ) {
861
898
return packageExportsResolve (
862
- packageJSONUrl , packageSubpath , packageConfig , base , conditions
863
- ) . resolved ;
899
+ packageJSONUrl , packageSubpath , packageConfig , base , conditions ) ;
864
900
}
865
901
}
866
902
@@ -882,13 +918,26 @@ function packageResolve(specifier, base, conditions) {
882
918
883
919
// Package match.
884
920
const packageConfig = getPackageConfig ( packageJSONPath , specifier , base ) ;
885
- if ( packageConfig . exports !== undefined && packageConfig . exports !== null )
921
+ if ( packageConfig . exports !== undefined && packageConfig . exports !== null ) {
886
922
return packageExportsResolve (
887
- packageJSONUrl , packageSubpath , packageConfig , base , conditions
888
- ) . resolved ;
889
- if ( packageSubpath === '.' )
890
- return legacyMainResolve ( packageJSONUrl , packageConfig , base ) ;
891
- return new URL ( packageSubpath , packageJSONUrl ) ;
923
+ packageJSONUrl , packageSubpath , packageConfig , base , conditions ) ;
924
+ }
925
+
926
+ if ( packageSubpath === '.' ) {
927
+ return {
928
+ resolved : legacyMainResolve (
929
+ packageJSONUrl ,
930
+ packageConfig ,
931
+ base ) ,
932
+ ...( packageConfig . type !== 'none' ) && { format : packageConfig . type }
933
+ } ;
934
+ }
935
+
936
+ return {
937
+ resolved : new URL ( packageSubpath , packageJSONUrl ) ,
938
+ ...( packageConfig . type !== 'none' ) && { format : packageConfig . type }
939
+ } ;
940
+
892
941
// Cross-platform root check.
893
942
} while ( packageJSONPath . length !== lastPath . length ) ;
894
943
@@ -932,6 +981,7 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) {
932
981
// Order swapped from spec for minor perf gain.
933
982
// Ok since relative URLs cannot parse as URLs.
934
983
let resolved ;
984
+ let format ;
935
985
if ( shouldBeTreatedAsRelativeOrAbsolutePath ( specifier ) ) {
936
986
resolved = new URL ( specifier , base ) ;
937
987
} else if ( specifier [ 0 ] === '#' ) {
@@ -940,12 +990,15 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) {
940
990
try {
941
991
resolved = new URL ( specifier ) ;
942
992
} catch {
943
- resolved = packageResolve ( specifier , base , conditions ) ;
993
+ ( { resolved, format } = packageResolve ( specifier , base , conditions ) ) ;
944
994
}
945
995
}
946
996
if ( resolved . protocol !== 'file:' )
947
997
return resolved ;
948
- return finalizeResolution ( resolved , base , preserveSymlinks ) ;
998
+ return {
999
+ url : finalizeResolution ( resolved , base , preserveSymlinks ) ,
1000
+ ...( format != null ) && { format }
1001
+ } ;
949
1002
}
950
1003
951
1004
/**
@@ -1034,9 +1087,15 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {
1034
1087
1035
1088
conditions = getConditionsSet ( conditions ) ;
1036
1089
let url ;
1090
+ let format ;
1037
1091
try {
1038
- url = moduleResolve ( specifier , parentURL , conditions ,
1039
- isMain ? preserveSymlinksMain : preserveSymlinks ) ;
1092
+ ( { url, format } =
1093
+ moduleResolve (
1094
+ specifier ,
1095
+ parentURL ,
1096
+ conditions ,
1097
+ isMain ? preserveSymlinksMain : preserveSymlinks
1098
+ ) ) ;
1040
1099
} catch ( error ) {
1041
1100
// Try to give the user a hint of what would have been the
1042
1101
// resolved CommonJS module
@@ -1064,7 +1123,10 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {
1064
1123
url . protocol !== 'node:' )
1065
1124
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME ( url ) ;
1066
1125
1067
- return { url : `${ url } ` } ;
1126
+ return {
1127
+ url : `${ url } ` ,
1128
+ ...( format != null ) && { format }
1129
+ } ;
1068
1130
}
1069
1131
1070
1132
module . exports = {
0 commit comments