This repo attempts to write code that does the following:
- "host" package (/module_fed_package/packages/host) is an webpack based package that use module federation to load code from a remote, at runtime
- "nav" package (/module_fed_package/packages/nav) contains a header component that is exposed via module federation for runtime consumption. It is be used by the host
- "legacyApp" application (/legacyApp) does a standard yarn or npm local import of host.
The goal of the exercise is to try to include module federation code indirectly in a legacy application that doesn't know about module federation. And maybe doesn't even use webpack (this one does, but another bundler like rollup could be used alternatively in the legacy application). The package ("host") that is imported into the legacy application is built to run webpack module federation, and has all the knowledge of the remote.
As originally written, using the standard ModuleFederationPlugin, trying to access the remote didn't work in this scenario. I switched to using the custom dynamic loading approach, and that appears to work.
The basic module federation code, including the custom dynamic loading code in module_fed_package\packages\host\src\App.jsx, is based on https://github.com/module-federation/module-federation-examples, and the Practical Module Federation book. Any mistakes in this repo are mine, not those of the original authors (which does not promoted the static import approach attempted here)
- Clone the repo
- Navigate to module_fed_package. Run "yarn"
- Navigate to module_fed_package/packages/nav. Run "yarn start".
- In another shell, navigate to module_fed_package/packages/host. Run "yarn build:dev"
- In yet another shell, navigate to legacyApp. Run "yarn"
- In the same shell, run "yarn start"
Web page shows content like this:
This is the legacy application I'm the header! This is the npm included host, which attempts to use module federation to include the header from another package
This demonstrates a "dumb", legacy application can statically load a smart module federation based host, which includes functionality from a modular federation remote