generated from dan3988/node-ts-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup-plugin-hotreload.js
129 lines (111 loc) · 3.28 KB
/
rollup-plugin-hotreload.js
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
import * as http from "node:http"
import * as fs from "node:fs";
import * as crypto from "node:crypto";
import * as ws from "ws";
import path from "node:path";
/** @type {http.RequestListener} */
function onServerRequest(req, res) {
const url = new URL(req.url, "http://localhost");
if (url.pathname !== "/hotreload.js") {
res.writeHead(404);
res.end();
} else {
const stream = fs.createReadStream("./rollup-plugin-hotreload-client.js");
res.writeHead(200, {
"content-type": "text/javascript",
"access-control-allow-origin": "*"
});
stream.pipe(res, { end: true });
}
}
/** @typedef {(socket: ws.WebSocket, req: http.IncomingMessage) => void} UpgradeHandler */
/** @type {Map<number, ReturnType<openServer>>} */
const servers = new Map();
/**
* @param {number} port
* @returns {Promise<Map<string, UpgradeHandler>>}
*/
function openServer(port) {
return new Promise((resolve, reject) => {
const handlers = new Map();
function onUpgrade(req, socket, head) {
const url = new URL(req.url, "http://localhost");
const id = url.searchParams.get("id");
if (url.pathname !== "/hotreload" || !id)
return;
const handler = handlers.get(id);
if (handler)
wss.handleUpgrade(req, socket, head, handler);
}
const wss = new ws.WebSocketServer({ noServer: true });
const server = http.createServer();
server.on("request", onServerRequest);
server.on("upgrade", onUpgrade);
server.on("error", reject)
server.listen(port, () => {
console.log("hotreload: listening on port %d", port);
resolve(handlers);
});
});
}
async function listen(port, guid) {
let promise = servers.get(port);
if (promise == null)
servers.set(port, promise = openServer(port));
return promise.then(handlers => {
const sockets = [];
let files;
/**
* @param {ws.WebSocket} socket
*/
function onConnection(socket) {
sockets.push(socket);
socket.send(JSON.stringify({ type: "init", files }));
socket.on("close", () => {
const ix = sockets.indexOf(socket);
ix >= 0 && sockets.splice(ix, 1);
});
}
function push(_files) {
files = _files;
const data = JSON.stringify({ type: "update", files });
sockets.forEach(v => v.send(data));
}
handlers.set(guid, onConnection);
return push;
});
}
function checksum(data) {
return crypto.createHash("md5").update(data).digest("base64");
}
/**
* @param {Options} options
* @returns {import("rollup").Plugin}
*/
export async function hotreload(options = {}) {
const { port = 58997, baseDir = ".", prefix } = options;
const guid = crypto.randomUUID();
const push = await listen(port, guid);
return {
footer() {
return `(() => {const script = document.createElement("script");script.src="http://localhost:${port}/hotreload.js?id=${guid}&prefix=${prefix}";script.type="module";document.head.appendChild(script);})();`
},
writeBundle(options, output) {
const files = {};
for (const file in output) {
const full = path.join(options.dir, file);
const relative = '/' + path.relative(baseDir, full).replaceAll('\\', '/');
const val = output[file];
files[relative] = checksum(val.type === "chunk" ? val.code : val.source);
}
push(files);
}
}
}
export default hotreload;
/**
* @typedef {object} Options
* @prop {string} prefix
* @prop {number} [port]
* @prop {string} [baseDir]
*/