Created
May 9, 2018 13:36
-
-
Save vladbat00/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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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