From 6f7d17e5928bafdc530a268ee92e9e4953de6af8 Mon Sep 17 00:00:00 2001 From: Adam Hines Date: Tue, 11 Oct 2022 13:04:21 -0600 Subject: [PATCH] chore(cache): cache tsconfig parsing to avoid the cost per vue file / interpolated string --- packages/vue2-jest/lib/utils.js | 12 +++++++++++- packages/vue3-jest/lib/utils.js | 13 +++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/packages/vue2-jest/lib/utils.js b/packages/vue2-jest/lib/utils.js index 257af1ad..a6eb5d90 100644 --- a/packages/vue2-jest/lib/utils.js +++ b/packages/vue2-jest/lib/utils.js @@ -70,12 +70,18 @@ const getBabelOptions = function loadBabelOptions(filename, options = {}) { return loadPartialConfig(opts).options } +const tsConfigCache = {} + /** * Load TypeScript config from tsconfig.json. * @param {string | undefined} path tsconfig.json file path (default: root) * @returns {import('typescript').TranspileOptions | null} TypeScript compilerOptions or null */ const getTypeScriptConfig = function getTypeScriptConfig(path) { + if (path in tsConfigCache) { + return tsConfigCache[path] + } + ensureRequire('typescript', ['typescript']) const typescript = require('typescript') @@ -103,12 +109,16 @@ const getTypeScriptConfig = function getTypeScriptConfig(path) { const compilerOptions = parsedConfig ? parsedConfig.options : {} - return { + const transpileConfig = { compilerOptions: { ...compilerOptions, module: typescript.ModuleKind.CommonJS } } + + tsConfigCache[path] = transpileConfig + + return transpileConfig } function isValidTransformer(transformer) { diff --git a/packages/vue3-jest/lib/utils.js b/packages/vue3-jest/lib/utils.js index e137e6ba..8a11be32 100644 --- a/packages/vue3-jest/lib/utils.js +++ b/packages/vue3-jest/lib/utils.js @@ -70,12 +70,18 @@ const getBabelOptions = function loadBabelOptions(filename, options = {}) { return loadPartialConfig(opts).options } +const tsConfigCache = {} + /** * Load TypeScript config from tsconfig.json. * @param {string | undefined} path tsconfig.json file path (default: root) * @returns {import('typescript').TranspileOptions | null} TypeScript compilerOptions or null */ const getTypeScriptConfig = function getTypeScriptConfig(path) { + if (path in tsConfigCache) { + return tsConfigCache[path] + } + ensureRequire('typescript', ['typescript']) const typescript = require('typescript') @@ -103,14 +109,17 @@ const getTypeScriptConfig = function getTypeScriptConfig(path) { const compilerOptions = parsedConfig ? parsedConfig.options : {} - // Force es5 to prevent const vue_1 = require('vue') from conflicting - return { + const transpileConfig = { compilerOptions: { ...compilerOptions, target: typescript.ScriptTarget.ES5, module: typescript.ModuleKind.CommonJS } } + + tsConfigCache[path] = transpileConfig + + return transpileConfig } function isValidTransformer(transformer) {