rstackjs / rstackjs/rspack-plugin-preact-refresh

Bug: $RefreshSig$ and $RefreshReg$ become undefined during the hot reload process

Open
#24 0 comments 0 reactions 1 assignee View on GitHub

@LingyuCoder is already working on this.

Since Mar 30, 2025.

Dominant language
JavaScript
Stars
9
Forks
4
Avg merge
3h 54m
Merged PRs (30d)
5

Description

Issue Description

When using @rspack/plugin-preact-refresh with Preact in development mode, I encountered an issue where both $RefreshSig$ and $RefreshReg$ become undefined during hot reload. This causes a runtime error: TypeError: $RefreshSig$ is not a function.

Root Cause

In node_modules/@rspack/plugin-preact-refresh/client/intercept.js, the code captures previous values of these functions and restores them in a finally block:

try {
  originalFactory.call(this, moduleObject, moduleExports, webpackRequire);
} finally {
  self.$RefreshReg$ = prevRefreshReg;
  self.$RefreshSig$ = prevRefreshSig;  // This can set it to undefined
}

When the first module (@prefresh/core/src/index.js) is loaded, prevRefreshSig and prevRefreshReg are both undefined. After the finally block executes, it sets the global functions back to undefined, breaking the hot reload functionality.

Steps to Reproduce

  1. Create a Preact application with Rspack
  2. Configure @rspack/plugin-preact-refresh and @prefresh/babel-plugin
  3. Run the application in development mode
  4. Check the browser console for errors during component updates
  5. Observe the error: TypeError: $RefreshSig$ is not a function

Solution

The issue can be fixed by adding a null check before restoring the previous functions:

try {
  originalFactory.call(this, moduleObject, moduleExports, webpackRequire);
} finally {
  // Only restore if previous values are defined
  if (prevRefreshReg !== undefined) {
    self.$RefreshReg$ = prevRefreshReg;
  }
  if (prevRefreshSig !== undefined) {
    self.$RefreshSig$ = prevRefreshSig;
  }
}

Environment

  • Rspack version: 1.2.8
  • @rspack/plugin-preact-refresh version: 1.1.2
  • @prefresh/babel-plugin version: 0.5.1

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.