Skip to content

Commit 44b3afb

Browse files
committed
feat: add getTypeScriptConfig util function
1 parent 6f150de commit 44b3afb

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

packages/vue2-jest/lib/utils.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const constants = require('./constants')
22
const loadPartialConfig = require('@babel/core').loadPartialConfig
3+
const { loadSync: loadTsConfigSync } = require('tsconfig')
34
const chalk = require('chalk')
45
const path = require('path')
56
const fs = require('fs')
@@ -76,6 +77,26 @@ const getTsJestConfig = function getTsJestConfig(config) {
7677
}
7778
}
7879

80+
/**
81+
* Load TypeScript config from tsconfig.json.
82+
* @param {string | undefined} path tsconfig.json file path (default: root)
83+
* @returns {import('typescript').TranspileOptions | null} TypeScript compilerOptions or null
84+
*/
85+
const getTypeScriptConfig = function getTypeScriptConfig(path) {
86+
const tsconfig = loadTsConfigSync(process.cwd(), path || '')
87+
if (!tsconfig.path) {
88+
info(`Not found tsconfig.json.`)
89+
return null
90+
}
91+
info(`Loaded TypeScript config from "${tsconfig.path}".`)
92+
const compilerOptions =
93+
(tsconfig.config && tsconfig.config.compilerOptions) || {}
94+
95+
return {
96+
compilerOptions: { ...compilerOptions, module: 'commonjs' }
97+
}
98+
}
99+
79100
function isValidTransformer(transformer) {
80101
return (
81102
isFunction(transformer.process) ||
@@ -153,6 +174,7 @@ module.exports = {
153174
logResultErrors,
154175
getCustomTransformer,
155176
getTsJestConfig,
177+
getTypeScriptConfig,
156178
getBabelOptions,
157179
getVueJestConfig,
158180
transformContent,

packages/vue3-jest/lib/utils.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
const constants = require('./constants')
22
const loadPartialConfig = require('@babel/core').loadPartialConfig
3-
const { resolveSync: resolveTsConfigSync } = require('tsconfig')
3+
const {
4+
loadSync: loadTsConfigSync,
5+
resolveSync: resolveTsConfigSync
6+
} = require('tsconfig')
47
const chalk = require('chalk')
58
const path = require('path')
69
const fs = require('fs')
@@ -84,6 +87,27 @@ const getTsJestConfig = function getTsJestConfig(config) {
8487
}
8588
}
8689

90+
/**
91+
* Load TypeScript config from tsconfig.json.
92+
* @param {string | undefined} path tsconfig.json file path (default: root)
93+
* @returns {import('typescript').TranspileOptions | null} TypeScript compilerOptions or null
94+
*/
95+
const getTypeScriptConfig = function getTypeScriptConfig(path) {
96+
const tsconfig = loadTsConfigSync(process.cwd(), path || '')
97+
if (!tsconfig.path) {
98+
info(`Not found tsconfig.json.`)
99+
return null
100+
}
101+
info(`Loaded TypeScript config from "${tsconfig.path}".`)
102+
const compilerOptions =
103+
(tsconfig.config && tsconfig.config.compilerOptions) || {}
104+
105+
// Force es5 to prevent const vue_1 = require('vue') from conflicting
106+
return {
107+
compilerOptions: { ...compilerOptions, target: 'es5', module: 'commonjs' }
108+
}
109+
}
110+
87111
function isValidTransformer(transformer) {
88112
return (
89113
isFunction(transformer.process) ||
@@ -162,6 +186,7 @@ module.exports = {
162186
logResultErrors,
163187
getCustomTransformer,
164188
getTsJestConfig,
189+
getTypeScriptConfig,
165190
getBabelOptions,
166191
getVueJestConfig,
167192
transformContent,

0 commit comments

Comments
 (0)