Skip to content

Commit

Permalink
Merge pull request #286 from tvald-contrib/bugfix/cache-tsconfig
Browse files Browse the repository at this point in the history
Cache tsconfig when generating per-file cache key.
  • Loading branch information
GeeWee committed Aug 4, 2017
2 parents 95bd323 + 7d9aea9 commit c04e3bd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-jest",
"version": "20.0.7",
"version": "20.0.8",
"main": "index.js",
"types": "./dist/index.d.ts",
"description": "A preprocessor with sourcemap support to help use Typescript with Jest",
Expand Down Expand Up @@ -31,7 +31,8 @@
"brian ruddy <briancruddy@gmail.com> (https://github.com/bcruddy)",
"Emil Persson <emil.n.persson@gmail.com> (https://github.com/emilniklas)",
"Ihor Chulinda <ichulinda@gmail.com> (https://github.com/Igmat)",
"OJ Kwon <kwon.ohjoong@gmail.com> (https://github.com/kwonoj)"
"OJ Kwon <kwon.ohjoong@gmail.com> (https://github.com/kwonoj)",
"Tony Valderrama <tony.valderrama@outlook.com> (https://github.com/tvald)"
],
"license": "MIT",
"bugs": {
Expand Down
19 changes: 17 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,21 @@ export function mockGlobalTSConfigSchema(globals: any) {
{ __TS_CONFIG__: config };
}

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

// check cache before resolving configuration
// NB: config is a string unless taken from __TS_CONFIG__, which should be immutable (and is deprecated anyways)
// NB: We use JSON.stringify() to create a consistent, unique signature. Although it lacks a uniform
// shape, this is simpler and faster than using the crypto package to generate a hash signature.
const tsConfigCacheKey = JSON.stringify([skipBabel, collectCoverage, isReferencedExternalFile ? config : undefined]);
if (tsConfigCacheKey in tsConfigCache) {
return tsConfigCache[tsConfigCacheKey];
}

if (isReferencedExternalFile) {
const configFileName = config;
const configPath = path.resolve(config);
Expand Down Expand Up @@ -170,14 +180,15 @@ export function getTSConfig(globals, collectCoverage: boolean = false) {
// to their string properties, and convert the result accordingly afterwards.
// In case of an external file, reading the config file already converted it as well, and
// an additional attempt would lead to errors.
let result;
if (isReferencedExternalFile) {
config.jsx = config.jsx || tsc.JsxEmit.React;
config.module = config.module || tsc.ModuleKind.CommonJS;
if (config.allowSyntheticDefaultImports && !skipBabel) {
// compile ts to es2015 and transform with babel afterwards
config.module = tsc.ModuleKind.ES2015;
}
return config;
result = config;
} else {
config.jsx = config.jsx || 'react';
config.module = config.module || 'commmonjs';
Expand All @@ -190,6 +201,10 @@ export function getTSConfig(globals, collectCoverage: boolean = false) {
const formattedErrors = formatTscParserErrors(converted.errors);
throw new Error(`Some errors occurred while attempting to convert ${JSON.stringify(config)}: ${formattedErrors}`);
}
return converted.options;
result = converted.options;
}

// cache result for future requests
tsConfigCache[tsConfigCacheKey] = result;
return result;
}

0 comments on commit c04e3bd

Please sign in to comment.