Skip to content

Commit

Permalink
Fixes for dev workflow (#221)
Browse files Browse the repository at this point in the history
* Properly restart server on code changes on Linux (based on
  sveltejs/template#213)
* Add a short delay before reloading to wait for any dependencies
  to finish saving
  • Loading branch information
wlach authored Jan 9, 2022
1 parent 5c83285 commit 2af81f8
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions packages/viewer/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,17 @@ const production = !process.env.ROLLUP_WATCH;
function serve() {
let server;

async function toExit() {
if (server) server.kill(0);
function killServer() {
if (server) {
/* On linux, server.kill() only kills the parent shell (sh) process but not the child sirv instance
See https://nodejs.org/docs/latest-v14.x/api/child_process.html#child_process_subprocess_kill_signal
Passing the negation of PID of a detached process to 'kill' stops all its children */
try {
spawn("kill", ["--", `-${server.pid}`], { shell: true });
} catch (_) {
server.kill();
}
}
}

return {
Expand All @@ -23,22 +32,24 @@ function serve() {
["run", "start", "--", `../../${process.argv.slice(4)}`],
{
stdio: ["ignore", "inherit", "inherit"],
shell: true,
detached: true,
}
);
}

// if server has already started, kill it and wait a short while
// if server has already started, kill it and restart
if (server) {
server.on("exit", startServer);
server.kill();
killServer();
} else {
startServer();
}

process.on("SIGTERM", toExit);
process.on("exit", toExit);
process.on("SIGTERM", killServer);
process.on("exit", killServer);
},
/* Rollup restarts on detecting changes to this config file.
This hook makes sure the previously started instance is stopped before starting a new one */
closeWatcher: killServer,
};
}

Expand Down Expand Up @@ -91,5 +102,9 @@ export default [
output: [
{ file: "dist/cli.js", format: "cjs", interop: false, sourcemap: false },
],
watch: {
clearScreen: false,
buildDelay: 100,
},
},
];

0 comments on commit 2af81f8

Please sign in to comment.