Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache tsconfig when generating per-file cache key. #286

Merged
merged 6 commits into from
Aug 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}