Skip to content

Commit

Permalink
chore(cache): cache tsconfig parsing to avoid the cost per vue file /…
Browse files Browse the repository at this point in the history
… interpolated string
  • Loading branch information
Adam Hines committed Oct 11, 2022
1 parent e7f4571 commit 6f7d17e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
12 changes: 11 additions & 1 deletion packages/vue2-jest/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 11 additions & 2 deletions packages/vue3-jest/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 6f7d17e

Please sign in to comment.