|
1 | 1 | const constants = require('./constants') |
2 | 2 | const loadPartialConfig = require('@babel/core').loadPartialConfig |
| 3 | +const { loadSync: loadTsConfigSync } = require('tsconfig') |
3 | 4 | const chalk = require('chalk') |
4 | 5 | const path = require('path') |
5 | 6 | const fs = require('fs') |
@@ -67,17 +68,28 @@ const getBabelOptions = function loadBabelOptions(filename, options = {}) { |
67 | 68 | return loadPartialConfig(opts).options |
68 | 69 | } |
69 | 70 |
|
70 | | -const getTsJestConfig = function getTsJestConfig(config) { |
71 | | - const { ConfigSet } = require('ts-jest/dist/legacy/config/config-set') |
72 | | - const configSet = new ConfigSet(config.config) |
73 | | - var tsConfig = configSet.typescript || configSet.parsedTsConfig |
| 71 | +/** |
| 72 | + * Load TypeScript config from tsconfig.json. |
| 73 | + * @param {string | undefined} path tsconfig.json file path (default: root) |
| 74 | + * @returns {import('typescript').TranspileOptions | null} TypeScript compilerOptions or null |
| 75 | + */ |
| 76 | +const getTypeScriptConfig = function getTypeScriptConfig(path) { |
| 77 | + const tsconfig = loadTsConfigSync(process.cwd(), path || '') |
| 78 | + if (!tsconfig.path) { |
| 79 | + warn(`Not found tsconfig.json.`) |
| 80 | + return null |
| 81 | + } |
| 82 | + const compilerOptions = |
| 83 | + (tsconfig.config && tsconfig.config.compilerOptions) || {} |
| 84 | + |
74 | 85 | return { |
75 | | - compilerOptions: { ...tsConfig.options, module: 'commonjs' } |
| 86 | + compilerOptions: { ...compilerOptions, module: 'commonjs' } |
76 | 87 | } |
77 | 88 | } |
78 | 89 |
|
79 | 90 | function isValidTransformer(transformer) { |
80 | 91 | return ( |
| 92 | + isFunction(transformer.createTransformer) || |
81 | 93 | isFunction(transformer.process) || |
82 | 94 | isFunction(transformer.postprocess) || |
83 | 95 | isFunction(transformer.preprocess) |
@@ -110,12 +122,13 @@ const getCustomTransformer = function getCustomTransformer( |
110 | 122 |
|
111 | 123 | if (!isValidTransformer(transformer)) { |
112 | 124 | throwError( |
113 | | - `transformer must contain at least one process, preprocess, or ` + |
114 | | - `postprocess method` |
| 125 | + `transformer must contain at least one createTransformer(), process(), preprocess(), or postprocess() method` |
115 | 126 | ) |
116 | 127 | } |
117 | 128 |
|
118 | | - return transformer |
| 129 | + return isFunction(transformer.createTransformer) |
| 130 | + ? transformer.createTransformer() |
| 131 | + : transformer |
119 | 132 | } |
120 | 133 |
|
121 | 134 | const throwError = function error(msg) { |
@@ -152,7 +165,7 @@ module.exports = { |
152 | 165 | throwError, |
153 | 166 | logResultErrors, |
154 | 167 | getCustomTransformer, |
155 | | - getTsJestConfig, |
| 168 | + getTypeScriptConfig, |
156 | 169 | getBabelOptions, |
157 | 170 | getVueJestConfig, |
158 | 171 | transformContent, |
|
0 commit comments