react-refresh-webpack-plugin icon indicating copy to clipboard operation
react-refresh-webpack-plugin copied to clipboard

Component modifications are not applied in module federation

Open kabel2 opened this issue 4 years ago • 30 comments

I'm having an issue getting react refresh working on a project with module fedration. The ws connection is working, also in the console it seems its working, but the modifications are not applied to the component.

[webpack-dev-server] App updated. Recompiling...
index.js:3210 [webpack-dev-server] App hot update...
log.js:24 [HMR] Checking for updates on the server...
log.js:24 [HMR] Updated modules:
log.js:24 [HMR]  - ./src/Button.js
log.js:24 [HMR] App is up to date.

I have created an basic example: https://github.com/kabel2/module-federation-examples/tree/feature/example-react-refresh/basic-host-remote

Changing the innertext of the button in the "Button.js" component in "app2" does not apply the changes live, only after reload.

With the old react-hot-loader it seems to work.

kabel2 avatar May 14 '21 09:05 kabel2

Same with me

Ridermansb avatar Jun 22 '21 09:06 Ridermansb

Are you externalising React?

pmmmwh avatar Jun 27 '21 17:06 pmmmwh

React is shared from App1 via the ModuleFederation plugin. I tried remove react from the list of shared components, but the problem remains.

kabel2 avatar Jun 28 '21 06:06 kabel2

Any news on this?

Ridermansb avatar Jul 07 '21 17:07 Ridermansb

No. This is likely due to externalising and injection order of stuff. I currently do not have much time to check this, but I can revisit this later.

pmmmwh avatar Jul 08 '21 15:07 pmmmwh

There are 2 key events for it to work correctly.

  • react-refresh should be injecting before react-dom this seem to work at app1;
  • Also react-refresh should be executed only once, or if executed more than once (it seems like the case on app 2), the react-dom should be injected again, that it's not possible right now because this plugin only injects it once.

Put this piece of code at the start of your app2 entry and it should work right now. A PR would be welcome to fix it, maybe I'll do it :).

//A reference to the react refresh injector
const { injectIntoGlobalHook } = require("react-refresh/cjs/react-refresh-runtime.development")

//Injects the react refresh replacing the one from the app1
injectIntoGlobalHook(window);

//Injects the react-dom instance again, that will now be referenced by the refresher from app2
(window as any).__REACT_DEVTOOLS_GLOBAL_HOOK__.inject(ReactDOM);

Muritavo avatar Jul 10 '21 21:07 Muritavo

Maybe you could use the library option to ensure react refresh is injected for both apps?

pmmmwh avatar Jul 11 '21 15:07 pmmmwh

HMR cannot work with federated modules, you can use @module-federation/fmr as an alternative solution.

We would have to rewrite HMR graphs in webpack in order to achieve this. Webpack needs to be aware of all module and parent modules in the graph which means we would have to build a way for federated webpack graphs to be interconnected as well. Changing a module in a remote doesn't mean that the container knows where its being used inside a host.

ScriptedAlchemy avatar Sep 08 '21 05:09 ScriptedAlchemy

i figured out how resovle to this in #516, maybe not a greate idea, but it works

ije avatar Sep 28 '21 14:09 ije

Does anyone have an example how react-refresh etc. should be loaded on the page in order for hot reload to work?

In my module federation setup, hot reload works occasionally, which suggests there's some indeterminism going on in the order that scripts are loading on the page.

The suggestion of bundling react-refresh separately, placing it in window and then making it external in MF libs does not seem to work.

raine avatar Apr 11 '22 13:04 raine

Maybe you could use the library option to ensure react refresh is injected for both apps?

Indeed. Seems like using Webpack 5's output.uniqueName helped me solve this same issue with 2 separate builds used within the same page.

ccambournac avatar Jun 13 '22 10:06 ccambournac

I would be happy to curate examples where React Refresh can be used properly with MF if anyone can provide a small sample repo.

pmmmwh avatar Jun 13 '22 12:06 pmmmwh

I would be happy to curate examples where React Refresh can be used properly with MF if anyone can provide a small sample repo.

I'll definitely try to find time to do so @pmmmwh.

ccambournac avatar Jun 13 '22 12:06 ccambournac

使用ModuleFederation注意要满足这些条件才可以热更新, 我这里有一个例子可以供参考 https://www.npmjs.com/package/react-refresh-umd

zhangHongEn avatar Aug 15 '22 09:08 zhangHongEn

For anyone who runs into this issue, the solution from @ije and @danieloprado in https://github.com/pmmmwh/react-refresh-webpack-plugin/pull/516 works perfectly for me.

I end up with pnpm patch to patch the package, you can also do it with yarn patch if you're using yarn.

@pmmmwh would you reconsider applying the changes suggested by @ije and @danieloprado?

ruanyl avatar Aug 19 '22 13:08 ruanyl

As someone else who is experiencing issues here with module federation, I am hoping to re-up this conversation.

@pmmmwh, would you be open to applying the changes described here: https://github.com/pmmmwh/react-refresh-webpack-plugin/pull/516#issuecomment-1159038683

As module federation becomes more popular I can imagine this will become a more common issue.

ESoch avatar Mar 10 '23 03:03 ESoch

Although, to further complicate things, I am not able to get it working even with the suggested fixes. I continue to get this error:

   [HMR] Update failed: ChunkLoadError: Loading hot update chunk main failed.

@ruanyl, does your patch continue to work as expected for you?

ESoch avatar Mar 10 '23 04:03 ESoch

@ESoch try change runtimeChunk to single during development

//...
optimization: {
    runtimeChunk: isDevelopment ? 'single' : false
 }

danieloprado avatar Mar 10 '23 14:03 danieloprado

@danieloprado, thanks for the suggestion. I've tried that and it does indeed fix this issue for the host application. The problem, however, is that we're using bi-directional module federation and when we set runtimeChunk: single it breaks any applications that are consuming a component from the host.

ESoch avatar Mar 10 '23 18:03 ESoch

If you want to configure react-refresh to shared, you need react-refresh-webpack-plugin to change the import method of source code from react-refresh/runtime to react-refresh, and you also need to configure "react-refresh-webpack-plugin" include and exclude to avoid shared being referenced in the entry chunk, even so there will be other problems, so I also tend to force react-refresh to become a singleton to solve it quickly

zhangHongEn avatar Mar 16 '23 11:03 zhangHongEn

https://github.com/zhangHongEn/universal-module-federation-plugin/tree/main/packages/single-react-refresh-plugin

zhangHongEn avatar Mar 16 '23 11:03 zhangHongEn

As someone else who is experiencing issues here with module federation, I am hoping to re-up this conversation.

@pmmmwh, would you be open to applying the changes described here: #516 (comment)

As module federation becomes more popular I can imagine this will become a more common issue.

I cannot really offer much help without any example to look into. For the patch, my stance still stands that polluting global and binding this package to browser only is a bad thing to do. I personally do not use module federation and is not knoledgeable with it by any means, so help would be appreciated.

pmmmwh avatar Apr 12 '23 11:04 pmmmwh

As someone else who is experiencing issues here with module federation, I am hoping to re-up this conversation. @pmmmwh, would you be open to applying the changes described here: #516 (comment) As module federation becomes more popular I can imagine this will become a more common issue.

I cannot really offer much help without any example to look into. For the patch, my stance still stands that polluting global and binding this package to browser only is a bad thing to do. I personally do not use module federation and is not knoledgeable with it by any means, so help would be appreciated.

@pmmmwh You can refer to this example, app2 achieves hmr by mounting react-refresh/runtime to the global maintain singleton, app3's react-refresh/runtime is not a singleton so it cannot hmr https://github.com/wpmjs/examples/tree/main/module-federation-react-hmr

zhangHongEn avatar Apr 17 '23 03:04 zhangHongEn

We also have this issue.

Workaround for us is to opt-into Module Federation when starting the dev server (via a command line arg), so that most developers are not impacted by this when developing the standalone application. When consuming a component via Module Federation, Fast Refresh doesn't work anyway as stated by @ScriptedAlchemy, so we are not losing anything.

    if (shouldEnableModuleFederation()) {
        config.plugins.push(getModuleFederationPlugin());
    }

…

    function shouldEnableModuleFederation() {
        return isEnvProduction || process.argv.includes('--enable-module-federation');
    }

felixmokross avatar May 02 '23 16:05 felixmokross

@felixmokross Maybe you can provide small reproducible example? It helps, thank you

alexander-akait avatar May 02 '23 16:05 alexander-akait