From 0c14f0f0e125a3704bd01eb43930d871242d6a8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnaud=20Barr=C3=A9?= Date: Sun, 4 Oct 2026 19:18:01 +0200 Subject: [PATCH] fix(plugin-react): disable refresh for non-jsx with compiler enabled --- packages/plugin-react/CHANGELOG.md | 4 ++++ packages/plugin-react/src/index.ts | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/plugin-react/CHANGELOG.md b/packages/plugin-react/CHANGELOG.md index a44a758ed..be74a27f0 100644 --- a/packages/plugin-react/CHANGELOG.md +++ b/packages/plugin-react/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Disable refresh for non-jsx with compiler enabled (fix [#1478](https://github.com/vitejs/vite-plugin-react/issues/1478)) ([#1485](https://github.com/vitejs/vite-plugin-react/pull/1485)) + +When the compiler was enabled, TS files could trigger fast refresh which would cause runtime errors. The logic now follows what the builtin oxc plugin in Vite does. + ## 6.1.1 (2026-08-28) ### Add `compiler.logDiagnostics` option diff --git a/packages/plugin-react/src/index.ts b/packages/plugin-react/src/index.ts index 778858962..b2be0bb13 100644 --- a/packages/plugin-react/src/index.ts +++ b/packages/plugin-react/src/index.ts @@ -309,6 +309,9 @@ function createReactCompilerPlugin( reactCompilerOptions.target === '17' || reactCompilerOptions.target === '18' ? 'react-compiler-runtime' : 'react/compiler-runtime' + const jsxImportSource = reactOptions.jsxImportSource ?? 'react' + const jsxImportRuntime = `${jsxImportSource}/jsx-runtime` + const jsxImportDevRuntime = `${jsxImportSource}/jsx-dev-runtime` const loadCompiler = async ( onError: (message: string) => never, @@ -348,7 +351,17 @@ function createReactCompilerPlugin( }, }, async handler(code, id) { + // Follow what the builtin oxc plugin does: https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/oxc.ts#L250-L267 + const [filepath] = id.split('?') + const isJSX = filepath.endsWith('x') const isClient = this.environment?.config.consumer !== 'server' + const refreshEnabled = + isClient && + isFastRefreshEnabled() && + (isJSX || + code.includes(jsxImportRuntime) || + code.includes(jsxImportDevRuntime)) + const shouldCompile = isClient && (reactCompilerOptions.compilationMode === 'annotation' @@ -363,7 +376,7 @@ function createReactCompilerPlugin( runtime: reactOptions.jsxRuntime, development: jsxDevelopment, importSource: reactOptions.jsxImportSource, - refresh: isClient && isFastRefreshEnabled(), + refresh: refreshEnabled, }, reactCompiler: shouldCompile ? reactCompilerOptions : false, sourcemap: this.environment