You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I’ve encountered an issue when using Vite's Hot Module Replacement (HMR) with libraries(@tonejs/midi src/Note.ts:87) that store instance-specific data in a WeakMap. It appears that HMR wraps modules in a Proxy for hot reloading, and this leads to problems with WeakMap when accessing values based on this.
Here’s the problem:
In a class, I store instance-specific data using WeakMap.set(this, value).
When the class instance is proxied by Vite (due to HMR), the proxy object and the original object are treated as different references.
This causes issues because the proxy object does not match the original object stored in the WeakMap, and as a result, I cannot retrieve the value correctly from the WeakMap after the module is hot-reloaded.
Expected behavior:
I expect the class to retrieve the value correctly from the WeakMap, regardless of whether it is proxied by Vite’s HMR.
Actual behavior:
The value is not retrievable from the WeakMap after HMR because the this in the class and the proxy object are not the same reference.
Possible solutions:
One possible solution would be to provide an option to disable Proxy wrapping for certain modules or instances during HMR.
Another solution could involve providing a way to access the original instance even when it is wrapped in a proxy.
This issue may affect other libraries(e.g. @tonejs/midi src/Note.ts:87) that rely on WeakMap for instance-specific data storage and is related to how HMR interacts with Proxy.
Thank you for your time, and I appreciate any guidance or potential solutions to this issue!
Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
Describe the bug
Hello Vite team,
I’ve encountered an issue when using Vite's Hot Module Replacement (HMR) with libraries(@tonejs/midi src/Note.ts:87) that store instance-specific data in a
WeakMap
. It appears that HMR wraps modules in aProxy
for hot reloading, and this leads to problems withWeakMap
when accessing values based onthis
.Here’s the problem:
WeakMap.set(this, value)
.WeakMap
, and as a result, I cannot retrieve the value correctly from theWeakMap
after the module is hot-reloaded.Expected behavior:
I expect the class to retrieve the value correctly from the
WeakMap
, regardless of whether it is proxied by Vite’s HMR.Actual behavior:
The value is not retrievable from the
WeakMap
after HMR because thethis
in the class and the proxy object are not the same reference.Possible solutions:
Proxy
wrapping for certain modules or instances during HMR.This issue may affect other libraries(e.g. @tonejs/midi src/Note.ts:87) that rely on
WeakMap
for instance-specific data storage and is related to how HMR interacts withProxy
.Thank you for your time, and I appreciate any guidance or potential solutions to this issue!
Reproduction
https://stackblitz.com/edit/vitejs-vite-remkcupg?file=src%2Fmain.js
Steps to reproduce
WeakMap
usingthis
as the key.WeakMap
after the module is hot-reloaded.System Info
Used Package Manager
pnpm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: