@rspack/plugin-react-refresh
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published
Rspack Banner

@rspack/plugin-react-refresh

npm version downloads license

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

Credits

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

License

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

Readme

Keywords

none

Package Sidebar

Install

npm i @rspack/plugin-react-refresh

Weekly Downloads

142,298

Version

1.0.0

License

MIT

Unpacked Size

83.3 kB

Total Files

40

Last publish

Collaborators

  • harpsealjs
  • chenjiahan
  • hardfist