module-federation / module-federation-examples

Implementation examples of module federation , by the creators of module federation
https://module-federation.io/
MIT License
5.67k stars 1.76k forks source link

Multiple react versions runtime plugin example does not work with `loadRemote` #4278

Open p12y opened 2 months ago

p12y commented 2 months ago

Problem

Multiple react version runtime plugin example does not work with loadRemote, only import.

Example

https://github.com/module-federation/module-federation-examples/tree/master/runtime-plugins/multiple-react-versions

To recreate

In app1/App.js, update the lazy imports to use loadRemote instead of import:

+ import { loadRemote } from '@module-federation/enhanced/runtime';

- const ModernComponent = React.lazy(() => import('app2/ModernComponent'));
- const RemoteButton = React.lazy(() => import('app2/Button'));
+ const ModernComponent = React.lazy(() => loadRemote('app2/ModernComponent'));
+ const RemoteButton = React.lazy(() => loadRemote('app2/Button'));

Expected behaviour

The app should continue to work as expected

Actual behaviour

The app crashes with "ERROR Element type is invalid. Received a promise that resolves to: undefined. Lazy element type must resolve to a class or function."

image

Impact

The plugin approach to supporting multiple react versions does not work when using the runtime utils, which are needed when loading modules dynamically.

ScriptedAlchemy commented 1 month ago

Ill need to check this again, its been a while and we have since created the framework bridge capability.

https://module-federation.io/practice/bridge/index.html

ScriptedAlchemy commented 1 month ago

You also have to return an object with a default key. So loading remotely may not work without returning an object like {default: value}.