Skip to content

[plugin-react] Native React Compiler injects Fast Refresh registrations into Web Worker .ts modules #1478

Description

@Sam-Kruglov

Related plugins

Describe the bug

Enabling the native React Compiler with react({ compiler: true }) can inject React Fast Refresh registrations into an ordinary .ts module imported by a Vite Web Worker.

The minimal reproduction uses a generic TypeScript value factory:

const Factory = { Values: <Value>(values: Value): Value => values }

export enum MessageKind {
  Ready = 'ready',
  Complete = 'complete'
}

export const MessageKinds = Factory.Values(Object.values(MessageKind))

With react({ compiler: true }), Vite serves that worker dependency with these additional registrations:

export const MessageKinds = Factory.Values(_c = Object.values(MessageKind));
_c2 = MessageKinds;
var _c, _c2;
$RefreshReg$(_c, "MessageKinds$Factory.Values");
$RefreshReg$(_c2, "MessageKinds");

The module contains no React import, JSX, component, or hook. The worker does not have the React Refresh preamble installed, so evaluating the transformed module fails:

Uncaught ReferenceError: $RefreshReg$ is not defined

This worked in the same application before I migrated its React Compiler integration from Babel to Oxc.

Previously working configuration:

@vitejs/plugin-react 4.6.0
vite 7.2.4
babel-plugin-react-compiler 1.0.0
import react from '@vitejs/plugin-react'
import reactCompiler from 'babel-plugin-react-compiler'

react({
  babel: {
    plugins: [reactCompiler]
  }
})

Failing configuration after migration:

@vitejs/plugin-react 6.1.1
vite 8.3.1
oxc-transform-react 0.152.0
react({ compiler: true })

The Babel-based setup did not inject $RefreshReg$ into this worker dependency graph. After switching to the native Oxc compiler integration, the browser started failing while loading the worker.

The current workaround is to exclude the worker dependency graph from @vitejs/plugin-react. That prevents the runtime error, but the plugin's include and exclude options also control React Compiler coverage. As a result, excluding worker modules from Fast Refresh also prevents otherwise eligible .ts custom-hook and repository modules from being compiled.

Expected behavior:

  • Web Worker modules should not receive browser-window Fast Refresh registrations.
  • React Compiler coverage and Fast Refresh instrumentation should be independently filterable, or Fast Refresh should be disabled automatically for worker environments.
  • The compiler should remain able to process eligible .ts modules without requiring browser-only Refresh instrumentation throughout the same dependency graph.

There is a close precedent in @vitejs/plugin-react-swc: issue #1225 was fixed by #1226 so non-JSX worker modules would not receive a React Refresh wrapper. This report concerns the native plugin-react + oxc-transform-react compiler path.

Reproduction

https://stackblitz.com/edit/vitejs-vite-r7hgslar?file=README.md

Steps to reproduce

  1. Run pnpm install.
  2. Run pnpm dev.
  3. Open the provided local URL and the browser console.
  4. Observe worker error Uncaught ReferenceError: $RefreshReg$ is not defined.
  5. Fetch or inspect the transformed /src/message.ts module and observe the injected $RefreshReg$ calls.
  6. Change the Vite config to react() or exclude message.ts; the worker loads successfully.

System Info

System:
  OS: macOS 26.3.1
  CPU: (10) arm64 Apple M1 Pro
  Memory: 68.66 MB / 32.00 GB
  Shell: 5.9 - /bin/zsh
Binaries:
  Node: 26.7.0 - /opt/homebrew/bin/node
  npm: 11.19.0 - /opt/homebrew/bin/npm
  pnpm: 11.18.0 - /opt/homebrew/bin/pnpm
  bun: 1.3.11 - /opt/homebrew/bin/bun
Browsers:
  Chrome: 154.0.8037.92
  Safari: 26.3.1
npmPackages:
  @vitejs/plugin-react: 6.1.1
  oxc-transform-react: 0.152.0
  react: 19.2.8
  react-dom: 19.2.8
  vite: 8.3.1

Used Package Manager

pnpm

Logs

[vite] connected.
worker error Uncaught ReferenceError: $RefreshReg$ is not defined
[vite] (client) [Unhandled error] Error: Uncaught ReferenceError: $RefreshReg$ is not defined

[vite-plugin-react-worker-refresh-repro.zip](https://github.com/user-attachments/files/32878427/vite-plugin-react-worker-refresh-repro.zip)

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions