Skip to content

Instantly share code, notes, and snippets.

@munkiwarra
Created January 9, 2025 08:58
Show Gist options
  • Save munkiwarra/e0a5676e876cd7c68fac14157b3418b5 to your computer and use it in GitHub Desktop.
Save munkiwarra/e0a5676e876cd7c68fac14157b3418b5 to your computer and use it in GitHub Desktop.
esbuild restart express
import childProcess from "child_process";
let serverProcess;
const startServer = () => {
serverProcess = childProcess.spawn("node", ["app.js"], {
stdio: "inherit",
});
};
const startPlugin = (hasWatch = true) => {
return {
name: "startPlugin",
setup(build) {
build.onEnd((res) => {
if (!hasWatch) return;
if (serverProcess) {
serverProcess.kill("SIGINT");
serverProcess.on("exit", () => {
// Make sure it's dead!
console.log("Restarting server...");
setTimeout(() => startServer(), 50);
});
} else {
startServer();
}
});
},
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment