0Pricing
Micro Frontends Architecture with Module Federation · บทเรียน

รู้จัก Module Federation

ทำความเข้าใจแนวคิดหลักของ Module Federation และวิธีที่ช่วยให้แอปพลิเคชันเปิดเผยและเรียกใช้โมดูลแบบไดนามิกได้

รู้จัก Module Federation เป็นบทเรียน Micro Frontends Architecture with Module Federation ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส 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 ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Micro Frontends Architecture with Module Federation หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Micro Frontends Architecture with Module Federation บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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