Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

window is not defined in WebWorkers #163

Closed
val-o opened this issue Oct 27, 2023 · 9 comments
Closed

window is not defined in WebWorkers #163

val-o opened this issue Oct 27, 2023 · 9 comments
Labels
bug Something isn't working

Comments

@val-o
Copy link

val-o commented Oct 27, 2023

Describe the bug
If you are using Vite with barrel exports, you might end up with situation when you have a barrel file like this:

file: dashboard/index.ts
tsexport * from './helpers' export * from './components'

If there is a web worker (or any other worker) which imports helper from dashboard
tsimport {dashboardHelpers} from './dashboard' // some non-ui related work

Vite will load components anyway (that's how it works now) and there will be ReactRefresh injected code in all of the components codes, which will fail with following error, since there are hard references to window

Uncaught ReferenceError: window is not defined

Exptected:
Plugin should have some checks inside injected code which guards from failing inside non-window contexts

@ArnaudBarre
Copy link
Member

Yeah I will fix that, but you will be better moving off barrels files anyway, this slow down most tools:
https://marvinh.dev/blog/speeding-up-javascript-ecosystem-part-7/

@ArnaudBarre ArnaudBarre added the bug Something isn't working label Oct 27, 2023
@ArnaudBarre
Copy link
Member

So I wanted to add the same support than in the Babel plugin, but I discovered that this is barely working because as soon as you use a hook, the ReactRefresh transformation inject a global call to $RefreshSig$ which fails.
Given that adding this will just make errors more obscured and that in other cases this is just hiding the fact that you have too much code bundled into your worker because of barrels. Rewrite your exports and imports will make your app load faster.

For anyone that find this, better support in worker requires either:

  • HMR support inside worker: HMR on service worker vite#5396
  • Having different processing for files in worker and outside of worker (I'm not sure there is an issue open for that in core)

@ArnaudBarre ArnaudBarre closed this as not planned Won't fix, can't repro, duplicate, stale Oct 30, 2023
@deathemperor
Copy link

@ArnaudBarre is there a workaround to this window is not defined in WebWorkers? I'm open for patches.

@ArnaudBarre
Copy link
Member

What causes some HMR code to end-up in your bundle? Most of the time the solution is just to review imports/file split which improve the size of the bundled worker

@deathemperor
Copy link

What causes some HMR code to end-up in your bundle? Most of the time the solution is just to review imports/file split which improve the size of the bundled worker

I don't get what you mean. Are you talking about separate worker files so that they won't get detected by the plugin => this error won't appear? I actually get them first time running which I think is not a hot reload?

@ArnaudBarre
Copy link
Member

Most of time the issue happens when you import JSX inside a worker. Is it you case? What file containing HMR code is imported by your worker?

@deathemperor
Copy link

I'm experimenting with this https://github.com/Shopify/remote-ui/tree/main/examples/kitchen-sink-vite/app/react-worker. It enables rendering JSX inside worker.

@ArnaudBarre
Copy link
Member

In that case I would recommend to not use any React plugins and let esbuild do the JSX transpilation. This will lead to full reload on every edit but for web workers there is no choice.
Having HMR for web code and no-op for webworker until implemented is something discussed here: vitejs/vite#15262 (comment)

If using React component inside webworker becames a thing, I hope someone will add HMR support to webworkers instead of having no-op code and bad HMR experience

@deathemperor
Copy link

thanks for the advice, good one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants