diff --git a/src/esmockCache.js b/src/esmockCache.js index 51c625b6..6e5e535b 100644 --- a/src/esmockCache.js +++ b/src/esmockCache.js @@ -1,3 +1,5 @@ +import esmockPostMessage from './esmockRegister.js' + const esmockCache = { isESM: {}, @@ -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 => ( diff --git a/src/esmockLoader.js b/src/esmockLoader.js index b5f0024e..9245b4ce 100644 --- a/src/esmockLoader.js +++ b/src/esmockLoader.js @@ -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) @@ -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) || []) @@ -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 @@ -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 +} diff --git a/src/esmockRegister.js b/src/esmockRegister.js new file mode 100644 index 00000000..fdbcf5cc --- /dev/null +++ b/src/esmockRegister.js @@ -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) +} diff --git a/tests/tests-node/package.json b/tests/tests-node/package.json index a75d8ae8..f79a3f70 100644 --- a/tests/tests-node/package.json +++ b/tests/tests-node/package.json @@ -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" } }