Skip to content

fix(hmr): skip HMR for JSX files with hooks #480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/common/refresh-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const runtimePublicPath = '/@react-refresh'

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

// NOTE: this is exposed publicly via plugin-react
export const preambleCode = `import { injectIntoGlobalHook } from "__BASE__${runtimePublicPath.slice(
Expand Down
6 changes: 5 additions & 1 deletion packages/plugin-react-oxc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

## Unreleased

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

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

### Skip HMR for JSX files with hooks [#480](https://github.com/vitejs/vite-plugin-react/pull/480)

This removes the HMR warning for hooks with JSX.

## 0.1.1 (2025-04-10)

## 0.1.0 (2025-04-09)
Expand Down
8 changes: 6 additions & 2 deletions packages/plugin-react-swc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

## Unreleased

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

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

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

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

### Skip HMR for JSX files with hooks [#480](https://github.com/vitejs/vite-plugin-react/pull/480)

This removes the HMR warning for hooks with JSX.

## 3.9.0 (2025-04-15)

### Make compatible with rolldown-vite
Expand Down
6 changes: 5 additions & 1 deletion packages/plugin-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

## Unreleased

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

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

### Skip HMR for JSX files with hooks [#480](https://github.com/vitejs/vite-plugin-react/pull/480)

This removes the HMR warning for hooks with JSX.

## 4.4.1 (2025-04-19)

Fix type issue when using `moduleResolution: "node"` in tsconfig [#462](https://github.com/vitejs/vite-plugin-react/pull/462)
Expand Down
27 changes: 27 additions & 0 deletions playground/hook-with-jsx/__tests__/hook-with-jsx.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { expect, test } from 'vitest'
import {
editFile,
isServe,
page,
untilBrowserLogAfter,
untilUpdated,
} from '~utils'

test('should render', async () => {
expect(await page.textContent('button')).toMatch('count is 0')
expect(await page.click('button'))
expect(await page.textContent('button')).toMatch('count is 1')
})

if (isServe) {
test('Hook with JSX HMR', async () => {
editFile('src/useButtonHook.tsx', (code) =>
code.replace('count is {count}', 'count is {count}!'),
)
await untilBrowserLogAfter(
() => page.textContent('button'),
'[vite] hot updated: /src/App.tsx',
)
await untilUpdated(() => page.textContent('button'), 'count is 1!')
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '../hook-with-jsx.spec'
13 changes: 13 additions & 0 deletions playground/hook-with-jsx/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>React hook with JSX</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/index.tsx"></script>
</body>
</html>
19 changes: 19 additions & 0 deletions playground/hook-with-jsx/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@vitejs/test-hook-with-jsx",
"private": true,
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"react": "^19.1.0",
"react-dom": "^19.1.0"
},
"devDependencies": {
"@types/react": "^19.1.2",
"@types/react-dom": "^19.1.2",
"@vitejs/plugin-react": "workspace:*"
}
}
1 change: 1 addition & 0 deletions playground/hook-with-jsx/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions playground/hook-with-jsx/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useButtonHook } from './useButtonHook.tsx'

export function App() {
const button = useButtonHook()
return <div>{button}</div>
}
9 changes: 9 additions & 0 deletions playground/hook-with-jsx/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import { App } from './App.tsx'

createRoot(document.getElementById('root')!).render(
<StrictMode>
<App />
</StrictMode>,
)
10 changes: 10 additions & 0 deletions playground/hook-with-jsx/src/useButtonHook.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useState } from 'react'

export function useButtonHook() {
const [count, setCount] = useState(0)
return (
<button onClick={() => setCount((count) => count + 1)}>
count is {count}
</button>
)
}
23 changes: 23 additions & 0 deletions playground/hook-with-jsx/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"include": ["src"],
"compilerOptions": {
"module": "ESNext",
"lib": ["ESNext", "DOM", "DOM.Iterable"],
"target": "ESNext",
"jsx": "react-jsx",
"types": ["vite/client"],
"noEmit": true,
"isolatedModules": true,
"skipLibCheck": true,
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"useUnknownInCatchVariables": true
}
}
7 changes: 7 additions & 0 deletions playground/hook-with-jsx/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

export default defineConfig({
server: { port: 8909 /* Should be unique */ },
plugins: [react()],
})
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading