-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
index.ts
43 lines (36 loc) · 1.25 KB
/
index.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
import { xxh64 } from '@node-rs/xxhash'
import type { Output } from '@swc/core'
import { Options, transformJest } from '@swc-node/core'
import { readDefaultTsConfig, tsCompilerOptionsToSwcConfig } from '@swc-node/register/read-default-tsconfig'
interface JestConfig26 {
transform: [match: string, transformerPath: string, options: Options][]
}
interface JestConfig27 {
transformerConfig: Options
}
function getJestTransformConfig(jestConfig: JestConfig26 | JestConfig27): Options {
if ('transformerConfig' in jestConfig) {
// jest 27
return jestConfig.transformerConfig
}
if ('transform' in jestConfig) {
// jest 26
return jestConfig.transform.find(([, transformerPath]) => transformerPath === __filename)?.[2] ?? {}
}
return {}
}
const defaultTsConfig = readDefaultTsConfig()
export = {
process(src: string, path: string, jestConfig: JestConfig26 | JestConfig27): Output | string {
if (/\.(tsx?|jsx?|mjs)$/.test(path)) {
return transformJest(src, path, {
...tsCompilerOptionsToSwcConfig(defaultTsConfig, path),
...getJestTransformConfig(jestConfig),
})
}
return src
},
getCacheKey(src: string, _filepath: string, config: Options) {
return xxh64(src + JSON.stringify(config)).toString(16)
},
}