-
Notifications
You must be signed in to change notification settings - Fork 450
/
preprocessor.ts
101 lines (84 loc) · 2.52 KB
/
preprocessor.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import * as crypto from 'crypto';
import * as tsc from 'typescript';
import { JestConfig, Path, TransformOptions } from './jest-types';
import { flushLogs, logOnce } from './logger';
import { getPostProcessHook } from './postprocess';
import {
cacheFile,
getTSConfig,
getTSJestConfig,
runTsDiagnostics,
injectSourcemapHook,
} from './utils';
export function process(
src: string,
filePath: Path,
jestConfig: JestConfig,
transformOptions: TransformOptions = { instrument: false },
) {
// transformOptions.instrument is a proxy for collectCoverage
// https://github.com/kulshekhar/ts-jest/issues/201#issuecomment-300572902
const compilerOptions = getTSConfig(
jestConfig.globals,
jestConfig.rootDir,
transformOptions.instrument,
);
logOnce('final compilerOptions:', compilerOptions);
const isTsFile = /\.tsx?$/.test(filePath);
const isJsFile = /\.jsx?$/.test(filePath);
const isHtmlFile = /\.html$/.test(filePath);
// This is to support angular 2. See https://github.com/kulshekhar/ts-jest/pull/145
if (isHtmlFile && jestConfig.globals.__TRANSFORM_HTML__) {
src = 'module.exports=`' + src + '`;';
}
const processFile =
compilerOptions.allowJs === true ? isTsFile || isJsFile : isTsFile;
if (!processFile) {
return src;
}
const tsJestConfig = getTSJestConfig(jestConfig.globals);
logOnce('tsJestConfig: ', tsJestConfig);
if (tsJestConfig.enableTsDiagnostics) {
runTsDiagnostics(filePath, compilerOptions);
}
const tsTranspiled = tsc.transpileModule(src, {
compilerOptions,
fileName: filePath,
});
const postHook = getPostProcessHook(
compilerOptions,
jestConfig,
tsJestConfig,
);
const outputText = postHook(
tsTranspiled.outputText,
filePath,
jestConfig,
transformOptions,
);
const modified =
tsJestConfig.disableSourceMapSupport === true
? outputText
: injectSourcemapHook(filePath, tsTranspiled.outputText, outputText);
flushLogs();
return modified;
}
export function getCacheKey(
fileData: string,
filePath: Path,
jestConfigStr: string,
transformOptions: TransformOptions = { instrument: false },
): string {
const jestConfig: JestConfig = JSON.parse(jestConfigStr);
const tsConfig = getTSConfig(
jestConfig.globals,
jestConfig.rootDir,
transformOptions.instrument,
);
return crypto
.createHash('md5')
.update(JSON.stringify(tsConfig), 'utf8')
.update(JSON.stringify(transformOptions), 'utf8')
.update(fileData + filePath + jestConfigStr, 'utf8')
.digest('hex');
}