test: using esbuild instead of babel-jest (#10154)

* test: using esbuild instead of babel-jest

* chore: update lock
This commit is contained in:
neverland
2022-01-06 11:49:21 +08:00
committed by GitHub
parent eab5792153
commit b042e1cbc1
4 changed files with 44 additions and 14 deletions

View File

@@ -17,7 +17,7 @@ const DEFAULT_CONFIG = {
moduleFileExtensions: ['js', 'jsx', 'vue', 'ts', 'tsx'],
transform: {
'\\.(vue)$': 'vue3-jest',
'\\.(js|jsx|ts|tsx)$': 'babel-jest',
'\\.(js|jsx|ts|tsx)$': '<rootDir>/node_modules/@vant/cli/cjs/jest.transformer.cjs',
},
transformIgnorePatterns: ['/node_modules/(?!(@vant/cli))/'],
snapshotSerializers: ['jest-serializer-html'],

View File

@@ -0,0 +1,32 @@
const { transform: babelTransform } = require('@babel/core');
const { transformSync: esbuildTransformSync } = require('esbuild');
const isJsxFile = (path) => /\.(j|t)sx$/.test(path);
const isTsxFile = (path) => /\.tsx$/.test(path);
const transformJsx = (code, path) => {
const babelResult = babelTransform(code, {
filename: path,
babelrc: false,
presets: isTsxFile(path) ? ['@babel/preset-typescript'] : [],
plugins: [['@vue/babel-plugin-jsx']],
});
return babelResult?.code || '';
};
const transformScript = (code) =>
esbuildTransformSync(code, {
target: 'es2016',
format: 'cjs',
loader: 'ts',
}).code;
module.exports = {
canInstrument: true,
process(code, path) {
if (isJsxFile(path)) {
code = transformJsx(code, path);
}
return transformScript(code);
},
};