vitejs / vite-plugin-react-swc

Speed up your Vite dev server with SWC
MIT License
830 stars 54 forks source link

fix: React in WebWorker window is undefined #220

Closed connorgmeehan closed 3 days ago

connorgmeehan commented 3 months ago

Previously the HMR code would use window as the global scope which is undefined in webworkers.

This change uses some code lifted from @vitejs/plugin-react to check if the context is in a webworker before doing trying to use window.

HMR is not currently possible in vite (https://github.com/vitejs/vite-plugin-react/issues/152) so nothing happens when the worker file is edited.

connorgmeehan commented 3 months ago

Actually it seems you've had this issue a few times and it seems to be out of scope/waiting for HMR support to provide a full experience/avoid a breaking change. What if this fix could be enabled in the config by a feature flag?

Something like:

{
  plugins: [
    react({
      experimentalJsxInWebWorker: true,
    }),
  ],
}
ArnaudBarre commented 3 months ago

As previously mentioned, I looking for an explanation of the use case of using JSX (and more specifically React components, I think just using JSX nodes doesn't trigger HMR code) in WebWorkers. For now these PRs feels like an XY problem

ArnaudBarre commented 3 days ago

I'm closing things because despite explanations I'm still convince that JSX inside workers are: