|
1 | 1 | const constants = require('./constants')
|
2 | 2 | const loadPartialConfig = require('@babel/core').loadPartialConfig
|
3 |
| -const { resolveSync: resolveTsConfigSync } = require('tsconfig') |
| 3 | +const { |
| 4 | + loadSync: loadTsConfigSync, |
| 5 | + resolveSync: resolveTsConfigSync |
| 6 | +} = require('tsconfig') |
4 | 7 | const chalk = require('chalk')
|
5 | 8 | const path = require('path')
|
6 | 9 | const fs = require('fs')
|
@@ -84,6 +87,27 @@ const getTsJestConfig = function getTsJestConfig(config) {
|
84 | 87 | }
|
85 | 88 | }
|
86 | 89 |
|
| 90 | +/** |
| 91 | + * Load TypeScript config from tsconfig.json. |
| 92 | + * @param {string | undefined} path tsconfig.json file path (default: root) |
| 93 | + * @returns {import('typescript').TranspileOptions | null} TypeScript compilerOptions or null |
| 94 | + */ |
| 95 | +const getTypeScriptConfig = function getTypeScriptConfig(path) { |
| 96 | + const tsconfig = loadTsConfigSync(process.cwd(), path || '') |
| 97 | + if (!tsconfig.path) { |
| 98 | + info(`Not found tsconfig.json.`) |
| 99 | + return null |
| 100 | + } |
| 101 | + info(`Loaded TypeScript config from "${tsconfig.path}".`) |
| 102 | + const compilerOptions = |
| 103 | + (tsconfig.config && tsconfig.config.compilerOptions) || {} |
| 104 | + |
| 105 | + // Force es5 to prevent const vue_1 = require('vue') from conflicting |
| 106 | + return { |
| 107 | + compilerOptions: { ...compilerOptions, target: 'es5', module: 'commonjs' } |
| 108 | + } |
| 109 | +} |
| 110 | + |
87 | 111 | function isValidTransformer(transformer) {
|
88 | 112 | return (
|
89 | 113 | isFunction(transformer.process) ||
|
@@ -162,6 +186,7 @@ module.exports = {
|
162 | 186 | logResultErrors,
|
163 | 187 | getCustomTransformer,
|
164 | 188 | getTsJestConfig,
|
| 189 | + getTypeScriptConfig, |
165 | 190 | getBabelOptions,
|
166 | 191 | getVueJestConfig,
|
167 | 192 | transformContent,
|
|
0 commit comments