React refresh plugin for Rspack.
2.x: For Rspack v2.1.x: For Rspack v1, see v1.x - README for usage guide.
First you need to install this plugin and its dependencies:
npm add @rspack/plugin-react-refresh react-refresh -D
# or
yarn add @rspack/plugin-react-refresh react-refresh -D
# or
pnpm add @rspack/plugin-react-refresh react-refresh -D
# or
bun add @rspack/plugin-react-refresh react-refresh -DImport the plugin in your code:
import { ReactRefreshRspackPlugin } from '@rspack/plugin-react-refresh';To enable React Fast Refresh, you need both runtime injection and source transformation.
- This plugin handles runtime injection, including code from react-refresh and the custom runtime it requires.
- Source transformation should be enabled in your loader, for example with jsc.transform.react.refresh in swc-loader.
import { ReactRefreshRspackPlugin } from '@rspack/plugin-react-refresh';
const isDev = process.env.NODE_ENV === 'development';
export default {
mode: isDev ? 'development' : 'production',
module: {
rules: [
{
test: /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/,
use: {
loader: 'builtin:swc-loader',
options: {
detectSyntax: 'auto',
jsc: {
transform: {
react: {
runtime: 'automatic',
development: isDev,
refresh: isDev,
},
},
},
},
},
},
],
},
plugins: [isDev && new ReactRefreshRspackPlugin()],
};Compared to the previous approach, this method decouples the React Fast Refresh code injection logic from the transformation logic. The code injection logic is handled uniformly by this plugin, while the code transformation is handled by loaders. This means that this plugin can be used in conjunction with builtin:swc-loader, swc-loader, or babel-loader.
- For usage with
builtin:swc-loader, you can refer to the example at examples/react-refresh, When using withswc-loader, simply replacebuiltin:swc-loaderwithswc-loader. - For usage with
babel-loader, you can refer to the example at examples/react-refresh-babel-loader
- Type: Rspack.RuleSetCondition
- Default:
/\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/
Specifies which files should be processed by the React Refresh loader. This option is passed to the builtin:react-refresh-loader as the rule.test condition.
Works identically to Rspack's rule.test option.
new ReactRefreshPlugin({
test: [/\.jsx$/, /\.tsx$/],
});- Type: Rspack.RuleSetCondition
- Default:
undefined
Explicitly includes files to be processed by the React Refresh loader. This option is passed to the builtin:react-refresh-loader as the rule.include condition.
Use this to limit processing to specific directories or file patterns.
Works identically to Rspack's rule.include option.
new ReactRefreshPlugin({
include: [/\.jsx$/, /\.tsx$/],
});- Type: Rspack.RuleSetCondition
- Default:
/node_modules/
Exclude files from being processed by the plugin. The value is the same as the rule.exclude option in Rspack.
new ReactRefreshPlugin({
exclude: [/node_modules/, /some-other-module/],
});- Type: Rspack.RuleSetCondition
- Default:
undefined
Can be used to exclude certain resources from being processed by the plugin by the resource query. The value is the same as the rule.resourceQuery option in Rspack.
For example, to exclude all resources with the raw query, such as import rawTs from './ReactComponent.ts?raw';, use the following:
new ReactRefreshPlugin({
resourceQuery: { not: /raw/ },
});- Type:
boolean - Default:
false
Whether to force enable the plugin.
By default, the plugin will not be enabled in non-development environments. If you want to force enable the plugin, you can set this option to true.
new ReactRefreshPlugin({
forceEnable: true,
});It is useful if you want to:
- Use the plugin in production.
- Use the plugin with the
nonemode without settingNODE_ENV. - Use the plugin in environments we do not support, such as
electron-prerender(WARNING: Proceed at your own risk).
- Type:
string - Default:
output.uniqueName || output.library
Sets a namespace for the React Refresh runtime.
It is most useful when multiple instances of React Refresh is running together simultaneously.
- Type:
boolean - Default:
false
Config the plugin whether trigger a full page reload when an unrecoverable runtime error is encountered.
Currently, only module factory undefined error is considered as unrecoverable runtime error.
new ReactRefreshPlugin({
reloadOnRuntimeErrors: true,
});- Type:
string - Default:
builtin:react-refresh-loader
By default, the plugin uses the builtin:react-refresh-loader loader implementation from Rspack to inject the React Refresh utilities into each module. The reactRefreshLoader option allows you to specify a loader that implements custom React Refresh instrumentation if needed.
new ReactRefreshPlugin({
reactRefreshLoader: 'my-advanced-react-refresh-loader',
});Thanks to the react-refresh-webpack-plugin created by @pmmmwh, which inspires implement this plugin.
@rspack/plugin-react-refresh is MIT licensed.