Skip to content

Commit

Permalink
Stop sirv before rollup reload
Browse files Browse the repository at this point in the history
Fixes sveltejs#199

Rollup reloads the config on detecting changes in it. This should also restart the `sirv` server. But the current config keeps creating new instances of `sirv` on each reload without properly stopping the previous ones. This PR fixes that behaviour.
  • Loading branch information
ranjan-purbey authored Jan 22, 2021
1 parent 47ed448 commit 12c5936
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,39 @@ import resolve from '@rollup/plugin-node-resolve';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import css from 'rollup-plugin-css-only';
import { spawn } from 'child_process';

const production = !process.env.ROLLUP_WATCH;

function serve() {
let server;

function toExit() {
if (server) server.kill(0);
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}`]);
} catch (_) {
server.kill();
}
}
}

return {
writeBundle() {
if (server) return;
server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
server = spawn('npm', ['start', '--', '--dev'], {
stdio: ['ignore', 'inherit', 'inherit'],
shell: true
detached: true,
});

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

Expand Down

0 comments on commit 12c5936

Please sign in to comment.