diff --git a/.eslintrc.yaml b/.eslintrc.yaml index bd522c0..55dec56 100644 --- a/.eslintrc.yaml +++ b/.eslintrc.yaml @@ -21,3 +21,6 @@ ignorePatterns: - test/fixtures/circular-a.js - test/fixtures/circular-b.js - test/fixtures/reexport.js + - test/fixtures/duplicate-explicit.mjs + - test/fixtures/duplicate.mjs + - test/fixtures/export-types/default-call-expression-renamed.mjs diff --git a/hook.js b/hook.js index 6f0adbd..bbeac27 100644 --- a/hook.js +++ b/hook.js @@ -259,6 +259,7 @@ async function processModule ({ srcUrl, context, parentGetSource, parentResolve, $${n} = v return true } + get.${n} = () => $${n} `) } } @@ -402,10 +403,11 @@ const _ = Object.assign( namespace ) const set = {} +const get = {} ${Array.from(setters.values()).join('\n')} -register(${JSON.stringify(realUrl)}, _, set, ${JSON.stringify(specifiers.get(realUrl))}) +register(${JSON.stringify(realUrl)}, _, set, get, ${JSON.stringify(specifiers.get(realUrl))}) ` } } catch (cause) { diff --git a/lib/register.js b/lib/register.js index a81c48c..9c06cb3 100644 --- a/lib/register.js +++ b/lib/register.js @@ -4,6 +4,7 @@ const importHooks = [] // TODO should this be a Set? const setters = new WeakMap() +const getters = new WeakMap() const specifiers = new Map() const toHook = [] @@ -12,6 +13,13 @@ const proxyHandler = { return setters.get(target)[name](value) }, + get (target, name) { + if (name === Symbol.toStringTag) { + return 'Module' + } + return getters.get(target)[name]() + }, + defineProperty (target, property, descriptor) { if ((!('value' in descriptor))) { throw new Error('Getters/setters are not supported for exports property descriptors.') @@ -21,9 +29,10 @@ const proxyHandler = { } } -function register (name, namespace, set, specifier) { +function register (name, namespace, set, get, specifier) { specifiers.set(name, specifier) setters.set(namespace, set) + getters.set(namespace, get) const proxy = new Proxy(namespace, proxyHandler) importHooks.forEach(hook => hook(name, proxy)) toHook.push([name, proxy]) diff --git a/test/hook/v14-double-hook.mjs b/test/hook/v14-double-hook.mjs new file mode 100644 index 0000000..feb997c --- /dev/null +++ b/test/hook/v14-double-hook.mjs @@ -0,0 +1,24 @@ +import { fileURLToPath } from 'url' +import { join } from 'path' +import { strictEqual } from 'assert' +import Hook from '../../index.js' + +const toWrap = join(fileURLToPath(import.meta.url), '..', '..', 'fixtures', 'foo.mjs') + +Hook([toWrap], (exports) => { + const original = exports.foo + exports.foo = function foo () { + return original() + '-first' + } +}) + +Hook([toWrap], (exports) => { + const original = exports.foo + exports.foo = function foo () { + return original() + '-second' + } +}) + +const { foo } = await import('../fixtures/foo.mjs') + +strictEqual(foo(), 'foo-first-second')