1
1
import process from 'process'
2
+ import { isMainThread } from 'node:worker_threads'
2
3
import urlDummy from './esmockDummy.js'
3
4
import esmockErr from './esmockErr.js'
4
5
@@ -16,6 +17,13 @@ const withHashRe = /.*#-#/
16
17
const isesmRe = / i s e s m = t r u e /
17
18
const isnotfoundRe = / i s f o u n d = f a l s e /
18
19
20
+ const globalPreload = isMainThread && ( ( { port } ) => (
21
+ port . addEventListener ( 'message' , ev => (
22
+ global . mockKeys [ ev . data . key ] = ev . data . keylong ) ) ,
23
+ port . unref ( ) ,
24
+ 'global.postMessageEsmk = d => port.postMessage(d)'
25
+ ) )
26
+
19
27
// new versions of node: when multiple loaders are used and context
20
28
// is passed to nextResolve, the process crashes in a recursive call
21
29
// see: /esmock/issues/#48
@@ -123,4 +131,4 @@ const load = async (url, context, nextLoad) => {
123
131
// node lt 16.12 require getSource, node gte 16.12 warn remove getSource
124
132
const getSource = isLT1612 && load
125
133
126
- export { load , resolve , getSource , loaderIsVerified }
134
+ export { load , resolve , getSource , loaderIsVerified , globalPreload }
0 commit comments