Skip to content

Commit cee4b25

Browse files
authored
fix(oxc): disable refresh transform when server.hmr: false (#502)
1 parent 4951c90 commit cee4b25

File tree

10 files changed

+118
-1
lines changed

10 files changed

+118
-1
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
8282
},
8383
}
8484

85+
const viteConfigPost: Plugin = {
86+
name: 'vite:react-oxc:config-post',
87+
enforce: 'post',
88+
config(userConfig) {
89+
if (userConfig.server?.hmr === false) {
90+
return {
91+
oxc: {
92+
jsx: {
93+
refresh: false,
94+
},
95+
},
96+
}
97+
}
98+
},
99+
}
100+
85101
const viteRefreshRuntime: Plugin = {
86102
name: 'vite:react-oxc:refresh-runtime',
87103
enforce: 'pre',
@@ -148,5 +164,5 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
148164
},
149165
}
150166

151-
return [viteConfig, viteRefreshRuntime, viteRefreshWrapper]
167+
return [viteConfig, viteConfigPost, viteRefreshRuntime, viteRefreshWrapper]
152168
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { expect, test } from 'vitest'
2+
import { page } from '~utils'
3+
4+
test('basic', async () => {
5+
expect(await page.textContent('button')).toMatch('count is 0')
6+
expect(await page.click('button'))
7+
expect(await page.textContent('button')).toMatch('count is 1')
8+
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '../hmr-false.spec'

playground/hmr-false/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<div id="app"></div>
2+
<script type="module" src="./src/main.tsx"></script>

playground/hmr-false/package.json

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

playground/hmr-false/src/App.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { useState } from 'react'
2+
3+
export default function App() {
4+
const [count, setCount] = useState(0)
5+
6+
return (
7+
<>
8+
<button onClick={() => setCount((count) => count + 1)}>
9+
count is {count}
10+
</button>
11+
</>
12+
)
13+
}

playground/hmr-false/src/main.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import ReactDOM from 'react-dom/client'
2+
import App from './App.jsx'
3+
4+
ReactDOM.createRoot(document.getElementById('app')!).render(<App />)

playground/hmr-false/tsconfig.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"include": ["src"],
3+
"compilerOptions": {
4+
"target": "ES2020",
5+
"useDefineForClassFields": true,
6+
"lib": ["ES2020", "DOM", "DOM.Iterable"],
7+
"types": ["vite/client"],
8+
"module": "ESNext",
9+
"skipLibCheck": true,
10+
11+
/* Bundler mode */
12+
"moduleResolution": "bundler",
13+
"allowImportingTsExtensions": true,
14+
"resolveJsonModule": true,
15+
"isolatedModules": true,
16+
"noEmit": true,
17+
"jsx": "react-jsx",
18+
19+
/* Linting */
20+
"strict": true,
21+
"noUnusedLocals": true,
22+
"noUnusedParameters": true,
23+
"noFallthroughCasesInSwitch": true
24+
}
25+
}

playground/hmr-false/vite.config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import react from '@vitejs/plugin-react'
2+
import { defineConfig } from 'vite'
3+
4+
export default defineConfig({
5+
server: {
6+
hmr: false,
7+
},
8+
plugins: [react()],
9+
})

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)