Skip to content

Commit

Permalink
perf: lazy load @babel/core (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Aug 29, 2023
1 parent 533fc09 commit ccd25dd
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<string | RegExp>
exclude?: string | RegExp | Array<string | RegExp>
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit ccd25dd

Please sign in to comment.