Hot module replacement

ende desktop --hmr main.ts

--hmr enables hot module replacement during development: a file watcher plus V8's Debugger.setScriptSource. The Ende runtime and the rendering backend (CEF, WebView, …) stay alive across changes. There is no full restart and no webview teardown.

How it works

--hmr watches your source files and uses V8's Debugger.setScriptSource to hot-swap modules into the running isolate.

main.ts
Ende.serve((req) => {
  return new Response("hello world");
});
ende desktop --hmr main.ts

Edit main.ts (change the response body, add a route) and the change applies on save. The runtime does not restart and the listening socket stays bound. After a change is applied, every open window reloads its page, so it fetches from the new code.

Only modules the Ende runtime has loaded are hot-swapped. A change to any other file in the project (HTML, CSS, or scripts that only the page loads) doesn't trigger anything; reload the window yourself to see it.

What persists across reloads

Debugger.setScriptSource replaces the code of a function with new code. Live values stay the same:

  • Module-level state (top-level let, top-level Map, etc.) is preserved.
  • Open file handles, network connections, child processes: all preserved.
  • The HTTP listener is preserved.
  • Timers and intervals keep firing on their original schedule unless you clearTimeout / clearInterval them.

What changes on the next call

The replaced functions execute their new bodies the next time they are called. So:

  • A request handler change takes effect on the next request.
  • A timer callback change takes effect on the next firing.
  • An event listener change takes effect on the next event.

What HMR cannot do

Debugger.setScriptSource has limits. It cannot replace:

  • Top-level statements that have already executed (a console.log at module scope only runs when the module is first loaded).
  • The signature of a class, such as adding fields or changing constructors. The class declaration is replaced; existing instances keep their old shape.
  • The set of imports. Adding a new import line requires a full reload.

When V8 rejects a change because it changes a module's imports, exports, or top-level let / const declarations, the runtime keeps running the old code. --hmr logs an error and reloads the open windows, which still get the old code from the server. Stop and start ende desktop --hmr to pick up such a change. Deleting a module the runtime has loaded also reloads the windows.

Any other failure, such as a syntax error in the edited file or a change V8 refuses for another reason, is logged and nothing reloads. The runtime keeps the old code until a later save succeeds.

Limitations and caveats

  • --hmr is for development only. It packages nothing: it builds a throwaway library under <ENDE_DIR>/desktop/ and runs it, so there is nothing to ship. Build without --hmr to make the app.
  • Source maps are required for accurate line numbers in stack traces after a hot swap. They are emitted by default; do not disable them in your bundler config.
  • HMR cooperates with --inspect (see DevTools). You can attach a debugger to a running --hmr session and step through newly-swapped code.