# @rspack/plugin-react-refresh

> React refresh plugin for Rspack

Latest version **2.0.2** (published 2026-06-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install @rspack/plugin-react-refresh
pnpm add @rspack/plugin-react-refresh
yarn add @rspack/plugin-react-refresh
bun add @rspack/plugin-react-refresh
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2026-06-05 |
| First published | 2023-09-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 0 |
| Unpacked size | 27.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 20 |
| Maintainers | hardfist, chenjiahan |

## Links

- npm: https://www.npmjs.com/package/@rspack/plugin-react-refresh
- Repository: https://github.com/rstackjs/rspack-plugin-react-refresh
- npm.io page: https://npm.io/package/@rspack/plugin-react-refresh

## Recent versions

- 2.0.2 (latest) — 2026-06-05
- 2.0.0-beta.0 (beta) — 2026-04-08
- 1.0.3 (canary) — 2025-01-25
- 1.0.0-alpha.5 (alpha) — 2024-07-17
- 0.7.6-canary-f272bd8-20240625122346 (nightly) — 2024-06-25
- 2.0.1 — 2026-05-28
- 2.0.0 — 2026-04-08
- 1.6.2 — 2026-04-07
- 1.6.1 — 2026-02-12
- 1.6.0 — 2026-01-07
- 1.5.3 — 2025-11-12
- 1.5.2 — 2025-10-21
- 1.5.1 — 2025-09-08
- 1.5.0 — 2025-08-28
- 1.4.3 — 2025-05-12
- … 567 more at https://npm.io/package/@rspack/plugin-react-refresh/versions

## README

# @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](https://github.com/web-infra-dev/rspack).

## Versions

- `2.x`: For Rspack v2.
- `1.x`: For Rspack v1, see [v1.x - README](https://github.com/rstackjs/rspack-plugin-react-refresh/tree/v1.x?tab=readme-ov-file#rspackplugin-react-refresh) for usage guide.

## Installation

First you need to install this plugin and its dependencies:

```bash
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
```

## Import the plugin

Import the plugin in your code:

```js
import { ReactRefreshRspackPlugin } from '@rspack/plugin-react-refresh';
```

## Usage

To enable [React Fast Refresh](https://reactnative.dev/docs/fast-refresh), you need both runtime injection and source transformation.

- This plugin handles runtime injection, including code from [react-refresh](https://www.npmjs.com/package/react-refresh) and the custom runtime it requires.
- Source transformation should be enabled in your loader, for example with [jsc.transform.react.refresh](https://swc.rs/docs/configuration/compilation#jsctransformreactrefresh) in [swc-loader](https://rspack.rs/guide/features/builtin-swc-loader).

```js
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`.

## Example

- For usage with `builtin:swc-loader`, you can refer to the example at [examples/react-refresh](https://github.com/rstackjs/rstack-examples/blob/main/rspack/react-refresh/rspack.config.js), When using with `swc-loader`, simply replace `builtin:swc-loader` with `swc-loader`.
- For usage with `babel-loader`, you can refer to the example at [examples/react-refresh-babel-loader](https://github.com/rstackjs/rstack-examples/blob/main/rspack/react-refresh-babel-loader/rspack.config.js)

## Options

### test

- Type: [Rspack.RuleSetCondition](https://rspack.rs/config/module-rules#condition)
- 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](https://rspack.rs/config/module-rules#rulestest) option.

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

### include

- Type: [Rspack.RuleSetCondition](https://rspack.rs/config/module-rules#condition)
- 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](https://rspack.rs/config/module-rules#rulesinclude) option.

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

### exclude

- Type: [Rspack.RuleSetCondition](https://rspack.rs/config/module-rules#condition)
- Default: `/[\\/]node_modules[\\/]/`

Exclude files from being processed by the plugin. The value is the same as the [rule.exclude](https://rspack.rs/config/module-rules#rulesexclude) option in Rspack.

```js
new ReactRefreshPlugin({
  exclude: [/[\\/]node_modules[\\/]/, /some-other-module/],
});
```

### resourceQuery

- Type: [Rspack.RuleSetCondition](https://rspack.rs/config/module-rules#condition)
- 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](https://rspack.rs/config/module-rules#rulesresourcequery) option in Rspack.

For example, to exclude all resources with the `raw` query, such as `import rawTs from './ReactComponent.ts?raw';`, use the following:

```js
new ReactRefreshPlugin({
  resourceQuery: { not: /raw/ },
});
```

### forceEnable

- 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`.

```js
new ReactRefreshPlugin({
  forceEnable: true,
});
```

It is useful if you want to:

- Use the plugin in production.
- Use the plugin with the `none` mode without setting `NODE_ENV`.
- Use the plugin in environments we do not support, such as `electron-prerender` (**WARNING: Proceed at your own risk**).

### library

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

### reloadOnRuntimeErrors

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

```js
new ReactRefreshPlugin({
  reloadOnRuntimeErrors: true,
});
```

### reactRefreshLoader

- Type: `string`
- Default: `builtin:react-refresh-loader`

By default, the plugin uses the `builtin:react-refresh-loader` loader implementation [from Rspack](https://github.com/web-infra-dev/rspack/tree/main/crates/rspack_loader_react_refresh) 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.

```js
new ReactRefreshPlugin({
  reactRefreshLoader: 'my-advanced-react-refresh-loader',
});
```

## Credits

Thanks to the [react-refresh-webpack-plugin](https://github.com/pmmmwh/react-refresh-webpack-plugin) created by [@pmmmwh](https://github.com/pmmmwh), which inspires implement this plugin.

## License

`@rspack/plugin-react-refresh` is [MIT licensed](https://github.com/web-infra-dev/rspack/blob/main/LICENSE).

---
_Source: https://npm.io/package/@rspack/plugin-react-refresh · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
