feat(@vant/cli): using esbuild to transform script (#10143)
* feat(@vant/cli): using esbuild to transform script * chore: update babel doc * chore: update lock * chore: update * fix: format
This commit is contained in:
@@ -1,36 +1,56 @@
|
||||
import fse from 'fs-extra';
|
||||
import babel from '@babel/core';
|
||||
import esbuild, { type Format } from 'esbuild';
|
||||
import { sep } from 'path';
|
||||
import { transformAsync } from '@babel/core';
|
||||
import { replaceExt } from '../common/index.js';
|
||||
import { isJsx, replaceExt } from '../common/index.js';
|
||||
import { replaceCSSImportExt } from '../common/css.js';
|
||||
import { replaceScriptImportExt } from './get-deps.js';
|
||||
|
||||
const { readFileSync, removeSync, outputFileSync } = fse;
|
||||
|
||||
export async function compileScript(filePath: string): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (filePath.includes('.d.ts')) {
|
||||
resolve();
|
||||
return;
|
||||
export async function compileScript(
|
||||
filePath: string,
|
||||
format: Format
|
||||
): Promise<void> {
|
||||
if (filePath.includes('.d.ts')) {
|
||||
return;
|
||||
}
|
||||
|
||||
let code = readFileSync(filePath, 'utf-8');
|
||||
|
||||
if (!filePath.includes(`${sep}style${sep}`)) {
|
||||
code = replaceCSSImportExt(code);
|
||||
}
|
||||
code = replaceScriptImportExt(code, '.vue', '');
|
||||
|
||||
if (isJsx(filePath)) {
|
||||
const babelResult = await babel.transformAsync(code, {
|
||||
filename: filePath,
|
||||
babelrc: false,
|
||||
presets: ['@babel/preset-typescript'],
|
||||
plugins: [
|
||||
[
|
||||
'@vue/babel-plugin-jsx',
|
||||
{
|
||||
enableObjectSlots: false,
|
||||
},
|
||||
],
|
||||
],
|
||||
});
|
||||
if (babelResult?.code) {
|
||||
({ code } = babelResult);
|
||||
}
|
||||
}
|
||||
|
||||
let code = readFileSync(filePath, 'utf-8');
|
||||
|
||||
if (!filePath.includes(`${sep}style${sep}`)) {
|
||||
code = replaceCSSImportExt(code);
|
||||
}
|
||||
code = replaceScriptImportExt(code, '.vue', '');
|
||||
|
||||
transformAsync(code, { filename: filePath })
|
||||
.then((result) => {
|
||||
if (result) {
|
||||
const jsFilePath = replaceExt(filePath, '.js');
|
||||
|
||||
removeSync(filePath);
|
||||
outputFileSync(jsFilePath, result.code);
|
||||
resolve();
|
||||
}
|
||||
})
|
||||
.catch(reject);
|
||||
const esbuildResult = await esbuild.transform(code, {
|
||||
loader: 'ts',
|
||||
target: 'es2016',
|
||||
format,
|
||||
});
|
||||
|
||||
({ code } = esbuildResult);
|
||||
|
||||
const jsFilePath = replaceExt(filePath, '.js');
|
||||
removeSync(filePath);
|
||||
outputFileSync(jsFilePath, code);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user