Skip to content

Commit

Permalink
fix(jest): internal cache logic duplicated with jest cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Nov 14, 2021
1 parent 2e6e3e9 commit 43a36b9
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions packages/jest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { xxh64 } from '@node-rs/xxhash'
import { Options, transformJest } from '@swc-node/core'
import type { Output } from '@swc/core'

const Cache = new Map<string, Output>()

interface JestConfig26 {
transform: [match: string, transformerPath: string, options: Options][]
}
Expand All @@ -27,18 +25,13 @@ function getJestTransformConfig(jestConfig: JestConfig26 | JestConfig27): Option
}

export = {
process(src: string, path: string, jestConfig: JestConfig26 | JestConfig27) {
process(src: string, path: string, jestConfig: JestConfig26 | JestConfig27): Output | string {
if (/\.(tsx?|jsx?|mjs)$/.test(path)) {
const hash = xxh64(src).toString(16)
const cacheKey = `${path}-${hash}`
const maybeCachedEntry = Cache.get(cacheKey)
if (maybeCachedEntry !== undefined) {
return maybeCachedEntry
}
const output = transformJest(src, path, getJestTransformConfig(jestConfig))
Cache.set(cacheKey, output)
return output
return transformJest(src, path, getJestTransformConfig(jestConfig))
}
return src
},
getCacheKey(src: string, _filepath: string, config: Options) {
return xxh64(src + JSON.stringify(config)).toString(16)
},
}

0 comments on commit 43a36b9

Please sign in to comment.