Coder Social home page Coder Social logo

Comments (19)

VanTigranyan avatar VanTigranyan commented on September 28, 2024 1

@Coly010 I ended up using @module-federation/enhanced with rspack on react and angular now just consumes these remotes using loadRemote(). It worked flawlessly. I was able to solve all problems I had. I used its plugin instead of NX' one. Thank you so much for pointing me to it. I would struggle for days if not that suggestion. So all problems are solved. Thank you very much again.

from nx.

VanTigranyan avatar VanTigranyan commented on September 28, 2024

I think to fix this, react remotes should generate remoteEntry.mjs instead of remoteEntry.js, otherwise the browser basically doesn't recognize remoteEntry as a module, hence import.meta is not available in that js file.

from nx.

VanTigranyan avatar VanTigranyan commented on September 28, 2024

I want to add that when I load a react remote app supported by the same NX setup in my angular shell app, it works without any issues.

from nx.

VanTigranyan avatar VanTigranyan commented on September 28, 2024

@Coly010 Hi. Maybe you have some context?

from nx.

Coly010 avatar Coly010 commented on September 28, 2024

I think it does just boil down to the ESM issue. I would have bet that we did generate .mjs remotes for React, but it seems not. I’ll have a look

from nx.

VanTigranyan avatar VanTigranyan commented on September 28, 2024

@Coly010 Thank you very much. I'll wait for your update.

from nx.

VanTigranyan avatar VanTigranyan commented on September 28, 2024

Just FYI: I did dig into MF plugin config and seems like it is set to output a module, but the fileName is set to remoteEntry.js. Taking into account that webpack bundles react and doesn't output ESM, I think this is where the conflict resides. CommonJS react bundles try to execute an ESM module. Obviously this doesn't work.

from nx.

Coly010 avatar Coly010 commented on September 28, 2024

Interestingly, I wasn't able to reproduce this on a new workspace I just created.

I'll take a look at your repo now, but let me know if this one works for you: https://github.com/Coly010/nx-react-dyn-fed-demo

from nx.

Coly010 avatar Coly010 commented on September 28, 2024

@VanTigranyan Found the problem, you didn't have loadRemoteModule wrapped in React.lazy. Unfortunately, this is a requirement currently due to how the scopes are initialized with MF.

I created a PR to show this: VanTigranyan/nx-react-dynamic-mf#1

from nx.

VanTigranyan avatar VanTigranyan commented on September 28, 2024

@Coly010 OMG. I am so stupid. How could I forget that. I understand how that works for components. But what if I want to get a shared library from my react remote?

from nx.

Coly010 avatar Coly010 commented on September 28, 2024

@VanTigranyan Like this: VanTigranyan/nx-react-dynamic-mf#2 :)

image image image

I'll admit the ergonomics of this are not great.
However, we use the @module-federation/enhanced package now, so instead of using the setRemoteDefinitions and loadRemoteModule that we export, you could use the following:

https://module-federation.io/guide/basic/runtime.html#registerremotes

Do a fetch for the module-federation.manifest.json and iterate through it and call registerRemotes
Then when you need the remote use loadRemote: https://module-federation.io/guide/basic/runtime.html#loadremote

from nx.

VanTigranyan avatar VanTigranyan commented on September 28, 2024

@Coly010 I can confirm that even with React.lazy() I can import only components. So we are blocked if we want to import other things, such as shared libraries. And even if I export a dumb component with the library, I pretty much have to count the component so that React.lazy() works a loads the lib. How do we solve this problem?

from nx.

VanTigranyan avatar VanTigranyan commented on September 28, 2024

@Coly010 I tried your solution. It's great. The problem is I wanted to expose another module (shared state) from the MFE itself, without creating another MFE just for sharing a library. But I guess I can work with this for now. Thanks a lot for looking into this so quickly and for great suggestions. I am at least unblocked right now.

from nx.

VanTigranyan avatar VanTigranyan commented on September 28, 2024

One more question I have is whether @module-federation/enhanced is used for Angular Module federation as well?

from nx.

Coly010 avatar Coly010 commented on September 28, 2024

You can follow the pattern that i created with the new MFE and just apply it to an existing MFE. the @nx/react:federate-module generator allows you to specify and existing remote and it'll add a new export to it that can be consumed via loadRemoteModule.

Yes @module-federation/enhanced is also being used for Angular

from nx.

VanTigranyan avatar VanTigranyan commented on September 28, 2024

Just tried your example with @module-federation/enhanced, and I got the same error as before. So it works with loadRemoteModule but not with loadRemote. I'll try @nx/react:federate-module

from nx.

VanTigranyan avatar VanTigranyan commented on September 28, 2024

@Coly010 Sorry for bothering so much. I just want to get answers to all problems that we have so that I can start to work on building the actual application. Is it possible to load a dynamic remote into another dynamic remote? Let's say we have menu that we want to share and setup it as an MFE. then I want to load this menu dynamically in another dynamic remote (let's say dashboard) and that remote gets dynamically loaded into the shell. Right now I cannot get it to work.

from nx.

Coly010 avatar Coly010 commented on September 28, 2024

@VanTigranyan Theoretically yes it should. I'd be interesting in seeing the error you're facing with it

from nx.

VanTigranyan avatar VanTigranyan commented on September 28, 2024

Oh, it's the same error with import.meta not being available.
For the context, we have a huge Angular application, that we are trying to decouple into smaller React-based MFEs. So I added MF setup to Angular to act as a shell, then created a react app as a remote. I managed to dynamically load the remote and render it. I managed to solve the routing, navigation and state-sharing problems. The only thing that remains for me to solve is loading another react-remote in the first one I mentioned. After experimenting today I found out that when I set the definitions in the shell, even if I use loadRemoteModule in a remote, it still sees the definitions and can load another remote. But this works only if the host is react. I tried to set definitions in Angular shell using both react/mf and angular/mf libs, so that each app type gets the definitions but that didn't help either. I found that when I share @nx/react in Angular and when I do it in a react shell, the output is not the same, hence, I guess, Even when I share @nx/react from Angular, the react remote loads it's own instance of this library, meaning the 'singletion' param doesn't work in multi-stack MF setup.

from nx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.