source: imaps-frontend/node_modules/@vitejs/plugin-react/README.md

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 3.8 KB
Line 
1# @vitejs/plugin-react [![npm](https://img.shields.io/npm/v/@vitejs/plugin-react.svg)](https://npmjs.com/package/@vitejs/plugin-react)
2
3The 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
12import { defineConfig } from 'vite'
13import react from '@vitejs/plugin-react'
14
15export default defineConfig({
16 plugins: [react()],
17})
18```
19
20## Options
21
22### include/exclude
23
24Includes `.js`, `.jsx`, `.ts` & `.tsx` by default. This option can be used to add fast refresh to `.mdx` files:
25
26```js
27import { defineConfig } from 'vite'
28import react from '@vitejs/plugin-react'
29import mdx from '@mdx-js/rollup'
30
31export 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
43Control where the JSX factory is imported from. Default to `'react'`
44
45```js
46react({ jsxImportSource: '@emotion/react' })
47```
48
49### jsxRuntime
50
51By 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
54react({ jsxRuntime: 'classic' })
55```
56
57### babel
58
59The `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
62react({
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
75Note: When not using plugins, only esbuild is used for production builds, resulting in faster builds.
76
77#### Proposed syntax
78
79If 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
82react({
83 babel: {
84 parserOpts: {
85 plugins: ['decorators-legacy'],
86 },
87 },
88})
89```
90
91This option does not enable _code transformation_. That is handled by esbuild.
92
93**Note:** TypeScript syntax is handled automatically.
94
95Here's the [complete list of Babel parser plugins](https://babeljs.io/docs/en/babel-parser#ecmascript-proposalshttpsgithubcombabelproposals).
96
97## Middleware mode
98
99In [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
102app.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
116Otherwise, you'll probably get this error:
117
118```
119Uncaught Error: @vitejs/plugin-react can't detect preamble. Something is wrong.
120```
121
122## Consistent components exports
123
124For 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
126If 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
128You can catch mistakes and get more detailed warning with this [eslint rule](https://github.com/ArnaudBarre/eslint-plugin-react-refresh).
Note: See TracBrowser for help on using the repository browser.