From e0d92df3210237476bc6937f4dab05272435ecce Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Tue, 18 Jun 2024 15:52:33 +0200 Subject: [PATCH] fix: CJS `require('.')` resolution --- lib/get-exports.js | 3 +++ test/fixtures/index.js | 1 + test/fixtures/require-root.js | 1 + test/hook/require-root-cjs.mjs | 12 ++++++++++++ 4 files changed, 17 insertions(+) create mode 100644 test/fixtures/index.js create mode 100644 test/fixtures/require-root.js create mode 100644 test/hook/require-root-cjs.mjs diff --git a/lib/get-exports.js b/lib/get-exports.js index 9a78e8c..de88a37 100644 --- a/lib/get-exports.js +++ b/lib/get-exports.js @@ -43,6 +43,9 @@ async function getCjsExports (url, context, parentLoad, source) { full.add(each) } } else { + if (re === '.') { + re = './' + } // Resolve the re-exported module relative to the current module. const newUrl = pathToFileURL(require.resolve(re, { paths: [dirname(fileURLToPath(url))] })).href for (const each of await getExports(newUrl, context, parentLoad)) { diff --git a/test/fixtures/index.js b/test/fixtures/index.js new file mode 100644 index 0000000..a115263 --- /dev/null +++ b/test/fixtures/index.js @@ -0,0 +1 @@ +module.exports.foo = 'something' diff --git a/test/fixtures/require-root.js b/test/fixtures/require-root.js new file mode 100644 index 0000000..8b4cc65 --- /dev/null +++ b/test/fixtures/require-root.js @@ -0,0 +1 @@ +module.exports = { ...require('.') } diff --git a/test/hook/require-root-cjs.mjs b/test/hook/require-root-cjs.mjs new file mode 100644 index 0000000..9f99909 --- /dev/null +++ b/test/hook/require-root-cjs.mjs @@ -0,0 +1,12 @@ +import Hook from '../../index.js' +import { foo } from '../fixtures/require-root.js' +import { strictEqual } from 'assert' + +Hook((exports, name) => { + if (name.endsWith('require-root.js')) { + strictEqual(exports.foo, 'something') + exports.foo += '-wrap' + } +}) + +strictEqual(foo, 'something-wrap')