Skip to content
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: 2 additions & 0 deletions packages/plugin-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

### HMR did not work for components imported with queries with rolldown-vite ([#872](https://github.com/vitejs/vite-plugin-react/pull/872))

### Perf: simplify refresh wrapper generation ([#835](https://github.com/vitejs/vite-plugin-react/pull/835))

## 5.0.2 (2025-08-28)
Expand Down
8 changes: 4 additions & 4 deletions packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ export default function viteReact(opts: Options = {}): Plugin[] {
// if development is enabled and those properties are already present
development: false,
},
jsxRefreshInclude: include,
jsxRefreshExclude: exclude,
jsxRefreshInclude: makeIdFiltersToMatchWithQuery(include),
jsxRefreshExclude: makeIdFiltersToMatchWithQuery(exclude),
},
}
} else {
Expand All @@ -156,8 +156,8 @@ export default function viteReact(opts: Options = {}): Plugin[] {
importSource: opts.jsxImportSource,
refresh: command === 'serve',
},
jsxRefreshInclude: include,
jsxRefreshExclude: exclude,
jsxRefreshInclude: makeIdFiltersToMatchWithQuery(include),
jsxRefreshExclude: makeIdFiltersToMatchWithQuery(exclude),
},
optimizeDeps: {
rollupOptions: { transform: { jsx: { runtime: 'automatic' } } },
Expand Down
3 changes: 3 additions & 0 deletions playground/react-classic/App.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useState } from 'react'
import WithQuery from './components/WithQuery?qs-should-not-break-plugin-react'

function App() {
const [count, setCount] = useState(0)
Expand All @@ -23,6 +24,8 @@ function App() {
Learn React
</a>
</header>

<WithQuery />
</div>
)
}
Expand Down
22 changes: 22 additions & 0 deletions playground/react-classic/__tests__/react.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@ test.runIf(isServe)('should hmr', async () => {
expect(await page.textContent('button')).toMatch('count is: 1')
})

test.runIf(isServe)('should hmr files with queries', async () => {
expect(await page.textContent('#WithQuery')).toBe('With Query')

expect(await page.textContent('#WithQuery-button')).toMatch('count is: 0')
await page.click('#WithQuery-button')
expect(await page.textContent('#WithQuery-button')).toMatch('count is: 1')

editFile('components/WithQuery.jsx', (code) =>
code.replace('With Query', 'With Query Updated'),
)
await expect
.poll(() => page.textContent('#WithQuery'))
.toBe('With Query Updated')
// preserve state
expect(await page.textContent('#WithQuery-button')).toMatch('count is: 1')

editFile('components/WithQuery.jsx', (code) =>
code.replace('With Query Updated', 'With Query'),
)
await expect.poll(() => page.textContent('#WithQuery')).toBe('With Query')
})

test.runIf(isServe)(
'should have annotated jsx with file location metadata',
async () => {
Expand Down
16 changes: 16 additions & 0 deletions playground/react-classic/components/WithQuery.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React, { useState } from 'react'

export default function WithQuery() {
const [count, setCount] = useState(0)
return (
<>
<div id="WithQuery">With Query</div>
<button
id="WithQuery-button"
onClick={() => setCount((count) => count + 1)}
>
count is: {count}
</button>
</>
)
}
4 changes: 2 additions & 2 deletions playground/react/App.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState } from 'react'
import Button from 'jsx-entry'
import Dummy from './components/Dummy?qs-should-not-break-plugin-react'
import WithQuery from './components/WithQuery?qs-should-not-break-plugin-react'
import { Accordion } from './components/Accordion'
import Parent from './hmr/parent'
import { JsxImportRuntime } from './hmr/jsx-import-runtime'
Expand Down Expand Up @@ -39,7 +39,7 @@ function App() {
</a>
</header>

<Dummy />
<WithQuery />
<Accordion.Root>
<Accordion.Item>First Item</Accordion.Item>
<Accordion.Item>Second Item</Accordion.Item>
Expand Down
22 changes: 22 additions & 0 deletions playground/react/__tests__/react.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,28 @@ test.runIf(isServe)('should hmr', async () => {
await expect.poll(() => page.textContent('h1')).toMatch('Hello Vite + React')
})

test.runIf(isServe)('should hmr files with queries', async () => {
expect(await page.textContent('#WithQuery')).toBe('With Query')

expect(await page.textContent('#WithQuery-button')).toMatch('count is: 0')
await page.click('#WithQuery-button')
expect(await page.textContent('#WithQuery-button')).toMatch('count is: 1')

editFile('components/WithQuery.jsx', (code) =>
code.replace('With Query', 'With Query Updated'),
)
await expect
.poll(() => page.textContent('#WithQuery'))
.toBe('With Query Updated')
// preserve state
expect(await page.textContent('#WithQuery-button')).toMatch('count is: 1')

editFile('components/WithQuery.jsx', (code) =>
code.replace('With Query Updated', 'With Query'),
)
await expect.poll(() => page.textContent('#WithQuery')).toBe('With Query')
})

test.runIf(isServe)('should not invalidate when code is invalid', async () => {
editFile('App.jsx', (code) =>
code.replace('<div className="App">', '<div className="App"}>'),
Expand Down
3 changes: 0 additions & 3 deletions playground/react/components/Dummy.jsx

This file was deleted.

16 changes: 16 additions & 0 deletions playground/react/components/WithQuery.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useState } from 'react'

export default function WithQuery() {
const [count, setCount] = useState(0)
return (
<>
<div id="WithQuery">With Query</div>
<button
id="WithQuery-button"
onClick={() => setCount((count) => count + 1)}
>
count is: {count}
</button>
</>
)
}
Loading