Allow Dynamic Imports of Remotely Exposed Modules Using Webpack Module Federation
npm install module-federation-import-remotejs
new ModuleFederationPlugin({
name: "Foo",
library: { type: "var", name: "Foo" },
filename: "remoteEntry.js",
exposes: {
"./Bar": "./src/Bar",
},
});
`
2. Build the remote application and serve it so that remoteEntry.js is accessible for example on the following URL http://localhost:3001.
3. Load the remotely exposed module in the host application using importRemote() and use it:
`js
import { importRemote } from "module-federation-import-remote";
// If it's a regular js module:
importRemote({ url: "http://localhost:3001", scope: "Foo", module: "Bar" }).then(
(
{
/ list of Bar exports /
},
) => {
// Use Bar exports
},
);
// If Bar is a React component you can use it with lazy and Suspense just like a dynamic import:
const Bar = lazy(() => importRemote({ url: "http://localhost:3001", scope: "Foo", module: "Bar" }));
return (
Loading Bar...