You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After starting the dev server, hot module replacement should work for all JS and CSS files from all webpack entries.
Actual Behavior
Hot module replacement doesn't work for all entries. Only files from the second entry are hot reloaded when changed. Files from the first entry do not get hot reloaded when changed.
Upon further testing, it seems that HMR only works for the last loaded JS bundle on the page. HMR will not work for any JS bundle loaded before it. The order of the entries in webpack.config.js does not matter.
Thanks for the tip @MohsenElgendy, but it's still not really ideal as you need to then load the runtime chunk alongside your other bundles. Webpack 4 handled embedding the runtime in multiple bundles at the same time no problem.
Reacted by Mohsen Elgendy, Francesco Caveglia Beatris, Herz3h and AnsonKong
Same issue here, setting optimization.runtimeChunk: "single" fixed the problem for me as well, but I would consider this a workaround for a bug that should be fixed anyway. This certainly worked before.
@evilebottnawi Can't test properly because it seems writeToDisk has been removed in v4 for some reason?
Initial compilation also takes much longer than the regular build time now. Over 6 seconds compared to <2 seconds for regular build. On v3 it only takes about 2.2 seconds for the initial dev server compilation.
It was not removed, it was moved to dev: { writeToDisk: true }
Aha.. that's working now, but can confirm this bug is still present. Only the last loaded bundle in the document hot reloads properly. Bundles from other entries do not hot reload at all.
Also had to explicitly disable liveReload, otherwise it live reloaded the page after every change, even though hot is enabled.
There is no changes between v3 and v4 in perf
Evidence would suggest otherwise sir. Will open an issue after release of v4 if it persists.
Is there a reason to avoid multiple entry points / bundles, because I'm surprised that not a ton of people are also reporting this bug? I know I need to have them. One example is I need certain things loaded in my site header and the rest at the end of the body.
One thing to note / add to the error report from @slightlyfaulty, I noticed all the console messages come in as warnings not errors, even though indeed HMR spews out a tons of warning messages like so:
@khelkun I recently discovered that if you use the dependOn option for an entry, then it will use the runtime that's embedded in the entry it depends on.
In this case you don't need to use runtimeChunk: 'single' anymore, as long as the dependOn entry is always loaded first.
Reacted by Alexander Akait, Vaughan Rouesnel, Ihor Shalkauskas, Sébastien PERIN, Radek Šerý, Pedro Ferreira and Maksim
I'm having a problem with HMR when Webpack has multiple entries and one of them is a service worker.
If I use runtimeChunk: 'single' hot reloading works as intended. But none of service worker's hooks runs.
I found this solution. With this service worker's hooks run, but this breaks hot reloading. When I try to update the app code I get warnings and nothing gets updated: [HMR] Update failed: ReferenceError: document is not defined [HMR] Update failed: ChunkLoadError: Loading hot update chunk runtime-app failed.
Has anyone faced this problem? I'm using Webpack 5 with the following options:
Closing this. In Webpack 5 optimization.runtimeChunk: "single" and/or dependOn/runtime should be used appropriately to control where the runtime is embedded. Otherwise each entry contains a runtime resulting in this issue.
Activity
slightlyfaulty commentedon Oct 22, 2020
Upon further testing, it seems that HMR only works for the last loaded JS bundle on the page. HMR will not work for any JS bundle loaded before it. The order of the entries in
webpack.config.jsdoes not matter.MohsenElgendy commentedon Nov 9, 2020
@slightlyfaulty setting
optimization.runtimeChunk: "single"fixed the problem for me.slightlyfaulty commentedon Nov 9, 2020
Thanks for the tip @MohsenElgendy, but it's still not really ideal as you need to then load the
runtimechunk alongside your other bundles. Webpack 4 handled embedding the runtime in multiple bundles at the same time no problem.waldronmatt commentedon Nov 11, 2020
I can also confirm optimization.runtimeChunk: "single" fixed the problem for me handling multiple bundles.
webpack.dev.config
webpack.common.js
uxsoft commentedon Nov 20, 2020
Same issue here, setting optimization.runtimeChunk: "single" fixed the problem for me as well, but I would consider this a workaround for a bug that should be fixed anyway. This certainly worked before.
alexander-akait commentedon Nov 20, 2020
I think it fixed in v4 branch, you can try it
slightlyfaulty commentedon Nov 25, 2020
@evilebottnawi Can't test properly because it seems
writeToDiskhas been removed in v4 for some reason?Initial compilation also takes much longer than the regular build time now. Over 6 seconds compared to <2 seconds for regular build. On v3 it only takes about 2.2 seconds for the initial dev server compilation.
alexander-akait commentedon Nov 25, 2020
It was not removed, it was moved to
dev: { writeToDisk: true }alexander-akait commentedon Nov 25, 2020
There is no changes between v3 and v4 in perf
slightlyfaulty commentedon Nov 25, 2020
Aha.. that's working now, but can confirm this bug is still present. Only the last loaded bundle in the document hot reloads properly. Bundles from other entries do not hot reload at all.
Also had to explicitly disable
liveReload, otherwise it live reloaded the page after every change, even thoughhotis enabled.Evidence would suggest otherwise sir. Will open an issue after release of v4 if it persists.
alexander-akait commentedon Nov 26, 2020
@slightlyfaulty Please create reproducible test repo, it should work
slightlyfaulty commentedon Nov 26, 2020
@alexander-akait Sure. https://github.com/slightlyfaulty/webpack-hmr-multi-entry-repro
yarn && yarn start- Open http://localhost:8080/src/first.jsandsrc/first.css- notice they are not hot reloaded, and there are some errors in the browser consolesrc/third.jsandsrc/third.css- notice they are hot reloaded correctly, because they're loaded last in the pageonetrev commentedon Dec 6, 2020
Is there a reason to avoid multiple entry points / bundles, because I'm surprised that not a ton of people are also reporting this bug? I know I need to have them. One example is I need certain things loaded in my site header and the rest at the end of the body.
One thing to note / add to the error report from @slightlyfaulty, I noticed all the console messages come in as warnings not errors, even though indeed HMR spews out a tons of warning messages like so:
38 remaining items
slightlyfaulty commentedon Mar 4, 2022
@khelkun I recently discovered that if you use the dependOn option for an entry, then it will use the runtime that's embedded in the entry it depends on.
In this case you don't need to use
runtimeChunk: 'single'anymore, as long as thedependOnentry is always loaded first.shalkauskas commentedon Jun 13, 2022
@slightlyfaulty thank you, worked great with webpack 5!
bradyclifford commentedon Jun 25, 2022
@slightlyfaulty you wouldn't happen to have an example you could share with the
dependOnworking for you?slightlyfaulty commentedon Jun 25, 2022
@bradyclifford I've pretty much just got my entry points listed like the examples in the docs. Nothing special.
https://webpack.js.org/concepts/entry-points/#entrydescription-object
thebat93 commentedon Aug 10, 2022
I'm having a problem with HMR when Webpack has multiple entries and one of them is a service worker.
If I use
runtimeChunk: 'single'hot reloading works as intended. But none of service worker's hooks runs.I found this solution. With this service worker's hooks run, but this breaks hot reloading. When I try to update the app code I get warnings and nothing gets updated:
[HMR] Update failed: ReferenceError: document is not defined[HMR] Update failed: ChunkLoadError: Loading hot update chunk runtime-app failed.Has anyone faced this problem? I'm using Webpack 5 with the following options:
thebat93 commentedon Aug 15, 2022
Here is a minimal test repo with the issue: https://github.com/thebat93/sw-webpack-hmr
I mentioned solutions that I tried in webpack config file.
develobora commentedon Dec 9, 2022
Not working for me also with multiple entries. "webpack": "^5.53.0", "webpack-dev-server": "^4.7.4".
alexcloudstar commentedon Jul 11, 2023
Thank you! is working perfectly fine.
Here is a part of my package.json (i'm doing micro-frontends btw I don't know if is matter or not because I do have the same packages installed)
devDeps:
slightlyfaulty commentedon Jul 11, 2023
Closing this. In Webpack 5
optimization.runtimeChunk: "single"and/ordependOn/runtimeshould be used appropriately to control where the runtime is embedded. Otherwise each entry contains a runtime resulting in this issue.fix(core): hmr issue on dev (#5006)
fix(core): hmr issue on dev (#5006)
fix(core): hmr issue on dev (#5006)
fix(core): hmr issue on dev (#5006)
PR1 (#1)