Skip to content

Commit

Permalink
Cache result within getTSConfig, keyed by relevant parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
tvald committed Aug 2, 2017
1 parent 5564fdb commit a6d1dd2
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,19 @@ 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)
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 +178,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 +199,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 a6d1dd2

Please sign in to comment.