smart-restart
CommonJS module load hook that restarts or does Webpack-style hot module replacement when a file is changed
nodemon
and piping
are great, but each has their limitations:
nodemon
sometimes restarts when you change files the server isn't using, which is a hassle when you're working on
isomorphic apps and just want a webpack hot update on the client.
piping
is difficult to use with node-inspector
because it runs a cluster; the supervisor process gets debug port 5858,
and your app process gets something else. It's even more of a pain to use with --debug-brk
because the supervisor
process starts with a breakpoint as well, so you have to open node-inspector
for it, resume it, then open
node-inspector
for your actual app.
On top of that, neither supports any hot module replacement, which is the only way to react to changes quickly in a
large project.
smart-restart
combines both approaches: it uses piping
's require hook to only watch files that have been required,
but it spawns
your app instead of running a cluster, so that you can pass --debug
or --debug-brk
to your app.
And it provides a basic version of Webpack's hot module replacement API for Node CommonJS, without using Webpack or
doing any bundling.
Usage
To run ./src/index.js
in a child process and watch files it require
s:
var launch = require('smart-restart')
launch({
main: './src/index.js',
command: 'node',
commandOptions: ['--inspect'],
args: [...],
spawnOptions: {...},
onChildSpawned: child => {},
ignore: /(\/\.|~$)/,
usePolling: false,
interval: 100,
binaryInterval: 300,
includeModules: false,
killSignal: 'SIGTERM',
killTimeout: 5000,
deleteRequireCache: [
'src/server/ssr/serverSideRender.js',
],
restartOnError: true,
restartOnExit: true,
})
You can launch
as many other processes as you want in the same supervisor process.
Hot Module Replacement
smart-restart
supports the following subset of Webpack's hot module replacement API:
if (module.hot) {
module.hot.accept('./myModule', () => {
const newVersion = require('./myModule')
})
}
Webpack's other overloads for module.hot.accept
aren't supported.
If not all ancestors of a changed module have module.hot.accept
hooks, then smart-restart
will
relaunch the whole process.
Exiting for good
You can process.send({exit: <code>})
to tell smart-restart
to exit
immediately instead of waiting to relaunch your process.