1 | # @vitejs/plugin-react [![npm](https://img.shields.io/npm/v/@vitejs/plugin-react.svg)](https://npmjs.com/package/@vitejs/plugin-react)
|
---|
2 |
|
---|
3 | The default Vite plugin for React projects.
|
---|
4 |
|
---|
5 | - enable [Fast Refresh](https://www.npmjs.com/package/react-refresh) in development (requires react >= 16.9)
|
---|
6 | - use the [automatic JSX runtime](https://legacy.reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html)
|
---|
7 | - use custom Babel plugins/presets
|
---|
8 | - small installation size
|
---|
9 |
|
---|
10 | ```js
|
---|
11 | // vite.config.js
|
---|
12 | import { defineConfig } from 'vite'
|
---|
13 | import react from '@vitejs/plugin-react'
|
---|
14 |
|
---|
15 | export default defineConfig({
|
---|
16 | plugins: [react()],
|
---|
17 | })
|
---|
18 | ```
|
---|
19 |
|
---|
20 | ## Options
|
---|
21 |
|
---|
22 | ### include/exclude
|
---|
23 |
|
---|
24 | Includes `.js`, `.jsx`, `.ts` & `.tsx` by default. This option can be used to add fast refresh to `.mdx` files:
|
---|
25 |
|
---|
26 | ```js
|
---|
27 | import { defineConfig } from 'vite'
|
---|
28 | import react from '@vitejs/plugin-react'
|
---|
29 | import mdx from '@mdx-js/rollup'
|
---|
30 |
|
---|
31 | export default defineConfig({
|
---|
32 | plugins: [
|
---|
33 | { enforce: 'pre', ...mdx() },
|
---|
34 | react({ include: /\.(mdx|js|jsx|ts|tsx)$/ }),
|
---|
35 | ],
|
---|
36 | })
|
---|
37 | ```
|
---|
38 |
|
---|
39 | > `node_modules` are never processed by this plugin (but esbuild will)
|
---|
40 |
|
---|
41 | ### jsxImportSource
|
---|
42 |
|
---|
43 | Control where the JSX factory is imported from. Default to `'react'`
|
---|
44 |
|
---|
45 | ```js
|
---|
46 | react({ jsxImportSource: '@emotion/react' })
|
---|
47 | ```
|
---|
48 |
|
---|
49 | ### jsxRuntime
|
---|
50 |
|
---|
51 | By default, the plugin uses the [automatic JSX runtime](https://legacy.reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html). However, if you encounter any issues, you may opt out using the `jsxRuntime` option.
|
---|
52 |
|
---|
53 | ```js
|
---|
54 | react({ jsxRuntime: 'classic' })
|
---|
55 | ```
|
---|
56 |
|
---|
57 | ### babel
|
---|
58 |
|
---|
59 | The `babel` option lets you add plugins, presets, and [other configuration](https://babeljs.io/docs/en/options) to the Babel transformation performed on each included file.
|
---|
60 |
|
---|
61 | ```js
|
---|
62 | react({
|
---|
63 | babel: {
|
---|
64 | presets: [...],
|
---|
65 | // Your plugins run before any built-in transform (eg: Fast Refresh)
|
---|
66 | plugins: [...],
|
---|
67 | // Use .babelrc files
|
---|
68 | babelrc: true,
|
---|
69 | // Use babel.config.js files
|
---|
70 | configFile: true,
|
---|
71 | }
|
---|
72 | })
|
---|
73 | ```
|
---|
74 |
|
---|
75 | Note: When not using plugins, only esbuild is used for production builds, resulting in faster builds.
|
---|
76 |
|
---|
77 | #### Proposed syntax
|
---|
78 |
|
---|
79 | If you are using ES syntax that are still in proposal status (e.g. class properties), you can selectively enable them with the `babel.parserOpts.plugins` option:
|
---|
80 |
|
---|
81 | ```js
|
---|
82 | react({
|
---|
83 | babel: {
|
---|
84 | parserOpts: {
|
---|
85 | plugins: ['decorators-legacy'],
|
---|
86 | },
|
---|
87 | },
|
---|
88 | })
|
---|
89 | ```
|
---|
90 |
|
---|
91 | This option does not enable _code transformation_. That is handled by esbuild.
|
---|
92 |
|
---|
93 | **Note:** TypeScript syntax is handled automatically.
|
---|
94 |
|
---|
95 | Here's the [complete list of Babel parser plugins](https://babeljs.io/docs/en/babel-parser#ecmascript-proposalshttpsgithubcombabelproposals).
|
---|
96 |
|
---|
97 | ## Middleware mode
|
---|
98 |
|
---|
99 | In [middleware mode](https://vitejs.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:
|
---|
100 |
|
---|
101 | ```js
|
---|
102 | app.get('/', async (req, res, next) => {
|
---|
103 | try {
|
---|
104 | let html = fs.readFileSync(path.resolve(root, 'index.html'), 'utf-8')
|
---|
105 |
|
---|
106 | // Transform HTML using Vite plugins.
|
---|
107 | html = await viteServer.transformIndexHtml(req.url, html)
|
---|
108 |
|
---|
109 | res.send(html)
|
---|
110 | } catch (e) {
|
---|
111 | return next(e)
|
---|
112 | }
|
---|
113 | })
|
---|
114 | ```
|
---|
115 |
|
---|
116 | Otherwise, you'll probably get this error:
|
---|
117 |
|
---|
118 | ```
|
---|
119 | Uncaught Error: @vitejs/plugin-react can't detect preamble. Something is wrong.
|
---|
120 | ```
|
---|
121 |
|
---|
122 | ## Consistent components exports
|
---|
123 |
|
---|
124 | 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).
|
---|
125 |
|
---|
126 | 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.
|
---|
127 |
|
---|
128 | You can catch mistakes and get more detailed warning with this [eslint rule](https://github.com/ArnaudBarre/eslint-plugin-react-refresh).
|
---|