Skip to content
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

Issue with WeakMap and Proxy in Vite's Hot Module Replacement (HMR) #19216

Closed
7 tasks done
ltxhhz opened this issue Jan 17, 2025 · 1 comment
Closed
7 tasks done

Issue with WeakMap and Proxy in Vite's Hot Module Replacement (HMR) #19216

ltxhhz opened this issue Jan 17, 2025 · 1 comment

Comments

@ltxhhz
Copy link

ltxhhz commented Jan 17, 2025

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 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!

Reproduction

https://stackblitz.com/edit/vitejs-vite-remkcupg?file=src%2Fmain.js

Steps to reproduce

  1. Create a class that stores instance-specific data in a WeakMap using this as the key.
  2. Use Vite for development with HMR enabled.
  3. Access the value stored in the WeakMap after the module is hot-reloaded.

System Info

System:
    OS: Windows 11 10.0.22631
    CPU: (16) x64 AMD Ryzen 7 7735H with Radeon Graphics
    Memory: 6.79 GB / 27.69 GB
  Binaries:
    Node: 18.20.4 - d:\Program Files\nodejs\node.EXE
    Yarn: 4.3.0 - D:\node_global\yarn.CMD
    npm: 10.9.1 - d:\Program Files\nodejs\npm.CMD
    pnpm: 9.15.0 - ~\AppData\Local\pnpm\pnpm.CMD
    bun: 1.1.26 - D:\node_global\bun.CMD
  npmPackages:
    @vitejs/plugin-vue: ^5.2.1 => 5.2.1
    vite: ^6.0.7 => 6.0.7

Used Package Manager

pnpm

Logs

No response

Validations

@ltxhhz
Copy link
Author

ltxhhz commented Jan 17, 2025

Closed because I can't confirm if the issue is vite related or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant