@@ -36,14 +36,16 @@ const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
36
36
const experimentalNetworkImports =
37
37
getOptionValue ( '--experimental-network-imports' ) ;
38
38
const typeFlag = getOptionValue ( '--input-type' ) ;
39
- const { URL , pathToFileURL, fileURLToPath, isURL, toPathIfFileURL } = require ( 'internal/url' ) ;
39
+ const { URL , pathToFileURL, fileURLToPath, isURL } = require ( 'internal/url' ) ;
40
40
const { canParse : URLCanParse } = internalBinding ( 'url' ) ;
41
+ const { legacyMainResolve : URLLegacyMainResolve } = internalBinding ( 'fs' ) ;
41
42
const {
42
43
ERR_INPUT_TYPE_NOT_ALLOWED ,
43
44
ERR_INVALID_ARG_TYPE ,
44
45
ERR_INVALID_MODULE_SPECIFIER ,
45
46
ERR_INVALID_PACKAGE_CONFIG ,
46
47
ERR_INVALID_PACKAGE_TARGET ,
48
+ ERR_INVALID_URL ,
47
49
ERR_MANIFEST_DEPENDENCY_MISSING ,
48
50
ERR_MODULE_NOT_FOUND ,
49
51
ERR_PACKAGE_IMPORT_NOT_DEFINED ,
@@ -132,13 +134,18 @@ function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) {
132
134
133
135
const realpathCache = new SafeMap ( ) ;
134
136
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
+ ] ;
142
149
143
150
/**
144
151
* Legacy CommonJS main resolution:
@@ -153,44 +160,29 @@ function fileExists(url) {
153
160
* @returns {URL }
154
161
*/
155
162
function 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
+ // less than zero means that happened some error
166
+ // -2: LegacyMainResolveReturnType::kInvalidUrl
167
+ // -1: LegacyMainResolveReturnType::kModuleNotFound
168
+ if ( resolvedOption < 0 ) {
169
+ if ( resolvedOption === - 2 ) {
170
+ throw new ERR_INVALID_URL ( packageConfig . main || './index.js' ) ;
171
+ } else {
172
+ throw new ERR_MODULE_NOT_FOUND (
173
+ fileURLToPath ( new URL ( '.' , packageJSONUrl ) ) , fileURLToPath ( base ) ) ;
179
174
}
180
- // Fallthrough.
181
175
}
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 ;
190
- }
191
- // Not found.
192
- throw new ERR_MODULE_NOT_FOUND (
193
- fileURLToPath ( new URL ( '.' , packageJSONUrl ) ) , fileURLToPath ( base ) ) ;
176
+
177
+ // 0-6: when packageConfig.main is defined
178
+ // 7-9: when packageConfig.main is NOT defined,
179
+ // or when the previous case didn't found the file
180
+ const baseUrl = resolvedOption <= 6 ? `./${ packageConfig . main } ` : '' ;
181
+ const guess = new URL ( baseUrl + mainResolveExtensions [ resolvedOption ] , packageJSONUrl ) ;
182
+
183
+ emitLegacyIndexDeprecation ( guess , packageJSONUrl , base , packageConfig . main ) ;
184
+
185
+ return guess ;
194
186
}
195
187
196
188
const encodedSepRegEx = / % 2 F | % 5 C / i;
@@ -1078,6 +1070,7 @@ module.exports = {
1078
1070
packageExportsResolve,
1079
1071
packageImportsResolve,
1080
1072
throwIfInvalidParentURL,
1073
+ legacyMainResolve,
1081
1074
} ;
1082
1075
1083
1076
// cycle
0 commit comments