Skip to content

Commit 02b1ede

Browse files
authored
fix(hmr): skip HMR for JSX files with hooks (#480)
1 parent 5a94bd7 commit 02b1ede

File tree

15 files changed

+152
-5
lines changed

15 files changed

+152
-5
lines changed

packages/common/refresh-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export const runtimePublicPath = '/@react-refresh'
22

33
const reactCompRE = /extends\s+(?:React\.)?(?:Pure)?Component/
4-
const refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/
4+
const refreshContentRE = /\$RefreshReg\$\(/
55

66
// NOTE: this is exposed publicly via plugin-react
77
export const preambleCode = `import { injectIntoGlobalHook } from "__BASE__${runtimePublicPath.slice(

packages/plugin-react-oxc/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
## Unreleased
44

5-
### Add `filter` for rolldown-vite
5+
### Add `filter` for rolldown-vite [#470](https://github.com/vitejs/vite-plugin-react/pull/470)
66

77
Added `filter` so that it is more performant when running this plugin with rolldown-powered version of Vite.
88

9+
### Skip HMR for JSX files with hooks [#480](https://github.com/vitejs/vite-plugin-react/pull/480)
10+
11+
This removes the HMR warning for hooks with JSX.
12+
913
## 0.1.1 (2025-04-10)
1014

1115
## 0.1.0 (2025-04-09)

packages/plugin-react-swc/CHANGELOG.md

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

33
## Unreleased
44

5-
### Add `filter` for rolldown-vite
5+
### Add `filter` for rolldown-vite [#470](https://github.com/vitejs/vite-plugin-react/pull/470)
66

77
Added `filter` so that it is more performant when running this plugin with rolldown-powered version of Vite.
88

9-
### Skip HMR preamble in Vitest browser mode
9+
### Skip HMR preamble in Vitest browser mode [#478](https://github.com/vitejs/vite-plugin-react/pull/478)
1010

1111
This was causing annoying `Sourcemap for "/@react-refresh" points to missing source files` and is unnecessary in test mode.
1212

13+
### Skip HMR for JSX files with hooks [#480](https://github.com/vitejs/vite-plugin-react/pull/480)
14+
15+
This removes the HMR warning for hooks with JSX.
16+
1317
## 3.9.0 (2025-04-15)
1418

1519
### Make compatible with rolldown-vite

packages/plugin-react/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
## Unreleased
44

5-
### Add `filter` for rolldown-vite
5+
### Add `filter` for rolldown-vite [#470](https://github.com/vitejs/vite-plugin-react/pull/470)
66

77
Added `filter` so that it is more performant when running this plugin with rolldown-powered version of Vite.
88

9+
### Skip HMR for JSX files with hooks [#480](https://github.com/vitejs/vite-plugin-react/pull/480)
10+
11+
This removes the HMR warning for hooks with JSX.
12+
913
## 4.4.1 (2025-04-19)
1014

1115
Fix type issue when using `moduleResolution: "node"` in tsconfig [#462](https://github.com/vitejs/vite-plugin-react/pull/462)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { expect, test } from 'vitest'
2+
import {
3+
editFile,
4+
isServe,
5+
page,
6+
untilBrowserLogAfter,
7+
untilUpdated,
8+
} from '~utils'
9+
10+
test('should render', async () => {
11+
expect(await page.textContent('button')).toMatch('count is 0')
12+
expect(await page.click('button'))
13+
expect(await page.textContent('button')).toMatch('count is 1')
14+
})
15+
16+
if (isServe) {
17+
test('Hook with JSX HMR', async () => {
18+
editFile('src/useButtonHook.tsx', (code) =>
19+
code.replace('count is {count}', 'count is {count}!'),
20+
)
21+
await untilBrowserLogAfter(
22+
() => page.textContent('button'),
23+
'[vite] hot updated: /src/App.tsx',
24+
)
25+
await untilUpdated(() => page.textContent('button'), 'count is 1!')
26+
})
27+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '../hook-with-jsx.spec'

playground/hook-with-jsx/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>React hook with JSX</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/index.tsx"></script>
12+
</body>
13+
</html>

playground/hook-with-jsx/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "@vitejs/test-hook-with-jsx",
3+
"private": true,
4+
"type": "module",
5+
"scripts": {
6+
"dev": "vite",
7+
"build": "vite build",
8+
"preview": "vite preview"
9+
},
10+
"dependencies": {
11+
"react": "^19.1.0",
12+
"react-dom": "^19.1.0"
13+
},
14+
"devDependencies": {
15+
"@types/react": "^19.1.2",
16+
"@types/react-dom": "^19.1.2",
17+
"@vitejs/plugin-react": "workspace:*"
18+
}
19+
}
Lines changed: 1 addition & 0 deletions
Loading

playground/hook-with-jsx/src/App.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { useButtonHook } from './useButtonHook.tsx'
2+
3+
export function App() {
4+
const button = useButtonHook()
5+
return <div>{button}</div>
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { StrictMode } from 'react'
2+
import { createRoot } from 'react-dom/client'
3+
import { App } from './App.tsx'
4+
5+
createRoot(document.getElementById('root')!).render(
6+
<StrictMode>
7+
<App />
8+
</StrictMode>,
9+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { useState } from 'react'
2+
3+
export function useButtonHook() {
4+
const [count, setCount] = useState(0)
5+
return (
6+
<button onClick={() => setCount((count) => count + 1)}>
7+
count is {count}
8+
</button>
9+
)
10+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"include": ["src"],
3+
"compilerOptions": {
4+
"module": "ESNext",
5+
"lib": ["ESNext", "DOM", "DOM.Iterable"],
6+
"target": "ESNext",
7+
"jsx": "react-jsx",
8+
"types": ["vite/client"],
9+
"noEmit": true,
10+
"isolatedModules": true,
11+
"skipLibCheck": true,
12+
"moduleResolution": "bundler",
13+
"allowImportingTsExtensions": true,
14+
"resolveJsonModule": true,
15+
16+
/* Linting */
17+
"strict": true,
18+
"noUnusedLocals": true,
19+
"noUnusedParameters": true,
20+
"noFallthroughCasesInSwitch": true,
21+
"useUnknownInCatchVariables": true
22+
}
23+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'vite'
2+
import react from '@vitejs/plugin-react'
3+
4+
export default defineConfig({
5+
server: { port: 8909 /* Should be unique */ },
6+
plugins: [react()],
7+
})

pnpm-lock.yaml

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)