The React plugin provides support for React, integrating features such as JSX compilation and React Refresh.
You can install the plugin using the following command:
You can register the plugin in the rsbuild.config.ts file:
After registering the plugin, you can directly develop React.
Configure the behavior of SWC to transform React code, the same as SWC's jsc.transform.react option.
'automatic' | 'classic''automatic'By default, Rsbuild uses the new JSX runtime introduced in React 17, which is runtime: 'automatic'.
If your current React version is lower than 16.14.0, you can set runtime to 'classic':
React 16.14.0 can use the new JSX runtime.
When using the classic JSX runtime, you need to manually import React in your code:
string'react'When runtime is 'automatic', you can specify the import path of the JSX runtime through importSource.
For example, when using Emotion, you can set importSource to '@emotion/react':
See Customize JSX for more details.
booleanWhether to enable React Fast Refresh.
Most of the time, you should use the plugin's fastRefresh option to enable or disable Fast Refresh.
When chunkSplit.strategy set to split-by-experience, Rsbuild will automatically split react and router related packages into separate chunks by default:
lib-react.js: includes react, react-dom, and react's sub-dependencies (scheduler).lib-router.js: includes react-router, react-router-dom, and react-router's sub-dependencies (history, @remix-run/router).This option is used to control this behavior and determine whether the react and router related packages need to be split into separate chunks.
booleanfalseWhen set to true, enables the React Profiler for performance analysis in production builds. Use the React DevTools to examine profiling results and identify potential performance optimizations. Profiling adds a slight overhead, so it's disabled by default in production mode.
Set REACT_PROFILER=true when running build script:
As Windows does not support the above usage, you can also use cross-env to set environment variables. This ensures compatibility across different systems:
See the React docs for details about profiling using the React DevTools.
Set the options for @rspack/plugin-react-refresh. The passed value will be shallowly merged with the default value.
booleantrueWhether to enable React Fast Refresh in development mode.
If fastRefresh is set to true, @rsbuild/plugin-react will automatically register the @rspack/plugin-react-refresh plugin.
To disable Fast Refresh, set it to false: