共享依赖与版本管理
了解 Module Federation 如何在微前端之间共享库,单例和版本协商如何工作,以及防止资源包重复和运行时故障的最佳实践。
共享依赖与版本管理 是 CoddyKit 上的免费 Micro Frontends Architecture with Module Federation 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Micro Frontends Architecture with Module Federation 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Micro Frontends Architecture with Module Federation 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Sharing Matters
In a Module Federation setup every remote can bundle its own copy of react, react-dom, or a design system. Without coordination, a single page may load three copies of React.
Shared dependencies let independently deployed apps agree on a single instance of a library at runtime, cutting payload and avoiding state bugs.
The shared Key
Both host and remotes declare a shared block in their ModuleFederationPlugin config. Webpack then negotiates which version actually loads.
new ModuleFederationPlugin({
name: 'host',
shared: {
react: { singleton: true },
'react-dom': { singleton: true }
}
})Singletons Explained
singleton: true forces one and only one copy across the whole app. This is essential for libraries that hold internal state, like React (hooks) or a router.
Without it, two React instances cause the dreaded Invalid hook call error.
Version Negotiation
Each shared module declares a requiredVersion. At load time Module Federation picks the highest compatible version among all that are offered.
shared: {
react: {
singleton: true,
requiredVersion: '^18.2.0'
}
}Strict Version Mismatch
When a remote needs a version incompatible with the loaded singleton, Module Federation logs a warning and uses the existing one. With strictVersion: true it instead throws, surfacing the mismatch early.
shared: {
react: { singleton: true, strictVersion: true, requiredVersion: '18.2.0' }
}Eager vs Lazy Sharing
By default shared modules load asynchronously, which requires a bootstrap entry. Setting eager: true bundles the dependency into the initial chunk so no async boundary is needed.
Use eager only for the host shell; eager everywhere defeats the purpose of sharing.
shared: {
react: { singleton: true, eager: true }
}The bootstrap Pattern
Because shared modules resolve asynchronously, the standard pattern splits the entry into index.js (just an import) and bootstrap.js (the real app). This defers execution until the share scope is ready.
// index.js
import('./bootstrap');
// bootstrap.js
import App from './App';
// ... mount AppSharing a Design System
A shared component library should usually be a singleton too, so theming context and CSS-in-JS instances are not duplicated.
shared: {
'@acme/ui': { singleton: true, requiredVersion: '^3.0.0' }
}Auto-Sharing from package.json
Instead of listing versions by hand, you can pass an array and let the plugin read versions from package.json. Tools like the Module Federation Enhanced plugin can even auto-share all dependencies.
shared: ['react', 'react-dom', 'react-router-dom']Diagnosing Duplicate Copies
To confirm sharing works, inspect the Network tab: you should see one vendor chunk for the shared lib. Bundle analyzers and the runtime share scope (__webpack_share_scopes__.default) help debug.
Best Practices Summary
- Singleton for stateful libs (React, router, stores).
- Set
requiredVersionto align teams. - Keep
eageronly on the host shell. - Audit bundles regularly for accidental duplicates.
Quick Check
Which option ensures only one instance of React loads across all micro frontends?
Recap
You learned how Module Federation shares dependencies: singletons for stateful libs, version negotiation via requiredVersion/strictVersion, eager vs lazy loading, and the bootstrap pattern. Proper sharing slashes bundle size and prevents subtle runtime bugs.
常见问题解答
「共享依赖与版本管理」课时是免费的吗?
是的 — 「共享依赖与版本管理」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Micro Frontends Architecture with Module Federation 课程的其余内容,请升级到 CoddyKit PRO。 Micro Frontends Architecture with Module Federation 课程共包含 4 节课。
「共享依赖与版本管理」这节课中我会学到什么?
了解 Module Federation 如何在微前端之间共享库,单例和版本协商如何工作,以及防止资源包重复和运行时故障的最佳实践。 你通过在浏览器中直接运行的动手代码来练习 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 反馈 — 无需本地设置。
此课程中的所有课时
- 单体仓库与多仓库
- 组织挑战与解决方案
- 微前端与联邦化的未来
- 共享依赖与版本管理