-
Notifications
You must be signed in to change notification settings - Fork 306
/
initialize.mjs
55 lines (47 loc) · 1.51 KB
/
initialize.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* This file serves one of two purposes, depending on how it's used.
*
* If used with --import, it will import init.js and register the loader hook.
* If used with --loader, it will act as the loader hook, except that it will
* also import init.js inside the source code of the entrypoint file.
*
* The result is that no matter how this file is used, so long as it's with
* one of the two flags, the tracer will always be initialized, and the loader
* hook will always be active for ESM support.
*/
import { isMainThread } from 'worker_threads'
import { fileURLToPath } from 'node:url'
import {
load as origLoad,
resolve as origResolve,
getFormat as origGetFormat,
getSource as origGetSource
} from 'import-in-the-middle/hook.mjs'
let hasInsertedInit = false
function insertInit (result) {
if (!hasInsertedInit) {
hasInsertedInit = true
result.source = `
import '${fileURLToPath(new URL('./init.js', import.meta.url))}';
${result.source}`
}
return result
}
export async function load (...args) {
return insertInit(await origLoad(...args))
}
export const resolve = origResolve
export const getFormat = origGetFormat
export async function getSource (...args) {
return insertInit(await origGetSource(...args))
}
if (isMainThread) {
// Need this IIFE for versions of Node.js without top-level await.
(async () => {
await import('./init.js')
const { register } = await import('node:module')
if (register) {
register('./loader-hook.mjs', import.meta.url)
}
})()
}