Support Shared Tree Shaking #4302
2heal1
started this conversation in
Show and tell
Replies: 1 comment 1 reply
-
|
Looking forward to this, wondering if there's an expected release date? Thanks for your work! |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
How It Works
Shared dependency tree shaking can be summarized as a closed-loop pipeline: “collect used exports → compute a global set → secondary build → runtime on-demand loading”.
The problem it addresses is: in Module Federation shared-dependency scenarios, you typically want both high reuse (less duplicate downloading) and smaller shared bundles (less unused code). By persisting “which exports are actually used” (via
usedExports) as build metadata and driving a secondary build from it, the shared dependency can stay reusable while being as small as possible.Below is the process broken down into 4 stages.
Diagram
Stage 1: Build assets
In this stage you build your apps as usual, and for shared dependencies with tree shaking enabled you produce:
usedExports), e.g.Badge,Progressin the diagram.Stage 2: Calculate the dependency set
The goal of this stage is to determine: “within the current system scope, which exports of a given shared dependency must be kept?”
In the diagram, Consumer A depends on Provider. Using the manifest/stats generated in Stage 1, you can infer the set of exports it needs from the shared dependency (e.g. antd):
Avatar,Badge,Calendar,QRCode,Modal,Progress, etc.Depending on whether you have a centralized Deploy Server, there are typically two approaches:
usedExportsfrom all apps for the same shared dependency, computes a global “minimum union”, and generates an optimized artifact that all consumers can reuse.Stage 3: Secondary build (shared dependency)
In this stage you run an isolated secondary build for the shared dependency based on the export set computed in Stage 2, producing a shared bundle that is smaller and still reusable.
usedExportsto keep (the Stage 2 result).usedExportsin your local MF config. In the Consumer A example, the “missing” exports to include are the purple-highlighted modules:Calendar,QRCode.Stage 4: Runtime on-demand loading
This stage happens at runtime. The consumer app decides which shared resource to load based on whether tree shaking is enabled and whether it has the guidance metadata for the secondary artifact (e.g. related fields in Snapshot/manifest):
Beta Was this translation helpful? Give feedback.
All reactions