-
I had a day long debugging session to get to the bottom of why HMR was not working in my app, and it turns out a simple Background with minmally reproducible case: https://github.com/fatso83/break-vite-hmr-with-export The single line needed to screw up HMR: fatso83/break-vite-hmr-with-export@e3159d5#diff-26ad4b834941d9b19ebf9db8082bd202aaf72ea0ddea85f5a8a0cb3c729cc6f2R7 |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 15 replies
-
I have tried debugging this and I know approximately where in the code stuff happens, but 20 breakpoints and 2 hours later I still haven't really figured this out. Mostly because I do not understand the code at a conceptual level and how the lexer works, I suppose. I wrote this on the Vite discord after researching the code around https://github.com/vitejs/vite/blob/main/packages/vite/src/node/server/hmr.ts#L121:
|
Beta Was this translation helpful? Give feedback.
-
Any update on this issue. I am facing this issue with SolidJS. Updating JavaScript works fine but updating JSX or CSS doesn't work. |
Beta Was this translation helpful? Give feedback.
-
I found out the reason that cause this problem is If your entry file export a module which not match the rule defined in There is a discussion about this: |
Beta Was this translation helpful? Give feedback.
-
Hi, maintainer of the Webpack integration of React Refresh here, maybe I can chime in and provide some insights ... (This answer only relates to the React parts of HMR) TL; DR: This is intentional behaviour, and is necessary to provide guarantees that the HMR would end up in a consistent state. The reason why it doesn't work is that the additional export is not considered as a component (well, With that in mind, and also with the knowledge that HMR would propagate up in the dependency tree, sometimes you will see interesting cases that a single line would break HMR for the whole app because that line is close to the root. |
Beta Was this translation helpful? Give feedback.
-
For people googling: ViteJS HMR loop through modules and breaks when you have a named export in your top-level. |
Beta Was this translation helpful? Give feedback.
-
Any tricks to make it work without default exports injection? |
Beta Was this translation helpful? Give feedback.
Hi, maintainer of the Webpack integration of React Refresh here, maybe I can chime in and provide some insights ... (This answer only relates to the React parts of HMR)
TL; DR: This is intentional behaviour, and is necessary to provide guarantees that the HMR would end up in a consistent state.
The reason why it doesn't work is that the additional export is not considered as a component (well,
CamelCase
functions/classes). If a file contains any exports that is not a component, the transform will not mark the component as side-effects free. This is important because anything else other than a component is generally not guaranteed to be side-effects free, but if there are side-effects, to …