An all in one opinionated preset for writing React apps with the vite bundler.
Features:
- Sets up Hot Module Replacement & Automatic JSX runtime(No need for
import React from 'react'
) via @vitejs/plugin-react - Remove devtools in production if needed
First install the preset package from npm:
npm install --save-dev vite-preset-react
# or
yarn add -D vite-preset-react
Enhance your vite config with the React preset plugin in your vite.config.ts
or vite.config.js
:
import { defineConfig } from 'vite';
import react from 'vite-preset-react';
export default defineConfig({
plugins: [react()],
});
Options can be passed to our preset plugin via the first argument:
export default defineConfig({
plugins: [react({ removeDevtoolsInProd: true, injectReact: true })],
});
Option | Type | Default | Description |
---|---|---|---|
removeDevtoolsInProd |
boolean |
false |
Removes React Devtools in production build |
injectReact |
boolean |
true |
Injects import React from 'react' in every JS file to avoid importing it in every file manually |
reactPluginOptions |
Options |
undefined |
Options to pass to the underlying @vitejs/plugin-react . See here |
If you are using the official react
or react-ts
template, and wanna switch to this one, follow this:
-
Remove
@vitejs/plugin-react
frompackage.json
. -
Install this preset:
npm install -D vite-preset-react
Or if you're a Yarn person
yarn add -D vite-preset-react
- If you're using
react-ts
template, opentsconfig.json
, and changejsx
field topreserve
.
There!! You're all set!
Theoretically, this package should work well with preact. However, it's highly recommended to use the official @preactjs/preset-vite.
If you're getting red squiggles under your JSX, follow this:
- Go to tsconfig.json
- Set the
jsx
option topreserve
.
There, that should fix it.
When would you want to use this plugin instead of the official @vitejs/plugin-react
? Well, the answer is: If you don't need control over whether React devtools are removed or not
Yep, that's pretty much it. Earlier, when the official react plugin was @vitejs/plugin-react-refresh
, this plugin would enable automatic JSX, so you wouldn't have import React in every file. Now with the new @vitejs/plugin-react
providing the much better automatic JSX runtime, eliminating the need for vite-preset-react
altogether, unless you need to remove react devtools.
MIT, see the license file.