Skip to content

Client hmr injection heuristics break workers that are not using react #6148

@Rheeseyb

Description

@Rheeseyb

Describe the bug

If a worker uses a source file that both includes the word "react" somewhere (e.g. in a comment, or somewhere in an import statement), and a PascalCase function, the result is that the client code will be injected into the worker. This breaks the worker because that client code contains a reference to HTMLElement, leading to a very hard to debug Uncaught ReferenceError: HTMLElement is not defined error.

This bug appears to be similar in nature to the previously closed #1841

Reproduction

To reproduce it you can use the following code in a file imported as a worker:

// react
export function Thing() { return 1 }

There is a full repo example available at https://github.com/Rheeseyb/vite-worker-bug-repro, where the bug can be enabled / disabled by commenting this line

System Info

System:
    OS: Linux 5.15 NixOS 21.11 (Porcupine) 21.11 (Porcupine)
    CPU: (8) x64 Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz
    Memory: 10.35 GB / 31.28 GB
    Container: Yes
    Shell: 5.1.8 - /run/current-system/sw/bin/bash
  Binaries:
    Node: 16.13.0 - /nix/store/nxkymgnvww1caiq1g73cn2inn1hraa5w-nodejs-16.13.0/bin/node
    Yarn: 1.22.17 - /nix/store/i310mdb86zvhkrrvv5bqfirwcgk7hxkm-scripts/bin/yarn
    npm: 8.1.0 - /nix/store/nxkymgnvww1caiq1g73cn2inn1hraa5w-nodejs-16.13.0/bin/npm
  Browsers:
    Chromium: 96.0.4664.93
    Firefox: 95.0
  npmPackages:
    @vitejs/plugin-react: ^1.0.7 => 1.1.3 
    vite: ^2.7.2 => 2.7.3

Used Package Manager

pnpm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions