| 1 | import { CLIENT_MARKER } from "./client-marker.ts"; |
| 2 | import type * as ClientMarker from "./client-marker.ts"; |
| 3 | |
| 4 | const marker = document.querySelector<HTMLParagraphElement>("#client-marker")!; |
| 5 | const render = (value: string) => { |
| 6 | marker.textContent = value; |
| 7 | }; |
| 8 | render(CLIENT_MARKER); |
| 9 | |
| 10 | // The counter is the HMR state witness: a HOT update to ./client-marker.ts |
| 11 | // leaves this module (and `count`) alive, while a full page reload resets |
| 12 | // the counter to 0 — the client HMR spec asserts the count survives. |
| 13 | const button = document.querySelector<HTMLButtonElement>("#counter")!; |
| 14 | let count = 0; |
| 15 | const setCount = (next: number) => { |
| 16 | count = next; |
| 17 | button.textContent = `Count is ${count}`; |
| 18 | }; |
| 19 | button.addEventListener("click", () => setCount(count + 1)); |
| 20 | setCount(0); |
| 21 | |
| 22 | if (import.meta.hot) { |
| 23 | // Accept updates to the marker module only: vite re-fetches the dep and |
| 24 | // runs this callback in place — this module is NOT re-executed. |
| 25 | import.meta.hot.accept("./client-marker.ts", (mod) => { |
| 26 | if (mod) { |
| 27 | render((mod as typeof ClientMarker).CLIENT_MARKER); |
| 28 | } |
| 29 | }); |
| 30 | } |