Frontend & CSS 4 min read 0 reads

Micro-Frontends with Module Federation: Enterprise Architecture, Routing & Shared State

A comprehensive guide to scaling enterprise frontend architectures. Learn Webpack & Vite Module Federation, runtime dependency sharing, and micro-frontend state isolation.

Quick Takeaways & Overview

A comprehensive guide to scaling enterprise frontend architectures. Learn Webpack & Vite Module Federation, runtime dependency sharing, and micro-frontend state isolation.

The Scaling Limits of Frontend Monoliths

As software organizations grow from ten engineers to hundreds across multiple independent feature teams, monolithic frontend repositories experience severe friction: pull request queues bottleneck, build and test suites take forty minutes in CI/CD pipelines, and a single broken dependency in an unrelated feature can prevent the entire organization from deploying to production.

Micro-Frontends apply the proven architectural principles of backend microservices to the frontend layer: breaking a massive single-page application into autonomous, independently deployable web applications that collaborate at runtime to form a cohesive user interface. With the advent of Module Federation (native in Webpack 5 and modern Vite plugins), micro-frontends can share dependencies dynamically without runtime performance degradation.


1. Module Federation Mechanics: Host and Remotes

Historically, frontend teams attempted micro-frontends using <iframe> elements (which suffer from terrible accessibility, SEO penalties, and visual styling friction) or independent npm packages (which require redeploying the entire parent application every time a package version bumps).

Module Federation enables an application to dynamically import JavaScript code from another completely independent application deployed at a different URL at runtime, while sharing common singleton dependencies (like react, react-dom, or global design tokens):

// Host Application (webpack.config.js)
const { ModuleFederationPlugin } = require('webpack').container;

module.exports = {
  plugins: [
    new ModuleFederationPlugin({
      name: 'host_shell',
      remotes: {
        analyticsApp: 'analyticsApp@https://analytics.hifitoolkit.com/remoteEntry.js',
        editorApp: 'editorApp@https://editor.hifitoolkit.com/remoteEntry.js'
      },
      shared: {
        react: { singleton: true, requiredVersion: '^18.2.0', eager: false },
        'react-dom': { singleton: true, requiredVersion: '^18.2.0', eager: false }
      }
    })
  ]
};

Consuming a Federated Remote Component in React:

// Host Shell Component consuming a federated remote seamlessly
import React, { Suspense } from 'react';

// Remote container imported dynamically across the network!
const RemoteAnalyticsDashboard = React.lazy(() => import('analyticsApp/Dashboard'));

export function MainLayout() {
  return (
    <div className="app-shell-layout">
      <h1>HiFi Toolkit Enterprise Console</h1>
      <Suspense fallback={<div className="p-4">Loading remote analytics module...</div>}>
        <RemoteAnalyticsDashboard tenantId="ten_4920" />
      </Suspense>
    </div>
  );
}

2. Managing Cross-Application State & CSS Isolation

State Management Without Coupling:

Avoid sharing a monolithic global state store (such as a single Redux or Zustand store) across micro-frontend boundaries because it introduces tight runtime coupling. Instead, adopt an Event-Driven Pub/Sub model using standard browser CustomEvent APIs or lightweight event emitters:

// Decoupled Cross-Micro-Frontend Event Dispatcher
export function dispatchGlobalAppEvent(eventName: string, payload: any) {
  const event = new CustomEvent(`hifi:${eventName}`, { detail: payload });
  window.dispatchEvent(event);
}

// Any micro-frontend can listen without direct package imports:
window.addEventListener('hifi:theme_changed', (e: any) => {
  applyLocalTheme(e.detail.theme);
});

CSS Isolation & Collision Defense:

Global CSS classes can easily leak across micro-frontends, leading to catastrophic visual bugs. Enforce strict styling isolation using:

  • CSS Modules: Compiles class names into unique scoped hashes (e.g., styles_button__a8f3d).
  • Tailwind CSS Prefixing: Configure unique prefixes in tailwind.config.js (e.g., prefix: 'tw-analytics-').
  • Shadow DOM Encapsulation: Mount micro-frontend components inside native Shadow DOM boundaries where styles cannot escape or enter.

Frequently Asked Questions (FAQs)

Does Module Federation cause duplicate downloads of React or common libraries?

No. When configured with singleton: true, the Module Federation runtime negotiates semver version compatibility between the host and remote. If both require compatible versions of React (e.g., ^18.2.0), the browser downloads React exactly once and shares the instance in memory.

When should an engineering team NOT adopt micro-frontends?

If your engineering team has fewer than twenty developers working in a single unified codebase, micro-frontends introduce unnecessary deployment complexity, networking overhead, and orchestration friction. A well-structured modular monolith is almost always the superior choice for early-stage and medium-sized teams.

P
Written By

Priya Patel

Senior engineering contributor at HiFi Toolkit. Writing comprehensive deep dives on software architecture, high-throughput backend APIs, Next.js performance tuning, and cryptographic standards.