简体   繁体   English

如何使用 vite 启用“@babel/plugin-proposal-decorators”

[英]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']
            }
        }),
        // ...
    ],

    // ...
})

The documentation as of today has a section for exactly this, as well as a code snippet.截止到今天的文档有一个专门针对这一点的部分,以及一个代码片段。

react({
  babel: {
    parserOpts: {
      plugins: ['decorators-legacy']
    }
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Babel 7的插件提议装饰器和异步函数来装饰异步方法? - How do I decorate an async method using Babel 7's plugin-proposal-decorators with an async function? Babel插件 - 提议装饰器未按预期工作 - Babel plugin-proposal-decorators not working as expected 错误:无法从“/home/dolphin/sync/source/cruise-web”找到模块“@babel/plugin-proposal-decorators” - Error: Cannot find module '@babel/plugin-proposal-decorators' from '/home/dolphin/sync/source/cruise-web' 使用 Firebase 在 CodeSandbox 上找不到/安装 babel 插件“proposal-decorators” - Could not find/install babel plugin 'proposal-decorators' on CodeSandbox with Firebase 如何通过Babel在类实例上使用JavaScript装饰器? - How do I use JavaScript decorators on a class instance via Babel? 如何让装饰工人使用babel和webpack? - How do I get decorators working with babel & webpack? 如何在Codesandbox中使用Babel插件提案抛出表达式? - How to use Babel Plugin Proposal Throw Expressions in Codesandbox? 如何在 Meteor 中启用 babel-plugin-rewire - How to enable babel-plugin-rewire in Meteor 如何让 @babel/plugin-proposal-private-methods 在 vue.js webpack 模板中工作? - How to make @babel/plugin-proposal-private-methods work in the vue.js webpack template? 如何在 babel 插件中遍历 Path 的范围 - How do I traverse the scope of a Path in a babel plugin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM