Skip to content

Commit c04e3bd

Browse files
authored
Merge pull request #286 from tvald-contrib/bugfix/cache-tsconfig
Cache tsconfig when generating per-file cache key.
2 parents 95bd323 + 7d9aea9 commit c04e3bd

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-jest",
3-
"version": "20.0.7",
3+
"version": "20.0.8",
44
"main": "index.js",
55
"types": "./dist/index.d.ts",
66
"description": "A preprocessor with sourcemap support to help use Typescript with Jest",
@@ -31,7 +31,8 @@
3131
"brian ruddy <[email protected]> (https://github.com/bcruddy)",
3232
"Emil Persson <[email protected]> (https://github.com/emilniklas)",
3333
"Ihor Chulinda <[email protected]> (https://github.com/Igmat)",
34-
"OJ Kwon <[email protected]> (https://github.com/kwonoj)"
34+
"OJ Kwon <[email protected]> (https://github.com/kwonoj)",
35+
"Tony Valderrama <[email protected]> (https://github.com/tvald)"
3536
],
3637
"license": "MIT",
3738
"bugs": {

src/utils.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,21 @@ export function mockGlobalTSConfigSchema(globals: any) {
130130
{ __TS_CONFIG__: config };
131131
}
132132

133+
const tsConfigCache: { [key: string]: any } = {};
133134
export function getTSConfig(globals, collectCoverage: boolean = false) {
134135
let config = getTSConfigOptionFromConfig(globals);
135136
const skipBabel = getTSJestConfig(globals).skipBabel;
136137
const isReferencedExternalFile = typeof config === 'string';
137138

139+
// check cache before resolving configuration
140+
// NB: config is a string unless taken from __TS_CONFIG__, which should be immutable (and is deprecated anyways)
141+
// NB: We use JSON.stringify() to create a consistent, unique signature. Although it lacks a uniform
142+
// shape, this is simpler and faster than using the crypto package to generate a hash signature.
143+
const tsConfigCacheKey = JSON.stringify([skipBabel, collectCoverage, isReferencedExternalFile ? config : undefined]);
144+
if (tsConfigCacheKey in tsConfigCache) {
145+
return tsConfigCache[tsConfigCacheKey];
146+
}
147+
138148
if (isReferencedExternalFile) {
139149
const configFileName = config;
140150
const configPath = path.resolve(config);
@@ -170,14 +180,15 @@ export function getTSConfig(globals, collectCoverage: boolean = false) {
170180
// to their string properties, and convert the result accordingly afterwards.
171181
// In case of an external file, reading the config file already converted it as well, and
172182
// an additional attempt would lead to errors.
183+
let result;
173184
if (isReferencedExternalFile) {
174185
config.jsx = config.jsx || tsc.JsxEmit.React;
175186
config.module = config.module || tsc.ModuleKind.CommonJS;
176187
if (config.allowSyntheticDefaultImports && !skipBabel) {
177188
// compile ts to es2015 and transform with babel afterwards
178189
config.module = tsc.ModuleKind.ES2015;
179190
}
180-
return config;
191+
result = config;
181192
} else {
182193
config.jsx = config.jsx || 'react';
183194
config.module = config.module || 'commmonjs';
@@ -190,6 +201,10 @@ export function getTSConfig(globals, collectCoverage: boolean = false) {
190201
const formattedErrors = formatTscParserErrors(converted.errors);
191202
throw new Error(`Some errors occurred while attempting to convert ${JSON.stringify(config)}: ${formattedErrors}`);
192203
}
193-
return converted.options;
204+
result = converted.options;
194205
}
206+
207+
// cache result for future requests
208+
tsConfigCache[tsConfigCacheKey] = result;
209+
return result;
195210
}

0 commit comments

Comments
 (0)