@cabloy/vite-plugin-babel

中文文档
1.3.3发布于 2026/03/22MITTypeScriptES Module近 30 天下载 5964

Runs Babel in Vite during all commands

原始使用文档
文档目录7 节
  1. Motivations
  2. Installation
  3. Usage
  4. Config
  5. Tips
  6. Troubleshooting
  7. License

Vite Plugin Babel

Run Babel during any Vite command, also during serve.

Motivations

Most Vite plugins runs Babel only during build, not serve, and only other possible way to do this is via @vitejs/plugin-react. ESBuild is awesome tool, but doesn't support some experimental features, like decorators (issue #2349) or class instance fields, out of box. You can use them in TypeScript, but not pure JS. This plugin was made to enable usage of such features and runs babel during optimizeDeps, dev and build stages, but it can be configured.

Installation

# yarn
yarn add -D vite-plugin-babel

# npm
npm install -D vite-plugin-babel

Usage

Add it to your Vite config

import { defineConfig } from 'vite';
import babel from 'vite-plugin-babel';

export default defineConfig({
    plugins: [
        // Babel will try to pick up Babel config files (.babelrc or .babelrc.json)
        babel(),
        // ...
    ],

    // ...
})

Config

Babel config can be either passed to babelConfig field or via Babel config file. For all babel options see: Babel Options.

By default, babel is run for JS/JSX files. You can change that vie filter option.

NameTypeDefaultDescription
apply'serve' | 'build' | (config: UserConfig, env: ConfigEnv) => booleanundefinedLimits plugin usage to only build or only serve. If not specified, will be run during both cycles. Same as apply in Vite Plugins API
enforce'pre' | 'post'preForce plugin's order execution. More details: Vite Plugin Ordering
babelConfigobject{}Babel Transform Options
filterRegExp | (id: string) => boolean/\.jsx?$/Which files is babel applied to. By default, it's js/jsx files. You can pass a filter function that accepts file name and returns true | false
includestring | RegExp | Array<string|RegExp>)undefinedwhich files to include. If omitted, all are included
excludestring | RegExp | Array<string|RegExp>)undefinedwhich files to exclude. If used with include, it will have higher priority and can exclude files, that match include pattern
loaderLoader or (path: string) => LoaderundefinedThis tells esbuild how to interpret the contents after babel's transformation. For example, the js loader interprets the contents as JavaScript and the css loader interprets the contents as CSS. The loader defaults to js if it's not specified. See the Content Types page for a complete list of all built-in loaders.

Tips

Vite team didn't enable and include Babel by default, because they wanted to keep experience as fast as possible and esbuild can already do a lot of things, you would probably do with Babel. Because of that, we recommend to only include those Babel plugins you really need. You can use option babelConfig.configFile and disable usage of Babel config file, ex.:

babel({
    babelConfig: {
        babelrc: false,
        configFile: false,
        plugins: ['@babel/plugin-proposal-decorators']
    }
})

or just use .babelrc.json.

NOTE: Any babel plugins and presets need to be installed seperately and are not included with this package.

Troubleshooting

[ERROR] The JSX syntax extension is not currently enabled

This usually happens when you're using this plugin to only transform part of a .jsx file (such as decorators), and leaving the JSX syntax untouched. By default, esbuild interprets contents as .js, so you'll need to specify the loader esbuild should use.

Example:

import { extname } from 'path';
// ...
babel({
    babelConfig: {
        babelrc: false,
        configFile: false,
        plugins: ['@babel/plugin-proposal-decorators'],
        
        // uses the jsx loader for .jsx files
        loader: path => {
          if (extname(path) === '.jsx') {
            return 'jsx';
          }
        },
    }
})

License

Library is under MIT License

版本与发布记录

@cabloy/vite-plugin-babel 共发布 3 个版本,首次发布于 2024/03/30;过去 12 个月发布了 1 个版本。

版本发布日期距今
1.3.3latest2026/03/227 个月前
1.3.12025/05/061.5 年前
1.1.32024/03/302 年前

关于 @cabloy/vite-plugin-babel 的常见问题

@cabloy/vite-plugin-babel 是什么?

Runs Babel in Vite during all commands

如何安装 @cabloy/vite-plugin-babel?

在项目目录运行 npm install @cabloy/vite-plugin-babel,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@babel/core(^7.29.0)、vite(^2.7.0 || ^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@cabloy/vite-plugin-babel 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cabloy/vite-plugin-babel 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@cabloy/vite-plugin-babel 可以商用吗?

@cabloy/vite-plugin-babel 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cabloy/vite-plugin-babel 的下载量有多少?

根据 npm 官方下载统计,@cabloy/vite-plugin-babel 在 2026/10/09 – 2026/11/07 这 30 天内被下载 5,964 次,日均约 199 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cabloy/vite-plugin-babel 还在维护吗?

最新版本 1.3.3 发布于 2026/03/22(7 个月前);至今共发布 3 个版本,其中过去 12 个月发布了 1 个;首次发布于 2024/03/30。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。