27
loading...
This website collects cookies to deliver better user experience
exposes
certain component/s via a separate javascript file, and other application that wishes to use that component, async loads that remote
javascript file and consume that component. accounts-summary-app
- Micro frontend that provides the summary of all the accounts account-details-app
- Micro frontend that provides details of a selected accountmain-app
- app that hosts above two components. Also acts as a medium to communicate with each other. ModuleFederationPlugin
is a high level webpack plugin that provides a very convenient way to configure module federation in your projects. Also plugin comes along with webpack library without need of installing another dependency as well.expose
a component. So let's add following to webpack.config.js
of accounts-summary-app
project. const { ModuleFederationPlugin } = require("webpack").container;
plugins: [
new ModuleFederationPlugin({
name: "AccountsSummaryApp",
filename: "accountsSummaryApp_remote.js",
exposes: {
"./AccountsSummary": "./src/components/AccountsSummary",
},
}),
...
],
name
is unique identification of your module. Normally this is the name of your micro frontend project. filename
is the name of the javascript file that exposes the componentsexposes
is a map (key and a value) of components that are exposed from this module. (key will act as an alias for the component while the value is where the component located in the project)AccountsSummary
component into a separate javascript file, as we instructed in the webpack configuration. account-details-app
project as wellplugins: [
new ModuleFederationPlugin({
name: "AccountDetailsApp",
filename: "accountDetailsApp_remote.js",
exposes: {
"./AccountDetails": "./src/components/AccountDetails",
},
}),
...
],
main-app
is responsible for loading the components from micro frontend projects.exposes
, Host app's webpack configuration defines remotes
that tells webpack where to find those external components.plugins: [
new ModuleFederationPlugin({
remotes: {
AccountsSummaryApp_Remote: "AccountsSummaryApp@http://localhost:9001/accountsSummaryApp_remote.js",
AccountDetailsApp_Remote: "AccountDetailsApp@http://localhost:9002/accountDetailsApp_remote.js",
},
}),
...
],
remotes
is a map (key and value) that defines all the external modules that it consumes. Key will act as an alias for the module and value defines the remote javascript file location for that module. <Name of the Exposed Module>@<Remote URL of the javascript file>
const AccountsSummary = React.lazy(() =>
import("AccountsSummaryApp_Remote/AccountsSummary")
);
<Suspense fallback={<h1>Error while loading Account Summary</h1>}>
<AccountsSummary onAccountSelected={handleAccountSelected} />
</Suspense>
main-app
to see the code.