-
Notifications
You must be signed in to change notification settings - Fork 170
/
hmr.ts
executable file
·205 lines (184 loc) · 5.27 KB
/
hmr.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// ESM Hot Module Replacement (ESM-HMR) Specification
// https://github.com/withastro/esm-hmr
import util from "../../shared/util.ts";
import events from "./events.ts";
class Module {
private _specifier: string;
private _isAccepted = false;
private _isDeclined = false;
private _isLocked = false;
private _data: Record<string, unknown> = {};
private _acceptCallbacks: CallableFunction[] = [];
private _disposeCallbacks: CallableFunction[] = [];
constructor(specifier: string) {
this._specifier = specifier;
}
get data(): Record<string, unknown> {
return this._data;
}
accept(callback?: CallableFunction): void {
if (this._isLocked) {
return;
}
if (!this._isAccepted) {
sendMessage({ specifier: this._specifier, type: "hotAccept" });
this._isAccepted = true;
}
if (typeof callback === "function") {
this._acceptCallbacks.push(callback);
}
}
decline(): void {
this._isDeclined = true;
this.accept();
}
dispose(callback: CallableFunction) {
if (typeof callback === "function") {
this._disposeCallbacks.push(callback);
}
}
invalidate(): void {
location.reload();
}
watchFile(filename: string, callback: () => void) {
const specifier = "." + util.cleanPath(filename);
const handler = (data: Record<string, unknown>) => {
if (data.specifier === specifier) {
callback();
}
};
events.on("hmr:modify", handler);
sendMessage({ specifier, type: "hotAccept" });
return () => events.off("hmr:modify", handler);
}
// don't accept updates if the module is locked
lock(): void {
this._isLocked = true;
}
async applyUpdate() {
if (this._isDeclined) {
location.reload();
return;
}
const disposeCallbacks = this._disposeCallbacks;
const data = this._data;
this._disposeCallbacks = [];
this._data = {};
disposeCallbacks.map((callback) => callback(data));
try {
const url = this._specifier.slice(1) + (this._specifier.endsWith(".css") ? "?module&" : "?") + "t=" + Date.now();
const module = await import(url);
this._acceptCallbacks.forEach((cb) => cb(module));
} catch (err) {
console.error(err);
}
}
}
export default function createHotContext(specifier: string) {
if (modules.has(specifier)) {
const mod = modules.get(specifier)!;
mod.lock();
return mod;
}
const mod = new Module(specifier);
modules.set(specifier, mod);
return mod;
}
const modules: Map<string, Module> = new Map();
const messageQueue: string[] = [];
let conn: WebSocket | null = null;
function sendMessage(msg: Record<string, unknown>) {
const json = JSON.stringify(msg);
if (!conn || conn.readyState !== WebSocket.OPEN) {
messageQueue.push(json);
} else {
conn.send(json);
}
}
function connect() {
const { location, __hmrWebSocketUrl } = window as { location: Location; __hmrWebSocketUrl?: string };
const { protocol, host } = location;
const wsUrl = __hmrWebSocketUrl || `${protocol === "https:" ? "wss" : "ws"}://${host}/-/hmr`;
const ws = new WebSocket(wsUrl);
const ping = (callback: () => void) => {
setTimeout(() => {
const ws = new WebSocket(wsUrl);
ws.addEventListener("open", callback);
ws.addEventListener("close", () => {
ping(callback); // retry
});
}, 500);
};
const colors = {
modify: "#056CF0",
create: "#20B44B",
remove: "#F00C08",
};
ws.addEventListener("open", () => {
conn = ws;
messageQueue.splice(0, messageQueue.length).forEach((msg) => ws.send(msg));
console.log("%c[HMR]", "color:#999", "listening for file changes...");
});
ws.addEventListener("close", () => {
if (conn !== null) {
conn = null;
console.log("[HMR] closed.");
// try to re-connect
connect();
} else {
// ping to reload the page
ping(() => location.reload());
}
});
ws.addEventListener("message", ({ data }: { data?: string }) => {
if (data) {
try {
const { type, specifier, ...rest } = JSON.parse(data);
if (specifier) {
for (const node of document.body.children) {
if (
node.classList.contains("transform-error") &&
node.getAttribute("data-specifier") === specifier
) {
node.remove();
break;
}
}
}
switch (type) {
case "create": {
events.emit("hmr:create", { specifier, ...rest });
break;
}
case "modify": {
const mod = modules.get(specifier);
if (mod) {
mod.applyUpdate();
}
events.emit("hmr:modify", { specifier });
break;
}
case "remove": {
if (modules.has(specifier)) {
modules.delete(specifier);
}
events.emit("hmr:remove", { specifier });
break;
}
case "reload": {
location.reload();
}
}
console.log(
`%c[HMR] %c${type}`,
"color:#999",
`color:${colors[type as keyof typeof colors]}`,
`${JSON.stringify(specifier)}`,
);
} catch (err) {
console.warn(err);
}
}
});
}
addEventListener("load", connect);