-
-
Save munkiwarra/e0a5676e876cd7c68fac14157b3418b5 to your computer and use it in GitHub Desktop.
esbuild restart express
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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