denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
94.44k stars 5.24k forks source link

deno serve --watch-hmr does not emit hmr event #25600

Open JTRNS opened 2 weeks ago

JTRNS commented 2 weeks ago

Version: Deno 2.0.0-rc.2

The "hmr" event does not seem to get emitted when the --watch-hmr flag is used in combination with deno serve. In combination with deno run is works as expected.

Example

Running the code below with deno serve --watch-hmr main.ts and making a change will log:

// HMR File change detected! Restarting!
// deno serve: Listening on http://0.0.0.0:8000/

addEventListener("hmr", (e) => console.log((e as CustomEvent).detail));

export default {
  fetch(_request: Request) {
    return new Response("Hi Mom!");
  },
} satisfies Deno.ServeDefaultExport;

Running the more or less equivalent code below with deno run --watch-hmr --allow-net main.ts will log:

// HMR File change detected! Restarting!
// Listening on http://0.0.0.0:8000/
// { path: "file:///home/jtrns/tmp/deno-hmr-serve/main.ts" }
// HMR Replaced changed module file:///home/jtrns/tmp/deno-hmr-serve/main.ts

addEventListener("hmr", (e) => console.log((e as CustomEvent).detail));

Deno.serve((_request) => {
  return new Response("Hi Mom!");
});
HasanAlrimawi commented 1 week ago

Using serve: Could you please check if the content on browser updates when you update the file, it should do, also the logs // HMR File change detected! Restarting! // deno serve: Listening on http://0.0.0.0:8000/ actually refresh when you update the TS file. I believe that hmr-watch is functioning for both serveand run, the only difference is the logs.

The additional logs for run: // { path: "file:///home/jtrns/tmp/deno-hmr-serve/main.ts" } // HMR Replaced changed module file:///home/jtrns/tmp/deno-hmr-serve/main.ts I believe they're caused by some behavior only for run. I noticed that fn run in cli/tools/run/hmr.rs is responsible for it.

JTRNS commented 3 days ago

@HasanAlrimawi Having some difficulty trying to understand exactly what you are asking. Obviously nothing changes in the browser in this simple server example, as (Vite like) HMR for the front-end, is a bit more complex than that.

It is not just the logs. The "watch" part, of --watch-hmr works fine. Or in other words, the file watcher that ensures a process restart on file changes works well. However HMR, which is different than a basic file watcher that restarts the process, without access to the path of the file that changed, would be somewhat useless.