|
| 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')
|
@@ -68,24 +70,57 @@ const getBabelOptions = function loadBabelOptions(filename, options = {}) {
|
68 | 70 | return loadPartialConfig(opts).options
|
69 | 71 | }
|
70 | 72 |
|
| 73 | +const tsConfigCache = new Map() |
| 74 | + |
71 | 75 | /**
|
72 | 76 | * Load TypeScript config from tsconfig.json.
|
73 | 77 | * @param {string | undefined} path tsconfig.json file path (default: root)
|
74 | 78 | * @returns {import('typescript').TranspileOptions | null} TypeScript compilerOptions or null
|
75 | 79 | */
|
76 | 80 | const getTypeScriptConfig = function getTypeScriptConfig(path) {
|
77 |
| - const tsconfig = loadTsConfigSync(process.cwd(), path || '') |
78 |
| - if (!tsconfig.path) { |
| 81 | + if (tsConfigCache.has(path)) { |
| 82 | + return tsConfigCache.get(path) |
| 83 | + } |
| 84 | + |
| 85 | + ensureRequire('typescript', ['typescript']) |
| 86 | + const typescript = require('typescript') |
| 87 | + |
| 88 | + const tsconfigPath = resolveTsConfigSync(process.cwd(), path || '') |
| 89 | + if (!tsconfigPath) { |
79 | 90 | warn(`Not found tsconfig.json.`)
|
80 | 91 | return null
|
81 | 92 | }
|
82 |
| - const compilerOptions = |
83 |
| - (tsconfig.config && tsconfig.config.compilerOptions) || {} |
84 | 93 |
|
85 |
| - // Force es5 to prevent const vue_1 = require('vue') from conflicting |
86 |
| - return { |
87 |
| - compilerOptions: { ...compilerOptions, target: 'es5', module: 'commonjs' } |
| 94 | + const parsedConfig = typescript.getParsedCommandLineOfConfigFile( |
| 95 | + tsconfigPath, |
| 96 | + {}, |
| 97 | + { |
| 98 | + ...typescript.sys, |
| 99 | + onUnRecoverableConfigFileDiagnostic: e => { |
| 100 | + const errorMessage = typescript.formatDiagnostic(e, { |
| 101 | + getCurrentDirectory: () => process.cwd(), |
| 102 | + getNewLine: () => `\n`, |
| 103 | + getCanonicalFileName: file => file.replace(/\\/g, '/') |
| 104 | + }) |
| 105 | + warn(errorMessage) |
| 106 | + } |
| 107 | + } |
| 108 | + ) |
| 109 | + |
| 110 | + const compilerOptions = parsedConfig ? parsedConfig.options : {} |
| 111 | + |
| 112 | + const transpileConfig = { |
| 113 | + compilerOptions: { |
| 114 | + ...compilerOptions, |
| 115 | + // Force es5 to prevent const vue_1 = require('vue') from conflicting |
| 116 | + target: typescript.ScriptTarget.ES5, |
| 117 | + module: typescript.ModuleKind.CommonJS |
| 118 | + } |
88 | 119 | }
|
| 120 | + |
| 121 | + tsConfigCache.set(path, transpileConfig) |
| 122 | + |
| 123 | + return transpileConfig |
89 | 124 | }
|
90 | 125 |
|
91 | 126 | function isValidTransformer(transformer) {
|
@@ -133,10 +168,6 @@ const getCustomTransformer = function getCustomTransformer(
|
133 | 168 | : transformer
|
134 | 169 | }
|
135 | 170 |
|
136 |
| -const throwError = function error(msg) { |
137 |
| - throw new Error('\n[vue-jest] Error: ' + msg + '\n') |
138 |
| -} |
139 |
| - |
140 | 171 | const stripInlineSourceMap = function(str) {
|
141 | 172 | return str.slice(0, str.indexOf('//# sourceMappingURL'))
|
142 | 173 | }
|
|
0 commit comments