I have gone through many answers on StackOverflow & on GitHub issues as well but, I am still stuck in Hot Module Replacement in Webpack. I am using npm start
to
I use the following version : "webpack": "~1.12.14" "webpack-hot-middleware": "^2.10.0" "webpack-dev-middleware": "^1.6.1"
And I use following code in app.js in my react.js project.
var webpackconfig =require('./webpack.config');
var webpack = require('webpack');
var webpackMiddleware = require('webpack-dev-middleware');
var webpackHotMiddleware = require('webpack-hot-middleware');
var http = require('http');
var express = require('express');
var app = require('express')();
var isDeveloping = process.env.NODE_ENV != 'production';
// var isDeveloping = false;
console.log("IS developing ",isDeveloping);
var serverConfig = require('./globalconfig.js')
var serverPort = serverConfig.port
app.get('/css/bootstrap.min.css', function (req, res) {
res.sendFile(path.join(__dirname, 'public/lib/bootstrap/css/bootstrap.min.css'));
});
// swaggerRouter configuration
var options = {
controllers: './controllers',
useStubs: process.env.NODE_ENV === 'development' ? true : false // Conditionally turn on stubs (mock mode)
}
var config = {
appRoot: __dirname // required config
}
// Start the server
app.listen(serverPort, function () {
console.log('Your server is listening * on port %d (http://localhost:%d)', serverPort, serverPort);
});
if (isDeveloping) {
app.use('/node_modules', express.static('/node_modules'));
app.use(express.static('src/web-ui/public/'));
app.use(express.static('src/web-ui/public/'));
const compiler = webpack(webpackconfig);
const middleware = webpackMiddleware(compiler,{
publicPath: webpackconfig.output.publicPath,
headers: {
"Cache-Control" : "public, max-age=604800"
},
constentBase:'dist',
stats:{
color:true,
hash:false,
timings:true,
chunks:false,
chunkModules:false,
modules:false
}
});
app.use(middleware);
app.use(webpackHotMiddleware(compiler));
app.get('/',function response(req,res){
res.write(middleware.fileSystem.readFileSync(path.join(_dirname,'dist/index.html')));
res.end();
});
} else {
app.use('/node_modules', express.static('/node_modules'));
app.use(express.static('dist/public'));
app.use(express.static('dist'));
app.get('/', function response(req, res,next) {
console.log("Processing req");
var entryFile = path.join(__dirname, 'dist', 'index.html');
console.log("Hitting the Root",entryFile);
res.sendFile(entryFile);
});
}
Same code get hot replaced on other employees computer,but not always,but many times hot replace won't work in my computer.