Skip to content

Commit

Permalink
add support for initialize hook
Browse files Browse the repository at this point in the history
  • Loading branch information
bumblehead committed Sep 8, 2023
1 parent d953587 commit 5bab2ac
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/esmockCache.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import esmockPostMessage from './esmockRegister.js'

const esmockCache = {
isESM: {},

Expand All @@ -7,8 +9,7 @@ const esmockCache = {
}

const esmockTreeIdSet = (key, keylong) => (
typeof global.postMessageEsmk === 'function'
&& global.postMessageEsmk({ key, keylong }),
esmockPostMessage({ key, keylong }),
global.mockKeys[String(key)] = keylong)

const esmockTreeIdGet = key => (
Expand Down
27 changes: 24 additions & 3 deletions src/esmockLoader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'node:fs/promises'
import process from 'process'
import esmockErr from './esmockErr.js'
// import './esmockCache.js'

const [major, minor] = process.versions.node.split('.').map(it => +it)
const isLT1612 = major < 16 || (major === 16 && minor < 12)
Expand Down Expand Up @@ -28,13 +29,26 @@ const hashbangRe = /^(#![^\n]*\n)/
const moduleIdReCreate = (moduleid, treeid) => new RegExp(
`.*(${moduleid}(\\?${treeid}(?:(?!#-#).)*)).*`)

// node v12.0-v18.x, global
const mockKeys = global.mockKeys || {}

// node v20.0-v20.6
const globalPreload = (({ port }) => (
port.addEventListener('message', ev => (
global.mockKeys[ev.data.key] = ev.data.keylong)),
mockKeys[ev.data.key] = ev.data.keylong)),
port.unref(),
'global.postMessageEsmk = d => port.postMessage(d)'
))

// node v20.6-current
const initialize = data => {
if (data && data.port) {
data.port.on('message', msg => {
mockKeys[msg.key] = msg.keylong
})
}
}

const parseImports = defstr => {
const [specifier, imports] = (defstr.match(esmkImportRe) || [])

Expand All @@ -57,7 +71,7 @@ const parseImportsTree = treeidspec => {
}

const treeidspecFromUrl = url => esmkIdRe.test(url)
&& global.esmockTreeIdGet(url.match(esmkIdRe)[0].split('=')[1])
&& mockKeys[url.match(esmkIdRe)[0].split('=')[1]]

// new versions of node: when multiple loaders are used and context
// is passed to nextResolve, the process crashes in a recursive call
Expand Down Expand Up @@ -201,4 +215,11 @@ const load = async (url, context, nextLoad) => {
// node lt 16.12 require getSource, node gte 16.12 warn remove getSource
const getSource = isLT1612 && load

export {load, resolve, getSource, globalPreload, loaderIsVerified as default}
export {
load,
resolve,
getSource,
initialize,
globalPreload,
loaderIsVerified as default
}
17 changes: 17 additions & 0 deletions src/esmockRegister.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { register } from 'node:module'
import { MessageChannel } from 'node:worker_threads'

const { port2, port1 } = new MessageChannel()

register('./esmockLoader.js', {
parentURL: import.meta.url,
data: { port: port2 },
transferList: [port2]
})

export default msg => {
if (typeof global.postMessageEsmk === 'function')
global.postMessageEsmk(msg)
if (port1)
port1.postMessage(msg)
}
3 changes: 2 additions & 1 deletion tests/tests-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"scripts": {
"test:metaresolve": "node --experimental-import-meta-resolve --loader=esmock --test",
"test": "node --loader=esmock --test && npm run test:metaresolve",
"test-one": "node --loader=esmock --test"
"test-one": "node --loader=esmock --test",
"test-one-no--loader": "node --test"
}
}

0 comments on commit 5bab2ac

Please sign in to comment.