From a8de326aaeedb3a0287f2841a594460027fdc70d Mon Sep 17 00:00:00 2001 From: Tony Valderrama Date: Tue, 1 Aug 2017 18:12:56 -0700 Subject: [PATCH 1/6] Cache tsconfig when generating per-file cache key. --- src/preprocessor.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/preprocessor.ts b/src/preprocessor.ts index 083dcced98..28fe8a4dad 100644 --- a/src/preprocessor.ts +++ b/src/preprocessor.ts @@ -78,6 +78,7 @@ export function process( return src; } +let tsConfig = undefined; export function getCacheKey( fileData: string, filePath: Path, @@ -85,7 +86,9 @@ export function getCacheKey( options: TransformOptions = { instrument: false }): string { const jestConfig: JestConfig = JSON.parse(configStr); - const tsConfig = getTSConfig(jestConfig.globals, options.instrument); + if (!tsConfig) { + tsConfig = getTSConfig(jestConfig.globals, options.instrument); + } return crypto.createHash('md5') .update(JSON.stringify(tsConfig), 'utf8') From 5564fdb4b141370c7ea44ae20a17eeeb3b7da622 Mon Sep 17 00:00:00 2001 From: Tony Valderrama Date: Wed, 2 Aug 2017 01:05:32 -0700 Subject: [PATCH 2/6] Revert "Cache tsconfig when generating per-file cache key." This reverts commit a8de326aaeedb3a0287f2841a594460027fdc70d. --- src/preprocessor.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/preprocessor.ts b/src/preprocessor.ts index 28fe8a4dad..083dcced98 100644 --- a/src/preprocessor.ts +++ b/src/preprocessor.ts @@ -78,7 +78,6 @@ export function process( return src; } -let tsConfig = undefined; export function getCacheKey( fileData: string, filePath: Path, @@ -86,9 +85,7 @@ export function getCacheKey( options: TransformOptions = { instrument: false }): string { const jestConfig: JestConfig = JSON.parse(configStr); - if (!tsConfig) { - tsConfig = getTSConfig(jestConfig.globals, options.instrument); - } + const tsConfig = getTSConfig(jestConfig.globals, options.instrument); return crypto.createHash('md5') .update(JSON.stringify(tsConfig), 'utf8') From a6d1dd2bc8298cf9323576a1656ac8be596157a9 Mon Sep 17 00:00:00 2001 From: Tony Valderrama Date: Wed, 2 Aug 2017 01:18:43 -0700 Subject: [PATCH 3/6] Cache result within getTSConfig, keyed by relevant parameters. --- src/utils.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 05641da805..fbc247c894 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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); @@ -170,6 +178,7 @@ 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; @@ -177,7 +186,7 @@ export function getTSConfig(globals, collectCoverage: boolean = false) { // 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'; @@ -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; } From e404b347013ac733a59d1a5cd9177ab32d484054 Mon Sep 17 00:00:00 2001 From: Tony Valderrama Date: Wed, 2 Aug 2017 01:26:01 -0700 Subject: [PATCH 4/6] Add Tony Valderrama to contributors list. --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index edaf5a621c..f22568e8a3 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,8 @@ "brian ruddy (https://github.com/bcruddy)", "Emil Persson (https://github.com/emilniklas)", "Ihor Chulinda (https://github.com/Igmat)", - "OJ Kwon (https://github.com/kwonoj)" + "OJ Kwon (https://github.com/kwonoj)", + "Tony Valderrama (https://github.com/tvald)" ], "license": "MIT", "bugs": { From a7fc1ae74843477caa1cca018808e85b1d6041ee Mon Sep 17 00:00:00 2001 From: Tony Valderrama Date: Wed, 2 Aug 2017 01:26:15 -0700 Subject: [PATCH 5/6] bump patch version to 20.0.8 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f22568e8a3..9576986172 100644 --- a/package.json +++ b/package.json @@ -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", From 7d9aea92827235427226f5433011b206d972d97c Mon Sep 17 00:00:00 2001 From: Tony Valderrama Date: Thu, 3 Aug 2017 10:21:14 -0700 Subject: [PATCH 6/6] Added comment explaining use of JSON.stringify() to generate cache keys. --- src/utils.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/utils.ts b/src/utils.ts index fbc247c894..81ba66c783 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -138,6 +138,8 @@ export function getTSConfig(globals, collectCoverage: boolean = false) { // 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];