-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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(hmr): don't bind hmr methods to the context class #15679
Conversation
Run & review this pull request in StackBlitz Codeflow. |
This means that all the functions needs to be created for each new |
I don't see how this is related. We don't call |
This link shows that to notice a difference you need to load at least 2 million files (context is called once in a file) a second. I think DX would be more important in that case. |
A new hot context is created for each module that has HMR in the browser. Using vite-dev-server-perf, the effect of binding the context functions for the plugin context was ~1.2% of the total work done by the server. The effect was visible for the 10k modules there. So it should be similar extra work that the browser needs to do here. These functions are used by plugin authors and I don't think they need the extra DX of not needing to auto-bind them, it doesn't seems common enough to pass these functions around too. Vite 5 is working everywhere, and this is the first issue we get about this. I would consider this a feature request more than a fix too. |
I don't think they can be compared equally. The code in the server runs in the same thread while this code runs in the new realm every time. To measure performance, we would need an actual browser benchmark.
Wasn't this change part of 5.1?
I don't see how this is a feature request. Internal implementation changed and it affected public usage - this is a regression. TypeScript allows both usages for example. |
The linked issue said "since v5" so I misread that part. It seems initially we had all the functions working without this, but since v3.2, Given the above and that being able to use the functions without the |
If it was working like this before the refactor, then I am fine to keep it as a feature request rather than a bug. |
Another point: Vite's vite/packages/vite/src/node/plugins/importAnalysis.ts Lines 455 to 465 in 93fce55
Not that this is a great counter-argument since that Vite shouldn't had relied on this (because I believe webpack was able to figure it out), but we'd need a significant refactor to move the module graph to the browser-side when computing HMR propagation. Either way since Patak pointed out that |
Fair, but it's also worth pointing out that the current APIs are borderline unusable manually, like there are multiple layers of problems between being able to write something like Either way using arrow functions here would cause a bit more memory to be used, it would be bad if arrays or plain objects wasted memory like that, but I doubt that for the purposes of hmr this would make any significant dent. Maybe the "right" solution here is that the static analysis should be loosened up and it should just look for |
Closing this for now as I think the consensus here is that this is acceptable for now. Vite's HMR implementation is not perfect, static analysis is required for now for it to work nicely, and is also documented: https://vitejs.dev/guide/api-hmr.html#hot-accept-cb
We could refactor this to fix it in the future, but it's not an easy task. |
Yes, but what I'm saying is: why look for
Presumably looking for |
As linked in my comment above (#15679 (comment)), we need to lex the accepted deps. It's not only used to inject the hot context. |
Makes sense, but it's worth refining the logic behind this imo, because if I'm writing |
Description
Fixes #15667
Instead of rewriting the context back to the object implementation, I decided it would be nice to keep the class for organizational purposes. Providing methods via an arrow function allows us to use
this
without binding the method because it will be transformed into something like this:Additional context
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).