Skip to content

Instantly share code, notes, and snippets.

@mvlabat
Created May 9, 2018 13:36
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvlabat/265b3f5a89417d30d891642cf93ad642 to your computer and use it in GitHub Desktop.
Save mvlabat/265b3f5a89417d30d891642cf93ad642 to your computer and use it in GitHub Desktop.
Parcel bundler for hotreloading node.js target. Start with `run` argument in order get your index.js running after each rebuild
const Bundler = require('parcel-bundler');
const childProcess = require('child_process');
const file = 'index.js';
const options = {};
const bundler = new Bundler(file, options);
const runBundle = process.argv.includes('run');
let bundle = null;
let child = null;
bundler.on('bundled', (compiledBundle) => {
bundle = compiledBundle;
});
bundler.on('buildEnd', () => {
if (runBundle && bundle !== null) {
if (child) {
child.stdout.removeAllListeners('data');
child.stderr.removeAllListeners('data');
child.removeAllListeners('exit');
child.kill();
}
child = childProcess.spawn('node', [bundle.name]);
child.stdout.on('data', (data) => {
process.stdout.write(data);
});
child.stderr.on('data', (data) => {
process.stdout.write(data);
});
child.on('exit', (code) => {
console.log(`Child process exited with code ${code}`);
child = null;
});
}
bundle = null;
});
bundler.bundle();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment