🚀React Micro Frontend using Module Federation and Vite
🚀 Introducing React Micro Frontend using Module Federation and Vite: A Game-Changer! 🎉
📣 Hey LinkedIn fam! I've got a story to share with you today that will change the way you think about building web applications. It's all about my recent experience with React Micro Frontend using Module Federation and Vite, and let me tell you, it was a game-changer! 💥
💡 Picture this: I was working on a large-scale e-commerce platform that required different teams to work on separate sections of the website. Each team had its own set of features and dependencies, and coordinating the development efforts was becoming a nightmare. It felt like we were constantly tangled in a web of code! 😩
🔍 That's when I discovered React Micro Frontend, a powerful concept that promised to solve our development woes. It allowed us to break down our monolithic frontend into smaller, self-contained micro frontends that could function independently while seamlessly communicating with each other. It was the light at the end of the tunnel! 🌟
🏗️ Let me guide you through a real-world example of setting up and running React Micro Frontend using Module Federation and Vite:
1️⃣ Install dependencies:
npm install --save react react-dom
2️⃣ Create a new React app using Vite:
npx create-vite@latest my-app --template react
3️⃣ Configure Module Federation in your vite.config.js file:
Recommended by LinkedIn
import { defineConfig } from 'vite'
import reactRefresh from '@vitejs/plugin-react-refresh';
import { VitePluginModuleFederation } from 'vite-plugin-module-federation';
export default defineConfig({
plugins: [
reactRefresh(),
VitePluginModuleFederation({
remotes: {
'remote-app': 'remote-app@http://localhost:3001/remoteEntry.js',
},
}),
],
});
;
4️⃣ Create your micro frontend module using React and export it as a remote: In your remote application's source code:
import React from 'react'
const ProductCard = () => {
// Your micro frontend component code for displaying a product card
};
export default ProductCard;
;
5️⃣ Import the remote module in your host application and render it: In your host application's source code:
import React from 'react'
import { loadRemoteEntry, loadRemoteModule } from 'remote-app';
const HostApp = () => {
const [ProductCard, setProductCard] = React.useState(null);
React.useEffect(() => {
loadRemoteEntry('http://localhost:3001/remoteEntry.js').then(() => {
loadRemoteModule('remote-app', './ProductCard').then((module) => {
setProductCard(module.default);
});
});
}, []);
return ProductCard ? <ProductCard /> : null;
};
export default HostApp;
;
🚀 And there you have it! You've just set up a React Micro Frontend architecture using Module Federation and Vite. In this example, the host application imports the remote module (ProductCard) from the remote-app. This allows you to develop and deploy the remote-app independently and have it seamlessly integrated into the host application. 🙌
🌐 Feel free to visit my profile to learn more about my journey and explore the exciting projects I've been working on. And don't forget to interact with this post—I'm here to answer your questions and geek out over all things React! Let's take our frontend development to new heights together! ✨
Nice post! The latest vite module federation seems to have issue with the shared dependencies. They are fetched multiple times