Skip to content

Commit 6e3fe6a

Browse files
committed
Fixed issues when nested folders and added test for specifier and esm
import.
1 parent a153042 commit 6e3fe6a

File tree

6 files changed

+34
-14
lines changed

6 files changed

+34
-14
lines changed

lib/get-exports.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const getEsmExports = require('./get-esm-exports.js')
44
const { parse: parseCjs } = require('cjs-module-lexer')
5-
const { readFileSync, existsSync } = require('fs')
5+
const { readFileSync } = require('fs')
66
const { builtinModules } = require('module')
77
const { fileURLToPath, pathToFileURL } = require('url')
88
const { dirname, join } = require('path')
@@ -30,8 +30,8 @@ const urlsBeingProcessed = new Set() // Guard against circular imports.
3030
/**
3131
* This function looks for the package.json which contains the specifier trying to resolve.
3232
* 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
3535
* @returns file to url to file export
3636
*/
3737
function resolvePackageImports (specifier, fromUrl) {
@@ -47,9 +47,18 @@ function resolvePackageImports (specifier, fromUrl) {
4747
while (currentDir !== dirname(currentDir)) {
4848
const packageJsonPath = join(currentDir, 'package.json')
4949

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+
}
5260

61+
if (packageJson) {
5362
if (packageJson.imports && packageJson.imports[specifier]) {
5463
const imports = packageJson.imports[specifier]
5564

@@ -70,13 +79,12 @@ function resolvePackageImports (specifier, fromUrl) {
7079
}
7180

7281
if (resolvedPath) {
73-
return resolvedPath
82+
return pathToFileURL(require.resolve(resolvedPath, { paths: [currentDir] })).href
7483
}
7584
}
7685
// return if we find a package.json but did not find an import
7786
return null
7887
}
79-
currentDir = dirname(currentDir)
8088
}
8189
} catch (error) {
8290
throw new Error(`Failed to find sub path export: ${specifier}`)
@@ -105,17 +113,18 @@ async function getCjsExports (url, context, parentLoad, source) {
105113
re = './'
106114
}
107115

116+
let newUrl
108117
// Entries in the import field should always start with #
109118
if (re.startsWith('#')) {
110-
re = resolvePackageImports(re, url)
111-
if (!re) {
119+
newUrl = resolvePackageImports(re, url)
120+
if (!newUrl) {
112121
// Unable to resolve specifier import
113122
return
114123
}
124+
} else {
125+
newUrl = pathToFileURL(require.resolve(re, { paths: [dirname(fileURLToPath(url))] })).href
115126
}
116127

117-
const newUrl = pathToFileURL(require.resolve(re, { paths: [dirname(fileURLToPath(url))] })).href
118-
119128
if (newUrl.endsWith('.node') || newUrl.endsWith('.json')) {
120129
return
121130
}
File renamed without changes.

test/fixtures/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"import": {
1010
"node":"./something.mjs",
11-
"default": "./something.js"
11+
"default": "./something.mjs"
1212
}
1313
},
1414
"#main-entry-point-string" : "./something.js",

test/fixtures/specifier.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '#main-entry-point'
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { foo } from '../fixtures/specifier.mjs'
2+
import Hook from '../../index.js'
3+
import { strictEqual } from 'assert'
4+
Hook((exports, name) => {
5+
if (name.endsWith('fixtures/specifier.mjs')) {
6+
exports.foo = 1
7+
}
8+
})
9+
10+
strictEqual(foo, 1)

test/hook/specifier-imports.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { foo } from '../fixtures/specifier.js'
1+
import { foo } from '../fixtures/nested-folder/specifier.js'
22
import Hook from '../../index.js'
33
import { strictEqual } from 'assert'
44

55
Hook((exports, name) => {
6-
if (name.endsWith('fixtures/specifier.js')) {
6+
if (name.endsWith('fixtures/nested-folder/specifier.js')) {
77
exports.foo = 1
88
}
99
})

0 commit comments

Comments
 (0)