@@ -36,8 +36,9 @@ 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 : FSLegacyMainResolve } = internalBinding ( 'fs' ) ;
41
42
const {
42
43
ERR_INPUT_TYPE_NOT_ALLOWED ,
43
44
ERR_INVALID_ARG_TYPE ,
@@ -133,13 +134,34 @@ function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) {
133
134
134
135
const realpathCache = new SafeMap ( ) ;
135
136
136
- /**
137
- * @param {string | URL } url
138
- * @returns {boolean }
139
- */
140
- function fileExists ( url ) {
141
- return internalModuleStat ( toNamespacedPath ( toPathIfFileURL ( url ) ) ) === 0 ;
142
- }
137
+ const legacyMainResolveExtensions = [
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
+ ] ;
149
+
150
+ const legacyMainResolveExtensionsIndexes = {
151
+ // 0-6: when packageConfig.main is defined
152
+ kResolvedByMain : 0 ,
153
+ kResolvedByMainJs : 1 ,
154
+ kResolvedByMainJson : 2 ,
155
+ kResolvedByMainNode : 3 ,
156
+ kResolvedByMainIndexJs : 4 ,
157
+ kResolvedByMainIndexJson : 5 ,
158
+ kResolvedByMainIndexNode : 6 ,
159
+ // 7-9: when packageConfig.main is NOT defined,
160
+ // or when the previous case didn't found the file
161
+ kResolvedByPackageAndJs : 7 ,
162
+ kResolvedByPackageAndJson : 8 ,
163
+ kResolvedByPackageAndNode : 9 ,
164
+ } ;
143
165
144
166
/**
145
167
* Legacy CommonJS main resolution:
@@ -154,44 +176,22 @@ function fileExists(url) {
154
176
* @returns {URL }
155
177
*/
156
178
function legacyMainResolve ( packageJSONUrl , packageConfig , base ) {
157
- let guess ;
158
- if ( packageConfig . main !== undefined ) {
159
- // Note: fs check redundances will be handled by Descriptor cache here.
160
- if ( fileExists ( guess = new URL ( `./${ packageConfig . main } ` ,
161
- packageJSONUrl ) ) ) {
162
- return guess ;
163
- } else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } .js` ,
164
- packageJSONUrl ) ) ) ;
165
- else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } .json` ,
166
- packageJSONUrl ) ) ) ;
167
- else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } .node` ,
168
- packageJSONUrl ) ) ) ;
169
- else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } /index.js` ,
170
- packageJSONUrl ) ) ) ;
171
- else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } /index.json` ,
172
- packageJSONUrl ) ) ) ;
173
- else if ( fileExists ( guess = new URL ( `./${ packageConfig . main } /index.node` ,
174
- packageJSONUrl ) ) ) ;
175
- else guess = undefined ;
176
- if ( guess ) {
177
- emitLegacyIndexDeprecation ( guess , packageJSONUrl , base ,
178
- packageConfig . main ) ;
179
- return guess ;
180
- }
181
- // Fallthrough.
182
- }
183
- if ( fileExists ( guess = new URL ( './index.js' , packageJSONUrl ) ) ) ;
184
- // So fs.
185
- else if ( fileExists ( guess = new URL ( './index.json' , packageJSONUrl ) ) ) ;
186
- else if ( fileExists ( guess = new URL ( './index.node' , packageJSONUrl ) ) ) ;
187
- else guess = undefined ;
188
- if ( guess ) {
189
- emitLegacyIndexDeprecation ( guess , packageJSONUrl , base , packageConfig . main ) ;
190
- return guess ;
179
+ const packageJsonUrlString = packageJSONUrl . href ;
180
+
181
+ if ( typeof packageJsonUrlString !== 'string' ) {
182
+ throw new ERR_INVALID_ARG_TYPE ( 'packageJSONUrl' , [ 'URL' ] , packageJSONUrl ) ;
191
183
}
192
- // Not found.
193
- throw new ERR_MODULE_NOT_FOUND (
194
- fileURLToPath ( new URL ( '.' , packageJSONUrl ) ) , fileURLToPath ( base ) ) ;
184
+
185
+ const baseStringified = isURL ( base ) ? base . href : base ;
186
+
187
+ const resolvedOption = FSLegacyMainResolve ( packageJsonUrlString , packageConfig . main , baseStringified ) ;
188
+
189
+ const baseUrl = resolvedOption <= legacyMainResolveExtensionsIndexes . kResolvedByMainIndexNode ? `./${ packageConfig . main } ` : '' ;
190
+ const resolvedUrl = new URL ( baseUrl + legacyMainResolveExtensions [ resolvedOption ] , packageJSONUrl ) ;
191
+
192
+ emitLegacyIndexDeprecation ( resolvedUrl , packageJSONUrl , base , packageConfig . main ) ;
193
+
194
+ return resolvedUrl ;
195
195
}
196
196
197
197
const encodedSepRegEx = / % 2 F | % 5 C / i;
@@ -1078,6 +1078,7 @@ module.exports = {
1078
1078
packageExportsResolve,
1079
1079
packageImportsResolve,
1080
1080
throwIfInvalidParentURL,
1081
+ legacyMainResolve,
1081
1082
} ;
1082
1083
1083
1084
// cycle
0 commit comments