Skip to content

Commit 6de9915

Browse files
authored
fix(plugin-react): disable refresh for non-jsx with compiler enabled (#1485)
1 parent 3b14e48 commit 6de9915

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

‎packages/plugin-react/CHANGELOG.md‎

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

33
## Unreleased
44

5+
### 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))
6+
7+
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.
8+
59
## 6.1.1 (2026-08-28)
610

711
### Add `compiler.logDiagnostics` option

‎packages/plugin-react/src/index.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ function createReactCompilerPlugin(
309309
reactCompilerOptions.target === '17' || reactCompilerOptions.target === '18'
310310
? 'react-compiler-runtime'
311311
: 'react/compiler-runtime'
312+
const jsxImportSource = reactOptions.jsxImportSource ?? 'react'
313+
const jsxImportRuntime = `${jsxImportSource}/jsx-runtime`
314+
const jsxImportDevRuntime = `${jsxImportSource}/jsx-dev-runtime`
312315

313316
const loadCompiler = async (
314317
onError: (message: string) => never,
@@ -348,7 +351,17 @@ function createReactCompilerPlugin(
348351
},
349352
},
350353
async handler(code, id) {
354+
// Follow what the builtin oxc plugin does: https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/oxc.ts#L250-L267
355+
const [filepath] = id.split('?')
356+
const isJSX = filepath.endsWith('x')
351357
const isClient = this.environment?.config.consumer !== 'server'
358+
const refreshEnabled =
359+
isClient &&
360+
isFastRefreshEnabled() &&
361+
(isJSX ||
362+
code.includes(jsxImportRuntime) ||
363+
code.includes(jsxImportDevRuntime))
364+
352365
const shouldCompile =
353366
isClient &&
354367
(reactCompilerOptions.compilationMode === 'annotation'
@@ -363,7 +376,7 @@ function createReactCompilerPlugin(
363376
runtime: reactOptions.jsxRuntime,
364377
development: jsxDevelopment,
365378
importSource: reactOptions.jsxImportSource,
366-
refresh: isClient && isFastRefreshEnabled(),
379+
refresh: refreshEnabled,
367380
},
368381
reactCompiler: shouldCompile ? reactCompilerOptions : false,
369382
sourcemap: this.environment

0 commit comments

Comments
 (0)