Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add module federation plugin #1764

Merged
merged 74 commits into from
Feb 20, 2025
Merged

Conversation

xusd320
Copy link
Contributor

@xusd320 xusd320 commented Jan 21, 2025

For module federation feature.

实现思路

MF 2.0 中提供了 bundler 无关的运行时 sdk @module-federation/webpack-bundler-runtime。bundler 中基于该 sdk 构建出所需产物及 MF 生产消费信息即可。

  1. Plugin modify_config 中添加 MF Container entry,并注入 @module-federation/webpack-bundler-runtime 依赖;
  2. Plugin load_transform 添加 container entry 的运行时初始化逻辑,及注册 MF 运行时插件
  3. Plugin resolve_id 将 import "${ remote }" resolve 成 Remote Module, 将 shared dependencies resolve 成 consume shared module;
  4. Plugin after_resolve 收集所有 shared dependencies;
  5. Plugin optimize_chunk 将所有使用 shared dependencies 添加进 container entry 的依赖中。原因是 MF exposed 不一定会用到某个 shared dependency, 但 MF exposed 所在的应用用到了,此时 shared 也是可以对外共享的,因此需要额外补充 container entry 到 shared dependency 的 edge;
  6. Plugin after_optimize_chunk_options 使得 container entry 不参与 code splitting,因为 MF 的 remoteEntry 通常是单个 js 文件;
  7. Plugin runtime_plugins :
    • init_federation_runtime_options 根据构建时的 MF 配置生成运行时配置信息;
    • init_federation_runtime_remotes 根据 import "${remote}" 消费信息生成 federation.bundlerRuntimeOptions.remotes 运行时代码;
    • init_federation_runtime_consume 根据 import "${shared}" 消费信息生成 federation.bundlerRuntimeOptions.consume 运行时代码;
    • init_federation_runtime_sharing 根据 import "${shared}" 生产信息生成 federation.bundlerRuntimeOptions.shared 运行时代码;
    • export_federation_container 本质是类似 webpack 的 library export,但 mako 中目前只支持挂载到 window 全局变量的方式;
  8. Plugin generate_end 生成 mf-manifest.json。

This reverts commit d3105d9.
This reverts commit 6179982.
@xusd320 xusd320 changed the title [WIP] feat: add module federation plugin feat: add module federation plugin Feb 8, 2025
@xusd320 xusd320 force-pushed the feat/module-federation-plugin branch from b2190a9 to 5cd3a19 Compare February 10, 2025 03:20
@xusd320 xusd320 force-pushed the feat/module-federation-plugin branch from 5cd3a19 to ebc21d6 Compare February 10, 2025 03:20
@xusd320 xusd320 force-pushed the feat/module-federation-plugin branch from fd67d72 to e7c1f18 Compare February 20, 2025 03:05
@xusd320 xusd320 merged commit e957fcd into master Feb 20, 2025
23 checks passed
@xusd320 xusd320 deleted the feat/module-federation-plugin branch February 20, 2025 03:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant