vitest icon indicating copy to clipboard operation
vitest copied to clipboard

HMR in vite-node, callback is not working

Open 20051231 opened this issue 3 years ago • 3 comments

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

20051231 avatar Sep 25 '22 14:09 20051231

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.

Screen Shot 2023-06-16 at 2 15 25 PM Screen Shot 2023-06-16 at 2 15 09 PM

ncoder avatar Jun 16 '23 21:06 ncoder

 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.

ncoder avatar Jun 16 '23 22:06 ncoder

The current HMR implementation is very flawed. The next major for vite-node should use the Vite Runtime API instead which actually supports HMR.

sheremet-va avatar Feb 16 '24 10:02 sheremet-va