Comments (19)
@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.
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.
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.
@Coly010 Hi. Maybe you have some context?
from nx.
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.
@Coly010 Thank you very much. I'll wait for your update.
from nx.
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.
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.
@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.
@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.
@VanTigranyan Like this: VanTigranyan/nx-react-dynamic-mf#2 :)
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.
@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.
@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.
One more question I have is whether @module-federation/enhanced is used for Angular Module federation as well?
from nx.
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.
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.
@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.
@VanTigranyan Theoretically yes it should. I'd be interesting in seeing the error you're facing with it
from nx.
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)
- nx run kkbets:build:production NX The "paths[1]" argument must be of type string. Received undefined
- TypeError: Cannot read properties of undefined (reading 'split') HOT 1
- generatePackageJson does not pick up patches
- nx release plan:check did not return a non-zero exit code when version plan file does not exist HOT 4
- Documentation Error: Recipes > Module Federation > Create A Host HOT 1
- nx-release-publish default target dependsOn is ignored HOT 8
- [Nx Gradle][Plugin] OutOfMemory errors when using @nx/gradle plugin on a complex graph
- Can't generate provenance for new or private package, you must set `access` to public. HOT 2
- nx build fails with error 'File name too long'
- NX Failed to process project graph when creating a new library.
- MFE Remotes stopped working with 19.5.2 HOT 3
- create-nx-plugin: e2e test failling
- Nx Vite build not cached out of the box
- MFE resolveRemoteUrl no longer working HOT 3
- Cypress Cloud Group Test into One Run Not Working with E2E Inferred Task
- Windows CMD popup with NX running in the background
- Inability to Configure Image Handling with Nx Rollup Plugin HOT 1
- Incorrect import paths in the bundled code of a React library in a multi-package monorepo
- Crash at build
- `@nx/esbuild:esbuild` option `tsConfig` no longer accepts absolute paths
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nx.