惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
PCI Perspectives
PCI Perspectives
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
H
Heimdal Security Blog
S
Security @ Cisco Blogs
N
News | PayPal Newsroom
J
Java Code Geeks
罗磊的独立博客
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic
V
V2EX
WordPress大学
WordPress大学
Google Online Security Blog
Google Online Security Blog
N
News and Events Feed by Topic
www.infosecurity-magazine.com
www.infosecurity-magazine.com
月光博客
月光博客
AI
AI
小众软件
小众软件
The GitHub Blog
The GitHub Blog
MongoDB | Blog
MongoDB | Blog
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
美团技术团队
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Tailwind CSS Blog
S
Schneier on Security
博客园 - 三生石上(FineUI控件)
F
Full Disclosure
B
Blog RSS Feed
Forbes - Security
Forbes - Security
S
SegmentFault 最新的问题
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
云风的 BLOG
云风的 BLOG
Jina AI
Jina AI
Cisco Talos Blog
Cisco Talos Blog
U
Unit 42
Project Zero
Project Zero
H
Hacker News: Front Page
Y
Y Combinator Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
The Cloudflare Blog
大猫的无限游戏
大猫的无限游戏
S
Secure Thoughts
The Hacker News
The Hacker News
Microsoft Azure Blog
Microsoft Azure Blog

Coding Your Life

lib库开发的一款PDF处理小工具 | Coding Your Life mirror-cli | Coding Your Life 最长递增子序列算法 | Coding Your Life webpack基础配置详解 | Coding Your Life canvas实现代码雨效果 | Coding Your Life 使用MessageChannel模拟React优先级执行队列 | Coding Your Life vue3 和 react 虚拟dom | Coding Your Life ES6中Reflect对象与Proxy结合实现代理和响应式编程 | Coding Your Life 前端技术分享MediaRecord实现运动相机 | Coding Your Life react基础概念 | Coding Your Life 微信小程序使用canvas创建像素头像 | Coding Your Life 前端基础概念 | Coding Your Life 中高级前端须注意的40条移动端H5坑位指南 | Coding Your Life native-code-push 热更新配置 | Coding Your Life Git 常用命令速查表 | Coding Your Life push-server 热更新常用命令速查表 | Coding Your Life 高效的js片段 | Coding Your Life
webpack5 模块联邦技术 | Coding Your Life
Sir_Liu · 2025-05-27 · via Coding Your Life

1. 项目准备

至少创建两个项目,分别作为容器应用(承载其他微应用)和远程应用(可被容器应用或其他应用引用) 。比如命名为 container - appremote - app

2. 安装依赖

在每个项目的根目录下,安装必要的 Webpack 相关依赖:

1
npm install webpack webpack-cli html - webpack - plugin webpack - dev - server - D

这些是开发时依赖,用于项目构建和开发服务器启动等功能。

3. 配置远程应用(以 remote - app 为例)

webpack.config.js 中进行如下配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const { ModuleFederationPlugin } = require('webpack').container;

module.exports = {

plugins: [
new ModuleFederationPlugin({
name: '远程应用名称',
filename: '远程入口文件名.js',
exposes: {

'./ModuleName': './src/具体模块路径',
},
shared: {


}
})
]
};

4. 配置容器应用(以 container - app 为例)

同样在 webpack.config.js 中配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const { ModuleFederationPlugin } = require('webpack').container;

module.exports = {

plugins: [
new ModuleFederationPlugin({
name: '容器应用名称',
remotes: {


},
shared: {


}
})
]
};

5. 构建应用

在远程应用和容器应用的项目目录下,分别运行构建命令:

1
npx webpack

构建完成后,远程应用会生成包含暴露模块信息的 remoteEntry.js 等文件 ;容器应用也会完成自身构建并准备好加载远程模块。

6. 在容器应用中使用远程模块

在容器应用的代码中,使用动态导入来加载远程模块:

1
2
3
4
5
6

async function loadRemoteModule() {
const remoteModule = await import('remoteAppAlias/./ModuleName');

remoteModule.default.someFunction();
}

7. 开发服务器运行(可选)

为方便开发调试,可在容器应用和远程应用中分别启动开发服务器:

1
npx webpack - dev - server

在实际项目中,还需考虑以下方面:

  • 路由配置:合理配置容器应用和远程应用的路由,确保模块加载和页面跳转正常 。比如使用 React Router 等路由库进行配置。
  • 错误处理:在模块加载过程中,处理可能出现的网络错误、模块未找到等异常情况 ,提高应用稳定性。
  • 版本管理:对于共享依赖,要仔细管理版本,避免版本冲突 。可在 shared 配置中明确依赖版本要求,或使用工具辅助版本管理。

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Coding Your Life