@@ -90,6 +90,36 @@ function emitFolderMapDeprecation(match, pjsonUrl, isExports, base) {
9090 ) ;
9191}
9292
93+ function emitLegacyIndexDeprecation ( url , packageJSONUrl , base , main ) {
94+ if ( ! pendingDeprecation )
95+ return ;
96+ const { format } = defaultGetFormat ( url ) ;
97+ if ( format !== 'module' )
98+ return ;
99+ const path = fileURLToPath ( url ) ;
100+ const pkgPath = fileURLToPath ( new URL ( '.' , packageJSONUrl ) ) ;
101+ const basePath = fileURLToPath ( base ) ;
102+ if ( main )
103+ process . emitWarning (
104+ `Package ${ pkgPath } has a "main" field set to ${ JSONStringify ( main ) } , ` +
105+ `excluding the full filename and extension to the resolved file at "${
106+ StringPrototypeSlice ( path , pkgPath . length ) } ", imported from ${
107+ basePath } .\n Automatic extension resolution of the "main" field is` +
108+ 'deprecated for ES modules.' ,
109+ 'DeprecationWarning' ,
110+ 'DEP0150'
111+ ) ;
112+ else
113+ process . emitWarning (
114+ `No "main" or "exports" field defined in the package.json for ${ pkgPath
115+ } resolving the main entry point "${
116+ StringPrototypeSlice ( path , pkgPath . length ) } ", imported from ${ basePath
117+ } .\nDefault "index" lookups for the main are deprecated for ES modules.`,
118+ 'DeprecationWarning' ,
119+ 'DEP0150'
120+ ) ;
121+ }
122+
93123function getConditionsSet ( conditions ) {
94124 if ( conditions !== undefined && conditions !== DEFAULT_CONDITIONS ) {
95125 if ( ! ArrayIsArray ( conditions ) ) {
@@ -209,41 +239,33 @@ function legacyMainResolve(packageJSONUrl, packageConfig, base) {
209239 if ( fileExists ( guess = new URL ( `./${ packageConfig . main } ` ,
210240 packageJSONUrl ) ) ) {
211241 return guess ;
212- }
213- if ( fileExists ( guess = new URL ( `./${ packageConfig . main } .js` ,
214- packageJSONUrl ) ) ) {
215- return guess ;
216- }
217- if ( fileExists ( guess = new URL ( `./${ packageConfig . main } .json` ,
218- packageJSONUrl ) ) ) {
219- return guess ;
220- }
221- if ( fileExists ( guess = new URL ( `./${ packageConfig . main } .node` ,
222- packageJSONUrl ) ) ) {
223- return guess ;
224- }
225- if ( fileExists ( guess = new URL ( `./${ packageConfig . main } /index.js` ,
226- packageJSONUrl ) ) ) {
227- return guess ;
228- }
229- if ( fileExists ( guess = new URL ( `./${ packageConfig . main } /index.json` ,
230- packageJSONUrl ) ) ) {
231- return guess ;
232- }
233- if ( fileExists ( guess = new URL ( `./${ packageConfig . main } /index.node` ,
234- packageJSONUrl ) ) ) {
242+ } else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } .js` ,
243+ packageJSONUrl ) ) ) ;
244+ else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } .json` ,
245+ packageJSONUrl ) ) ) ;
246+ else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } .node` ,
247+ packageJSONUrl ) ) ) ;
248+ else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } /index.js` ,
249+ packageJSONUrl ) ) ) ;
250+ else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } /index.json` ,
251+ packageJSONUrl ) ) ) ;
252+ else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } /index.node` ,
253+ packageJSONUrl ) ) ) ;
254+ else guess = undefined ;
255+ if ( guess ) {
256+ emitLegacyIndexDeprecation ( guess , packageJSONUrl , base ,
257+ packageConfig . main ) ;
235258 return guess ;
236259 }
237260 // Fallthrough.
238261 }
239- if ( fileExists ( guess = new URL ( './index.js' , packageJSONUrl ) ) ) {
240- return guess ;
241- }
262+ if ( fileExists ( guess = new URL ( './index.js' , packageJSONUrl ) ) ) ;
242263 // So fs.
243- if ( fileExists ( guess = new URL ( './index.json' , packageJSONUrl ) ) ) {
244- return guess ;
245- }
246- if ( fileExists ( guess = new URL ( './index.node' , packageJSONUrl ) ) ) {
264+ else if ( fileExists ( guess = new URL ( './index.json' , packageJSONUrl ) ) ) ;
265+ else if ( fileExists ( guess = new URL ( './index.node' , packageJSONUrl ) ) ) ;
266+ else guess = undefined ;
267+ if ( guess ) {
268+ emitLegacyIndexDeprecation ( guess , packageJSONUrl , base , packageConfig . main ) ;
247269 return guess ;
248270 }
249271 // Not found.
@@ -891,3 +913,6 @@ module.exports = {
891913 packageExportsResolve,
892914 packageImportsResolve
893915} ;
916+
917+ // cycle
918+ const { defaultGetFormat } = require ( 'internal/modules/esm/get_format' ) ;
0 commit comments