|
2 | 2 |
|
3 | 3 | 'use strict';
|
4 | 4 |
|
5 |
| -const fs = require('fs'); |
6 |
| -const crypto = require('crypto'); |
7 |
| -const tsc = require('typescript'); |
8 |
| -const tsconfigPath = require('app-root-path').resolve('/tsconfig.json'); |
9 |
| -const THIS_FILE = fs.readFileSync(__filename); |
| 5 | +const tsJestPreprocessor = require('ts-jest/preprocessor'); |
10 | 6 |
|
11 |
| -let compilerConfig = { |
12 |
| - module: tsc.ModuleKind.CommonJS, |
13 |
| - jsx: tsc.JsxEmit.React, |
14 |
| -}; |
15 |
| - |
16 |
| -if (fs.existsSync(tsconfigPath)) { |
17 |
| - try { |
18 |
| - const tsconfig = tsc.readConfigFile(tsconfigPath).config; |
19 |
| - |
20 |
| - if (tsconfig && tsconfig.compilerOptions) { |
21 |
| - compilerConfig = tsconfig.compilerOptions; |
22 |
| - } |
23 |
| - } catch (e) { |
24 |
| - /* Do nothing - default is set */ |
25 |
| - } |
26 |
| -} |
27 |
| - |
28 |
| -module.exports = { |
29 |
| - process(src, path, config, options) { |
30 |
| - if (path.endsWith('.ts') || path.endsWith('.tsx')) { |
31 |
| - let compilerOptions = compilerConfig; |
32 |
| - if (options.instrument) { |
33 |
| - // inline source with source map for remapping coverage |
34 |
| - compilerOptions = Object.assign({}, compilerConfig); |
35 |
| - delete compilerOptions.sourceMap; |
36 |
| - compilerOptions.inlineSourceMap = true; |
37 |
| - compilerOptions.inlineSources = true; |
38 |
| - // fix broken paths in coverage report if `.outDir` is set |
39 |
| - delete compilerOptions.outDir; |
40 |
| - } |
41 |
| - |
42 |
| - const tsTranspiled = tsc.transpileModule(src, { |
43 |
| - compilerOptions: compilerOptions, |
44 |
| - fileName: path, |
45 |
| - }); |
46 |
| - return tsTranspiled.outputText; |
47 |
| - } |
48 |
| - return src; |
49 |
| - }, |
50 |
| - getCacheKey(fileData, filePath, configStr, options) { |
51 |
| - return crypto |
52 |
| - .createHash('md5') |
53 |
| - .update(THIS_FILE) |
54 |
| - .update('\0', 'utf8') |
55 |
| - .update(fileData) |
56 |
| - .update('\0', 'utf8') |
57 |
| - .update(filePath) |
58 |
| - .update('\0', 'utf8') |
59 |
| - .update(configStr) |
60 |
| - .update('\0', 'utf8') |
61 |
| - .update(JSON.stringify(compilerConfig)) |
62 |
| - .update('\0', 'utf8') |
63 |
| - .update(options.instrument ? 'instrument' : '') |
64 |
| - .digest('hex'); |
65 |
| - }, |
66 |
| -}; |
| 7 | +module.exports = tsJestPreprocessor; |
0 commit comments