0Pricing
Micro Frontends Architecture with Module Federation · Lektion

Module und Komponenten gemeinsam nutzen

Lernen Sie, Komponenten aus einer Remote-Anwendung bereitzustellen und sie in einer Host-Anwendung zu verwenden.

Module und Komponenten gemeinsam nutzen ist eine kostenlose Micro Frontends Architecture with Module Federation-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Micro Frontends Architecture with Module Federation-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Micro Frontends Architecture with Module Federation-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

Intro to Module Sharing

In this lesson, we'll dive into the heart of Micro Frontends: sharing code!

We'll learn how one application can "expose" its components or modules, and how another application can "consume" them. This is key to building truly independent yet collaborative federated apps.

Understanding Exposed Modules

An exposed module is a piece of code (like a React component, a utility function, or even a data store) that one Micro Frontend application decides to make available to other applications.

  • Think of it like publishing a library.
  • The application making it available is called the Remote Application.
  • It's configured within the Remote's Webpack setup.

How a Remote Exposes

To expose a module, you configure the ModuleFederationPlugin in your remote application's webpack.config.js. The exposes property is where the magic happens.

You define a public name for the module and point it to its local path.

const { ModuleFederationPlugin } = require('webpack').container;

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

Example: Our Shared Button

Let's imagine a simple React button component that our remote application wants to share. This component lives in ./src/components/Button.jsx.

It's just a regular React component until we expose it.

// src/components/Button.jsx
import React from 'react';

const Button = ({ onClick, children }) => {
  return (
    <button
      onClick={onClick}
      style={{
        padding: '10px 20px',
        backgroundColor: '#007bff',
        color: 'white',
        border: 'none',
        borderRadius: '5px',
        cursor: 'pointer'
      }}
    >
      {children}
    </button>
  );
};

export default Button;

Consuming Remote Modules

On the flip side, a remote module is a module that an application wants to use, but it's hosted by another federated application.

  • The application consuming it is called the Host Application.
  • The Host needs to know where to find the Remote Application's exposed modules.

How a Host Consumes

The host application also uses the ModuleFederationPlugin. Here, you define the remotes property, mapping a local alias to the remote application's entry point.

The entry point is typically remoteEntry.js, generated by the remote app's Webpack build.

const { ModuleFederationPlugin } = require('webpack').container;

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

Importing & Using Shared Components

Once configured, the host application can import the remote component as if it were a local module. Webpack (via Module Federation) handles the dynamic loading from the remote URL.

Try running this simple example:

// This simulates a host app importing a remote component.
// In a real setup, 'remoteApp/Button' would resolve to the
// component exposed by the 'remoteApp' through Module Federation.
import React from 'react';
import ReactDOM from 'react-dom/client';

// Imagine 'remoteApp/Button' is the component we exposed earlier
// This import is resolved by Webpack's Module Federation Plugin
const RemoteButton = React.lazy(() => import('remoteApp/Button'));

function App() {
  return (
    <div>
      <h1>Host Application</h1>
      <React.Suspense fallback={<div>Loading Remote Button...</div>}>
        <RemoteButton onClick={() => alert('Button Clicked!')}>
          Click Me From Remote!
        </RemoteButton>
      </React.Suspense>
    </div>
  );
}

// Full entry point for a runnable React app
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);

Behind the Scenes: Dynamic Loading

When the host application tries to import remoteApp/Button, Webpack doesn't look for it in the local node_modules. Instead, it uses the remotes configuration to:

  • Fetch the remoteEntry.js from the specified URL.
  • Load the exposed Button module from within that remote entry.
  • This happens dynamically at runtime, allowing independent deployments!

Best Practices for Sharing

When defining your exposes and remotes, keep these tips in mind:

  • Clear Naming: Use descriptive names for your exposed modules (e.g., ./UserProfileCard).
  • Consistent Aliases: Ensure your remote aliases in the host are easy to understand (e.g., marketingApp).
  • Relative Paths: Use relative paths (./src/...) for exposed modules within the remote app.

Test Your Understanding

You've learned how to expose and consume modules. Let's check your understanding of the core configuration!

Lesson Summary & Next Steps

Great job! You've learned the fundamental mechanics of sharing modules with Webpack Module Federation:

  • Exposing: Remote apps use exposes to make components public.
  • Consuming: Host apps use remotes to import and use those components.
  • This dynamic loading allows for independent development and deployment.

Next, we'll build a simple federated application from scratch, putting these concepts into practice!

Häufig gestellte Fragen

Ist die Lektion „Module und Komponenten gemeinsam nutzen“ kostenlos?

Ja — der vollständige Text von „Module und Komponenten gemeinsam nutzen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Micro Frontends Architecture with Module Federation-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Micro Frontends Architecture with Module Federation-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Module und Komponenten gemeinsam nutzen“?

Lernen Sie, Komponenten aus einer Remote-Anwendung bereitzustellen und sie in einer Host-Anwendung zu verwenden. Du übst Micro Frontends Architecture with Module Federation mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Micro Frontends Architecture with Module Federation zu starten?

Keine Vorkenntnisse erforderlich. Micro Frontends Architecture with Module Federation auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „Module und Komponenten gemeinsam nutzen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Micro Frontends Architecture with Module Federation-Lektion Code schreiben und ausführen?

Ja. Jede Micro Frontends Architecture with Module Federation-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Projekteinrichtung und Konfiguration
  2. Module und Komponenten gemeinsam nutzen
  3. Eine einfache föderierte App entwickeln
  4. Ihre föderierte App lokal ausführen und debuggen
← Zurück zu Micro Frontends Architecture with Module Federation