Skip to content

Instantly share code, notes, and snippets.

@nrdobie
Last active September 29, 2019 14:17
Show Gist options
  • Save nrdobie/239de2097adcba85de30f13e2cc3f5cb to your computer and use it in GitHub Desktop.
Save nrdobie/239de2097adcba85de30f13e2cc3f5cb to your computer and use it in GitHub Desktop.
Node.js Server with webpack and auto reload
const { fork } = require('child_process');
const webpack = require('webpack');
const webpackServerConfig = require('./webpack.server.js');
let bundler = webpack(webpackServerConfig);
let child;
bundler.watch({}, (err, status) => {
if (err) {
console.error(err);
return;
}
if (child && child.connected) {
child.on('close', () => {
console.log('Starting server...');
child = fork('./build/backend.js');
});
console.log('Killing server...');
child.kill('SIGHUP');
} else {
console.log('Starting server...');
child = fork('./build/backend.js');
}
});
const webpack = require('webpack');
const path = require('path');
const fs = require('fs');
let nodeModules = {};
fs.readdirSync('node_modules')
.filter(function(x) {
return ['.bin'].indexOf(x) === -1;
})
.forEach(function(mod) {
nodeModules[mod] = 'commonjs ' + mod;
});
module.exports = {
entry: ['babel-polyfill', path.join(__dirname, 'server/index.js')],
target: 'node',
output: {
path: path.join(__dirname, 'build'),
filename: 'backend.js'
},
module: {
loaders: [
{
test: /\.jsx?$/,
include: [ path.join(__dirname, 'server') ],
loader: 'babel'
}
]
},
externals: nodeModules,
plugins: [
new webpack.BannerPlugin('require("source-map-support").install();',
{ raw: true, entryOnly: false })
],
devtool: 'source-map'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment