Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 163a655

Browse files
committed
hmr support watchFile
1 parent 10562cb commit 163a655

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

commands/dev.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ const handleHMRSocket = (req: Request): Response => {
5858
if (util.isFilledString(e.data)) {
5959
try {
6060
const { type, specifier } = JSON.parse(e.data);
61-
if (type === "hotAccept" && util.isFilledString(specifier)) {
62-
emitter.on(`hotUpdate:${specifier}`, () => send({ type: "modify", specifier }));
61+
if ((type === "modify" || type === "hotUpdate") && util.isFilledString(specifier)) {
62+
emitter.on(
63+
type === "modify" ? `modify:${specifier}` : `hotUpdate:${specifier}`,
64+
() => send({ type: "modify", specifier }),
65+
);
6366
}
6467
} catch (_e) {
6568
log.error("invlid socket message:", e.data);
@@ -103,7 +106,6 @@ if (import.meta.main) {
103106
if (kind === "modify") {
104107
emitters.forEach((e) => {
105108
e.emit(`modify:${specifier}`, { specifier });
106-
// emit HMR event
107109
if (e.all.has(`hotUpdate:${specifier}`)) {
108110
e.emit(`hotUpdate:${specifier}`, { specifier });
109111
} else if (specifier !== "./routes.gen.ts") {

framework/core/hmr.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import util from "../../lib/util.ts";
12
import events from "./events.ts";
23

34
// ESM Hot Module Replacement (ESM-HMR) Specification
@@ -32,6 +33,18 @@ class Module {
3233
this.accept();
3334
}
3435

36+
watchFile(filename: string, callback: () => void) {
37+
const specifier = "." + util.cleanPath(filename);
38+
const handler = (data: Record<string, unknown>) => {
39+
if (data.specifier === specifier) {
40+
callback();
41+
}
42+
};
43+
events.on("hmr:modify", handler);
44+
sendMessage({ specifier, type: "modify" });
45+
return () => events.off("hmr:modify", handler);
46+
}
47+
3548
// don't accept updates if the module is locked
3649
lock(): void {
3750
this._isLocked = true;
@@ -118,21 +131,23 @@ function connect() {
118131
const { type, specifier, routePattern } = JSON.parse(data);
119132
switch (type) {
120133
case "create": {
121-
events.emit("create-file", { routePattern, specifier });
134+
events.emit("hmr:create", { specifier, routePattern });
122135
break;
123136
}
124137
case "modify": {
125138
const mod = modules.get(specifier);
126139
if (mod) {
127140
mod.applyUpdate();
141+
} else {
142+
events.emit("hmr:modify", { specifier });
128143
}
129144
break;
130145
}
131146
case "remove": {
132147
if (modules.has(specifier)) {
133148
modules.delete(specifier);
134149
}
135-
events.emit("remove-file", { specifier });
150+
events.emit("hmr:remove", { specifier });
136151
break;
137152
}
138153
case "reload": {

types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ declare interface Middleware {
6969

7070
declare interface ImportMeta {
7171
readonly hot?: {
72+
watchFile: (filename: string, callback: () => void) => () => void;
7273
accept: (callback?: (module: unknown) => void) => void;
7374
decline: () => void;
7475
};

0 commit comments

Comments
 (0)