Home

Awesome

<picture> <source media="(prefers-color-scheme: dark)" srcset="https://assets.rspack.dev/rspack/rspack-banner-plain-dark.png"> <img alt="Rspack Banner" src="https://assets.rspack.dev/rspack/rspack-banner-plain-light.png"> </picture>

@rspack/plugin-react-refresh

<p> <a href="https://www.npmjs.com/package/@rspack/plugin-react-refresh?activeTab=readme"><img src="https://img.shields.io/npm/v/@rspack/plugin-react-refresh?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" /></a> <a href="https://npmcharts.com/compare/@rspack/plugin-react-refresh?minimal=true"><img src="https://img.shields.io/npm/dm/@rspack/plugin-react-refresh.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="downloads" /></a> <a href="https://github.com/web-infra-dev/rspack/blob/main/LICENSE"><img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="license" /></a> </p>

React refresh plugin for Rspack.

Installation

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 -D

Usage

Enabling React Fast Refresh functionality primarily involves two aspects: code injection and code transformation.

const ReactRefreshPlugin = require("@rspack/plugin-react-refresh");
const isDev = process.env.NODE_ENV === "development";

module.exports = {
  experiments: {
    rspackFuture: {
      disableTransformByDefault: true,
    },
  },
  // ...
  mode: isDev ? "development" : "production",
  module: {
    rules: [
      {
        test: /\.jsx$/,
        use: {
          loader: "builtin:swc-loader",
          options: {
            jsc: {
              parser: {
                syntax: "ecmascript",
                jsx: true,
              },
              transform: {
                react: {
                  development: isDev,
                  refresh: isDev,
                },
              },
            },
          },
        },
      },
    ],
  },
  plugins: [isDev && new ReactRefreshPlugin()].filter(Boolean),
};

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.

Example

Options

include

Include files to be processed by the plugin. The value is the same as the rule.test option in Rspack.

new ReactRefreshPlugin({
  include: [/\.jsx$/, /\.tsx$/],
});

exclude

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/],
});

forceEnable

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:

library

Sets a namespace for the React Refresh runtime.

It is most useful when multiple instances of React Refresh is running together simultaneously.

overlay

Modify the behavior of the error overlay.

new ReactRefreshPlugin({
  overlay: true,
});
new ReactRefreshPlugin({
  overlay: {
    // ...
  },
});

Credits

Thanks to the react-refresh-webpack-plugin created by @pmmmwh, which inspires implement this plugin.

License

@rspack/plugin-react-refresh is MIT licensed.