Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jest): support jest 27 transformerConfig #396

Merged
merged 1 commit into from Apr 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 25 additions & 5 deletions packages/jest/index.ts
@@ -1,14 +1,34 @@
import { createHash } from 'crypto'

import { transformJest } from '@swc-node/core'
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: any) {
const [, , transformOptions = {}] =
(jestConfig.transform || []).find(([, transformerPath]: [string, string]) => transformerPath === __filename) || []
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')
Expand All @@ -18,7 +38,7 @@ export = {
if (Cache.has(cacheKey)) {
return Cache.get(cacheKey)
}
const output = transformJest(src, path, transformOptions)
const output = transformJest(src, path, getJestTransformConfig(jestConfig))
Cache.set(cacheKey, output)
return output
}
Expand Down