I am trying to use Webpack Hot Module Replacement for my CSS.
I run the PHP built-in server (php -S localhost:8000 -t .
) to load the following index.php
file, which is at the root of my project :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test webpack</title>
</head>
<body>
<h1>Hello world</h1>
<script src="http://localhost:8080/app.js"></script>
</body>
</html>
Then I want to use webpack-dev-server
and its Hot Module Replacement feature to see my CSS changes everytime I save my CSS files.
Here is my webpack.config.js
:
const path = require("path");
module.exports = {
entry: {
app: "./src/app.js"
},
output: {
path: path.join(__dirname, "assets"),
filename: "[name].js"
},
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: "style-loader"
},
{
loader: "css-loader"
}
]
}
]
}
};
Here is my ./src/app.js
:
import "./app.css";
Here is my ./src/app.css
:
body {
background-color: #fff;
}
I am starting the webpack-dev-server
by typing : ./node_modules/.bin/webpack-dev-server --hot
.
The problem is that instead of hot reloading the CSS, it always does a full page reload.
Here is what is printed in my brower's console :
Navigated to http://localhost:8000/
[HMR] Waiting for update signal from WDS...
[WDS] Hot Module Replacement enabled.
[WDS] App updated. Recompiling...
[WDS] App hot update...
[HMR] Checking for updates on the server...
GET http://localhost:8000/8f2d15bbf1ed4e8f7e63.hot-update.json 404 (Not Found)
[HMR] Cannot find update. Need to do a full reload!
[HMR] (Probably because of restarting the webpack-dev-server)
Following these logs, the problem is that it searches the updates informations on localhost:8000/8f2d15bbf1ed4e8f7e63.hot-update.json
, which is obviously not found because it is my PHP server (8000 port). But I don't know why webpack-dev-server
is trying to reach localhost:8000
since it is running on localhost:8080
by default and I didn't overwrite anything.
I am running webpack@3.5.5 and webpack-dev-server@2.7.1.
Does anybody encountered the same problem and found a solution ?
Thanks.