0Pricing
Micro Frontends Architecture with Module Federation · 课时

配置共享依赖

了解 Module Federation 中的 shared 选项如何让宿主应用和远程应用共享 React 等库,从而避免重复下载和版本冲突。

配置共享依赖 是 CoddyKit 上的免费 Micro Frontends Architecture with Module Federation 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Micro Frontends Architecture with Module Federation 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Micro Frontends Architecture with Module Federation 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

The Duplication Problem

Without coordination, a host and each remote would each bundle their own copy of React, Lodash, and other libraries. Users would download the same code many times.

The shared option solves this.

What shared Does

The shared key in ModuleFederationPlugin tells webpack which dependencies should be loaded once and reused across federated apps at run time.

Basic shared Syntax

The simplest form lists package names as an array. Webpack then negotiates a single shared instance at run time.

new ModuleFederationPlugin({
  name: "host",
  shared: ["react", "react-dom"]
});

Object Form for Options

The object form gives fine-grained control per dependency, such as marking it required or singleton.

shared: {
  react: { singleton: true, requiredVersion: "^18.0.0" },
  "react-dom": { singleton: true }
}

The singleton Option

singleton: true forces every app to use ONE instance of the library. This is essential for React, whose hooks break if two copies load at once.

requiredVersion and Negotiation

Each app declares a requiredVersion. At run time Module Federation picks the highest version that satisfies everyone. If versions are incompatible, it warns or falls back.

shared: {
  react: { singleton: true, requiredVersion: "^18.2.0" }
}

The eager Option

eager: true bundles the shared dependency into the initial chunk instead of loading it asynchronously. Useful for the host shell but increases initial bundle size.

shared: {
  react: { singleton: true, eager: true }
}

Reading from package.json

To avoid hardcoding versions, you can derive them from your dependencies, keeping shared config in sync with what you actually install.

const deps = require("./package.json").dependencies;
shared: {
  react: { singleton: true, requiredVersion: deps.react }
}

strictVersion

strictVersion: true makes Module Federation throw instead of falling back when no compatible version is found — surfacing mismatches early during testing.

Common Pitfalls

Watch out for:

  • Forgetting singleton on React, causing hook errors
  • Mismatched major versions across remotes
  • Overusing eager, bloating the first load

Verifying Sharing Works

Open the browser network tab: if React loads only once across host and remotes, sharing is working. The webpack build log also reports shared-module resolution.

Quick Check

Test your shared-dependency knowledge.

Recap

You configured shared dependencies:

  • shared avoids duplicate libraries across apps
  • Use the object form for per-package control
  • singleton is critical for React and React-DOM
  • requiredVersion drives run-time negotiation
  • Use eager and strictVersion deliberately

Good sharing keeps federated apps fast and conflict-free.

常见问题解答

「配置共享依赖」课时是免费的吗?

是的 — 「配置共享依赖」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Micro Frontends Architecture with Module Federation 课程的其余内容,请升级到 CoddyKit PRO。 Micro Frontends Architecture with Module Federation 课程共包含 4 节课。

「配置共享依赖」这节课中我会学到什么?

了解 Module Federation 中的 shared 选项如何让宿主应用和远程应用共享 React 等库,从而避免重复下载和版本冲突。 你通过在浏览器中直接运行的动手代码来练习 Micro Frontends Architecture with Module Federation,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Micro Frontends Architecture with Module Federation 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Micro Frontends Architecture with Module Federation 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「配置共享依赖」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Micro Frontends Architecture with Module Federation 课中编写并运行代码吗?

能。每节 Micro Frontends Architecture with Module Federation 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Webpack 基础回顾
  2. 模块联邦入门
  3. 宿主应用与远程应用
  4. 配置共享依赖
← 返回 Micro Frontends Architecture with Module Federation