@@ -131,7 +131,7 @@ function emitTrailingSlashPatternDeprecation(match, pjsonUrl, isExports, base) {
131
131
* @returns
132
132
*/
133
133
function emitLegacyIndexDeprecation ( url , packageJSONUrl , base , main ) {
134
- const format = defaultGetFormat ( url ) ;
134
+ const format = defaultGetFormatWithoutErrors ( url ) ;
135
135
if ( format !== 'module' )
136
136
return ;
137
137
const path = fileURLToPath ( url ) ;
@@ -488,22 +488,6 @@ const patternRegEx = /\*/g;
488
488
function resolvePackageTargetString (
489
489
target , subpath , match , packageJSONUrl , base , pattern , internal , conditions ) {
490
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
-
507
491
if ( subpath !== '' && ! pattern && target [ target . length - 1 ] !== '/' )
508
492
throwInvalidPackageTarget ( match , target , packageJSONUrl , internal , base ) ;
509
493
@@ -536,18 +520,22 @@ function resolvePackageTargetString(
536
520
if ( ! StringPrototypeStartsWith ( resolvedPath , packagePath ) )
537
521
throwInvalidPackageTarget ( match , target , packageJSONUrl , internal , base ) ;
538
522
539
- if ( subpath === '' ) return composeResult ( resolved ) ;
523
+ if ( subpath === '' ) return resolved ;
540
524
541
525
if ( RegExpPrototypeTest ( invalidSegmentRegEx , subpath ) )
542
526
throwInvalidSubpath ( match + subpath , packageJSONUrl , internal , base ) ;
543
527
544
528
if ( pattern ) {
545
- return composeResult ( new URL ( RegExpPrototypeSymbolReplace ( patternRegEx ,
546
- resolved . href ,
547
- ( ) => subpath ) ) ) ;
529
+ return new URL (
530
+ RegExpPrototypeSymbolReplace (
531
+ patternRegEx ,
532
+ resolved . href ,
533
+ ( ) => subpath
534
+ )
535
+ ) ;
548
536
}
549
537
550
- return composeResult ( new URL ( subpath , resolved ) ) ;
538
+ return new URL ( subpath , resolved ) ;
551
539
}
552
540
553
541
/**
@@ -673,15 +661,15 @@ function packageExportsResolve(
673
661
! StringPrototypeIncludes ( packageSubpath , '*' ) &&
674
662
! StringPrototypeEndsWith ( packageSubpath , '/' ) ) {
675
663
const target = exports [ packageSubpath ] ;
676
- const resolveResult = resolvePackageTarget (
664
+ const resolved = resolvePackageTarget (
677
665
packageJSONUrl , target , '' , packageSubpath , base , false , false , conditions
678
666
) ;
679
667
680
- if ( resolveResult == null ) {
668
+ if ( resolved == null ) {
681
669
throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
682
670
}
683
671
684
- return { ... resolveResult , exact : true } ;
672
+ return { resolved , exact : true } ;
685
673
}
686
674
687
675
let bestMatch = '' ;
@@ -717,7 +705,7 @@ function packageExportsResolve(
717
705
if ( bestMatch ) {
718
706
const target = exports [ bestMatch ] ;
719
707
const pattern = StringPrototypeIncludes ( bestMatch , '*' ) ;
720
- const resolveResult = resolvePackageTarget (
708
+ const resolved = resolvePackageTarget (
721
709
packageJSONUrl ,
722
710
target ,
723
711
bestMatchSubpath ,
@@ -727,15 +715,15 @@ function packageExportsResolve(
727
715
false ,
728
716
conditions ) ;
729
717
730
- if ( resolveResult == null ) {
718
+ if ( resolved == null ) {
731
719
throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
732
720
}
733
721
734
722
if ( ! pattern ) {
735
723
emitFolderMapDeprecation ( bestMatch , packageJSONUrl , true , base ) ;
736
724
}
737
725
738
- return { ... resolveResult , exact : pattern } ;
726
+ return { resolved , exact : pattern } ;
739
727
}
740
728
741
729
throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
@@ -775,11 +763,11 @@ function packageImportsResolve(name, base, conditions) {
775
763
if ( ObjectPrototypeHasOwnProperty ( imports , name ) &&
776
764
! StringPrototypeIncludes ( name , '*' ) &&
777
765
! StringPrototypeEndsWith ( name , '/' ) ) {
778
- const resolveResult = resolvePackageTarget (
766
+ const resolved = resolvePackageTarget (
779
767
packageJSONUrl , imports [ name ] , '' , name , base , false , true , conditions
780
768
) ;
781
- if ( resolveResult != null ) {
782
- return { resolved : resolveResult . resolved , exact : true } ;
769
+ if ( resolved != null ) {
770
+ return { resolved, exact : true } ;
783
771
}
784
772
} else {
785
773
let bestMatch = '' ;
@@ -812,15 +800,15 @@ function packageImportsResolve(name, base, conditions) {
812
800
if ( bestMatch ) {
813
801
const target = imports [ bestMatch ] ;
814
802
const pattern = StringPrototypeIncludes ( bestMatch , '*' ) ;
815
- const resolveResult = resolvePackageTarget (
803
+ const resolved = resolvePackageTarget (
816
804
packageJSONUrl , target ,
817
805
bestMatchSubpath , bestMatch ,
818
806
base , pattern , true ,
819
807
conditions ) ;
820
- if ( resolveResult !== null ) {
808
+ if ( resolved !== null ) {
821
809
if ( ! pattern )
822
810
emitFolderMapDeprecation ( bestMatch , packageJSONUrl , false , base ) ;
823
- return { resolved : resolveResult . resolved , exact : pattern } ;
811
+ return { resolved, exact : pattern } ;
824
812
}
825
813
}
826
814
}
@@ -880,7 +868,7 @@ function parsePackageName(specifier, base) {
880
868
* @param {string } specifier
881
869
* @param {string | URL | undefined } base
882
870
* @param {Set<string> } conditions
883
- * @returns {resolved: URL, format? : string }
871
+ * @returns {URL }
884
872
*/
885
873
function packageResolve ( specifier , base , conditions ) {
886
874
if ( NativeModule . canBeRequiredByUsers ( specifier ) )
@@ -896,7 +884,8 @@ function packageResolve(specifier, base, conditions) {
896
884
if ( packageConfig . name === packageName &&
897
885
packageConfig . exports !== undefined && packageConfig . exports !== null ) {
898
886
return packageExportsResolve (
899
- packageJSONUrl , packageSubpath , packageConfig , base , conditions ) ;
887
+ packageJSONUrl , packageSubpath , packageConfig , base , conditions
888
+ ) . resolved ;
900
889
}
901
890
}
902
891
@@ -920,24 +909,19 @@ function packageResolve(specifier, base, conditions) {
920
909
const packageConfig = getPackageConfig ( packageJSONPath , specifier , base ) ;
921
910
if ( packageConfig . exports !== undefined && packageConfig . exports !== null ) {
922
911
return packageExportsResolve (
923
- packageJSONUrl , packageSubpath , packageConfig , base , conditions ) ;
912
+ packageJSONUrl , packageSubpath , packageConfig , base , conditions
913
+ ) . resolved ;
924
914
}
925
915
926
916
if ( packageSubpath === '.' ) {
927
- return {
928
- resolved : legacyMainResolve (
929
- packageJSONUrl ,
930
- packageConfig ,
931
- base ) ,
932
- ...( packageConfig . type !== 'none' ) && { format : packageConfig . type }
933
- } ;
917
+ return legacyMainResolve (
918
+ packageJSONUrl ,
919
+ packageConfig ,
920
+ base
921
+ ) ;
934
922
}
935
923
936
- return {
937
- resolved : new URL ( packageSubpath , packageJSONUrl ) ,
938
- ...( packageConfig . type !== 'none' ) && { format : packageConfig . type }
939
- } ;
940
-
924
+ return new URL ( packageSubpath , packageJSONUrl ) ;
941
925
// Cross-platform root check.
942
926
} while ( packageJSONPath . length !== lastPath . length ) ;
943
927
@@ -981,7 +965,6 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) {
981
965
// Order swapped from spec for minor perf gain.
982
966
// Ok since relative URLs cannot parse as URLs.
983
967
let resolved ;
984
- let format ;
985
968
if ( shouldBeTreatedAsRelativeOrAbsolutePath ( specifier ) ) {
986
969
resolved = new URL ( specifier , base ) ;
987
970
} else if ( specifier [ 0 ] === '#' ) {
@@ -990,15 +973,12 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) {
990
973
try {
991
974
resolved = new URL ( specifier ) ;
992
975
} catch {
993
- ( { resolved, format } = packageResolve ( specifier , base , conditions ) ) ;
976
+ resolved = packageResolve ( specifier , base , conditions ) ;
994
977
}
995
978
}
996
979
if ( resolved . protocol !== 'file:' )
997
980
return resolved ;
998
- return {
999
- url : finalizeResolution ( resolved , base , preserveSymlinks ) ,
1000
- ...( format != null ) && { format }
1001
- } ;
981
+ return finalizeResolution ( resolved , base , preserveSymlinks ) ;
1002
982
}
1003
983
1004
984
/**
@@ -1047,6 +1027,13 @@ function resolveAsCommonJS(specifier, parentURL) {
1047
1027
}
1048
1028
}
1049
1029
1030
+ function throwIfUnsupportedURLProtocol ( url ) {
1031
+ if ( url . protocol !== 'file:' && url . protocol !== 'data:' &&
1032
+ url . protocol !== 'node:' ) {
1033
+ throw new ERR_UNSUPPORTED_ESM_URL_SCHEME ( url ) ;
1034
+ }
1035
+ }
1036
+
1050
1037
function defaultResolve ( specifier , context = { } , defaultResolveUnused ) {
1051
1038
let { parentURL, conditions } = context ;
1052
1039
if ( parentURL && policy ?. manifest ) {
@@ -1087,15 +1074,9 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {
1087
1074
1088
1075
conditions = getConditionsSet ( conditions ) ;
1089
1076
let url ;
1090
- let format ;
1091
1077
try {
1092
- ( { url, format } =
1093
- moduleResolve (
1094
- specifier ,
1095
- parentURL ,
1096
- conditions ,
1097
- isMain ? preserveSymlinksMain : preserveSymlinks
1098
- ) ) ;
1078
+ url = moduleResolve ( specifier , parentURL , conditions ,
1079
+ isMain ? preserveSymlinksMain : preserveSymlinks ) ;
1099
1080
} catch ( error ) {
1100
1081
// Try to give the user a hint of what would have been the
1101
1082
// resolved CommonJS module
@@ -1119,13 +1100,11 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {
1119
1100
throw error ;
1120
1101
}
1121
1102
1122
- if ( url . protocol !== 'file:' && url . protocol !== 'data:' &&
1123
- url . protocol !== 'node:' )
1124
- throw new ERR_UNSUPPORTED_ESM_URL_SCHEME ( url ) ;
1103
+ throwIfUnsupportedURLProtocol ( url ) ;
1125
1104
1126
1105
return {
1127
1106
url : `${ url } ` ,
1128
- ... ( format != null ) && { format }
1107
+ format : defaultGetFormatWithoutErrors ( url ) ,
1129
1108
} ;
1130
1109
}
1131
1110
@@ -1140,4 +1119,6 @@ module.exports = {
1140
1119
} ;
1141
1120
1142
1121
// cycle
1143
- const { defaultGetFormat } = require ( 'internal/modules/esm/get_format' ) ;
1122
+ const {
1123
+ defaultGetFormatWithoutErrors,
1124
+ } = require ( 'internal/modules/esm/get_format' ) ;
0 commit comments