mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
fix(cli): failed to compile ts in sfc (#8109)
This commit is contained in:
parent
d8acec1671
commit
2e5535ba7f
26
packages/vant-cli/src/compiler/babel-preset-vue-ts.ts
Normal file
26
packages/vant-cli/src/compiler/babel-preset-vue-ts.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/**
|
||||||
|
* @babel/preset-typescript 不支持编译 Vue 文件中的 ts 代码
|
||||||
|
* 通过手动添加 @babel/plugin-transform-typescript 的方式来解决这个问题
|
||||||
|
* see: https://github.com/babel/babel-loader/pull/738
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { readFileSync } from 'fs';
|
||||||
|
import { declare } from '@babel/helper-plugin-utils';
|
||||||
|
|
||||||
|
module.exports = declare(() => ({
|
||||||
|
overrides: [
|
||||||
|
{
|
||||||
|
test: (filePath: string) => {
|
||||||
|
if (/\.vue$/.test(filePath)) {
|
||||||
|
const template = readFileSync(filePath, { encoding: 'utf8' });
|
||||||
|
return (
|
||||||
|
template.includes('lang="ts"') || template.includes("lang='ts'")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
plugins: [require('@babel/plugin-transform-typescript')],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}));
|
@ -83,7 +83,6 @@ export function parseSfc(filePath: string) {
|
|||||||
export async function compileSfc(filePath: string): Promise<any> {
|
export async function compileSfc(filePath: string): Promise<any> {
|
||||||
const tasks = [remove(filePath)];
|
const tasks = [remove(filePath)];
|
||||||
const source = readFileSync(filePath, 'utf-8');
|
const source = readFileSync(filePath, 'utf-8');
|
||||||
const jsFilePath = replaceExt(filePath, '.js');
|
|
||||||
const descriptor = parseSfc(filePath);
|
const descriptor = parseSfc(filePath);
|
||||||
const { template, styles } = descriptor;
|
const { template, styles } = descriptor;
|
||||||
|
|
||||||
@ -92,6 +91,9 @@ export async function compileSfc(filePath: string): Promise<any> {
|
|||||||
|
|
||||||
// compile js part
|
// compile js part
|
||||||
if (descriptor.script) {
|
if (descriptor.script) {
|
||||||
|
const lang = descriptor.script.lang || 'js';
|
||||||
|
const scriptFilePath = replaceExt(filePath, `.${lang}`);
|
||||||
|
|
||||||
tasks.push(
|
tasks.push(
|
||||||
new Promise((resolve, reject) => {
|
new Promise((resolve, reject) => {
|
||||||
let script = descriptor.script!.content;
|
let script = descriptor.script!.content;
|
||||||
@ -106,8 +108,8 @@ export async function compileSfc(filePath: string): Promise<any> {
|
|||||||
script = injectScopeId(script, scopeId);
|
script = injectScopeId(script, scopeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
writeFileSync(jsFilePath, script);
|
writeFileSync(scriptFilePath, script);
|
||||||
compileJs(jsFilePath).then(resolve).catch(reject);
|
compileJs(scriptFilePath).then(resolve).catch(reject);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,7 @@ module.exports = function(api?: ConfigAPI) {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
'@babel/preset-typescript',
|
'@babel/preset-typescript',
|
||||||
|
require('../compiler/babel-preset-vue-ts'),
|
||||||
],
|
],
|
||||||
plugins: [
|
plugins: [
|
||||||
[
|
[
|
||||||
|
1
packages/vant-cli/src/module.d.ts
vendored
1
packages/vant-cli/src/module.d.ts
vendored
@ -9,4 +9,5 @@ declare module 'release-it';
|
|||||||
declare module 'html-webpack-plugin';
|
declare module 'html-webpack-plugin';
|
||||||
declare module 'conventional-changelog';
|
declare module 'conventional-changelog';
|
||||||
declare module '@vant/markdown-vetur';
|
declare module '@vant/markdown-vetur';
|
||||||
|
declare module '@babel/helper-plugin-utils';
|
||||||
declare module '@nuxt/friendly-errors-webpack-plugin';
|
declare module '@nuxt/friendly-errors-webpack-plugin';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user