Hmr don't work in webpack 4
- Operating System: macos 10.13.3 (17D47)
- Node Version: 8.9.3
- NPM Version:5.5.1
- webpack Version:4.5.0 && 4.0.0 beta1
- webpack-dev-server Version:3.1.3
- [x] This is a bug
Code
i'd like to provide the resposity: https://github.com/webpack/webpack-dev-server/tree/master/examples/api/middleware
// change config in server.js
// set hot to true
const devServerOptions = Object.assign({}, webpackConfig.devServer, {
+ hot: true,
stats: {
colors: true
},
before(app) {
app.use((req, res, next) => {
console.log(`Using middleware for ${req.url}`);
next();
});
}
});
Expected Behavior
change code in app.js, the hmr will work. changes below:
- target.innerHTML = 'Scuccess!';
+ target.innerHTML = 'webpack!';
hmr is expected to run and the html will rerender
Actual Behavior
hmr not run
For Bugs; How can we reproduce the behavior?
see above
For Features; What is the motivation and/or use-case for the feature?
enable hmr with api usage in webpack 4
Could not get HRM working with v3.1.3. After enabling hot - everything stops updating. Also could not saw any "[HRM enabled]" string in console.
https://github.com/gaearon/react-hot-loader/issues/934
It didn't work for me as well.... Check out those steps and it will work: https://webpack.js.org/guides/hot-module-replacement/
@hopperhuang problem still exists?
@evilebottnawi i check it just now, this promble still remains...
I have the same problem. 😕
same issue. I add a simple console.log or small text change and updated code is not displayed
not sure why there is a 2 next to the first WDS though.
webpack: 4.8.3 webpack-dev-server: ^3.1.4
my webpack setup //webpack.common.js
const webpack = require("webpack");
const path = require("path");
const srcPath = path.join(__dirname, "/src");
module.exports = {
plugins: [
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify(process.env.NODE_ENV)
}
}),
],
module: {
rules: [
{ test: /\.js$/,
exclude: /node_modules/,
include: srcPath,
use: {
loader: "babel-loader",
}
},
]
},
optimization: {
noEmitOnErrors: true // as an aside not sure if this is needed in webpack 4 or is default
}
//webpack.dev.js
const webpack = require('webpack');
const merge = require('webpack-merge');
const path = require("path");
const common = require('./webpack.common.js');
const publicPath = "/assets/";
module.exports = merge(common, {
mode:'development',
entry: [ 'webpack-dev-server/client?http://localhost:8000',
'webpack/hot/only-dev-server',
'./src/index'
],
output: {
path: path.join(__dirname, '/../dist/assets'),
filename: 'app.js',
publicPath: publicPath
},
devServer: {
contentBase: "./src/",
historyApiFallback: true,
hot: true,
port: 8000,
publicPath: publicPath,
noInfo: false,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, GET, PUT, DELETE, OPTIONS"
}
},
cache: true,
plugins: [
new webpack.HotModuleReplacementPlugin()
],
devtool: 'eval-source-map',
});
+1 Same
my log.js is showing this error.
don't know what 'unaccepted module' means, or why it requires a full reload - i'm just adding some text to see if it reloads.
2
Ignored an update to unaccepted module ./src/components/Main/index.js -> ./src/components/MarketAnalysis/index.js -> ./src/containers/MarketAnalysis.js -> ./src/components/MarketAnalysis/Marketplace.js -> ./src/index.js -> 0
2
[HMR] The following modules couldn't be hot updated: (They would need a full reload!)
2
[HMR] - ./src/components/Main/index.js
+1 Same
@bartdominiak I thought this might be the solution because it seems like only my reducers files are being reloaded. But doing so gives me these errors:
<Provider> does not support changing store on the fly. It is most likely that you see this error because you updated to Redux 2.x and React Redux 2.x which no longer hot reload reducers automatically. See https://github.com/reactjs/react-redux/releases/tag/v2.0.0 for the migration instructions.
my setup matches the pattern described at that link, and has not changed for a while.
Warning: [react-router] You cannot change <Router routes>; it will be ignored
I still get a warning that the file that contains the module.hot code is an unaccepted module.
[HMR] The following modules couldn't be hot updated: (They would need a full reload!)
[HMR] - ./src/stores/index.js
Same here, any updates @hopperhuang ?
@bastienrobert nothing changes.
This is pretty frustrating.
By adding two entrypoints (like @indefinitelee comment), it works pretty well. The entrypoints are:
'webpack-dev-server/client?http://localhost:8080',
'webpack/hot/dev-server'
Because I'm using the API, I think this is a pretty good solution in my start.js script used to start my dev env:
config.entry = [
'webpack-dev-server/client?http://localhost:8080',
'webpack/hot/dev-server',
...config.entry
]
just after the config import.
Edit (04/2019): I just read back this comment, it may not be clear. Here is how I did it : https://github.com/bastienrobert/halo/blob/master/scripts/start.js (I'm editing my Webpack config from an external script, it's maybe why I look like a circular reference)
if you use React, you can see react-hot-loader doc
@bastienrobert if I only use those two entry points, and don't include 'src/index.js' then nothing loads, just a blank screen.
i'm confused by your example, is ...config.entry referencing itself?
i'm seeing that my app is intially loaded via app.js and code changes exist in main.[hash].js but those are never displayed, it only displays app.js https://imgur.com/a/9FzSb2W
@indefinitelee You need to add these two entry points before your app index.js in the entry points array.
My example is only for the webpack-dev-server API and for advanced use. Here I’m using the spread operator.
@bastienrobert I have those two in my entry array as well as the root of my project as seen in my earlier post
I’m confused how you can use the spread on config.entry inside of itself. It looks like a circular reference.
config.entry = [ 'webpack-dev-server/client?http://localhost:8080', 'webpack/hot/dev-server', ...config.entry ]
Ok, it seems you’re not using the API so it should look like that:
config.entry = [
'webpack-dev-server/client?http://localhost:8080',
'webpack/hot/dev-server',
'src/index.js'
]
If you’re curious, there’s some references about the API in the doc.
i'm going to close issues as it seems folks mainly have issue with configurating HMR 'correctly' for there setup(s), please go to https://webpack.js.org/ for more information on how to do so
Anyone with a concrete issue and a repo, feel free to open a new issue instead :)