2
2
3
3
const getEsmExports = require ( './get-esm-exports.js' )
4
4
const { parse : parseCjs } = require ( 'cjs-module-lexer' )
5
- const { readFileSync, existsSync } = require ( 'fs' )
5
+ const { readFileSync } = require ( 'fs' )
6
6
const { builtinModules } = require ( 'module' )
7
7
const { fileURLToPath, pathToFileURL } = require ( 'url' )
8
8
const { dirname, join } = require ( 'path' )
@@ -30,8 +30,8 @@ const urlsBeingProcessed = new Set() // Guard against circular imports.
30
30
/**
31
31
* This function looks for the package.json which contains the specifier trying to resolve.
32
32
* Once the package.json file has been found, we extract the file path from the specifier
33
- * @param {* } specifier The specifier that is being search for inside the imports object
34
- * @param {* } fromUrl The url from which the search starts from
33
+ * @param {string } specifier The specifier that is being search for inside the imports object
34
+ * @param {URL|string } fromUrl The url from which the search starts from
35
35
* @returns file to url to file export
36
36
*/
37
37
function resolvePackageImports ( specifier , fromUrl ) {
@@ -47,9 +47,18 @@ function resolvePackageImports (specifier, fromUrl) {
47
47
while ( currentDir !== dirname ( currentDir ) ) {
48
48
const packageJsonPath = join ( currentDir , 'package.json' )
49
49
50
- if ( existsSync ( packageJsonPath ) ) {
51
- const packageJson = JSON . parse ( readFileSync ( packageJsonPath , 'utf8' ) )
50
+ let packageJson
51
+ try {
52
+ packageJson = JSON . parse ( readFileSync ( packageJsonPath , 'utf8' ) )
53
+ } catch ( error ) {
54
+ if ( error . code === 'ENOENT' ) {
55
+ currentDir = dirname ( currentDir )
56
+ continue
57
+ }
58
+ throw error
59
+ }
52
60
61
+ if ( packageJson ) {
53
62
if ( packageJson . imports && packageJson . imports [ specifier ] ) {
54
63
const imports = packageJson . imports [ specifier ]
55
64
@@ -70,13 +79,12 @@ function resolvePackageImports (specifier, fromUrl) {
70
79
}
71
80
72
81
if ( resolvedPath ) {
73
- return resolvedPath
82
+ return pathToFileURL ( require . resolve ( resolvedPath , { paths : [ currentDir ] } ) ) . href
74
83
}
75
84
}
76
85
// return if we find a package.json but did not find an import
77
86
return null
78
87
}
79
- currentDir = dirname ( currentDir )
80
88
}
81
89
} catch ( error ) {
82
90
throw new Error ( `Failed to find sub path export: ${ specifier } ` )
@@ -105,17 +113,18 @@ async function getCjsExports (url, context, parentLoad, source) {
105
113
re = './'
106
114
}
107
115
116
+ let newUrl
108
117
// Entries in the import field should always start with #
109
118
if ( re . startsWith ( '#' ) ) {
110
- re = resolvePackageImports ( re , url )
111
- if ( ! re ) {
119
+ newUrl = resolvePackageImports ( re , url )
120
+ if ( ! newUrl ) {
112
121
// Unable to resolve specifier import
113
122
return
114
123
}
124
+ } else {
125
+ newUrl = pathToFileURL ( require . resolve ( re , { paths : [ dirname ( fileURLToPath ( url ) ) ] } ) ) . href
115
126
}
116
127
117
- const newUrl = pathToFileURL ( require . resolve ( re , { paths : [ dirname ( fileURLToPath ( url ) ) ] } ) ) . href
118
-
119
128
if ( newUrl . endsWith ( '.node' ) || newUrl . endsWith ( '.json' ) ) {
120
129
return
121
130
}
0 commit comments