Skip to content

Commit f46cdd6

Browse files
authored
feat: support shared tree shaking (#4084)
Co-authored-by: --global <--local>
1 parent 2843ab2 commit f46cdd6

File tree

256 files changed

+13637
-4242
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

256 files changed

+13637
-4242
lines changed

.changeset/brave-beans-exist.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/modern-js': minor
3+
---
4+
5+
fix(modernjs)!: ship .mjs files for correct ESM output

.changeset/dry-colts-cough.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/storybook-addon': patch
3+
---
4+
5+
prefer use `stroybook/internal/node-logger` as logger
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: E2E Test for Shared Tree Shaking Demo
2+
3+
on:
4+
workflow_call:
5+
6+
jobs:
7+
e2e-runtime:
8+
runs-on: ubuntu-latest
9+
timeout-minutes: 30
10+
steps:
11+
- name: Checkout Repository
12+
uses: actions/checkout@v5
13+
with:
14+
fetch-depth: 0
15+
16+
- name: Cache Tool Downloads
17+
uses: actions/cache@v4
18+
with:
19+
path: ~/.cache
20+
key: ${{ runner.os }}-toolcache-${{ hashFiles('pnpm-lock.yaml') }}
21+
restore-keys: |
22+
${{ runner.os }}-toolcache-
23+
24+
- name: Set Playwright cache status
25+
run: |
26+
if [ -d "$HOME/.cache/ms-playwright" ] || [ -d "$HOME/.cache/Cypress" ]; then
27+
echo "PLAYWRIGHT_CACHE_HIT=true" >> "$GITHUB_ENV"
28+
else
29+
echo "PLAYWRIGHT_CACHE_HIT=false" >> "$GITHUB_ENV"
30+
fi
31+
32+
- name: Install Pnpm
33+
run: |
34+
corepack prepare pnpm@8.11.0 --activate
35+
corepack enable
36+
37+
- name: Setup Node.js 18
38+
uses: actions/setup-node@v5
39+
with:
40+
node-version: '18'
41+
cache: 'pnpm'
42+
43+
- name: Set Nx SHA
44+
uses: nrwl/nx-set-shas@v3
45+
46+
- name: Set SKIP_DEVTOOLS_POSTINSTALL environment variable
47+
run: echo "SKIP_DEVTOOLS_POSTINSTALL=true" >> "$GITHUB_ENV"
48+
49+
- name: Install Dependencies
50+
run: pnpm install
51+
52+
- name: Install Cypress
53+
run: npx cypress install
54+
55+
- name: Run Build for All
56+
run: npx nx run-many --targets=build --projects=tag:type:pkg
57+
58+
- name: Run condition check script
59+
id: check-ci
60+
run: node tools/scripts/ci-is-affected.mjs --appName=shared-tree-shaking-with-server-host
61+
62+
- name: E2E Test for Shared Tree Shaking Demo - Mode runtime-infer
63+
if: steps.check-ci.outcome == 'success'
64+
run: npx kill-port --port 3001,3002 && nx run shared-tree-shaking-no-server-host:test:e2e && lsof -ti tcp:3001,3002 | xargs kill
65+
66+
- name: E2E Test for Shared Tree Shaking Demo - Mode server-calc
67+
if: steps.check-ci.outcome == 'success'
68+
run: npx kill-port --port 3001,3002,3003 && nx run shared-tree-shaking-with-server-host:test:e2e && lsof -ti tcp:3001,3002,3003 | xargs kill

apps/3000-home/next.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const nextConfig = {
5959
}),
6060
);
6161
config.plugins.push({
62-
name: 'xxx',
62+
name: 'disable-devtool',
6363
apply(compiler) {
6464
compiler.options.devtool = false;
6565
},

apps/3001-shop/next.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ const nextConfig = {
5050
},
5151
}),
5252
);
53+
config.plugins.push({
54+
name: 'disable-devtool',
55+
apply(compiler) {
56+
compiler.options.devtool = false;
57+
},
58+
});
5359
return config;
5460
},
5561
};

apps/3002-checkout/next.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ const nextConfig = {
5050
},
5151
}),
5252
);
53+
config.plugins.push({
54+
name: 'disable-devtool',
55+
apply(compiler) {
56+
compiler.options.devtool = false;
57+
},
58+
});
5359
return config;
5460
},
5561
};

apps/modernjs-ssr/nested-remote/test.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

apps/shared-tree-shaking/README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Shared Treeshaking Demos
2+
3+
Two Module Federation demo apps that showcase tree‑shaking for shared dependencies:
4+
5+
- `apps/shared-tree-shaking/no-server`: no external snapshot service; uses the `runtime-infer` mode to detect used exports.
6+
- `apps/shared-tree-shaking/with-server`: integrates “re‑shake” build artifacts (snapshots) and uses the `server-calc` mode to prune shared packages precisely.
7+
8+
## Project Layout
9+
10+
- `no-server/host` – consumer (Host)
11+
- `no-server/provider` – producer (Provider)
12+
- `with-server/host` – consumer (with re‑shake flow)
13+
- `with-server/provider` – producer
14+
15+
Example dependencies: `antd@6.0.1`, `react@18.3.x`.
16+
17+
## Quick Start (no‑server)
18+
19+
1. Install dependencies (at repo root)
20+
21+
- `pnpm i`
22+
23+
2. Build and serve the Provider (start the remote first)
24+
25+
- Build: `nx run shared-tree-shaking-no-server-provider:build`
26+
- Serve: `nx run shared-tree-shaking-no-server-provider:serve`
27+
- Default: `http://localhost:3002/`
28+
29+
3. Build and serve the Host (local dev)
30+
31+
- Build: `nx run shared-tree-shaking-no-server-host:build`
32+
- Serve: `nx run shared-tree-shaking-no-server-host:serve`
33+
- Default: `http://localhost:3001/`
34+
35+
4. Verify the page and the shared dependency
36+
37+
- Visit `http://localhost:3001/`; the Remote and Consumer content should render.
38+
- In the browser console, inspect the shared module:
39+
- `__FEDERATION__.__SHARE__[hostId].default['antd'][version].lib()`
40+
- Sample keys (based on your build): `hostId = 'mf_host:0.1.34'`, `version = '6.0.1'`.
41+
- With the `runtime-infer` mode, `lib()` initially returns a pruned component set (e.g., `Button/Divider/Space/Switch`).
42+
43+
5. Simulate a “full fallback” (no tree shaking)
44+
45+
- In the console: `localStorage.setItem('calc', 'no-use')`
46+
- Refresh the page and call `lib()` again; it should return the full component list (no pruning).
47+
48+
## Advanced Flow (with‑server)
49+
50+
> `nx run shared-tree-shaking-with-server-host:serve:all` to start all servers (Host, Provider, re‑shake static server).
51+
52+
This flow produces “re‑shake” artifacts and serves them via a URL. The Host loads the snapshot and prunes shared packages using the `server` mode.
53+
54+
1. Produce re‑shake artifacts for the Host
55+
56+
- `nx run shared-tree-shaking-with-server-host:build-re-shake`
57+
58+
2. Serve the re‑shake directory
59+
60+
- `nx run shared-tree-shaking-with-server-host:serve-re-shake`
61+
- Default: `http://localhost:3003/`, e.g., `/independent-packages/antd/xxx.js`
62+
63+
3. Configure the snapshot entry in the Host
64+
65+
- Open `apps/shared-tree-shaking/with-server/host/runtimePlugin.ts`
66+
- Set `secondarySharedTreeShakingEntry` to the URL above, e.g.:
67+
- `http://localhost:3003/independent-packages/antd/antd_mf_host.<hash>.js`
68+
69+
4. Build and serve the Provider
70+
71+
- Build (trigger tree‑shaking): `nx run shared-tree-shaking-with-server-provider:build`
72+
- Serve: `nx run shared-tree-shaking-with-server-provider:serve` (default `3002`)
73+
74+
5. Build and serve the Host
75+
76+
- `nx run shared-tree-shaking-with-server-host:build` (default `3001`)
77+
- Serve: `nx run shared-tree-shaking-with-server-host:serve` (default `3001`)
78+
79+
6. Check the loaded lib
80+
81+
- Refresh the page and check `lib()`; it should return a much smaller export subset (e.g., only 4–5 components).
82+
83+
## Cypress E2E (no‑server Host)
84+
85+
- Run from the Host root:
86+
- `nx run shared-tree-shaking-no-server-host:test:e2e`
87+
88+
## Cypress E2E (with-server Host)
89+
90+
- Run from the Host root:
91+
- `nx run shared-tree-shaking-with-server-host:test:e2e`
92+
93+
## FAQ
94+
95+
- Ports: Provider `3002`, Host `3001`, re‑shake static server `3003`.
96+
- Keys and versions: `__FEDERATION__.__SHARE__` keys contain `:` or `-`; use bracket notation (e.g., `['mf_host:0.1.34']`, `['ui-lib']`).
97+
- Console example:
98+
- `__FEDERATION__.__SHARE__['mf_host:0.1.34'].default['antd']['6.0.1'].lib()`
99+
- If you use Nx, you can also run `nx build/serve` for the projects; commands must match actual project names.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
strict-peer-dependencies=false
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# modernjs-ssr-nested-remote

0 commit comments

Comments
 (0)