If I set up a very simple webpack project and install webpack-dev-server, when I run "webpack-dev-server --open" from the command line or via an npm script I get live reloading by default. I.e. as soon as I edit a source file then the bundle is rebuilt and the browser is automatically reloaded.
However, if I use the Node API to fire up webpack-dev-server instead, with the following code:
const WebpackDevServer = require('webpack-dev-server');
const webpack = require('webpack');
const webpackConfig = require('../webpack.config.dev');
const open = require('open');
const port = 3000;
let compiler = webpack(webpackConfig);
let server = new WebpackDevServer(compiler, {
contentBase: "./src",
});
server.listen(port, "localhost", function(err) {
if(err){
console.log(err);
}
else{
open('http://localhost:' + port);
}
});
I loose the live reloading. When I change a source file I can see webpack rebuilds the bundle from the command line output but the browser won't refresh.
Please note that in my case, Hot Module Reloading is not required and is actually not desirable. I just want the page to refresh exactly as it does by default when using the webpack-dev-server CLI.