Skip to content

@vitejs/plugin-react-swc Refresh wrapper erroneously added to non-component, error in Web Worker #1225

@kenlyon

Description

@kenlyon

Related plugins

Describe the bug

I have a react project that imports some type information from another package for use by a typescript service which is running in a web worker.

The file in question just exports one Record<string, string> where the key is the path to a .d.ts file and the value is the content of said file.

I expect this file to be treated as a regular file without the need for HMR or anything fancy - it's just data. This was all fine until I upgraded @vitejs/plugin-react-swc and started to see ReferenceError: window is not defined in the console. This led me through the inner workings of vite until I finally discovered this in the plugin:

const reactCompRE = /extends\s+(?:React\.)?(?:Pure)?Component/;

This regular expression was matching within a string and causing the refresh wrapper to be added to the file and ultimately the script failing to load.

If the module was being used directly in the react app, this additional wrapping would cause no harm. However, because the module was being loaded within a web worker, the window object is not available and so you get the console error.

Reproduction

https://stackblitz.com/edit/vitejs-vite-1pqwywfy?file=src%2FApp.tsx

Steps to reproduce

There is technically no error without using a web worker. I hope what I have is adequate to prove the flaw in the logic, though. If not, let me know and I'll see if I can add a basic web worker to the example.

The instructions are on the page, but I will repeat them here:

Observe the network traffic for the loading of two files. Despite both containing just strings, example1.ts is given a refresh wrapper but example2.ts is not.

This is because the former includes what appears to be a declaration of a react component. The plugin uses a regular expression that matches this, failing to detect that it is within a literal string.

This does not cause any problem here because they are being loaded within a window. However, if example1.ts was used by a web worker, loading it would fail with an error about window not being defined.

System Info

System:
    OS: Windows 11 10.0.26200
    CPU: (22) x64 Intel(R) Core(TM) Ultra 7 165H
    Memory: 10.97 GB / 31.45 GB
  Binaries:
    Node: 22.20.0 - C:\nvm4w\nodejs\node.EXE
    Yarn: 4.14.1 - C:\nvm4w\nodejs\yarn.CMD
    npm: 10.9.3 - C:\nvm4w\nodejs\npm.CMD
  Browsers:
    Chrome: 148.0.7778.97
    Edge: Chromium (146.0.3856.72)
    Firefox: 150.0.3 - C:\Program Files\Mozilla Firefox\firefox.exe
    Internet Explorer: 11.0.26100.8115

Used Package Manager

yarn

Logs

Vite does not generate an error in the logs. The only evidence of the issue is in the browser console when it fails to load the affected module using a web worker.

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