|
| 1 | +const ensureRequire = require('./ensure-require') |
| 2 | +const throwError = require('./throw-error') |
1 | 3 | const constants = require('./constants')
|
2 | 4 | const loadPartialConfig = require('@babel/core').loadPartialConfig
|
3 |
| -const { loadSync: loadTsConfigSync } = require('tsconfig') |
| 5 | +const { resolveSync: resolveTsConfigSync } = require('tsconfig') |
4 | 6 | const chalk = require('chalk')
|
5 | 7 | const path = require('path')
|
6 | 8 | const fs = require('fs')
|
@@ -74,17 +76,41 @@ const getBabelOptions = function loadBabelOptions(filename, options = {}) {
|
74 | 76 | * @returns {import('typescript').TranspileOptions | null} TypeScript compilerOptions or null
|
75 | 77 | */
|
76 | 78 | const getTypeScriptConfig = function getTypeScriptConfig(path) {
|
77 |
| - const tsconfig = loadTsConfigSync(process.cwd(), path || '') |
78 |
| - if (!tsconfig.path) { |
| 79 | + ensureRequire('typescript', ['typescript']) |
| 80 | + const typescript = require('typescript') |
| 81 | + |
| 82 | + const tsconfigPath = resolveTsConfigSync(process.cwd(), path || '') |
| 83 | + if (!tsconfigPath) { |
79 | 84 | warn(`Not found tsconfig.json.`)
|
80 | 85 | return null
|
81 | 86 | }
|
82 |
| - const compilerOptions = |
83 |
| - (tsconfig.config && tsconfig.config.compilerOptions) || {} |
| 87 | + |
| 88 | + const parsedConfig = typescript.getParsedCommandLineOfConfigFile( |
| 89 | + tsconfigPath, |
| 90 | + {}, |
| 91 | + { |
| 92 | + ...typescript.sys, |
| 93 | + onUnRecoverableConfigFileDiagnostic: e => { |
| 94 | + const errorMessage = typescript.formatDiagnostic(e, { |
| 95 | + getCurrentDirectory: () => process.cwd(), |
| 96 | + getNewLine: () => `\n`, |
| 97 | + getCanonicalFileName: file => file.replace(/\\/g, '/') |
| 98 | + }) |
| 99 | + warn(errorMessage) |
| 100 | + } |
| 101 | + } |
| 102 | + ) |
| 103 | + |
| 104 | + const compilerOptions = parsedConfig ? parsedConfig.options : {} |
84 | 105 |
|
85 | 106 | // Force es5 to prevent const vue_1 = require('vue') from conflicting
|
86 | 107 | return {
|
87 |
| - compilerOptions: { ...compilerOptions, target: 'es5', module: 'commonjs' } |
| 108 | + compilerOptions: { |
| 109 | + ...compilerOptions, |
| 110 | + // Force es5 to prevent const vue_1 = require('vue') from conflicting |
| 111 | + target: typescript.ScriptTarget.ES5, |
| 112 | + module: typescript.ModuleKind.CommonJS |
| 113 | + } |
88 | 114 | }
|
89 | 115 | }
|
90 | 116 |
|
@@ -133,10 +159,6 @@ const getCustomTransformer = function getCustomTransformer(
|
133 | 159 | : transformer
|
134 | 160 | }
|
135 | 161 |
|
136 |
| -const throwError = function error(msg) { |
137 |
| - throw new Error('\n[vue-jest] Error: ' + msg + '\n') |
138 |
| -} |
139 |
| - |
140 | 162 | const stripInlineSourceMap = function(str) {
|
141 | 163 | return str.slice(0, str.indexOf('//# sourceMappingURL'))
|
142 | 164 | }
|
|
0 commit comments