A modern micro-frontend e-commerce application built with Vite, Module Federation, React 18, and TypeScript.
This application demonstrates a production-ready micro-frontend architecture with:
- True Module Federation: Direct component sharing without iframes
- Independent Development: Each MFE can be developed and deployed separately
- Shared State Management: Event-driven communication between MFEs
- Modern Tooling: Vite for fast development and builds
- Type Safety: Full TypeScript support across all MFEs
vite-react-mfe/
βββ shell-app/ # Host application (routing, layout, global state)
βββ product-mfe/ # Product catalog and management
βββ cart-mfe/ # Shopping cart functionality
βββ payment-mfe/ # Payment processing and checkout
βββ profile-mfe/ # User profile management
βββ shared/ # Shared types, utilities, and event bus
- Application routing and navigation
- Global cart state management
- Shared layout (Header, Footer)
- Event orchestration between MFEs
- Dynamic MFE loading with Module Federation
- Product catalog display
- Product search and filtering
- Add to cart functionality
- Responsive product grid
- Shopping cart display
- Quantity management
- Item removal and updates
- Price calculations
- Order review and summary
- Payment form handling
- Payment processing (mocked)
- Order confirmation
- User profile display
- Personal information management
- Address and preferences
- Account statistics
- Frontend: React 18 + TypeScript
- Build Tool: Vite 4
- Module Federation: @originjs/vite-plugin-federation
- Routing: React Router v6
- State Management: Event-driven architecture
- Styling: CSS Modules with modern CSS
- Package Management: npm workspaces
- Node.js 18+ and npm
- Git
- Clone and install dependencies
git clone <repository-url>
chmod +x setup.sh
./setup.shOr manually:
npm run install:all- Build and start all MFEs
npm run preview:allThis will:
- Build all MFEs for production
- Start them in preview mode on their respective ports
- Enable Module Federation between MFEs
- Access the application Open http://localhost:5000 in your browser
For development with hot reload (note: Module Federation works better in preview mode):
npm run devYou can also build and preview individual MFEs:
# Build and preview specific MFE
cd product-mfe && npm run build && npm run preview
cd cart-mfe && npm run build && npm run preview
cd payment-mfe && npm run build && npm run preview
cd profile-mfe && npm run build && npm run preview
cd shell-app && npm run build && npm run previewMFEs communicate through a custom event bus system:
// Adding item to cart
CartEvents.addToCart(product, quantity);
// Updating cart quantity
CartEvents.updateQuantity(productId, newQuantity);
// Navigation between MFEs
NavigationEvents.navigate('/cart');
// Listening to cart changes
EventBus.subscribe('cart:state-changed', (cartState) => {
// Handle cart state update
});cart:add- Add item to cartcart:update- Update item quantitycart:remove- Remove item from cartcart:clear- Clear entire cartcart:state-changed- Cart state updatednavigate- Navigation between routes
Each MFE exposes its main component through Module Federation:
// vite.config.ts example
federation({
name: 'product-mfe',
filename: 'remoteEntry.js',
exposes: {
'./ProductApp': './src/ProductApp'
},
shared: ['react', 'react-dom']
})The Shell App consumes these remote modules:
// Shell App vite.config.ts
federation({
name: 'shell-app',
remotes: {
productMfe: 'http://localhost:5001/assets/remoteEntry.js',
cartMfe: 'http://localhost:5002/assets/remoteEntry.js',
// ... other remotes
}
})- Responsive design for all screen sizes
- Smooth animations and transitions
- Professional gradient themes
- Interactive components
- Code splitting with Module Federation
- Lazy loading of remote components
- Optimized bundle sizes
- Fast development with Vite HMR
- Full TypeScript support
- Shared type definitions
- Error boundaries for MFE failures
- Hot module replacement
- Workspace-based development
# Install all dependencies
npm run install:all
# Start all MFEs in development
npm run dev
# Start individual MFEs
npm run dev:shell
npm run dev:product
npm run dev:cart
npm run dev:payment
npm run dev:profile
# Build all MFEs for production
npm run build
# Preview production builds
npm run previewEach MFE can be deployed independently:
- Build the MFE
cd product-mfe
npm run build-
Deploy the dist folder to your hosting service
-
Update remote URLs in the Shell App's vite.config.ts
-
Deploy Shell App with updated remote URLs
- Better Performance: No iframe overhead
- Shared Dependencies: Reduced bundle sizes
- Better UX: Seamless navigation and interactions
- SEO Friendly: Single-page application benefits
- Faster Builds: Vite's esbuild-powered development
- Modern Tooling: Native ES modules support
- Better DX: Instant HMR and fast startup
- Error Boundaries: Each MFE wrapped in error boundaries
- Graceful Degradation: Fallback UI when MFEs fail to load
- Event Error Handling: Try-catch blocks around event dispatching
- Network Resilience: Retry mechanisms for failed module loads
The application includes built-in monitoring for:
- MFE load times and failures
- Event communication patterns
- User interaction flows
- Performance metrics
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License.
Built with β€οΈ using Vite + Module Federation