How do you guys handle circular imports? #16424
Replies: 5 comments 3 replies
-
|
@SuspiciousLookingOwl any updates? |
Beta Was this translation helpful? Give feedback.
-
|
I was able to get out of some circular dependencies by using the global scope |
Beta Was this translation helpful? Give feedback.
-
|
So.... ? Does vite not allow a parent and child node of different classes to know about each other simultaneously, or what? |
Beta Was this translation helpful? Give feedback.
-
|
The point of Vite was to be a simple, plug-and-play solution that just worked - without having to reorganize your entire repository due to arbitrary constraints. Vite 5 breaking HMR due to circular references loses a lot of what was useful about Vite in the first place. |
Beta Was this translation helpful? Give feedback.
-
|
For those looking to use the method mentioned by the OP to bypass the circular dependency problem blocking HMR, this works when using Vite 7.0.2. Using the new export default defineConfig({
plugins: [
// other plugins...
{
name: 'ignoreHMRCircularDependencies',
hotUpdate({ modules }) {
modules.forEach((m) => {
m.importedModules = new Set()
m.importers = new Set()
})
return modules
},
},
],
}) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I've been battling with Vite's HMR for 1.5 years now. My project has some circular dependencies that cause Vite's HMR to not work properly.
It all started with getting
HMR error: Cannot access '...' before initializationerror from late 2022, until I found #2466 (comment), which magically makes the HMR work again (I also commented on the issue, sharing the solution)I recently did a code refactor and clean up on my project, including updating Vite from 4 to 5, and I decided to try to revisit this issue again, I thought "oh maybe Vite improved or fixed the cyclic imports issues", I tried removing that plugin after I updated to Vite 5 and my HMR started to break again. After some searching, I found #14975 (comment)
Which to me, it sounds like Vite will not / is not supposed to support circular imports, I tried to refactor my code so it doesn't have circular imports, but I decided that it's not worth it.
For now, I'm going back to the
singleHMRplugin, which seems to still work fine, but I'm worried that it might bite me in the butt later.How do you guys handle circular imports on your Vite project? Do you just try to avoid circular imports as much as possible? From what I see, this poor circular import experience is a bummer for some developers.
Beta Was this translation helpful? Give feedback.
All reactions