module-federation-examples
Implementation examples of module federation , by the creators of module federation
Top Related Projects
A bundler for javascript and friends. Packs many modules into a few bundled assets. Code Splitting allows for loading parts of the application on demand. Through "loaders", modules can be CommonJs, AMD, ES6 modules, CSS, Images, JSON, Coffeescript, LESS, ... and your custom stuff.
The React Framework
Next generation frontend tooling. It's fast!
Next-generation ES module bundler
The zero configuration build tool for the web. 📦🚀
Dynamic ES module loader
Quick Overview
Module Federation Examples is a GitHub repository that showcases various implementations and use cases of Webpack 5's Module Federation feature. It provides a collection of example projects demonstrating how to share code and dependencies between multiple applications, enabling micro-frontend architectures and improving build performance.
Pros
- Offers a wide range of examples covering different scenarios and frameworks
- Provides practical implementations of Module Federation for learning and reference
- Regularly updated with new examples and improvements
- Includes documentation and explanations for each example
Cons
- May be overwhelming for beginners due to the large number of examples
- Some examples might become outdated as the Module Federation feature evolves
- Requires a good understanding of Webpack and modern JavaScript development
- Limited focus on production-ready implementations
Code Examples
- Basic host-remote setup:
// webpack.config.js (host)
const { ModuleFederationPlugin } = require('webpack').container;
module.exports = {
// ...
plugins: [
new ModuleFederationPlugin({
name: 'host',
remotes: {
remote: 'remote@http://localhost:3002/remoteEntry.js',
},
}),
],
};
This code configures the host application to consume a remote module.
- Exposing components from a remote:
// webpack.config.js (remote)
const { ModuleFederationPlugin } = require('webpack').container;
module.exports = {
// ...
plugins: [
new ModuleFederationPlugin({
name: 'remote',
filename: 'remoteEntry.js',
exposes: {
'./Button': './src/Button',
},
}),
],
};
This code exposes a Button component from the remote application.
- Consuming a remote component:
// App.js (host)
import React, { Suspense } from 'react';
const RemoteButton = React.lazy(() => import('remote/Button'));
const App = () => (
<div>
<h1>Host Application</h1>
<Suspense fallback="Loading Button...">
<RemoteButton />
</Suspense>
</div>
);
export default App;
This code demonstrates how to consume a remote component in the host application.
Getting Started
-
Clone the repository:
git clone https://github.com/module-federation/module-federation-examples.git
-
Navigate to a specific example directory:
cd module-federation-examples/basic-host-remote
-
Install dependencies and run the example:
yarn yarn start
-
Open your browser and visit the URLs provided in the console output to see the example in action.
Competitor Comparisons
A bundler for javascript and friends. Packs many modules into a few bundled assets. Code Splitting allows for loading parts of the application on demand. Through "loaders", modules can be CommonJs, AMD, ES6 modules, CSS, Images, JSON, Coffeescript, LESS, ... and your custom stuff.
Pros of webpack
- Mature and widely adopted build tool with extensive documentation
- Supports a vast ecosystem of plugins and loaders
- Highly configurable for complex build scenarios
Cons of webpack
- Steeper learning curve, especially for complex configurations
- Can be overkill for smaller projects or simple use cases
- Module Federation is just one feature among many, not the primary focus
Code Comparison
webpack configuration:
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
// ... other configuration options
};
Module Federation example:
new ModuleFederationPlugin({
name: 'app1',
filename: 'remoteEntry.js',
remotes: {
app2: 'app2@http://localhost:3002/remoteEntry.js',
},
// ... other options
})
Summary
webpack is a powerful and flexible build tool with a broad range of features, while module-federation-examples focuses specifically on demonstrating Module Federation capabilities. webpack provides a comprehensive solution for various build scenarios, but may be complex for simpler use cases. module-federation-examples offers targeted examples for implementing Module Federation, making it easier to understand and implement this specific feature across different frameworks and scenarios.
The React Framework
Pros of Next.js
- Comprehensive full-stack React framework with built-in routing and server-side rendering
- Extensive ecosystem and community support
- Seamless integration with Vercel for easy deployment and scaling
Cons of Next.js
- Less flexible for micro-frontend architectures compared to Module Federation
- Steeper learning curve for developers new to server-side rendering concepts
- More opinionated structure, which may limit customization options
Code Comparison
Next.js routing:
// pages/about.js
export default function About() {
return <h1>About Page</h1>
}
Module Federation example:
// webpack.config.js
new ModuleFederationPlugin({
name: 'app1',
remotes: {
app2: 'app2@http://localhost:3002/remoteEntry.js',
},
})
Key Differences
- Next.js focuses on providing a complete React framework, while Module Federation Examples demonstrate micro-frontend architecture
- Module Federation Examples offer more flexibility in sharing components and modules between applications
- Next.js provides a more opinionated structure with built-in features, whereas Module Federation Examples showcase integration techniques across different frameworks
Use Cases
- Choose Next.js for building full-stack React applications with server-side rendering capabilities
- Opt for Module Federation when implementing micro-frontend architectures or sharing modules between separate applications
Next generation frontend tooling. It's fast!
Pros of Vite
- Faster build times and hot module replacement
- Simpler configuration and setup out of the box
- Native ES modules support for modern browsers
Cons of Vite
- Less mature ecosystem compared to webpack-based solutions
- Limited support for older browsers without additional configuration
- Fewer plugins and integrations available
Code Comparison
Module Federation Examples:
const { ModuleFederationPlugin } = require("webpack").container;
module.exports = {
plugins: [
new ModuleFederationPlugin({
name: "app1",
remotes: {
app2: "app2@http://localhost:3002/remoteEntry.js",
},
}),
],
};
Vite:
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()],
build: {
rollupOptions: {
input: {
main: 'src/main.jsx',
},
},
},
});
The Module Federation Examples repository showcases various implementations of Webpack 5's Module Federation feature, allowing for dynamic loading of remote modules. On the other hand, Vite focuses on providing a fast development experience with minimal configuration, leveraging native ES modules for improved performance. While Module Federation Examples offers more advanced micro-frontend capabilities, Vite excels in simplicity and speed for modern web development workflows.
Next-generation ES module bundler
Pros of Rollup
- Simpler and more focused on bundling JavaScript modules
- Excellent tree-shaking capabilities for smaller bundle sizes
- Supports a wide range of output formats (ES modules, CommonJS, UMD, etc.)
Cons of Rollup
- Less suited for complex micro-frontend architectures
- Doesn't provide built-in support for runtime module federation
- May require additional configuration for advanced use cases
Code Comparison
Module Federation Examples:
new ModuleFederationPlugin({
name: 'app1',
filename: 'remoteEntry.js',
remotes: {
app2: 'app2@http://localhost:3002/remoteEntry.js',
},
exposes: {
'./Button': './src/Button',
},
})
Rollup:
export default {
input: 'src/main.js',
output: {
file: 'bundle.js',
format: 'iife'
},
plugins: [
resolve(),
commonjs()
]
}
The Module Federation Examples repository showcases advanced micro-frontend architectures using Webpack's Module Federation feature. It provides examples of runtime module sharing and dynamic loading across multiple applications.
Rollup, on the other hand, is a more traditional bundler focused on creating efficient bundles for JavaScript libraries and applications. It excels in producing smaller bundle sizes through tree-shaking but lacks built-in support for advanced micro-frontend architectures.
The zero configuration build tool for the web. 📦🚀
Pros of Parcel
- Zero configuration bundler, making it easier to set up and use
- Faster build times due to its multi-core processing capabilities
- Built-in support for various file types without additional plugins
Cons of Parcel
- Less flexibility for complex configurations compared to Module Federation
- Limited support for advanced code-splitting techniques
- Smaller ecosystem and community compared to Webpack-based solutions
Code Comparison
Parcel (entry file):
<html>
<body>
<script src="./index.js"></script>
</body>
</html>
Module Federation (webpack.config.js):
const { ModuleFederationPlugin } = require('webpack').container;
module.exports = {
plugins: [
new ModuleFederationPlugin({
name: 'app',
remotes: {
remote: 'remote@http://localhost:3002/remoteEntry.js',
},
}),
],
};
Summary
Parcel offers simplicity and speed, making it ideal for smaller projects or quick prototypes. Module Federation Examples, based on Webpack, provides more advanced features for large-scale applications, especially those requiring micro-frontend architectures. The choice between them depends on project complexity and specific requirements.
Dynamic ES module loader
Pros of SystemJS
- Mature and well-established project with a long history
- Supports loading various module formats (CommonJS, AMD, UMD)
- Can be used in both browser and Node.js environments
Cons of SystemJS
- Requires more configuration and setup compared to Module Federation
- May have a steeper learning curve for developers new to module loading
- Less integrated with modern bundling tools like webpack
Code Comparison
SystemJS:
System.import('./module.js').then(function(module) {
module.doSomething();
});
Module Federation:
import('./module').then(module => {
module.doSomething();
});
Key Differences
- Module Federation is specifically designed for micro-frontends and code sharing between applications
- SystemJS is a more general-purpose module loader that can work with various module formats
- Module Federation is tightly integrated with webpack, while SystemJS can be used independently
Use Cases
- SystemJS: Legacy applications, supporting multiple module formats, or environments without modern bundlers
- Module Federation: Micro-frontends, large-scale applications with shared dependencies, or dynamic code loading in modern web applications
Community and Ecosystem
- SystemJS has a mature ecosystem with plugins and extensions
- Module Federation is newer but rapidly growing, with strong support from the webpack community
Performance Considerations
- Module Federation can potentially offer better performance in webpack-based applications due to its tight integration
- SystemJS may have additional overhead due to its flexibility in supporting multiple module formats
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Module Federation Examples
This repository is to showcase examples of how Webpack 5's new Module Federation can be used.
Module Federation Universe
- Module federation enhances collections: Universe
- Module Federation Docs: Module Federation Docs
List of Examples
Click here to see the detailed list of examples in this repo Full Examples List
Check out our book
Consultations
1 Hour group consultation | $100 |
---|---|
30 Min 1:1 consultation | $60 |
15 Min 1:1 consultation | $30 |
Bespoke API modifications and hands on code | $300-$500/hr |
Notes
The examples in this repository leverage pnpm and workspaces. To run from a git checkout locally, remove all of the proprietary example directories, ensure you have pnpm installed and run install pnpm i
at the repo root.
You can then run pnpm start
from any of the non-proprietary examples. Some examples may use a different command such as "dev" or "serve".
Module federation will work with any type of file that you're able to import, that Webpack understands how to process. It is not a JS only, or React only feature. Images, CSS, JSON, WASM, and anything else can be federated.
Companies using Module Federation
- Netflix
- Auth0
- Best Buy
- SAP
- AWS
- SemRush
- Ford Motor Company
- JPMorgan Chase
- Microsoft
- Lululemon
- Housing.com
- VMware
- Talkdesk
- Cricket Wireless
- Bytedance
- Rivian (in the cars themselves)
- Realtor.com
- FICO
- Digital Ocean
- Alibaba
- Tencent
- Wayfair
- RingCentral
- Indeed
- Telia
- Beamery
- Amazon
- Sony
- Paypal
- OVO Energy
- MGM
- Lowes
- Home Depot
- Epic Games
- ExpediaGroup
- Verizon
- MindTickle
- Experian
- Herodevs
- CloudFlare
- Cisco
- Business Insider
- Box.com
- AfterPay
- OLX
- Shopify
- adidas
- Zomato
- PayNet (Payments Network Malaysia)
- and many more I cant remember
Contribution to this repo
You decided to contribute to this project? Great, thanks a lot for pushing it!
Top Related Projects
A bundler for javascript and friends. Packs many modules into a few bundled assets. Code Splitting allows for loading parts of the application on demand. Through "loaders", modules can be CommonJs, AMD, ES6 modules, CSS, Images, JSON, Coffeescript, LESS, ... and your custom stuff.
The React Framework
Next generation frontend tooling. It's fast!
Next-generation ES module bundler
The zero configuration build tool for the web. 📦🚀
Dynamic ES module loader
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot