Skip to content

Instantly share code, notes, and snippets.

@sntran
Created May 19, 2023 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sntran/0b84521216bb18f43ebe35f69ecd8e65 to your computer and use it in GitHub Desktop.
Save sntran/0b84521216bb18f43ebe35f69ecd8e65 to your computer and use it in GitHub Desktop.
Esbuild serve and reload
import esbuild from "esbuild";
import { createServer } from "http";
const clients = [];
esbuild
.build({
entryPoints: ["./index.tsx"],
bundle: true,
outfile: "bundle.js",
banner: {
js: ' (() => new EventSource("http://localhost:8000").onmessage = () => location.reload())();',
},
watch: {
onRebuild(error) {
clients.forEach((res) => res.write("data: update\n\n"));
clients.length = 0;
console.log(error ? error : "...");
},
},
})
.catch(() => process.exit(1));
createServer((req, res) => {
return clients.push(
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Access-Control-Allow-Origin": "*",
Connection: "keep-alive",
}),
);
}).listen(8000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment