@@ -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,18 @@ 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+ ] ;
142149
143150/**
144151 * Legacy CommonJS main resolution:
@@ -153,44 +160,23 @@ function fileExists(url) {
153160 * @returns {URL }
154161 */
155162function 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 ;
163+ const resolvedOption = URLLegacyMainResolve ( packageJSONUrl . href , packageConfig . main ) ;
164+
165+ if ( resolvedOption < 0 ) {
166+ if ( resolvedOption === - 2 ) {
167+ throw new ERR_INVALID_URL ( packageConfig . main || './index.js' ) ;
168+ } else {
169+ throw new ERR_MODULE_NOT_FOUND (
170+ fileURLToPath ( new URL ( '.' , packageJSONUrl ) ) , fileURLToPath ( base ) ) ;
179171 }
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 ;
190172 }
191- // Not found.
192- throw new ERR_MODULE_NOT_FOUND (
193- fileURLToPath ( new URL ( '.' , packageJSONUrl ) ) , fileURLToPath ( base ) ) ;
173+
174+ const baseUrl = resolvedOption <= 6 ? `./${ packageConfig . main } ` : '' ;
175+ const guess = new URL ( baseUrl + mainResolveExtensions [ resolvedOption ] , packageJSONUrl ) ;
176+
177+ emitLegacyIndexDeprecation ( guess , packageJSONUrl , base , packageConfig . main ) ;
178+
179+ return guess ;
194180}
195181
196182const encodedSepRegEx = / % 2 F | % 5 C / i;
@@ -1078,6 +1064,7 @@ module.exports = {
10781064 packageExportsResolve,
10791065 packageImportsResolve,
10801066 throwIfInvalidParentURL,
1067+ legacyMainResolve,
10811068} ;
10821069
10831070// cycle
0 commit comments