perf(@vant/cli): remove vue3-jest (#10155)

This commit is contained in:
neverland
2022-01-06 14:25:58 +08:00
committed by GitHub
parent b042e1cbc1
commit 7b214cb87c
4 changed files with 66 additions and 159 deletions

View File

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

View File

@@ -1,11 +1,14 @@
const { transform: babelTransform } = require('@babel/core');
const { transformSync: esbuildTransformSync } = require('esbuild');
const sfc = require('vue/compiler-sfc');
const babel = require('@babel/core');
const esbuild = require('esbuild');
const nodePath = require('path');
const isJsxFile = (path) => /\.(j|t)sx$/.test(path);
const isTsxFile = (path) => /\.tsx$/.test(path);
const isVueFile = (path) => /\.vue$/.test(path);
const transformJsx = (code, path) => {
const babelResult = babelTransform(code, {
const babelResult = babel.transformSync(code, {
filename: path,
babelrc: false,
presets: isTsxFile(path) ? ['@babel/preset-typescript'] : [],
@@ -14,8 +17,62 @@ const transformJsx = (code, path) => {
return babelResult?.code || '';
};
const transformSFC = (code, path) => {
const parsedPath = nodePath.parse(path);
const { descriptor, errors } = sfc.parse(code, {
filename: parsedPath.base,
sourceRoot: parsedPath.dir,
});
if (errors.length) {
errors.forEach((error) => console.error(error));
return '';
}
const output = [];
let bindingMetadata = {};
if (descriptor.script) {
const content = descriptor.script.content.replace(
'export default',
'const script ='
);
output.push(content);
} else if (descriptor.scriptSetup) {
const result = sfc.compileScript(descriptor, {
id: 'mock',
});
const content = result.content.replace('export default', 'const script =');
output.push(content);
if (result.bindings) {
bindingMetadata = result.bindings;
}
} else {
output.push(`const script = {};`);
}
if (descriptor.template) {
const render = sfc.compileTemplate({
id: 'mock',
source: descriptor.template.content,
filename: path,
compilerOptions: {
bindingMetadata,
},
}).code;
output.push(render);
output.push('script.render = render;');
}
output.push('export default script;');
return output.join('\n');
};
const transformScript = (code) =>
esbuildTransformSync(code, {
esbuild.transformSync(code, {
target: 'es2016',
format: 'cjs',
loader: 'ts',
@@ -24,6 +81,9 @@ const transformScript = (code) =>
module.exports = {
canInstrument: true,
process(code, path) {
if (isVueFile(path)) {
code = transformSFC(code, path);
}
if (isJsxFile(path)) {
code = transformJsx(code, path);
}