Skip to content

Commit 29c77b5

Browse files
authored
fix: CJS require('.') resolution (#108)
1 parent 3b25c63 commit 29c77b5

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

lib/get-exports.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ async function getCjsExports (url, context, parentLoad, source) {
4343
full.add(each)
4444
}
4545
} else {
46+
if (re === '.') {
47+
re = './'
48+
}
4649
// Resolve the re-exported module relative to the current module.
4750
const newUrl = pathToFileURL(require.resolve(re, { paths: [dirname(fileURLToPath(url))] })).href
4851
for (const each of await getExports(newUrl, context, parentLoad)) {

test/fixtures/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports.foo = 'something'

test/fixtures/require-root.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = { ...require('.') }

test/hook/require-root-cjs.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Hook from '../../index.js'
2+
import { foo } from '../fixtures/require-root.js'
3+
import { strictEqual } from 'assert'
4+
5+
Hook((exports, name) => {
6+
if (name.endsWith('require-root.js')) {
7+
strictEqual(exports.foo, 'something')
8+
exports.foo += '-wrap'
9+
}
10+
})
11+
12+
strictEqual(foo, 'something-wrap')

0 commit comments

Comments
 (0)