Skip to content

Commit 206e22a

Browse files
authored
fix(plugin-react): return code if should skip in transform (fix #7586) (#8676)
1 parent 87e51f7 commit 206e22a

File tree

7 files changed

+132
-1
lines changed

7 files changed

+132
-1
lines changed

packages/plugin-react/src/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,10 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
276276
!(isProjectFile && babelOptions.babelrc)
277277

278278
if (shouldSkip) {
279-
return // Avoid parsing if no plugins exist.
279+
// Avoid parsing if no plugins exist.
280+
return {
281+
code
282+
}
280283
}
281284

282285
const parserPlugins: typeof babelOptions.parserOpts.plugins = [

playground/react-classic/App.jsx

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { useState } from 'react'
2+
3+
function App() {
4+
const [count, setCount] = useState(0)
5+
return (
6+
<div className="App">
7+
<header className="App-header">
8+
<h1>Hello Vite + React</h1>
9+
<p>
10+
<button onClick={() => setCount((count) => count + 1)}>
11+
count is: {count}
12+
</button>
13+
</p>
14+
<p>
15+
Edit <code>App.jsx</code> and save to test HMR updates.
16+
</p>
17+
<a
18+
className="App-link"
19+
href="https://reactjs.org"
20+
target="_blank"
21+
rel="noopener noreferrer"
22+
>
23+
Learn React
24+
</a>
25+
</header>
26+
</div>
27+
)
28+
}
29+
30+
export default App
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { editFile, isServe, page, untilUpdated } from '~utils'
2+
3+
test('should render', async () => {
4+
expect(await page.textContent('h1')).toMatch('Hello Vite + React')
5+
})
6+
7+
test('should update', async () => {
8+
expect(await page.textContent('button')).toMatch('count is: 0')
9+
await page.click('button')
10+
expect(await page.textContent('button')).toMatch('count is: 1')
11+
})
12+
13+
test('should hmr', async () => {
14+
editFile('App.jsx', (code) => code.replace('Vite + React', 'Updated'))
15+
await untilUpdated(() => page.textContent('h1'), 'Hello Updated')
16+
// preserve state
17+
expect(await page.textContent('button')).toMatch('count is: 1')
18+
})
19+
20+
test.runIf(isServe)(
21+
'should have annotated jsx with file location metadata',
22+
async () => {
23+
const meta = await page.evaluate(() => {
24+
const button = document.querySelector('button')
25+
const key = Object.keys(button).find(
26+
(key) => key.indexOf('__reactFiber') === 0
27+
)
28+
return button[key]._debugSource
29+
})
30+
// If the evaluate call doesn't crash, and the returned metadata has
31+
// the expected fields, we're good.
32+
expect(Object.keys(meta).sort()).toEqual([
33+
'columnNumber',
34+
'fileName',
35+
'lineNumber'
36+
])
37+
}
38+
)

playground/react-classic/index.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<div id="app"></div>
2+
<script type="module">
3+
import React from 'react'
4+
import ReactDOM from 'react-dom/client'
5+
import App from './App.jsx'
6+
7+
ReactDOM.createRoot(document.getElementById('app')).render(
8+
React.createElement(App)
9+
)
10+
</script>

playground/react-classic/package.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "test-react-classic",
3+
"private": true,
4+
"version": "0.0.0",
5+
"scripts": {
6+
"dev": "vite",
7+
"build": "vite build",
8+
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
9+
"preview": "vite preview"
10+
},
11+
"dependencies": {
12+
"react": "^18.1.0",
13+
"react-dom": "^18.1.0"
14+
},
15+
"devDependencies": {
16+
"@vitejs/plugin-react": "workspace:*"
17+
},
18+
"babel": {
19+
"presets": [
20+
"@babel/preset-env"
21+
]
22+
}
23+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import react from '@vitejs/plugin-react'
2+
import type { UserConfig } from 'vite'
3+
4+
const config: UserConfig = {
5+
plugins: [
6+
react({
7+
jsxRuntime: 'classic'
8+
})
9+
],
10+
build: {
11+
// to make tests faster
12+
minify: false
13+
}
14+
}
15+
16+
export default config

pnpm-lock.yaml

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)