You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The future default Vite plugin for React projects.
4
+
5
+
- enable [Fast Refresh](https://www.npmjs.com/package/react-refresh) in development
6
+
- use the [automatic JSX runtime](https://legacy.reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html)
7
+
- small installation size
8
+
9
+
```js
10
+
// vite.config.js
11
+
import { defineConfig } from'vite'
12
+
importreactfrom'@vitejs/plugin-react-oxc'
13
+
14
+
exportdefaultdefineConfig({
15
+
plugins: [react()],
16
+
})
17
+
```
18
+
19
+
## Caveats
20
+
21
+
-`jsx runtime` is always `automatic`
22
+
- this plugin only works with [`rolldown-vite`](https://vitejs.dev/guide/rolldown)
23
+
24
+
## Options
25
+
26
+
### include/exclude
27
+
28
+
Includes `.js`, `.jsx`, `.ts` & `.tsx` by default. This option can be used to add fast refresh to `.mdx` files:
29
+
30
+
```js
31
+
import { defineConfig } from'vite'
32
+
importreactfrom'@vitejs/plugin-react'
33
+
importmdxfrom'@mdx-js/rollup'
34
+
35
+
exportdefaultdefineConfig({
36
+
plugins: [
37
+
{ enforce:'pre', ...mdx() },
38
+
react({ include:/\.(mdx|js|jsx|ts|tsx)$/ }),
39
+
],
40
+
})
41
+
```
42
+
43
+
> `node_modules` are never processed by this plugin (but Oxc will)
44
+
45
+
### jsxImportSource
46
+
47
+
Control where the JSX factory is imported from. Default to `'react'`
48
+
49
+
```js
50
+
react({ jsxImportSource:'@emotion/react' })
51
+
```
52
+
53
+
## Middleware mode
54
+
55
+
In [middleware mode](https://vite.dev/config/server-options.html#server-middlewaremode), you should make sure your entry `index.html` file is transformed by Vite. Here's an example for an Express server:
56
+
57
+
```js
58
+
app.get('/', async (req, res, next) => {
59
+
try {
60
+
let html =fs.readFileSync(path.resolve(root, 'index.html'), 'utf-8')
61
+
62
+
// Transform HTML using Vite plugins.
63
+
html =awaitviteServer.transformIndexHtml(req.url, html)
64
+
65
+
res.send(html)
66
+
} catch (e) {
67
+
returnnext(e)
68
+
}
69
+
})
70
+
```
71
+
72
+
Otherwise, you'll probably get this error:
73
+
74
+
```
75
+
Uncaught Error: @vitejs/plugin-react-oxc can't detect preamble. Something is wrong.
76
+
```
77
+
78
+
## Consistent components exports
79
+
80
+
For React refresh to work correctly, your file should only export React components. You can find a good explanation in the [Gatsby docs](https://www.gatsbyjs.com/docs/reference/local-development/fast-refresh/#how-it-works).
81
+
82
+
If an incompatible change in exports is found, the module will be invalidated and HMR will propagate. To make it easier to export simple constants alongside your component, the module is only invalidated when their value changes.
83
+
84
+
You can catch mistakes and get more detailed warning with this [eslint rule](https://github.com/ArnaudBarre/eslint-plugin-react-refresh).
0 commit comments