-
-
Notifications
You must be signed in to change notification settings - Fork 125
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
fix: define stubs for react-refresh in Web Worker #311
Conversation
Can anyone review this? |
Does this feature is used to render part of a React tree of the whole tree? Because as said here the v3 hack is actually just using raw Vite without plugin. Do you expect any HMR to work after this PR? |
@ArnaudBarre
I'm sorry, it was just my project configuration that was wrong, I had missed a check for a file to be used in the worker, and once I properly excluded it, the code worked correctly. |
For anyone coming here, please provide a example of how adding this kind of check could lead to a better DX than just having bare Vite without plugins (which was what people experienced with v3 + disableFastRefresh) |
@ArnaudBarre The problem is that when using the worker plugin, Vite may choose to import modules with JSX in a worker (whether they are used or not). The worker then fails because those variables are not initialized in the worker. The only way to solve this is by adding a side-effectful module that you import before any other modules in a worker. See I don't think there are any downsides for the Vite React plugin to define the global variables in a worker if |
Are you running React component inside a worker or does JSX end up in the worker bundle because of imports being too wide? |
I assume most people run into the latter. In my case I didn't include any React components, but Vite chose to bundle them in development anyway (??). |
Yeah some people run in the latter, often because of barrel files, which as always been something that doesn't works well with unbundled dev. Changing this will improve development speed and reduce the number of times you get HMR for code that is actually unused at runtime. I'm a bit afraid that adding no-op for HMR now will make it a breaking change if at one point someone find a way to have HMR working for workers. |
We have no barrel files, and are still experiencing this with the latest versions of vite & react plugins. |
it probably means some files are exporting non React component alongside other thing that are used in a worker. This is something that will break Fast Refresh, checkout https://github.com/ArnaudBarre/eslint-plugin-react-refresh to lint this with eslint |
Here is the latest version for initializing workers used in Athena Crisis. It was necessary to add Life is too short to worry about this sort of stuff in development. I don't understand why this plugin, which is specifically for React, cannot just simply add these three lines in development when a lot of people keep running into it 🤷♂️ |
If the reason is barrel file: the drawbacks have been documented: https://marvinh.dev/blog/speeding-up-javascript-ecosystem-part-7/ If the reason is exporting both components and non components together: this breaks fast refresh and will lead to inconsistency in development and slower updates. If this issue allow people to notice that they are not doing this correctly, this could improve their dev experience on the opposite. |
I completely agree. This does work; thank you! @ArnaudBarre We don't have any barrel files (I triple-checked), but, we can't stop other libraries from using them. WebWorkers are a well-defined standard, which should "just work" in 2024. |
Library code will not trigger HMR to be injected in your worker. Why do you have React component code in your worker? |
Description
This PR fixes the issue below and improves #181.
Also the issue is mentioned in vitejs/vite#5396 (comment)
Define some stubs in Web Worker which is required by react-refresh to avoid the error:
Uncaught ReferenceError: $RefreshReg$ is not defined
.Issue
First, create Worker.tsx with the following content
Then, load Worker.tsx from App.tsx
In that case, we got the error below
Additional context
To avoid this error, @react-three/offscreen package, which renders React Three Fiber to OffscreenCanvas, shows the workaround to downgrade vite-plugin-react and disable all HMR.
This provides bad development experiences, so I hope that a new version will be released soon.
Reference:
https://github.com/pmndrs/react-three-offscreen?tab=readme-ov-file#vite
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).