-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Copy pathload-config.ts
59 lines (50 loc) · 1.41 KB
/
load-config.ts
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
import jitiFactory from 'jiti'
import { transform } from 'sucrase'
import { Config } from '../../types/config'
let jiti: ReturnType<typeof jitiFactory> | null = null
// @internal
// This WILL be removed in some future release
// If you rely on this your stuff WILL break
export function useCustomJiti(_jiti: () => ReturnType<typeof jitiFactory>) {
jiti = _jiti()
}
function lazyJiti() {
return (
jiti ??
(jiti = jitiFactory(__filename, {
interopDefault: true,
transform: (opts) => {
// Sucrase can't transform import.meta so we have to use Babel
if (opts.source.includes('import.meta')) {
return require('jiti/dist/babel.js')(opts)
}
return transform(opts.source, {
transforms: ['typescript', 'imports'],
})
},
}))
)
}
export function loadConfig(path: string): Config {
let config = (function () {
// Always use jiti for now. There is a a bug that occurs in Node v22.12+
// where imported files return invalid results
return lazyJiti()(path)
// Always use jiti for ESM or TS files
if (
path &&
(path.endsWith('.mjs') ||
path.endsWith('.ts') ||
path.endsWith('.cts') ||
path.endsWith('.mts'))
) {
return lazyJiti()(path)
}
try {
return path ? require(path) : {}
} catch {
return lazyJiti()(path)
}
})()
return config.default ?? config
}