@@ -36,14 +36,16 @@ const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
3636const experimentalNetworkImports =
3737 getOptionValue ( '--experimental-network-imports' ) ;
3838const typeFlag = getOptionValue ( '--input-type' ) ;
39- const { URL , pathToFileURL, fileURLToPath, isURL, toPathIfFileURL } = require ( 'internal/url' ) ;
39+ const { URL , pathToFileURL, fileURLToPath, isURL } = require ( 'internal/url' ) ;
4040const { canParse : URLCanParse } = internalBinding ( 'url' ) ;
41+ const { legacyMainResolve : URLLegacyMainResolve } = internalBinding ( 'fs' ) ;
4142const {
4243 ERR_INPUT_TYPE_NOT_ALLOWED ,
4344 ERR_INVALID_ARG_TYPE ,
4445 ERR_INVALID_MODULE_SPECIFIER ,
4546 ERR_INVALID_PACKAGE_CONFIG ,
4647 ERR_INVALID_PACKAGE_TARGET ,
48+ ERR_INVALID_URL ,
4749 ERR_MANIFEST_DEPENDENCY_MISSING ,
4850 ERR_MODULE_NOT_FOUND ,
4951 ERR_PACKAGE_IMPORT_NOT_DEFINED ,
@@ -132,13 +134,36 @@ function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) {
132134
133135const realpathCache = new SafeMap ( ) ;
134136
135- /**
136- * @param {string | URL } url
137- * @returns {boolean }
138- */
139- function fileExists ( url ) {
140- return internalModuleStat ( toNamespacedPath ( toPathIfFileURL ( url ) ) ) === 0 ;
141- }
137+ const mainResolveExtensions = [
138+ '' ,
139+ '.js' ,
140+ '.json' ,
141+ '.node' ,
142+ '/index.js' ,
143+ '/index.json' ,
144+ '/index.node' ,
145+ './index.js' ,
146+ './index.json' ,
147+ './index.node' ,
148+ ] ;
149+
150+ const mainResolveReturnType = {
151+ kInvalidUrl : - 2 ,
152+ kModuleNotFound : - 1 ,
153+ // 0-6: when packageConfig.main is defined
154+ kResolvedByMain : 0 ,
155+ kResolvedByMainJs : 1 ,
156+ kResolvedByMainJson : 2 ,
157+ kResolvedByMainNode : 3 ,
158+ kResolvedByMainIndexJs : 4 ,
159+ kResolvedByMainIndexJson : 5 ,
160+ kResolvedByMainIndexNode : 6 ,
161+ // 7-9: when packageConfig.main is NOT defined,
162+ // or when the previous case didn't found the file
163+ kResolvedByPackageAndJs : 7 ,
164+ kResolvedByPackageAndJson : 8 ,
165+ kResolvedByPackageAndNode : 9 ,
166+ } ;
142167
143168/**
144169 * Legacy CommonJS main resolution:
@@ -153,44 +178,23 @@ function fileExists(url) {
153178 * @returns {URL }
154179 */
155180function legacyMainResolve ( packageJSONUrl , packageConfig , base ) {
156- let guess ;
157- if ( packageConfig . main !== undefined ) {
158- // Note: fs check redundances will be handled by Descriptor cache here.
159- if ( fileExists ( guess = new URL ( `./${ packageConfig . main } ` ,
160- packageJSONUrl ) ) ) {
161- return guess ;
162- } else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } .js` ,
163- packageJSONUrl ) ) ) ;
164- else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } .json` ,
165- packageJSONUrl ) ) ) ;
166- else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } .node` ,
167- packageJSONUrl ) ) ) ;
168- else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } /index.js` ,
169- packageJSONUrl ) ) ) ;
170- else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } /index.json` ,
171- packageJSONUrl ) ) ) ;
172- else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } /index.node` ,
173- packageJSONUrl ) ) ) ;
174- else guess = undefined ;
175- if ( guess ) {
176- emitLegacyIndexDeprecation ( guess , packageJSONUrl , base ,
177- packageConfig . main ) ;
178- return guess ;
181+ const resolvedOption = URLLegacyMainResolve ( packageJSONUrl . href , packageConfig . main ) ;
182+
183+ if ( resolvedOption < mainResolveReturnType . kResolvedByMain ) {
184+ if ( resolvedOption === mainResolveReturnType . kInvalidUrl ) {
185+ throw new ERR_INVALID_URL ( packageConfig . main || './index.js' ) ;
186+ } else {
187+ throw new ERR_MODULE_NOT_FOUND (
188+ fileURLToPath ( new URL ( '.' , packageJSONUrl ) ) , fileURLToPath ( base ) ) ;
179189 }
180- // Fallthrough.
181- }
182- if ( fileExists ( guess = new URL ( './index.js' , packageJSONUrl ) ) ) ;
183- // So fs.
184- else if ( fileExists ( guess = new URL ( './index.json' , packageJSONUrl ) ) ) ;
185- else if ( fileExists ( guess = new URL ( './index.node' , packageJSONUrl ) ) ) ;
186- else guess = undefined ;
187- if ( guess ) {
188- emitLegacyIndexDeprecation ( guess , packageJSONUrl , base , packageConfig . main ) ;
189- return guess ;
190190 }
191- // Not found.
192- throw new ERR_MODULE_NOT_FOUND (
193- fileURLToPath ( new URL ( '.' , packageJSONUrl ) ) , fileURLToPath ( base ) ) ;
191+
192+ const baseUrl = resolvedOption <= mainResolveReturnType . kResolvedByMainNode ? `./${ packageConfig . main } ` : '' ;
193+ const guess = new URL ( baseUrl + mainResolveExtensions [ resolvedOption ] , packageJSONUrl ) ;
194+
195+ emitLegacyIndexDeprecation ( guess , packageJSONUrl , base , packageConfig . main ) ;
196+
197+ return guess ;
194198}
195199
196200const encodedSepRegEx = / % 2 F | % 5 C / i;
@@ -1078,6 +1082,7 @@ module.exports = {
10781082 packageExportsResolve,
10791083 packageImportsResolve,
10801084 throwIfInvalidParentURL,
1085+ legacyMainResolve,
10811086} ;
10821087
10831088// cycle
0 commit comments