ctrlplusb / react-universally

A starter kit for universal react applications.
MIT License
1.7k stars 244 forks source link

Fix webpack v4 issue where webpackHotUpdate is not defined on hard reload #566

Closed declanelcocks closed 6 years ago

declanelcocks commented 6 years ago

See here for related issue thread on webpack.

TLDR:

Seems that in webpack v3, new webpack.HotModuleReplacementPlugin(), works fine, but in v4 we need to add { multiStep: true }

birkir commented 6 years ago

Nice catch! Thank you

williamoliveira commented 6 years ago

Even with this fix I still see this error sometimes, its kinda intermittent

williamoliveira commented 6 years ago

Proof it still happens: https://youtu.be/kWI6YPANzVA?t=1m31s

declanelcocks commented 6 years ago

@williamoliveira any idea what situations are causing it? I haven't seen the error since adding this in yet. The issue on webpack is still quite new though, so maybe some more edge cases will crop up. The only suggestion so far on that issue from the core team was to make multiStep: true a default setting.

SleepWalker commented 6 years ago

If this issue is together with Webpack 4, this may be due to assets-webpack-plugin, which is a little bit outdated and has not so good logic for filtering updates. This plugin is used to resolve index chunk path for production build (but it is enabled for both dev and prod modes)

After updating to Webpack 4 it started to overwrite index file path in assets.json with hmr chunk path. The reason is that assets plugin detects hmr chunks with a regexp based on config.output.hotUpdateChunkFilename. Which seems to be undefined in Webpack 4 (or not enough valid. I was to busy for research).

So to fix this issue you need to add the following in configFactory.js:

let webpackConfig = {
    // ...
    // Bundle output configuration.
    output: {
      // ...
      hotUpdateChunkFilename: '[hash].hot-update.js', // use for AssetsPlugin to filter out hot updates
    },
    // ...
};

In future, it seems to be better to built-in use config.recordsPath and only for production mode, because during dev we have no hashes in file names. This should also allow to drop md5 chunk hash plugin

williamoliveira commented 6 years ago

@SleepWalker thank you very much, that did it

doc says the default value for this setting is '[id].[hash].hot-update.js' but with that value you get the error, '[hash].hot-update.js' works fine

declanelcocks commented 6 years ago

@SleepWalker or @williamoliveira Want to make a PR for it? If not, I can do it later.

ctrlplusb commented 6 years ago

Awesome @SleepWalker - I am all for dropping plugins! 👍 If we can take benefit of core Webpack functionality over existing plugins I would highly recommend we migrate towards this.

SleepWalker commented 6 years ago

@declanelcocks I've opened the PR. I left HotModuleReplacementPlugin with its defaults, because it should be enough to fix an issue with chunk path.

I am all for dropping plugins!

@ctrlplusb I will look into this, but not sure when. This may take some weeks.

declanelcocks commented 6 years ago

great @SleepWalker 👍