From e499e6336060bda1c121fe8c60d7a6bac29f26d8 Mon Sep 17 00:00:00 2001 From: cexbrayat Date: Wed, 14 Jul 2021 10:26:43 +0200 Subject: [PATCH] feat: use TS config to have commonjs modules 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. --- .../vue3-jest/lib/transformers/typescript.js | 20 +------------------ packages/vue3-jest/lib/utils.js | 4 +++- 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/packages/vue3-jest/lib/transformers/typescript.js b/packages/vue3-jest/lib/transformers/typescript.js index a7742db9..1aa2b656 100644 --- a/packages/vue3-jest/lib/transformers/typescript.js +++ b/packages/vue3-jest/lib/transformers/typescript.js @@ -1,7 +1,6 @@ const ensureRequire = require('../ensure-require') const babelJest = require('babel-jest').default const { - getBabelOptions, getTsJestConfig, stripInlineSourceMap, getCustomTransformer, @@ -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, @@ -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) } diff --git a/packages/vue3-jest/lib/utils.js b/packages/vue3-jest/lib/utils.js index 53eea2f9..5fe4ee1f 100644 --- a/packages/vue3-jest/lib/utils.js +++ b/packages/vue3-jest/lib/utils.js @@ -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) {