Skip to content

Repository files navigation

Micro-Frontend Monorepo Starter Framework

A production-grade starter framework for Micro-Frontends (MFE) powered by pnpm Workspaces, Turborepo, Module Federation 2.0 (@module-federation/vite), React 19, React Router v8, and Tailwind CSS v4.

Splits complex web applications into decoupled, independently deployable micro-frontends with live HMR, zero-touch dynamic routing, runtime URL overrides, type safety, and socket isolation.


πŸ“š Core Documentation

  • πŸ›οΈ Architecture Specification: Deep dive into Module Federation 2.0, Host Shell orchestration, route specificity sorting, and universal adapters.
  • πŸ’» Development & Workflows Guide: Local development workflows, targeted DX (pnpm dev:only), safe port guard, remote scaffolding (pnpm mfe:create), and repository garbage collection.
  • ⚑ Runtime Configuration & Event Architecture: Dynamic runtime remote URL injection (window.__MFE_RUNTIME_CONFIG__), type-safe cross-MFE event bus, and edge caching strategies.

πŸ—οΈ Architecture Overview

mfe/ (Root Workspace)
β”œβ”€β”€ .github/workflows/ci.yml       # GitHub Actions automated lint, typecheck, & build
β”œβ”€β”€ turbo.json                     # Turborepo task pipeline & caching graph
β”œβ”€β”€ remotes.manifest.json          # Single Source of Truth for all active micro-frontends
β”œβ”€β”€ scripts/                       # Orchestration, code generation, and integrity tools
β”‚   β”œβ”€β”€ orchestrate.js             # Dev and build concurrency coordinator
β”‚   β”œβ”€β”€ manifest.js                # Manifest validator and command builder
β”‚   β”œβ”€β”€ port-guard.js              # Safe listener-only socket inspector
β”‚   β”œβ”€β”€ generate-remotes-dts.js    # Zero-touch route registry & ambient type generator
β”‚   β”œβ”€β”€ create-remote.js           # Interactive / flag-based remote generator
β”‚   β”œβ”€β”€ garbage-collector.js       # Monorepo dead code and drift auditor
β”‚   β”œβ”€β”€ reset-demo.js              # Reset workspace to clean starter slate
β”‚   └── restore-demo.js            # Restore demo services snapshot
β”‚
β”œβ”€β”€ apps/                          # Deployable application containers & micro-frontends
β”‚   β”œβ”€β”€ host/                      # Primary Shell Container (Port 5000)
β”‚   β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”‚   β”œβ”€β”€ components/        # Shell UI (RemoteErrorBoundary, LoadingFallback)
β”‚   β”‚   β”‚   β”œβ”€β”€ pages/             # LandingPage dashboard and live event feed
β”‚   β”‚   β”‚   β”œβ”€β”€ plugins/           # runtimeRemoteOverride.js (MF 2.0 runtime plugin)
β”‚   β”‚   β”‚   β”œβ”€β”€ utils/             # resolveRemoteUrl.js & telemetry.js (APM hooks)
β”‚   β”‚   β”‚   β”œβ”€β”€ remotesRegistry.jsx# Auto-generated dynamic remote route registry
β”‚   β”‚   β”‚   β”œβ”€β”€ remotes.d.ts       # Auto-generated ambient TypeScript declarations
β”‚   β”‚   β”‚   β”œβ”€β”€ App.jsx            # Dynamic routing & error boundaries
β”‚   β”‚   β”‚   └── main.jsx           # Entry point (sets window.__IS_HOST__)
β”‚   β”‚   β”œβ”€β”€ vercel.json            # Host-specific edge routing & cache headers
β”‚   β”‚   └── vite.config.js         # Federation consumer configuration
β”‚   β”‚
β”‚   β”œβ”€β”€ marketing-mfe/             # Marketing Micro-Frontend (Port 5002)
β”‚   β”‚   β”œβ”€β”€ vercel.json            # Independent edge deployment rules
β”‚   β”‚   └── vite.config.js         # defineRemoteConfig preset
β”‚   β”‚
β”‚   └── auth-mfe/                  # Authentication Micro-Frontend (Port 5003)
β”‚       β”œβ”€β”€ vercel.json            # Independent edge deployment rules
β”‚       └── vite.config.js         # defineRemoteConfig preset
β”‚
β”œβ”€β”€ packages/                      # Shared libraries, adapters, and tools
β”‚   └── shared/                    # Core Library (@subrataroy100/mfe-shared)
β”‚       β”œβ”€β”€ src/
β”‚       β”‚   β”œβ”€β”€ adapters/          # UniversalRemoteMount, Shadow DOM, dual-mode mounts
β”‚       β”‚   β”œβ”€β”€ events/            # Scoped Event Bus, pure JS core, React hooks, event replay
β”‚       β”‚   β”œβ”€β”€ vite/              # defineRemoteConfig preset + CSS auto-injection
β”‚       β”‚   β”œβ”€β”€ components/        # Shared UI primitives (Button)
β”‚       β”‚   β”œβ”€β”€ constants/         # Shared ports, origins, and configuration
β”‚       β”‚   └── index.js           # Barrel export
β”‚       └── package.json           # Dual ESM/CJS exports with tracked .d.ts types
β”‚
β”œβ”€β”€ docs/                          # Architectural and operational guides
β”œβ”€β”€ netlify.toml                   # Root unified edge headers (preview deployments)
β”œβ”€β”€ vercel.json                    # Root unified edge headers (preview deployments)
β”œβ”€β”€ package.json                   # Root scripts and workspace devDependencies
└── pnpm-workspace.yaml            # Monorepo package directories (apps/*, packages/*)

⚑ Quick Start

1. Prerequisites

  • Node.js: >= 20.0.0
  • pnpm: 12.x (enforced via devEngines & packageManager)

2. Installation

Install all dependencies from the workspace root:

pnpm install

3. Start Development Environment

pnpm dev

This single command:

  1. Runs Safe Port Guard: Checks and frees orphaned background sockets on configured ports (5000, 5002, 5003...).
  2. Generates Route Registry & Types: Reads remotes.manifest.json and updates packages/host/src/remotesRegistry.jsx and remotes.d.ts.
  3. Boots Dev Servers Concurrently: Starts the host shell and each registered remote with live Vite HMR.

Open http://localhost:5000 in your browser to view the Host dashboard.


🎯 Key Features & Capabilities

1. Live Hot Module Replacement (HMR) for Remotes

Micro-frontends run standard Vite dev servers using Module Federation 2.0 (dev: { remoteHmr: true }). Edits to remote components appear instantly inside the running host application without requiring full rebuilds or browser reloads.

2. Targeted DX (pnpm dev:only)

Avoid running every remote simultaneously when working on a single feature:

# Boots only the Host Shell (port 5000) and marketingMfe (port 5002)
pnpm dev:only marketingMfe

3. Zero-Touch Dynamic Route Registry

No manual router editing is needed when adding or removing micro-frontends:

  • Manifest entries in remotes.manifest.json are automatically converted into lazy-loaded routes in remotesRegistry.jsx.
  • Specificity Sorting: Deeper and explicit routes (e.g., /marketing/features/*, /auth/*) are automatically mounted before shallow paths (/*), preventing wildcard hijacking.
  • Isolated Error Boundaries: Every remote route is wrapped in <RemoteErrorBoundary> and Suspense fallbacks. If a remote fails or throws an unhandled error, the Host navigation and sibling remotes remain completely unaffected.

4. Dynamic Runtime Remote URL Overrides (window.__MFE_RUNTIME_CONFIG__)

Deploy remotes independently to staging or CDN environments without rebuilding the host shell container:

<script>
  window.__MFE_RUNTIME_CONFIG__ = {
    marketingMfe: "https://staging-cdn.example.com/marketing/mf-manifest.json",
    authMfe: "https://staging-cdn.example.com/auth/mf-manifest.json"
  };
</script>

The Host's Module Federation 2.0 runtime plugin intercepts requests dynamically in the browser. Build-time defaults in .env (VITE_MARKETING_MFE_URL) are read automatically via loadEnv().

5. Decoupled Cross-MFE Event Bus

Micro-frontends communicate asynchronously without shared global stores:

import { createMfeEventBus } from "@subrataroy100/mfe-shared/events";

// Scoped event bus
const bus = createMfeEventBus({ sender: "authMfe", namespace: "auth" });
bus.send("user:login", { userId: "usr_100" });

// React Hook with automatic lifecycle unmount cleanup
bus.useListener("user:logout", (detail) => {
  console.log("Logged out:", detail);
});

6. One-Command Remote Scaffolding (pnpm mfe:create)

Scaffold a complete, fully connected micro-frontend with auto-allocated ports in one command:

pnpm mfe:create billingService --path /billing --framework react

πŸ› οΈ Monorepo Scripts Reference

Command Description
pnpm dev Runs port guard, generates registry & types, then launches host and remotes with live HMR.
pnpm dev:only <name> Targeted DX: Boots only the Host Shell and a specific Remote (e.g. pnpm dev:only marketingMfe).
pnpm mfe:create <name> Scaffolds a new remote package, auto-allocates port, updates manifest, and mounts routes.
pnpm clean:ports Safely detects and terminates orphaned listeners on configured MFE ports.
pnpm build Production build across all packages using Turborepo with outputs caching.
pnpm build:remotes Builds all remote micro-frontends using Turborepo (turbo run build --filter=!host).
pnpm build:host Builds the host shell container (turbo run build --filter=host).
pnpm remotes:list Prints a summary table of all active remotes configured in remotes.manifest.json.
pnpm typecheck Generates types and validates TypeScript definitions across workspaces and tests.
pnpm test Runs all unit and integration tests via Vitest.
pnpm test:watch Runs Vitest in interactive watch mode.
pnpm test:coverage Generates full test coverage report via @vitest/coverage-v8.
pnpm lint Runs oxlint across all workspace packages via Turborepo.
pnpm preview Serves the production build of the host shell.
pnpm reset:demo Resets workspace to clean starter slate (backs up demo remotes to .demo-backup/).
pnpm restore:demo Restores demo micro-frontends from .demo-backup/.
pnpm gc Runs the monorepo Garbage Collector to audit drift, dead code, and stale artifacts.
pnpm gc:check Non-destructive CI check for repo consistency and orphan configurations.
pnpm gc:fix Automatically remedies detected monorepo drift and cleans stale entries.

🌐 Production Deployment & HTTP Caching

For optimal CDN performance and instant updates:

  • remoteEntry.js / mf-manifest.json: Must never be cached (Cache-Control: no-cache, no-store, must-revalidate).
  • Hashed Assets (/assets/*): Cached permanently with max immutable lifetimes (Cache-Control: public, max-age=31536000, immutable).

Pre-configured headers are included for:


πŸ“„ License

MIT Β© Subrata Roy

About

Production-ready Micro-Frontend monorepo starter with Vite Module Federation, React 19, React Router v8, Tailwind CSS v4 & pnpm workspaces. Host + independent remotes, error boundaries, event bus, manifest-driven DX & CI.

Topics

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages