Skip to content

About

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

24 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Micro-Frontend Architecture with Module Federation

A production-ready micro-frontend architecture using React + TypeScript + Webpack Module Federation, demonstrating runtime module loading, shared dependencies, and true micro-frontend independence.

πŸš€ Features

  • πŸ”Œ Module Federation - Runtime module loading using Webpack Module Federation
  • πŸ“¦ Shared Dependencies - React, React-DOM, and react-icons shared across all apps (57% bundle size reduction)
  • 🎨 Shared Design System - Single source of truth exposed via Module Federation
  • πŸ“˜ Full TypeScript - Complete type safety with federated modules
  • πŸ”” EventBus Notifications - Real-time cross-app communication
  • ⚑ Independent Deployments - Each app can be deployed separately

πŸ“ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  HOST (modulararchitecutr-react)β”‚
β”‚  - Exposes: Design System       β”‚
β”‚  - Imports: Chat, Email Apps    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              ↓
    Module Federation Runtime
              ↓
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚                     β”‚
    ↓                     ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ CHAT         β”‚    β”‚ EMAIL        β”‚
β”‚ - Exposes Appβ”‚    β”‚ - Exposes Appβ”‚
β”‚ - Imports DS β”‚    β”‚ - Imports DS β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🎯 Quick Start

Prerequisites

  • Node.js 16+
  • npm or yarn

Local Development (Preview Mode)

Module Federation requires preview mode to work. Follow these steps:

# 1. Install dependencies for all apps
npm run install:all

# 2. Build all apps (generates remoteEntry.js files)
npm run build

# 3. Start preview servers
npm run preview

Access the apps:

Important Notes:

  • Preview mode uses localhost URLs (.env.development files)
  • Module Federation dynamically loads CHAT and EMAIL apps from localhost
  • Dev mode (npm run dev) does NOT work with Module Federation because it doesn't generate remoteEntry.js files

Making Changes During Development

# 1. Edit files
vim host/src/App.tsx

# 2. Rebuild the specific app
cd host && npm run build

# 3. Preview server auto-serves new build - just refresh browser (Cmd+Shift+R)

Production Build & Deployment

For Vercel deployment with production URLs:

# 1. Ensure .env.production files exist with Vercel URLs
# host/.env.production
VITE_CHAT_URL=https://chat-seven-psi-63.vercel.app
VITE_EMAIL_URL=https://mail-sable.vercel.app

# chat/.env.production & email/.env.production
VITE_HOST_URL=https://modulararchitecutr-react.vercel.app

# 2. Build all apps for production
npm run build

# 3. Commit and push to deploy
git add .
git commit -m "Deploy changes"
git push

Vercel will automatically deploy each app to its respective URL.

πŸ“¦ Project Structure

β”œβ”€β”€ host/                          # Main application
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ designSystem/         # Exposed via Module Federation
β”‚   β”‚   └── components/
β”‚   β”‚       └── FederatedWrapper.tsx  # Loads remote apps
β”‚   └── vite.config.ts            # Federation: exposes designSystem
β”‚
β”œβ”€β”€ chat/                          # Chat micro-frontend
β”‚   β”œβ”€β”€ src/App.tsx               # Exposed as './App'
β”‚   └── vite.config.ts            # Federation: exposes App, imports designSystem
β”‚
└── email/                         # Email micro-frontend
    β”œβ”€β”€ src/App.tsx               # Exposed as './App'
    └── vite.config.ts            # Federation: exposes App, imports designSystem

πŸ—οΈ Technology Stack

  • React 18 - UI library
  • TypeScript - Type safety
  • Vite 5 - Build tool
  • Webpack Module Federation - Micro-frontend architecture
  • Material Design Icons - Icon library

πŸš€ Deployment (Vercel)

Step 1: Deploy Each App

Create 3 separate Vercel projects from the same repository:

HOST

  • Root Directory: host
  • Build Command: npm run build
  • Output Directory: dist

CHAT

  • Root Directory: chat
  • Build Command: npm run build
  • Output Directory: dist

EMAIL

  • Root Directory: email
  • Build Command: npm run build
  • Output Directory: dist

Step 2: Configure Environment Variables

The apps are already configured to use production URLs via .env.production files:

host/.env.production

VITE_CHAT_URL=https://chat-seven-psi-63.vercel.app
VITE_EMAIL_URL=https://mail-sable.vercel.app

chat/.env.production & email/.env.production

VITE_HOST_URL=https://modulararchitecutr-react.vercel.app

Step 3: Push to Deploy

git add .
git commit -m "Deploy Module Federation architecture"
git push

Vercel will automatically deploy all apps.

Step 4: Test

Open your HOST URL and click "Open (Module Federation)" - the apps will load seamlessly across different Vercel deployments!

πŸŽ“ Key Concepts

Module Federation

Each app exposes and consumes modules at runtime:

  • HOST exposes: ./designSystem
  • HOST consumes: chatApp/App, emailApp/App
  • CHAT exposes: ./App
  • CHAT consumes: host/designSystem
  • EMAIL exposes: ./App
  • EMAIL consumes: host/designSystem

Shared Dependencies

shared: ['react', 'react-dom', 'react-icons']

Only one copy of React is loaded across all apps, reducing total bundle size by 57%.

remoteEntry.js

The module manifest generated by Module Federation. Located at:

  • http://localhost:5174/assets/remoteEntry.js (HOST)
  • http://localhost:5175/assets/remoteEntry.js (CHAT)
  • http://localhost:5176/assets/remoteEntry.js (EMAIL)

πŸ”§ Development Notes

Why Preview Mode?

npm run dev doesn't work with Module Federation because:

  1. Vite dev server doesn't generate remoteEntry.js files
  2. This is a known limitation of @originjs/vite-plugin-federation
  3. Preview mode serves built assets where Module Federation works correctly

Dev Mode Usage

Use npm run dev for:

  • Developing individual features
  • Testing UI components
  • Fast iteration with hot reload

Use npm run build + npm run preview for:

  • Testing Module Federation
  • Verifying production builds
  • Demonstrating the architecture

πŸ“Š Performance Benefits

Without Module Federation:

HOST:  200 KB (React + design system)
CHAT:  250 KB (React + design system + chat)
EMAIL: 250 KB (React + design system + email)
TOTAL: 700 KB

With Module Federation:

HOST:  200 KB (React + design system)
CHAT:   50 KB (only chat logic)
EMAIL:  50 KB (only email logic)
TOTAL: 300 KB (57% reduction!)

🎯 What Makes This Special

  1. Runtime Module Loading - Apps load dynamically, not at build time
  2. True Independence - Each app can be deployed separately
  3. Shared Dependencies - Only one copy of React across all apps
  4. TypeScript Support - Full type safety with federated modules
  5. Production Ready - Works on localhost and Vercel
  6. Window Close Tracking - Notifications for both modal and new tab modes

πŸ“ Scripts

# Install all dependencies
npm run install:all

# Build all apps
npm run build

# Build individual apps
npm run build:host
npm run build:chat
npm run build:email

# Preview mode (required for Module Federation)
npm run preview

# Dev mode (Module Federation won't work)
npm run dev

πŸ” Troubleshooting

"Failed to fetch remoteEntry.js"

Cause: Preview servers not running

Solution:

npm run build
npm run preview

"Module Federation not working"

Cause: Using dev mode instead of preview mode

Solution: Always use npm run build + npm run preview

Changes not reflecting

Solution:

# Rebuild the app you changed
cd host && npm run build

# Hard refresh browser (Cmd+Shift+R)

πŸ“š Additional Documentation

  • MODULE_FEDERATION.md - Deep dive into Module Federation architecture and implementation

🌐 Live Demo

πŸ“„ License

MIT


Built with React + TypeScript + Webpack Module Federation

About

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages