[英]How do i enable "@babel/plugin-proposal-decorators" with vite
> src/App.jsx:22:0: error: Unexpected "@"
22 │ @observer
error when starting dev server:
Error: Build failed with 1 error:
src/App.jsx:22:0: error: Unexpected "@"
I use the observer as a decorator then i got the error.我使用观察者作为装饰器然后我得到了错误。 Can not find a place to enable this option in documentation.
在文档中找不到启用此选项的位置。
No, you can't.不,你不能。 Have to wait for this issue resolved: https://github.com/vitejs/vite/issues/2238
必须等待这个问题解决: https://github.com/vitejs/vite/issues/2238
Not sure if you're using react but you can add babel plugins via the react plugin described here https://www.npmjs.com/package/@vitejs/plugin-react不确定您是否正在使用 react 但您可以通过此处描述的 react 插件添加 babel 插件https://www.npmjs.com/package/@vitejs/plugin-react
The recomended setup seems to require you to rename your files to .ts /.tsx
.推荐的设置似乎要求您将文件重命名为
.ts /.tsx
。 However, the following is allowing me to keep the .js /.jsx
extensions.但是,以下允许我保留
.js /.jsx
扩展名。
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
// https://www.npmjs.com/package/@vitejs/plugin-react
export default defineConfig({
plugins: [
react({
babel: {
plugins: [
["@babel/plugin-proposal-decorators", { legacy: true }],
[
"@babel/plugin-proposal-class-properties",
{ loose: true },
],
],
},
}),
],
});
Vite's react plugin already supports this. Vite 的 react 插件已经支持这个。 Check the docs here https://github.com/vitejs/vite/tree/main/packages/plugin-react#proposed-syntax .
在此处查看文档https://github.com/vitejs/vite/tree/main/packages/plugin-react#proposed-syntax 。 You need not install any new packages.
您无需安装任何新软件包。 The following configuration should work,
以下配置应该可以工作,
import { defineConfig } from 'vite';
import reactSupport from '@vitejs/plugin-react';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [reactSupport({
babel: {
parserOpts: {
plugins: ['decorators-legacy', 'classProperties']
}
}
})],
server: {
port: 3000
}
});
I made a package called vite-plugin-babel-dev that runs babel during dev part in Vite, if you don't need full react
plugin.如果您不需要完整的
react
插件,我制作了一个名为vite-plugin-babel-dev的 package,它在 Vite 的开发部分运行 babel。
Install @babel/plugin-proposal-decorators
and use it like this:安装
@babel/plugin-proposal-decorators
并像这样使用它:
import { defineConfig } from 'vite';
import babelDev from 'vite-plugin-babel-dev';
export default defineConfig({
plugins: [
babelDev({
babelConfig: {
plugin: ['@babel/plugin-proposal-decorators']
}
}),
// ...
],
// ...
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.