Skip to content

Commit d3934ad

Browse files
authored
perf(react): skip react compiler when compilationMode: "annotation" but no "use memo" (#734)
1 parent 268354a commit d3934ad

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

packages/plugin-react/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
### Perf: skip `babel-plugin-react-compiler` if code has no `"use memo"` when `{ compilationMode: "annotation" }` ([#734](https://github.com/vitejs/vite-plugin-react/pull/734))
6+
57
### Respect tsconfig `jsxImportSource` ([#726](https://github.com/vitejs/vite-plugin-react/pull/726))
68

79
### Fix `reactRefreshHost` option on rolldown-vite ([#716](https://github.com/vitejs/vite-plugin-react/pull/716))

packages/plugin-react/src/index.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export type ViteReactPluginApi = {
102102
const defaultIncludeRE = /\.[tj]sx?$/
103103
const defaultExcludeRE = /\/node_modules\//
104104
const tsRE = /\.tsx?$/
105+
const compilerAnnotationRE = /['"]use memo['"]/
105106

106107
export default function viteReact(opts: Options = {}): Plugin[] {
107108
const include = opts.include ?? defaultIncludeRE
@@ -247,11 +248,21 @@ export default function viteReact(opts: Options = {}): Plugin[] {
247248
const plugins = [...babelOptions.plugins]
248249

249250
// remove react-compiler plugin on non client environment
250-
if (ssr) {
251-
const reactCompilerPlugin = getReactCompilerPlugin(plugins)
252-
if (reactCompilerPlugin) {
253-
plugins.splice(plugins.indexOf(reactCompilerPlugin), 1)
254-
}
251+
let reactCompilerPlugin = getReactCompilerPlugin(plugins)
252+
if (reactCompilerPlugin && ssr) {
253+
plugins.splice(plugins.indexOf(reactCompilerPlugin), 1)
254+
reactCompilerPlugin = undefined
255+
}
256+
257+
// filter by "use memo" when react-compiler { compilationMode: "annotation" }
258+
// https://react.dev/learn/react-compiler/incremental-adoption#annotation-mode-configuration
259+
if (
260+
Array.isArray(reactCompilerPlugin) &&
261+
reactCompilerPlugin[1]?.compilationMode === 'annotation' &&
262+
!compilerAnnotationRE.test(code)
263+
) {
264+
plugins.splice(plugins.indexOf(reactCompilerPlugin), 1)
265+
reactCompilerPlugin = undefined
255266
}
256267

257268
const isJSX = filepath.endsWith('x')
@@ -305,10 +316,9 @@ export default function viteReact(opts: Options = {}): Plugin[] {
305316
// Required for esbuild.jsxDev to provide correct line numbers
306317
// This creates issues the react compiler because the re-order is too important
307318
// People should use @babel/plugin-transform-react-jsx-development to get back good line numbers
308-
retainLines:
309-
getReactCompilerPlugin(plugins) != null
310-
? false
311-
: !isProduction && isJSX && opts.jsxRuntime !== 'classic',
319+
retainLines: reactCompilerPlugin
320+
? false
321+
: !isProduction && isJSX && opts.jsxRuntime !== 'classic',
312322
parserOpts: {
313323
...babelOptions.parserOpts,
314324
sourceType: 'module',

0 commit comments

Comments
 (0)