@@ -59,6 +59,37 @@ const userConditions = getOptionValue('--conditions');
59
59
const DEFAULT_CONDITIONS = ObjectFreeze ( [ 'node' , 'import' , ...userConditions ] ) ;
60
60
const DEFAULT_CONDITIONS_SET = new SafeSet ( DEFAULT_CONDITIONS ) ;
61
61
62
+ const pendingDeprecation = getOptionValue ( '--pending-deprecation' ) ;
63
+
64
+ function emitLegacyIndexDeprecation ( url , packageJSONUrl , base , main ) {
65
+ if ( ! pendingDeprecation )
66
+ return ;
67
+ const { format } = defaultGetFormat ( url ) ;
68
+ if ( format !== 'module' )
69
+ return ;
70
+ const path = fileURLToPath ( url ) ;
71
+ const pkgPath = fileURLToPath ( new URL ( '.' , packageJSONUrl ) ) ;
72
+ const basePath = fileURLToPath ( base ) ;
73
+ if ( main )
74
+ process . emitWarning (
75
+ `Package ${ pkgPath } has a "main" field set to ${ JSONStringify ( main ) } , ` +
76
+ `excluding the full filename and extension to the resolved file at "${
77
+ StringPrototypeSlice ( path , pkgPath . length ) } ", imported from ${
78
+ basePath } .\n Automatic extension resolution of the "main" field is` +
79
+ 'deprecated for ES modules.' ,
80
+ 'DeprecationWarning' ,
81
+ 'DEP0150'
82
+ ) ;
83
+ else
84
+ process . emitWarning (
85
+ `No "main" or "exports" field defined in the package.json for ${ pkgPath
86
+ } resolving the main entry point "${
87
+ StringPrototypeSlice ( path , pkgPath . length ) } ", imported from ${ basePath
88
+ } .\nDefault "index" lookups for the main are deprecated for ES modules.`,
89
+ 'DeprecationWarning' ,
90
+ 'DEP0150'
91
+ ) ;
92
+ }
62
93
63
94
function getConditionsSet ( conditions ) {
64
95
if ( conditions !== undefined && conditions !== DEFAULT_CONDITIONS ) {
@@ -181,41 +212,33 @@ function legacyMainResolve(packageJSONUrl, packageConfig, base) {
181
212
if ( fileExists ( guess = new URL ( `./${ packageConfig . main } ` ,
182
213
packageJSONUrl ) ) ) {
183
214
return guess ;
184
- }
185
- if ( fileExists ( guess = new URL ( `./${ packageConfig . main } .js` ,
186
- packageJSONUrl ) ) ) {
187
- return guess ;
188
- }
189
- if ( fileExists ( guess = new URL ( `./${ packageConfig . main } .json` ,
190
- packageJSONUrl ) ) ) {
191
- return guess ;
192
- }
193
- if ( fileExists ( guess = new URL ( `./${ packageConfig . main } .node` ,
194
- packageJSONUrl ) ) ) {
195
- return guess ;
196
- }
197
- if ( fileExists ( guess = new URL ( `./${ packageConfig . main } /index.js` ,
198
- packageJSONUrl ) ) ) {
199
- return guess ;
200
- }
201
- if ( fileExists ( guess = new URL ( `./${ packageConfig . main } /index.json` ,
202
- packageJSONUrl ) ) ) {
203
- return guess ;
204
- }
205
- if ( fileExists ( guess = new URL ( `./${ packageConfig . main } /index.node` ,
206
- packageJSONUrl ) ) ) {
215
+ } else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } .js` ,
216
+ packageJSONUrl ) ) ) ;
217
+ else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } .json` ,
218
+ packageJSONUrl ) ) ) ;
219
+ else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } .node` ,
220
+ packageJSONUrl ) ) ) ;
221
+ else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } /index.js` ,
222
+ packageJSONUrl ) ) ) ;
223
+ else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } /index.json` ,
224
+ packageJSONUrl ) ) ) ;
225
+ else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } /index.node` ,
226
+ packageJSONUrl ) ) ) ;
227
+ else guess = undefined ;
228
+ if ( guess ) {
229
+ emitLegacyIndexDeprecation ( guess , packageJSONUrl , base ,
230
+ packageConfig . main ) ;
207
231
return guess ;
208
232
}
209
233
// Fallthrough.
210
234
}
211
- if ( fileExists ( guess = new URL ( './index.js' , packageJSONUrl ) ) ) {
212
- return guess ;
213
- }
235
+ if ( fileExists ( guess = new URL ( './index.js' , packageJSONUrl ) ) ) ;
214
236
// So fs.
215
- if ( fileExists ( guess = new URL ( './index.json' , packageJSONUrl ) ) ) {
216
- return guess ;
217
- }
218
- if ( fileExists ( guess = new URL ( './index.node' , packageJSONUrl ) ) ) {
237
+ else if ( fileExists ( guess = new URL ( './index.json' , packageJSONUrl ) ) ) ;
238
+ else if ( fileExists ( guess = new URL ( './index.node' , packageJSONUrl ) ) ) ;
239
+ else guess = undefined ;
240
+ if ( guess ) {
241
+ emitLegacyIndexDeprecation ( guess , packageJSONUrl , base , packageConfig . main ) ;
219
242
return guess ;
220
243
}
221
244
// Not found.
@@ -864,3 +887,6 @@ module.exports = {
864
887
packageExportsResolve,
865
888
packageImportsResolve
866
889
} ;
890
+
891
+ // cycle
892
+ const { defaultGetFormat } = require ( 'internal/modules/esm/get_format' ) ;
0 commit comments