Webpack Module Federation Plugin With Examples
Webpack Module Federation Plugin With Examples
Member-only story
https://betterprogramming.pub/how-to-use-webpack-module-federation-in-react-70455086b2b0 1/21
01/08/2024, 09:46 How to Use Webpack Module Federation in React | by Oskari Rautiainen | Better Programming
Doing this using Module Federation allows us to combine the applications at run
time in the client’s browsers and eliminate build-time dependencies and
coordination, allowing the teams building these applications to develop at scale.
Getting started
You can follow along with the final code found here: https://github.com/rautio/react-
micro-frontend-example
The host app is the “main” app and remote is a sub-app plugging into it.
Module Federation does support treating the host as a remote and making the
architecture peer-to-peer if it fits your use case. More on this later.
host/
remote/
Dependencies
Within each host/ and remote/ run:
https://betterprogramming.pub/how-to-use-webpack-module-federation-in-react-70455086b2b0 2/21
01/08/2024, 09:46 How to Use Webpack Module Federation in React | by Oskari Rautiainen | Better Programming
This will install wepback and the dependencies we need for our webpack
configuration. Open in app
Host App
We are going to start with our webpack configuration
https://betterprogramming.pub/how-to-use-webpack-module-federation-in-react-70455086b2b0 3/21
01/08/2024, 09:46 How to Use Webpack Module Federation in React | by Oskari Rautiainen | Better Programming
1 // host/webpack.config.js
2 const HtmlWebpackPlugin = require("html-webpack-plugin");
3
4 module.exports = {
5 entry: "./src/index",
6 mode: "development",
7 devServer: {
8 port: 3000,
9 },
10 module: {
11 rules: [
12 {
13 test: /\.(js|jsx)?$/,
14 exclude: /node_modules/,
15 use: [
16 {
17 loader: "babel-loader",
18 options: {
19 presets: ["@babel/preset-env", "@babel/preset-react"],
20 },
21 },
22 ],
23 },
24 ],
25 },
26 plugins: [
27 new HtmlWebpackPlugin({
28 template: "./public/index.html",
29 }),
30 ],
31 resolve: {
32 extensions: [".js", ".jsx"],
33 },
34 target: "web",
35 };
This is a basic webpack example to get our js and jsx code transpiled using babel-
https://betterprogramming.pub/how-to-use-webpack-module-federation-in-react-70455086b2b0 4/21
01/08/2024, 09:46 How to Use Webpack Module Federation in React | by Oskari Rautiainen | Better Programming
1 "scripts":{
2 "start": "webpack serve"
3 }
index.js
First, we need the index.js entry to our app. We are importing another file
bootstrap.js that renders the React app.
We need this extra layer of indirection because it gives Webpack a chance to load all
of the imports it needs to render the remote app.
1 // host/src/index.js
2 import("./bootstrap");
3
4 // Note: It is important to import bootstrap dynamically using import() otherwise you wi
bootstrap.js
Next, we define the bootstrap.js file that renders our React application.
1 // host/src/bootstrap.js
2 import React from "react";
3 import ReactDOM from "react-dom/client";
4 import App from "./App";
5
6 const root = ReactDOM.createRoot(document.getElementById("root"));
7 root.render(
8 <React.StrictMode>
9 <App />
10 </React.StrictMode>
11 );
https://betterprogramming.pub/how-to-use-webpack-module-federation-in-react-70455086b2b0 5/21
01/08/2024, 09:46 How to Use Webpack Module Federation in React | by Oskari Rautiainen | Better Programming
App.js
Now we are ready to write our App.js file where the app’s main logic happens. Here
we will load two components from remote which we will define later.
https://betterprogramming.pub/how-to-use-webpack-module-federation-in-react-70455086b2b0 6/21
01/08/2024, 09:46 How to Use Webpack Module Federation in React | by Oskari Rautiainen | Better Programming
1 // host/src/App.js
2 import React from "react";
3 import ErrorBoundary from "./ErrorBoundary";
4 const RemoteApp = React.lazy(() => import("Remote/App"));
5 const RemoteButton = React.lazy(() => import("Remote/Button"));
6
7 const RemoteWrapper = ({ children }) => (
8 <div
9 style={{
10 border: "1px solid red",
11 background: "white",
12 }}
13 >
14 <ErrorBoundary>{children}</ErrorBoundary>
15 </div>
16 );
17
18 export const App = () => (
19 <div style={{ background: "rgba(43, 192, 219, 0.3)" }}>
20 <h1>This is the Host!</h1>
21 <h2>Remote App:</h2>
22 <RemoteWrapper>
23 <RemoteApp />
24 </RemoteWrapper>
25 <h2>Remote Button:</h2>
26 <RemoteWrapper>
27 <RemoteButton />
28 </RemoteWrapper>
29 <br />
30 <a href="http://localhost:4000">Link to Remote App</a>
31 </div>
32 );
33 export default App;
https://betterprogramming.pub/how-to-use-webpack-module-federation-in-react-70455086b2b0 7/21
01/08/2024, 09:46 How to Use Webpack Module Federation in React | by Oskari Rautiainen | Better Programming
1 // host/webpack.config.js
2 const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");
3 const { dependencies } = require("./package.json");
4
5 module.exports = {
6 //...
7 plugins: [
8 new ModuleFederationPlugin({
9 name: "Host",
10 remotes: {
11 Remote: `Remote@http://localhost:4000/moduleEntry.js`,
12 },
13 shared: {
14 ...dependencies,
15 react: {
16 singleton: true,
17 requiredVersion: dependencies["react"],
18 },
19 "react-dom": {
20 singleton: true,
21 requiredVersion: dependencies["react-dom"],
22 },
23 },
24 }),
25 //...
26 ],
27 //...
28 };
https://betterprogramming.pub/how-to-use-webpack-module-federation-in-react-70455086b2b0 8/21
01/08/2024, 09:46 How to Use Webpack Module Federation in React | by Oskari Rautiainen | Better Programming
Now, if we run npm start in the host app we should see something like:
This means our host app is configured, but our remote app is not exposing anything
yet. So we need to configure that next.
Remote App
Let’s start with the webpack config. Since we now have some knowledge of Module
Federation, let’s use it from the get-go:
https://betterprogramming.pub/how-to-use-webpack-module-federation-in-react-70455086b2b0 9/21
01/08/2024, 09:46 How to Use Webpack Module Federation in React | by Oskari Rautiainen | Better Programming
1 // remote/webpack.config.js
2 const HtmlWebpackPlugin = require("html-webpack-plugin");
3 const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");
4 const path = require("path");
5 const { dependencies } = require("./package.json");
6
7 module.exports = {
8 entry: "./src/index",
9 mode: "development",
10 devServer: {
11 static: {
12 directory: path.join(__dirname, "public"),
13 },
14 port: 4000,
15 },
16 module: {
17 rules: [
18 {
19 test: /\.(js|jsx)?$/,
20 exclude: /node_modules/,
21 use: [
22 {
23 loader: "babel-loader",
24 options: {
25 presets: ["@babel/preset-env", "@babel/preset-react"],
26 },
27 },
28 ],
29 },
30 ],
31 },
32 plugins: [
33 new ModuleFederationPlugin({
34 name: "Remote",
35 filename: "moduleEntry.js",
36 exposes: {
37 "./App": "./src/App",
38 "./Button": "./src/Button",
39 },
40 shared: {
41 ...dependencies,
42 react: {
43 singleton: true,
44 requiredVersion: dependencies["react"],
45 },
46 "react-dom": {
47 singleton: true,
48 requiredVersion: dependencies["react-dom"],
https://betterprogramming.pub/how-to-use-webpack-module-federation-in-react-70455086b2b0 10/21
01/08/2024, 09:46 How to Use Webpack Module Federation in React | by Oskari Rautiainen | Better Programming
48 requiredVersion: dependencies[ react dom ],
49 },
50 },
51 }),
52 new HtmlWebpackPlugin({
53 template: "./public/index.html",
54 }),
55 ],
56 resolve: {
57 extensions: [".js", ".jsx"],
58 },
59 target: "web",
60 };
Combining these will allow our host to find the remote code at
Remote@http://localhost:4000/moduleEntry.js
exposes is where we define the code we want to share in the moduleEntry.js file.
Here we are exposing two: <App /> and <Button /> .
Now let’s set up those components and our Remote app so it can run independently.
index.js
Similar to the host app, we need a dynamic import in our webpack entry.
1 // /remote/src/index.js
2 import("./bootstrap");
bootstrap.js
https://betterprogramming.pub/how-to-use-webpack-module-federation-in-react-70455086b2b0 11/21
01/08/2024, 09:46 How to Use Webpack Module Federation in React | by Oskari Rautiainen | Better Programming
1 // remote/src/bootstrap.js
2 import React from "react";
3 import ReactDOM from "react-dom/client";
4 import App from "./App";
5
6 const root = ReactDOM.createRoot(document.getElementById("root"));
7 root.render(
8 <React.StrictMode>
9 <App />
10 </React.StrictMode>
11 );
App.js
The Remote app is much simpler than the Host:
1 // remote/src/App.js
2 import React from "react";
3
4 export const App = () => {
5 return <div>Hello from the other side</div>;
6 };
7 export default App;
Button.js
And we also want to expose a <Button /> component
1 // remote/src/Button.js
2 import React from "react";
3
4 export const Button = () => <button>Hello!</button>;
5
6 export default Button;
Now the Remote app is fully configured, and if you run npm start you should see a
blank page with “Hello from the other side.”
https://betterprogramming.pub/how-to-use-webpack-module-federation-in-react-70455086b2b0 12/21
01/08/2024, 09:46 How to Use Webpack Module Federation in React | by Oskari Rautiainen | Better Programming
Now if we run npm start in both the host/ and remote/ directories, we should see
the Host app running on localhost:3000 and the remote app running on
localhost:4000 .
Development
You can simplify the development flow by configuring yarn workspaces at the root
level: https://classic.yarnpkg.com/lang/en/docs/workspaces/
Deployment
We only covered running the Micro Frontend locally. If you want to deploy them,
you would deploy each app separately to their CDN or hosting service and configure
the webpack definitions to use environment variables or some other way to update
the URLs in the ModuleFederationPlugin definitions.
If you enjoyed this article, please follow me on Medium for more stories about
micro frontends and Webpack Module Federation.
Resources
https://betterprogramming.pub/how-to-use-webpack-module-federation-in-react-70455086b2b0 13/21
01/08/2024, 09:46 How to Use Webpack Module Federation in React | by Oskari Rautiainen | Better Programming
Follow
UI Architect focused on web development and data problems. Building products and sharing as I learn in the
process. https://oskari.io/
https://betterprogramming.pub/how-to-use-webpack-module-federation-in-react-70455086b2b0 14/21
01/08/2024, 09:46 How to Use Webpack Module Federation in React | by Oskari Rautiainen | Better Programming
https://betterprogramming.pub/how-to-use-webpack-module-federation-in-react-70455086b2b0 15/21
01/08/2024, 09:46 How to Use Webpack Module Federation in React | by Oskari Rautiainen | Better Programming
https://betterprogramming.pub/how-to-use-webpack-module-federation-in-react-70455086b2b0 16/21
01/08/2024, 09:46 How to Use Webpack Module Federation in React | by Oskari Rautiainen | Better Programming
https://betterprogramming.pub/how-to-use-webpack-module-federation-in-react-70455086b2b0 17/21
01/08/2024, 09:46 How to Use Webpack Module Federation in React | by Oskari Rautiainen | Better Programming
François Roget
Feb 24
Lester Sconyers
https://betterprogramming.pub/how-to-use-webpack-module-federation-in-react-70455086b2b0 18/21
01/08/2024, 09:46 How to Use Webpack Module Federation in React | by Oskari Rautiainen | Better Programming
May 14 56 2
Lists
ChatGPT
21 stories · 738 saves
https://betterprogramming.pub/how-to-use-webpack-module-federation-in-react-70455086b2b0 19/21
01/08/2024, 09:46 How to Use Webpack Module Federation in React | by Oskari Rautiainen | Better Programming
Mar 27 28
Ekinzdaviz
https://betterprogramming.pub/how-to-use-webpack-module-federation-in-react-70455086b2b0 20/21
01/08/2024, 09:46 How to Use Webpack Module Federation in React | by Oskari Rautiainen | Better Programming
Mar 29
Akhshy Ganesh
Jun 14
https://betterprogramming.pub/how-to-use-webpack-module-federation-in-react-70455086b2b0 21/21