Skip to content

Commit e0d8080

Browse files
authored
fix: Support Hooking multiple times (#153)
1 parent b74cd07 commit e0d8080

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

.eslintrc.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ ignorePatterns:
2121
- test/fixtures/circular-a.js
2222
- test/fixtures/circular-b.js
2323
- test/fixtures/reexport.js
24+
- test/fixtures/duplicate-explicit.mjs
25+
- test/fixtures/duplicate.mjs
26+
- test/fixtures/export-types/default-call-expression-renamed.mjs

hook.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ async function processModule ({ srcUrl, context, parentGetSource, parentResolve,
259259
$${n} = v
260260
return true
261261
}
262+
get.${n} = () => $${n}
262263
`)
263264
}
264265
}
@@ -402,10 +403,11 @@ const _ = Object.assign(
402403
namespace
403404
)
404405
const set = {}
406+
const get = {}
405407
406408
${Array.from(setters.values()).join('\n')}
407409
408-
register(${JSON.stringify(realUrl)}, _, set, ${JSON.stringify(specifiers.get(realUrl))})
410+
register(${JSON.stringify(realUrl)}, _, set, get, ${JSON.stringify(specifiers.get(realUrl))})
409411
`
410412
}
411413
} catch (cause) {

lib/register.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
const importHooks = [] // TODO should this be a Set?
66
const setters = new WeakMap()
7+
const getters = new WeakMap()
78
const specifiers = new Map()
89
const toHook = []
910

@@ -12,6 +13,13 @@ const proxyHandler = {
1213
return setters.get(target)[name](value)
1314
},
1415

16+
get (target, name) {
17+
if (name === Symbol.toStringTag) {
18+
return 'Module'
19+
}
20+
return getters.get(target)[name]()
21+
},
22+
1523
defineProperty (target, property, descriptor) {
1624
if ((!('value' in descriptor))) {
1725
throw new Error('Getters/setters are not supported for exports property descriptors.')
@@ -21,9 +29,10 @@ const proxyHandler = {
2129
}
2230
}
2331

24-
function register (name, namespace, set, specifier) {
32+
function register (name, namespace, set, get, specifier) {
2533
specifiers.set(name, specifier)
2634
setters.set(namespace, set)
35+
getters.set(namespace, get)
2736
const proxy = new Proxy(namespace, proxyHandler)
2837
importHooks.forEach(hook => hook(name, proxy))
2938
toHook.push([name, proxy])

test/hook/v14-double-hook.mjs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { fileURLToPath } from 'url'
2+
import { join } from 'path'
3+
import { strictEqual } from 'assert'
4+
import Hook from '../../index.js'
5+
6+
const toWrap = join(fileURLToPath(import.meta.url), '..', '..', 'fixtures', 'foo.mjs')
7+
8+
Hook([toWrap], (exports) => {
9+
const original = exports.foo
10+
exports.foo = function foo () {
11+
return original() + '-first'
12+
}
13+
})
14+
15+
Hook([toWrap], (exports) => {
16+
const original = exports.foo
17+
exports.foo = function foo () {
18+
return original() + '-second'
19+
}
20+
})
21+
22+
const { foo } = await import('../fixtures/foo.mjs')
23+
24+
strictEqual(foo(), 'foo-first-second')

0 commit comments

Comments
 (0)