0Pricing
Micro Frontends Architecture with Module Federation · レッスン

Module Federation入門

Module Federationの基本的な考え方と、アプリケーションがモジュールを動的に公開・利用できる仕組みを理解します。

「Module Federation入門」はCoddyKit上の無料Micro Frontends Architecture with Module Federationレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMicro Frontends Architecture with Module Federation学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Micro Frontends Architecture with Module Federationコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

What is Module Federation?

Welcome! In this lesson, we'll dive into Module Federation, a powerful feature of Webpack that's key to building modern Micro Frontends.

It allows different JavaScript applications to share and consume code from each other dynamically at runtime.

The Monolith Problem

Traditionally, large web applications are built as a single, giant codebase – a monolith. This can lead to:

  • Slow development cycles
  • Difficulty scaling teams
  • Tight coupling between parts

Module Federation helps overcome these challenges by enabling a more modular approach.

Dynamic Code Sharing

The core idea of Module Federation is dynamic code sharing. Instead of bundling all code together at build time, it lets applications load pieces of code from other applications at runtime.

Think of it like an app saying, "I need this component, and I know where to get it from another running app!"

Key Players: Host & Remote

In a Module Federation setup, we have two main types of applications:

  • Remote Application: This app exposes its code (components, functions) for others to use.
  • Host Application: This app consumes or uses the code exposed by a remote application.

An app can be both a host and a remote simultaneously!

Exposing Modules: Remote Apps

A remote application uses Webpack's ModuleFederationPlugin to declare which parts of its code it wants to expose. These exposed modules become available for other applications to consume.

Here's a simplified example of how a remote app might expose a Button component:

/* webpack.config.js (Remote App) */
const { ModuleFederationPlugin } = require('webpack').container;

module.exports = {
  // ... other webpack config
  plugins: [
    new ModuleFederationPlugin({
      name: 'remoteApp',
      filename: 'remoteEntry.js',
      exposes: {
        './Button': './src/Button.js',
      },
    }),
  ],
};

Consuming Modules: Host Apps

A host application also uses the ModuleFederationPlugin to specify which remote applications it wants to consume modules from. It provides a URL where the remote's entry file (e.g., remoteEntry.js) can be found.

Here's how a host app might declare its intent to use remoteApp:

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

module.exports = {
  // ... other webpack config
  plugins: [
    new ModuleFederationPlugin({
      name: 'hostApp',
      remotes: {
        remoteApp: 'remoteApp@http://localhost:8081/remoteEntry.js',
      },
    }),
  ],
};

How it Works: The Entry File

When a host application starts, it doesn't immediately load all remote code. Instead, it first loads a tiny file from the remote called remoteEntry.js.

This file acts as a manifest, telling the host what modules the remote exposes and how to dynamically load them if needed. Code is fetched only when requested!

The Power of Independent Deployments

One of the biggest advantages of Module Federation is enabling independent deployments.

Teams can develop, test, and deploy their micro frontends separately without coordinating a large, synchronized release for the entire application. This speeds up delivery and reduces risk.

Sharing More Than UI

Module Federation isn't just for sharing UI components. You can expose and consume various types of code:

  • Utility functions
  • Data services
  • React hooks or similar logic
  • Shared styles or themes

This allows for robust code reuse across your federated applications.

Test Your Knowledge

Module Federation introduces new ways for applications to interact. Which of the following statements correctly describe the core concepts?

Module Federation Recap

Great job! You've grasped the fundamental idea of Module Federation.

  • It's a Webpack feature for dynamic code sharing.
  • Apps can be hosts (consuming) or remotes (exposing).
  • It enables independent deployments and efficient code reuse.

Next, we'll look deeper into how host and remote applications are set up!

よくある質問

「Module Federation入門」レッスンは無料ですか?

はい。「Module Federation入門」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Micro Frontends Architecture with Module Federationコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Micro Frontends Architecture with Module Federationコースには全4レッスンが含まれています。

「Module Federation入門」で何を学びますか?

Module Federationの基本的な考え方と、アプリケーションがモジュールを動的に公開・利用できる仕組みを理解します。 ブラウザで直接実行するハンズオンコードでMicro Frontends Architecture with Module Federationを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Micro Frontends Architecture with Module Federationを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのMicro Frontends Architecture with Module Federationは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「Module Federation入門」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このMicro Frontends Architecture with Module Federationレッスンでコードを書いて実行できますか?

はい。すべてのMicro Frontends Architecture with Module Federationレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Webpackの基礎を復習
  2. Module Federation入門
  3. ホストアプリケーションとリモートアプリケーション
  4. 共有依存関係の設定
← Micro Frontends Architecture with Module Federationに戻る