Skip to content

Commit ccd25dd

Browse files
authored
perf: lazy load @babel/core (#212)
1 parent 533fc09 commit ccd25dd

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

packages/plugin-react/src/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
// eslint-disable-next-line import/no-duplicates
2+
import type * as babelCore from '@babel/core'
3+
// eslint-disable-next-line import/no-duplicates
14
import type { ParserOptions, TransformOptions } from '@babel/core'
2-
import * as babel from '@babel/core'
35
import { createFilter } from 'vite'
46
import type {
57
BuildOptions,
@@ -15,6 +17,15 @@ import {
1517
runtimePublicPath,
1618
} from './fast-refresh'
1719

20+
// lazy load babel since it's not used during build if plugins are not used
21+
let babel: typeof babelCore | undefined
22+
async function loadBabel() {
23+
if (!babel) {
24+
babel = await import('@babel/core')
25+
}
26+
return babel
27+
}
28+
1829
export interface Options {
1930
include?: string | RegExp | Array<string | RegExp>
2031
exclude?: string | RegExp | Array<string | RegExp>
@@ -215,6 +226,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
215226
parserPlugins.push('typescript')
216227
}
217228

229+
const babel = await loadBabel()
218230
const result = await babel.transformAsync(code, {
219231
...babelOptions,
220232
root: projectRoot,

0 commit comments

Comments
 (0)