@@ -40,6 +40,7 @@ const inputTypeFlag = getOptionValue('--input-type');
4040const { URL , pathToFileURL, fileURLToPath, isURL, toPathIfFileURL } = require ( 'internal/url' ) ;
4141const { getCWDURL } = require ( 'internal/util' ) ;
4242const { canParse : URLCanParse } = internalBinding ( 'url' ) ;
43+ const { legacyMainResolve : FSLegacyMainResolve } = internalBinding ( 'fs' ) ;
4344const {
4445 ERR_INPUT_TYPE_NOT_ALLOWED ,
4546 ERR_INVALID_ARG_TYPE ,
@@ -161,6 +162,35 @@ function fileExists(url) {
161162 return internalModuleStat ( toNamespacedPath ( toPathIfFileURL ( url ) ) ) === 0 ;
162163}
163164
165+ const legacyMainResolveExtensions = [
166+ '' ,
167+ '.js' ,
168+ '.json' ,
169+ '.node' ,
170+ '/index.js' ,
171+ '/index.json' ,
172+ '/index.node' ,
173+ './index.js' ,
174+ './index.json' ,
175+ './index.node' ,
176+ ] ;
177+
178+ const legacyMainResolveExtensionsIndexes = {
179+ // 0-6: when packageConfig.main is defined
180+ kResolvedByMain : 0 ,
181+ kResolvedByMainJs : 1 ,
182+ kResolvedByMainJson : 2 ,
183+ kResolvedByMainNode : 3 ,
184+ kResolvedByMainIndexJs : 4 ,
185+ kResolvedByMainIndexJson : 5 ,
186+ kResolvedByMainIndexNode : 6 ,
187+ // 7-9: when packageConfig.main is NOT defined,
188+ // or when the previous case didn't found the file
189+ kResolvedByPackageAndJs : 7 ,
190+ kResolvedByPackageAndJson : 8 ,
191+ kResolvedByPackageAndNode : 9 ,
192+ } ;
193+
164194/**
165195 * Legacy CommonJS main resolution:
166196 * 1. let M = pkg_url + (json main field)
@@ -174,49 +204,22 @@ function fileExists(url) {
174204 * @returns {URL }
175205 */
176206function legacyMainResolve ( packageJSONUrl , packageConfig , base ) {
177- let guess ;
178- if ( packageConfig . main !== undefined ) {
179- // Note: fs check redundances will be handled by Descriptor cache here.
180- if ( fileExists ( guess = new URL ( `./${ packageConfig . main } ` , packageJSONUrl ) ) ) {
181- return guess ;
182- } else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } .js` , packageJSONUrl ) ) ) {
183- // Handled below.
184- } else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } .json` , packageJSONUrl ) ) ) {
185- // Handled below.
186- } else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } .node` , packageJSONUrl ) ) ) {
187- // Handled below.
188- } else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } /index.js` , packageJSONUrl ) ) ) {
189- // Handled below.
190- } else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } /index.json` , packageJSONUrl ) ) ) {
191- // Handled below.
192- } else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } /index.node` , packageJSONUrl ) ) ) {
193- // Handled below.
194- } else {
195- guess = undefined ;
196- }
197- if ( guess ) {
198- emitLegacyIndexDeprecation ( guess , packageJSONUrl , base ,
199- packageConfig . main ) ;
200- return guess ;
201- }
202- // Fallthrough.
203- }
204- if ( fileExists ( guess = new URL ( './index.js' , packageJSONUrl ) ) ) {
205- // Handled below.
206- } else if ( fileExists ( guess = new URL ( './index.json' , packageJSONUrl ) ) ) {
207- // Handled below.
208- } else if ( fileExists ( guess = new URL ( './index.node' , packageJSONUrl ) ) ) {
209- // Handled below.
210- } else {
211- guess = undefined ;
212- }
213- if ( guess ) {
214- emitLegacyIndexDeprecation ( guess , packageJSONUrl , base , packageConfig . main ) ;
215- return guess ;
207+ const packageJsonUrlString = packageJSONUrl . href ;
208+
209+ if ( typeof packageJsonUrlString !== 'string' ) {
210+ throw new ERR_INVALID_ARG_TYPE ( 'packageJSONUrl' , [ 'URL' ] , packageJSONUrl ) ;
216211 }
217- // Not found.
218- throw new ERR_MODULE_NOT_FOUND (
219- fileURLToPath ( new URL ( '.' , packageJSONUrl ) ) , fileURLToPath ( base ) ) ;
212+
213+ const baseStringified = isURL ( base ) ? base . href : base ;
214+
215+ const resolvedOption = FSLegacyMainResolve ( packageJsonUrlString , packageConfig . main , baseStringified ) ;
216+
217+ const baseUrl = resolvedOption <= legacyMainResolveExtensionsIndexes . kResolvedByMainIndexNode ? `./${ packageConfig . main } ` : '' ;
218+ const resolvedUrl = new URL ( baseUrl + legacyMainResolveExtensions [ resolvedOption ] , packageJSONUrl ) ;
219+
220+ emitLegacyIndexDeprecation ( resolvedUrl , packageJSONUrl , base , packageConfig . main ) ;
221+
222+ return resolvedUrl ;
220223}
221224
222225/**
0 commit comments