Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

vite-plugin-babel

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vite-plugin-babel

Runs Babel in Vite during all commands

  • 1.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
32K
decreased by-23.11%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Instalation

# 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
applyserve or buildundefinedLimits plugin usage to only build or only serve. If not specified, will be run during both cycles.
babelConfigobject{}Babel Transform Options
filterRegExp/\.jsx?$/Which files is babel applied to. By default, it's js/jsx files.
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 enabled and included Babel by default, because they wanted to keep expirience 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.plugin and disable usage of Babel config file, ex.:

babel({
    babelConfig: {
        babelrc: false,
        configFile: false,
        plugin: ['@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,
        plugin: ['@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

Keywords

FAQs

Package last updated on 20 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc