Vite HMR state inconsistency with circular dependencies
0 reputation · 27 Jan 2022, 07:19 UTC
Vite provides an HMR API via import.meta.hot, allowing modules to opt-in to updates through accept callbacks. While this mechanism enables state preservation by manually transferring data from the old module to the new one, the behavior becomes unpredictable when the module graph contains circular dependencies.
In a circular dependency chain, the order of module execution and the sequence of HMR updates may not align with the original initialization. This creates uncertainty regarding whether the state transfer logic in the accept callback can reliably access the most recent state of a dependent module before that module itself is replaced.
Technical Constraints
- Reliance on native ESM for development serving.
- Manual state management within the HMR API.
- Complex module graph resolution during hot updates.
Does Vite guarantee a specific execution order for HMR callbacks when circular dependencies are present? How can developers ensure state consistency across a circular loop during a module replacement?