mirror of
https://github.com/youzan/vant.git
synced 2025-10-15 23:55:08 +00:00
test: using esbuild instead of babel-jest (#10154)
* test: using esbuild instead of babel-jest * chore: update lock
This commit is contained in:
@@ -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'],
|
||||
|
32
packages/vant-cli/cjs/jest.transformer.cjs
Normal file
32
packages/vant-cli/cjs/jest.transformer.cjs
Normal 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);
|
||||
},
|
||||
};
|
Reference in New Issue
Block a user