@@ -131,7 +131,7 @@ function emitTrailingSlashPatternDeprecation(match, pjsonUrl, isExports, base) {
131131 * @returns {void }
132132 */
133133function emitLegacyIndexDeprecation ( url , packageJSONUrl , base , main ) {
134- const format = defaultGetFormat ( url ) ;
134+ const format = defaultGetFormatWithoutErrors ( url ) ;
135135if ( format !== 'module' )
136136return ;
137137const path = fileURLToPath ( url ) ;
@@ -488,22 +488,6 @@ const patternRegEx = /\*/g;
488488function resolvePackageTargetString (
489489target , subpath , match , packageJSONUrl , base , pattern , internal , conditions ) {
490490
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-
507491if ( subpath !== '' && ! pattern && target [ target . length - 1 ] !== '/' )
508492throwInvalidPackageTarget ( match , target , packageJSONUrl , internal , base ) ;
509493
@@ -536,18 +520,22 @@ function resolvePackageTargetString(
536520if ( ! StringPrototypeStartsWith ( resolvedPath , packagePath ) )
537521throwInvalidPackageTarget ( match , target , packageJSONUrl , internal , base ) ;
538522
539- if ( subpath === '' ) return composeResult ( resolved ) ;
523+ if ( subpath === '' ) return resolved ;
540524
541525if ( RegExpPrototypeTest ( invalidSegmentRegEx , subpath ) )
542526throwInvalidSubpath ( match + subpath , packageJSONUrl , internal , base ) ;
543527
544528if ( 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+ ) ;
548536}
549537
550- return composeResult ( new URL ( subpath , resolved ) ) ;
538+ return new URL ( subpath , resolved ) ;
551539}
552540
553541/**
@@ -673,15 +661,15 @@ function packageExportsResolve(
673661! StringPrototypeIncludes ( packageSubpath , '*' ) &&
674662! StringPrototypeEndsWith ( packageSubpath , '/' ) ) {
675663const target = exports [ packageSubpath ] ;
676- const resolveResult = resolvePackageTarget (
664+ const resolved = resolvePackageTarget (
677665packageJSONUrl , target , '' , packageSubpath , base , false , false , conditions
678666) ;
679667
680- if ( resolveResult == null ) {
668+ if ( resolved == null ) {
681669throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
682670}
683671
684- return { ... resolveResult , exact : true } ;
672+ return { resolved , exact : true } ;
685673}
686674
687675let bestMatch = '' ;
@@ -717,7 +705,7 @@ function packageExportsResolve(
717705if ( bestMatch ) {
718706const target = exports [ bestMatch ] ;
719707const pattern = StringPrototypeIncludes ( bestMatch , '*' ) ;
720- const resolveResult = resolvePackageTarget (
708+ const resolved = resolvePackageTarget (
721709packageJSONUrl ,
722710target ,
723711bestMatchSubpath ,
@@ -727,15 +715,15 @@ function packageExportsResolve(
727715false ,
728716conditions ) ;
729717
730- if ( resolveResult == null ) {
718+ if ( resolved == null ) {
731719throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
732720}
733721
734722if ( ! pattern ) {
735723emitFolderMapDeprecation ( bestMatch , packageJSONUrl , true , base ) ;
736724}
737725
738- return { ... resolveResult , exact : pattern } ;
726+ return { resolved , exact : pattern } ;
739727}
740728
741729throwExportsNotFound ( packageSubpath , packageJSONUrl , base ) ;
@@ -775,11 +763,11 @@ function packageImportsResolve(name, base, conditions) {
775763if ( ObjectPrototypeHasOwnProperty ( imports , name ) &&
776764! StringPrototypeIncludes ( name , '*' ) &&
777765! StringPrototypeEndsWith ( name , '/' ) ) {
778- const resolveResult = resolvePackageTarget (
766+ const resolved = resolvePackageTarget (
779767packageJSONUrl , imports [ name ] , '' , name , base , false , true , conditions
780768) ;
781- if ( resolveResult != null ) {
782- return { resolved : resolveResult . resolved , exact : true } ;
769+ if ( resolved != null ) {
770+ return { resolved, exact : true } ;
783771}
784772} else {
785773let bestMatch = '' ;
@@ -812,15 +800,15 @@ function packageImportsResolve(name, base, conditions) {
812800if ( bestMatch ) {
813801const target = imports [ bestMatch ] ;
814802const pattern = StringPrototypeIncludes ( bestMatch , '*' ) ;
815- const resolveResult = resolvePackageTarget (
803+ const resolved = resolvePackageTarget (
816804packageJSONUrl , target ,
817805bestMatchSubpath , bestMatch ,
818806base , pattern , true ,
819807conditions ) ;
820- if ( resolveResult !== null ) {
808+ if ( resolved !== null ) {
821809if ( ! pattern )
822810emitFolderMapDeprecation ( bestMatch , packageJSONUrl , false , base ) ;
823- return { resolved : resolveResult . resolved , exact : pattern } ;
811+ return { resolved, exact : pattern } ;
824812}
825813}
826814}
@@ -880,7 +868,7 @@ function parsePackageName(specifier, base) {
880868 * @param {string } specifier
881869 * @param {string | URL | undefined } base
882870 * @param {Set<string> } conditions
883- * @returns {resolved: URL, format? : string }
871+ * @returns {URL }
884872 */
885873function packageResolve ( specifier , base , conditions ) {
886874if ( NativeModule . canBeRequiredByUsers ( specifier ) )
@@ -896,7 +884,8 @@ function packageResolve(specifier, base, conditions) {
896884if ( packageConfig . name === packageName &&
897885packageConfig . exports !== undefined && packageConfig . exports !== null ) {
898886return packageExportsResolve (
899- packageJSONUrl , packageSubpath , packageConfig , base , conditions ) ;
887+ packageJSONUrl , packageSubpath , packageConfig , base , conditions
888+ ) . resolved ;
900889}
901890}
902891
@@ -920,24 +909,19 @@ function packageResolve(specifier, base, conditions) {
920909const packageConfig = getPackageConfig ( packageJSONPath , specifier , base ) ;
921910if ( packageConfig . exports !== undefined && packageConfig . exports !== null ) {
922911return packageExportsResolve (
923- packageJSONUrl , packageSubpath , packageConfig , base , conditions ) ;
912+ packageJSONUrl , packageSubpath , packageConfig , base , conditions
913+ ) . resolved ;
924914}
925915
926916if ( 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+ ) ;
934922}
935923
936- return {
937- resolved : new URL ( packageSubpath , packageJSONUrl ) ,
938- ...( packageConfig . type !== 'none' ) && { format : packageConfig . type }
939- } ;
940-
924+ return new URL ( packageSubpath , packageJSONUrl ) ;
941925// Cross-platform root check.
942926} while ( packageJSONPath . length !== lastPath . length ) ;
943927
@@ -981,7 +965,6 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) {
981965// Order swapped from spec for minor perf gain.
982966// Ok since relative URLs cannot parse as URLs.
983967let resolved ;
984- let format ;
985968if ( shouldBeTreatedAsRelativeOrAbsolutePath ( specifier ) ) {
986969resolved = new URL ( specifier , base ) ;
987970} else if ( specifier [ 0 ] === '#' ) {
@@ -990,15 +973,12 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) {
990973try {
991974resolved = new URL ( specifier ) ;
992975} catch {
993- ( { resolved, format } = packageResolve ( specifier , base , conditions ) ) ;
976+ resolved = packageResolve ( specifier , base , conditions ) ;
994977}
995978}
996979if ( resolved . protocol !== 'file:' )
997980return resolved ;
998- return {
999- url : finalizeResolution ( resolved , base , preserveSymlinks ) ,
1000- ...( format != null ) && { format }
1001- } ;
981+ return finalizeResolution ( resolved , base , preserveSymlinks ) ;
1002982}
1003983
1004984/**
@@ -1047,6 +1027,13 @@ function resolveAsCommonJS(specifier, parentURL) {
10471027}
10481028}
10491029
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+
10501037function defaultResolve ( specifier , context = { } , defaultResolveUnused ) {
10511038let { parentURL, conditions } = context ;
10521039if ( parentURL && policy ?. manifest ) {
@@ -1087,15 +1074,9 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {
10871074
10881075conditions = getConditionsSet ( conditions ) ;
10891076let url ;
1090- let format ;
10911077try {
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 ) ;
10991080} catch ( error ) {
11001081// Try to give the user a hint of what would have been the
11011082// resolved CommonJS module
@@ -1119,13 +1100,11 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {
11191100throw error ;
11201101}
11211102
1122- if ( url . protocol !== 'file:' && url . protocol !== 'data:' &&
1123- url . protocol !== 'node:' )
1124- throw new ERR_UNSUPPORTED_ESM_URL_SCHEME ( url ) ;
1103+ throwIfUnsupportedURLProtocol ( url ) ;
11251104
11261105return {
11271106url : `${ url } ` ,
1128- ... ( format != null ) && { format }
1107+ format : defaultGetFormatWithoutErrors ( url ) ,
11291108} ;
11301109}
11311110
@@ -1140,4 +1119,6 @@ module.exports = {
11401119} ;
11411120
11421121// cycle
1143- const { defaultGetFormat } = require ( 'internal/modules/esm/get_format' ) ;
1122+ const {
1123+ defaultGetFormatWithoutErrors,
1124+ } = require ( 'internal/modules/esm/get_format' ) ;
0 commit comments