From 12c5936358d97a2a5fe670aa982cd600596777f3 Mon Sep 17 00:00:00 2001 From: Ranjan Purbey Date: Fri, 22 Jan 2021 23:45:33 +0530 Subject: [PATCH 1/2] Stop sirv before rollup reload Fixes https://github.com/sveltejs/template/issues/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. --- rollup.config.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/rollup.config.js b/rollup.config.js index e8965ec8..ff11e82b 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -4,6 +4,7 @@ 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; @@ -11,20 +12,31 @@ 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, }; } From d97d0e3a0947b6242e10dfcab3b920bef9dc9eb1 Mon Sep 17 00:00:00 2001 From: Ranjan Purbey Date: Sat, 23 Jan 2021 03:16:08 +0530 Subject: [PATCH 2/2] Fix for linux docker containers --- rollup.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rollup.config.js b/rollup.config.js index ff11e82b..b76d11a6 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -17,7 +17,7 @@ function serve() { 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}`]); + spawn('kill', ['--', `-${server.pid}`], { shell: true }); } catch (_) { server.kill(); }