@@ -22,6 +22,7 @@ const {
22
22
StringPrototypeStartsWith,
23
23
encodeURIComponent,
24
24
} = primordials ;
25
+ const assert = require ( 'internal/assert' ) ;
25
26
const internalFS = require ( 'internal/fs/utils' ) ;
26
27
const { BuiltinModule } = require ( 'internal/bootstrap/realm' ) ;
27
28
const { realpathSync } = require ( 'fs' ) ;
@@ -117,18 +118,17 @@ function emitInvalidSegmentDeprecation(target, request, match, pjsonUrl, interna
117
118
* Emits a deprecation warning if the given URL is a module and
118
119
* the package.json file does not define a "main" or "exports" field.
119
120
* @param {URL } url - The URL of the module being resolved.
120
- * @param {URL } packageJSONUrl - The URL of the package.json file for the module.
121
+ * @param {string } path - The path of the module being resolved.
122
+ * @param {string } pkgPath - The path of the parent dir of the package.json file for the module.
121
123
* @param {string | URL } [base] - The base URL for the module being resolved.
122
124
* @param {string } [main] - The "main" field from the package.json file.
123
125
*/
124
- function emitLegacyIndexDeprecation ( url , packageJSONUrl , base , main ) {
126
+ function emitLegacyIndexDeprecation ( url , path , pkgPath , base , main ) {
125
127
if ( process . noDeprecation ) {
126
128
return ;
127
129
}
128
130
const format = defaultGetFormatWithoutErrors ( url ) ;
129
131
if ( format !== 'module' ) { return ; }
130
- const path = fileURLToPath ( url ) ;
131
- const pkgPath = fileURLToPath ( new URL ( '.' , packageJSONUrl ) ) ;
132
132
const basePath = fileURLToPath ( base ) ;
133
133
if ( ! main ) {
134
134
process . emitWarning (
@@ -196,20 +196,19 @@ const legacyMainResolveExtensionsIndexes = {
196
196
* @returns {URL }
197
197
*/
198
198
function legacyMainResolve ( packageJSONUrl , packageConfig , base ) {
199
- const packageJsonUrlString = packageJSONUrl . href ;
200
-
201
- if ( typeof packageJsonUrlString !== 'string' ) {
202
- throw new ERR_INVALID_ARG_TYPE ( 'packageJSONUrl' , [ 'URL' ] , packageJSONUrl ) ;
203
- }
199
+ assert ( isURL ( packageJSONUrl ) ) ;
200
+ const pkgPath = fileURLToPath ( new URL ( '.' , packageJSONUrl ) ) ;
204
201
205
202
const baseStringified = isURL ( base ) ? base . href : base ;
206
203
207
- const resolvedOption = FSLegacyMainResolve ( packageJsonUrlString , packageConfig . main , baseStringified ) ;
204
+ const resolvedOption = FSLegacyMainResolve ( pkgPath , packageConfig . main , baseStringified ) ;
208
205
209
- const baseUrl = resolvedOption <= legacyMainResolveExtensionsIndexes . kResolvedByMainIndexNode ? `./${ packageConfig . main } ` : '' ;
210
- const resolvedUrl = new URL ( baseUrl + legacyMainResolveExtensions [ resolvedOption ] , packageJSONUrl ) ;
206
+ const maybeMain = resolvedOption <= legacyMainResolveExtensionsIndexes . kResolvedByMainIndexNode ?
207
+ packageConfig . main || './' : '' ;
208
+ const resolvedPath = resolve ( pkgPath , maybeMain + legacyMainResolveExtensions [ resolvedOption ] ) ;
209
+ const resolvedUrl = pathToFileURL ( resolvedPath ) ;
211
210
212
- emitLegacyIndexDeprecation ( resolvedUrl , packageJSONUrl , base , packageConfig . main ) ;
211
+ emitLegacyIndexDeprecation ( resolvedUrl , resolvedPath , pkgPath , base , packageConfig . main ) ;
213
212
214
213
return resolvedUrl ;
215
214
}
@@ -790,8 +789,8 @@ function packageResolve(specifier, base, conditions) {
790
789
// ResolveSelf
791
790
const packageConfig = packageJsonReader . getPackageScopeConfig ( base ) ;
792
791
if ( packageConfig . exists ) {
793
- const packageJSONUrl = pathToFileURL ( packageConfig . pjsonPath ) ;
794
792
if ( packageConfig . exports != null && packageConfig . name === packageName ) {
793
+ const packageJSONUrl = pathToFileURL ( packageConfig . pjsonPath ) ;
795
794
return packageExportsResolve (
796
795
packageJSONUrl , packageSubpath , packageConfig , base , conditions ) ;
797
796
}
0 commit comments