HMR in vite-node, callback is not working
Describe the bug
I am trying to use HMR in vite-node. But the HMR callback is not working at intended
import.meta.hot.accept(mod => {
})
the mod in official vite is the object of new module but in vite-node, it is an array with first element as the new module
I guess this line for reloading the module map should be
https://github.com/vitest-dev/vitest/blob/main/packages/vite-node/src/hmr/hmr.ts#L152
moduleMap.set(dep, newMod[0]);
Reproduction
just run the example in https://github.com/vitest-dev/vitest/tree/main/packages/vite-node
and accept self in the executing program
import.meta.hot.accept(mod => {
console.log('hot reloaded', mod)
})
System Info
System:
OS: Windows 10 10.0.22621
CPU: (18) x64 AMD Ryzen 9 3900X 12-Core Processor
Memory: 14.66 GB / 32.00 GB
Binaries:
Node: 18.9.0 - G:\tools\nvm\nodejs\node.EXE
Yarn: 1.22.19 - G:\tools\nvm\nodejs\yarn.CMD
npm: 8.19.1 - G:\tools\nvm\nodejs\npm.CMD
Browsers:
Chrome: 105.0.5195.127
Edge: Spartan (44.22621.521.0), Chromium (105.0.1343.42)
Internet Explorer: 11.0.22621.1
Used Package Manager
pnpm
Validations
- [X] Follow our Code of Conduct
- [X] Read the Contributing Guidelines.
- [X] Read the docs.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [X] The provided reproduction is a minimal reproducible example of the bug.
I also reproduce this bug. Looks like there are extra /@fs tags in front of the module path during registration that are missing when the reload event happens.
async function fetchUpdate(runner, { path, acceptedPath }) {
const maps = getCache(runner);
- const mod = maps.hotModulesMap.get(path);
+ var mod = maps.hotModulesMap.get(path);
if (!mod) {
- return;
+ mod = maps.hotModulesMap.get(`/@fs//@fs/${path}`);
+ if (!mod) {
+ return;
+ }
}```
this dumb workaround fixes it for me.
The current HMR implementation is very flawed. The next major for vite-node should use the Vite Runtime API instead which actually supports HMR.