forked from swc-project/swc-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
47 lines (39 loc) · 1.24 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
44
45
46
47
import { createHash } from 'crypto'
import { Options, transformJest } from '@swc-node/core'
import { Output } from '@swc/core'
const Cache = new Map<string, Output>()
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 {}
}
export = {
process(src: string, path: string, jestConfig: JestConfig26 | JestConfig27) {
if (/\.(t|j)sx?$/.test(path)) {
// sha1 is fast, and we don't care about security here
const cacheHash = createHash('sha1')
cacheHash.update(src)
const hash = cacheHash.digest('hex')
const cacheKey = `${path}-${hash}`
if (Cache.has(cacheKey)) {
return Cache.get(cacheKey)
}
const output = transformJest(src, path, getJestTransformConfig(jestConfig))
Cache.set(cacheKey, output)
return output
}
return src
},
}