diff --git a/src/esmock.js b/src/esmock.js index 5a3a1bd3..9fb7bf9a 100644 --- a/src/esmock.js +++ b/src/esmock.js @@ -1,3 +1,7 @@ +import module from 'node:module' +import threads from 'node:worker_threads' +import * as hooks from './esmockLoader.js' +import esmockRegister from './esmockRegister.js' import esmockLoader from './esmockLoader.js' import esmockModule from './esmockModule.js' import esmockArgs from './esmockArgs.js' @@ -5,7 +9,7 @@ import esmockErr from './esmockErr.js' const esmockGo = opts => async (...args) => { const [moduleId, parent, defs, gdefs, opt] = esmockArgs(args, opts) - if (!await esmockLoader()) + if (!esmockRegister && !await esmockLoader()) throw esmockErr.errMissingLoader() const fileURLKey = await esmockModule(moduleId, parent, defs, gdefs, opt) @@ -31,4 +35,10 @@ const esmock = Object.assign(esmockGo(), { purge, p: esmockGo({ purge: false }), strict, strictest }) export {esmock as default, strict, strictest} -export * from './esmockLoader.js' + + +const isMessageChannel = Boolean(module.register && threads.MessageChannel) +const hooksFinal = isMessageChannel ? {} : hooks +const { load, resolve, getSource, initialize, globalPreload } = hooksFinal +export { load, resolve, getSource, initialize, globalPreload } +// export * from './esmockLoader.js' diff --git a/src/esmockLoader.js b/src/esmockLoader.js index b78be4f5..269a9801 100644 --- a/src/esmockLoader.js +++ b/src/esmockLoader.js @@ -1,4 +1,6 @@ import fs from 'node:fs/promises' +import module from 'node:module' +import threads from 'node:worker_threads' import process from 'process' import esmockErr from './esmockErr.js' @@ -28,11 +30,13 @@ const hashbangRe = /^(#![^\n]*\n)/ const moduleIdReCreate = (moduleid, treeid) => new RegExp( `.*(${moduleid}(\\?${treeid}(?:(?!#-#).)*)).*`) +const isMessageChannel = Boolean(module.register && threads.MessageChannel) + // node v12.0-v18.x, global const mockKeys = global.mockKeys = (global.mockKeys || {}) // node v20.0-v20.6 -const globalPreload = (({ port }) => ( +const globalPreload = !isMessageChannel && (({ port }) => ( port.addEventListener('message', ev => ( mockKeys[ev.data.key] = ev.data.keylong)), port.unref(), @@ -40,13 +44,13 @@ const globalPreload = (({ port }) => ( )) // node v20.6-current -const initialize = data => { +const initialize = isMessageChannel && (data => { if (data && data.port) { data.port.on('message', msg => { mockKeys[msg.key] = msg.keylong }) } -} +}) const parseImports = defstr => { const [specifier, imports] = (defstr.match(esmkImportRe) || []) @@ -220,7 +224,7 @@ export { getSource, // if getSource and initialize exported at the time, // getSource is never called :( - // initialize, + initialize, globalPreload, loaderIsVerified as default } diff --git a/src/esmockRegister.js b/src/esmockRegister.js index e2cd2793..6c897b9a 100644 --- a/src/esmockRegister.js +++ b/src/esmockRegister.js @@ -8,8 +8,8 @@ const register = (res => () => { if (typeof res === 'boolean') return res - if (!(res = global.shouldRegister === true)) - return res + // if (!(res = global.shouldRegister === true)) + // return res if ((res = Boolean(channel && module.register))) { module.register('./esmockLoader.js', {