diff --git a/packages/plugin-react/src/index.ts b/packages/plugin-react/src/index.ts index 7f46660fa..4208c2e34 100644 --- a/packages/plugin-react/src/index.ts +++ b/packages/plugin-react/src/index.ts @@ -1,5 +1,7 @@ +// eslint-disable-next-line import/no-duplicates +import type * as babelCore from '@babel/core' +// eslint-disable-next-line import/no-duplicates import type { ParserOptions, TransformOptions } from '@babel/core' -import * as babel from '@babel/core' import { createFilter } from 'vite' import type { BuildOptions, @@ -15,6 +17,15 @@ import { runtimePublicPath, } from './fast-refresh' +// lazy load babel since it's not used during build if plugins are not used +let babel: typeof babelCore | undefined +async function loadBabel() { + if (!babel) { + babel = await import('@babel/core') + } + return babel +} + export interface Options { include?: string | RegExp | Array exclude?: string | RegExp | Array @@ -215,6 +226,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] { parserPlugins.push('typescript') } + const babel = await loadBabel() const result = await babel.transformAsync(code, { ...babelOptions, root: projectRoot,