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 Nov 1, 2022
1 parent 0fb2493 commit 76c5419
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 = new Map()

/**
* 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 (tsConfigCache.has(path)) {
return tsConfigCache.get(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.set(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 = new Map()

/**
* 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 (tsConfigCache.has(path)) {
return tsConfigCache.get(path)
}

ensureRequire('typescript', ['typescript'])
const typescript = require('typescript')

Expand Down Expand Up @@ -103,15 +109,18 @@ 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,
// Force es5 to prevent const vue_1 = require('vue') from conflicting
target: typescript.ScriptTarget.ES5,
module: typescript.ModuleKind.CommonJS
}
}

tsConfigCache.set(path, transpileConfig)

return transpileConfig
}

function isValidTransformer(transformer) {
Expand Down

0 comments on commit 76c5419

Please sign in to comment.