Modern.js provides runtime APIs to quickly export application-level Module Federation modules from your application.
We use the application created in Using Module Federation as an example to further explain how to import application-level modules.
Building on the existing application, we need to install the @module-federation/bridge-react
dependency to use Bridge for loading application-level modules.
Unlike directly exporting component-level modules, we need to create a separate entry for application-level modules to be exported via Module Federation
.
We create the src/export-App.tsx
file:
The filename can be arbitrary; Modern.js does not enforce a specific naming convention.
This file will pass the root component of the main
entry application to the Bridge API and render it to the specified node via Bridge's render function.
Next, we configure module-federation.config.ts
to modify the export to src/export-App.tsx
:
createBridgeComponent
is used to export application-level modules. Modern.js related APIs can be found at createRoot and render.
We then modify the consumer configuration by removing the previously created src/routes/remote/page.tsx
route file.
We want all routes that access /remote
to enter the aforementioned application-level module, so we add src/routes/remote/$.tsx
instead.
If you are not familiar with the capabilities of $.tsx
, please read Wildcard Routes.
createRemoteComponent
is used to load application-level modules.
Now, both the producer and consumer applications are set up. We can run modern dev
locally to start both applications.
After startup, when the consumer application accesses the /remote
route, it will enter the producer application. Accessing http://localhost:8080/remote
will display a complete page of the producer's remote module in the browser.
You can create new route files in the producer application and add route navigation in the code. These functionalities will also work as expected.
You can refer to the example here: Modern.js & Module Federation Application-Level Modules.