-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathruntime.js
140 lines (131 loc) · 3.04 KB
/
runtime.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'use strict'
// runtime cache
const cache = {}
let processArgv
function checkProcessArgv (moduleName) {
/* c8 ignore start */
// nullish needed for non Node.js runtime
processArgv ??= (process.execArgv ?? []).concat(process.argv ?? [])
/* c8 ignore stop */
return processArgv.some((arg) => arg.indexOf(moduleName) >= 0)
}
let preloadModules
function checkPreloadModules (moduleName) {
/* c8 ignore start */
// nullish needed for non Node.js runtime
preloadModules ??= (process._preload_modules ?? [])
/* c8 ignore stop */
return preloadModules.includes(moduleName)
}
let preloadModulesString
function checkPreloadModulesString (moduleName) {
preloadModulesString ??= preloadModules.toString()
return preloadModulesString.includes(moduleName)
}
function checkEnvVariable (name, value) {
return value
? process.env[name] === value
: process.env[name] !== undefined
}
function isBun () {
return 'Bun' in globalThis
}
function isDeno () {
return 'Deno' in globalThis
}
const runtime = {}
// use Object.defineProperties to provide lazy load
Object.defineProperties(runtime, {
tsNode: {
get () {
cache.tsNode ??= (
// --require tsnode/register
(Symbol.for('ts-node.register.instance') in process) ||
// --loader ts-node/esm
checkProcessArgv('ts-node/esm') ||
// ts-node-dev
!!process.env.TS_NODE_DEV
)
return cache.tsNode
}
},
babelNode: {
get () {
cache.babelNode ??= checkProcessArgv('babel-node')
return cache.babelNode
}
},
vitest: {
get () {
cache.vitest ??= (
checkEnvVariable('VITEST', 'true') ||
checkEnvVariable('VITEST_WORKER_ID')
)
return cache.vitest
}
},
jest: {
get () {
cache.jest ??= checkEnvVariable('JEST_WORKER_ID')
return cache.jest
}
},
swc: {
get () {
cache.swc ??= (
checkPreloadModules('@swc/register') ||
checkPreloadModules('@swc-node/register') ||
checkProcessArgv('.bin/swc-node')
)
return cache.swc
}
},
tsm: {
get () {
cache.tsm ??= checkPreloadModules('tsm')
return cache.tsm
}
},
esbuild: {
get () {
cache.esbuild ??= checkPreloadModules('esbuild-register')
return cache.esbuild
}
},
tsx: {
get () {
cache.tsx ??= checkPreloadModulesString('tsx')
return cache.tsx
}
},
supportTypeScript: {
get () {
cache.supportTypeScript ??= (
checkEnvVariable('FASTIFY_AUTOLOAD_TYPESCRIPT') ||
isBun() ||
isDeno() ||
runtime.tsNode ||
runtime.vitest ||
runtime.babelNode ||
runtime.jest ||
runtime.swc ||
runtime.tsm ||
runtime.tsx ||
runtime.esbuild
)
return cache.supportTypeScript
}
},
forceESM: {
get () {
cache.forceESM ??= (
checkProcessArgv('ts-node/esm') ||
runtime.vitest ||
false
)
return cache.forceESM
}
}
})
module.exports = runtime
module.exports.runtime = runtime