Skip to content

Commit

Permalink
feat: use TS config to have commonjs modules
Browse files Browse the repository at this point in the history
The previous logic was trying to figure out if CommonJS modules were used in the TS config and if not,
and if no babel configuration provided, then loaded a babel transformer to change the module format.

This changes the logic to use a more straightforward method by directly updating the tsconfig to commonjs,
which should hopefully have the same results without side-effects.
  • Loading branch information
cexbrayat committed Jul 14, 2021
1 parent c2e2913 commit e499e63
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 20 deletions.
20 changes: 1 addition & 19 deletions packages/vue3-jest/lib/transformers/typescript.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const ensureRequire = require('../ensure-require')
const babelJest = require('babel-jest').default
const {
getBabelOptions,
getTsJestConfig,
stripInlineSourceMap,
getCustomTransformer,
Expand All @@ -14,7 +13,6 @@ module.exports = {
const typescript = require('typescript')
const vueJestConfig = getVueJestConfig(config)
const tsconfig = getTsJestConfig(config)
const babelOptions = getBabelOptions(filePath)

const res = typescript.transpileModule(scriptContent, {
...tsconfig,
Expand All @@ -26,27 +24,11 @@ module.exports = {
const inputSourceMap =
res.sourceMapText !== undefined ? JSON.parse(res.sourceMapText) : ''

// handle ES modules in TS source code in case user uses non commonjs module
// output and there is no presets or plugins defined in package.json or babel config file
let inlineBabelOptions = {}
if (
tsconfig.compilerOptions.module !== typescript.ModuleKind.CommonJS &&
!(babelOptions.presets && babelOptions.presets.length) &&
!(babelOptions.plugins && babelOptions.plugins.length)
) {
inlineBabelOptions = {
plugins: [require('@babel/plugin-transform-modules-commonjs')]
}
}
const customTransformer =
getCustomTransformer(vueJestConfig['transform'], 'js') || {}
const transformer = customTransformer.process
? customTransformer
: babelJest.createTransformer(
Object.assign(inlineBabelOptions, {
inputSourceMap
})
)
: babelJest.createTransformer({ inputSourceMap })

return transformer.process(res.outputText, filePath, config)
}
Expand Down
4 changes: 3 additions & 1 deletion packages/vue3-jest/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ const getTsJestConfig = function getTsJestConfig(config) {
const configSet = new ConfigSet(config.config)
const tsConfig = configSet.typescript || configSet.parsedTsConfig
// Force es5 to prevent const vue_1 = require('vue') from conflicting
return { compilerOptions: { ...tsConfig.options, target: 'es5' } }
return {
compilerOptions: { ...tsConfig.options, target: 'es5', module: 'commonjs' }
}
}

function isValidTransformer(transformer) {
Expand Down

0 comments on commit e499e63

Please sign in to comment.