Skip to content

Commit 8f26d8b

Browse files
committed
feat: finish basic implementation
chore: add ut feat: done fix(modernjs)!: ship .mjs files for correct ESM output (#4240) fix: prefer use `stroybook/internal/node-logger` as logger (#4235) docs: remove invalid lark link (#4253) fix: manifest type
1 parent 7e19f00 commit 8f26d8b

File tree

175 files changed

+5451
-2821
lines changed

Some content is hidden

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

175 files changed

+5451
-2821
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

apps/shared-treeshake/README.md

Lines changed: 66 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,97 @@
1-
# shared-treeshake
1+
# Shared Treeshake Demos
22

3-
## How to start the demos ?
3+
Two Module Federation demo apps that showcase tree‑shaking for shared dependencies:
44

5-
### Basic
5+
- `apps/shared-treeshake/no-server`: no external snapshot service; uses the `infer` strategy to detect used exports.
6+
- `apps/shared-treeshake/with-server`: integrates “re‑shake” build artifacts (snapshots) and uses the `server` strategy to prune shared packages precisely.
67

7-
1. Build host and provider
8+
## Project Layout
89

9-
```bash
10-
# Root directory
11-
pnpm i
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
1214

13-
pnpm i -g serve
15+
Example dependencies: `[email protected]`, `[email protected]`.
1416

15-
nx build modern-js-plugin
17+
## Quick Start (no‑server)
1618

17-
nx build shared-treeshake-host
19+
1. Install dependencies (at repo root)
1820

19-
nx build shared-treeshake-provider
21+
- `pnpm i`
2022

21-
```
22-
2. Serve host and provider
23+
2. Build and serve the Provider (start the remote first)
2324

24-
```bash
25-
nx serve shared-treeshake-host
25+
- Build: `shared-treeshake-no-server-provider`
26+
- Serve: `nx run shared-treeshake-no-server-provider:serve`
27+
- Default: `http://localhost:3002/`
2628

27-
serve apps/shared-treeshake/provider/dist -C -p 3002
28-
```
29+
3. Build and serve the Host (local dev)
2930

30-
3. Visit page
31+
- Build: `shared-treeshake-no-server-host`
32+
- Serve: `nx run shared-treeshake-no-server-host:serve`
33+
- Default: `http://localhost:3001/`
3134

32-
open http://localhost:3001 , it will render success.
35+
4. Verify the page and the shared dependency
3336

34-
You can check the current loaded shared by executing `__FEDERATION__.__SHARE__["mf_host:0.1.34"].default.antd["4.24.15"].lib()` in browser console.
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 `infer` strategy, `lib()` initially returns a pruned component set (e.g., `Button/Divider/Space/Switch`).
3542

36-
It will show all antd components (fallback resources).
43+
5. Simulate a “full fallback” (no treeshake)
3744

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).
3847

39-
<!-- 4. Set localStorage to mock snapshot
48+
## Advanced Flow (with‑server)
4049

41-
```bash
42-
localStorage.setItem('calc','no-use')
43-
```
50+
This flow produces “re‑shake” artifacts and serves them via a URL. The Host loads the snapshot and prunes shared packages using the `server` strategy.
4451

45-
It will use the fallback resources.
52+
1. Produce re‑shake artifacts for the Host
4653

47-
5. Refresh the page (Use fallback)
54+
- `nx run shared-treeshake-with-server-host:build-re-shake`
4855

49-
Execute `__FEDERATION__.__SHARE__["mf_host:0.1.34"].default.antd["4.24.15"].lib()` in browser console.
56+
2. Serve the re‑shake directory
5057

51-
It will show export all components . -->
58+
- `nx run shared-treeshake-with-server-host:serve-re-shake`
59+
- Default: `http://localhost:3003/`, e.g., `/independent-packages/antd/xxx.js`
5260

53-
### Advanced
61+
3. Configure the snapshot entry in the Host
5462

55-
This is combined with deploy server , which can calculate the snapshot of shared resources.
63+
- Open `apps/shared-treeshake/with-server/host/runtimePlugin.ts`
64+
- Set `reShakeShareEntry` to the URL above, e.g.:
65+
- `http://localhost:3003/independent-packages/antd/antd_mf_host.<hash>.js`
5666

57-
In this demo , you can set `localStorage.setItem('calc','use')` to mock snapshot.
67+
4. Build and serve the Provider
5868

59-
First, need to re-shake the asset:
69+
- Build (trigger tree‑shaking): `nx run shared-treeshake-with-server-provider:build`
70+
- Serve: `nx run shared-treeshake-with-server-provider:serve` (default `3002`)
6071

61-
```bash
62-
nx build:re-shake shared-treeshake-host
63-
```
72+
5. Build and serve the Host
6473

65-
Second, serve it(`serve apps/shared-treeshake/host/dist-test -C -p 3003` ) and update the `reShakeShareEntry` with real url in `runtimePlugin.ts`
74+
- `nx run shared-treeshake-with-server-host:build` (default `3001`)
75+
- Serve: `nx run shared-treeshake-with-server-host:serve` (default `3001`)
6676

67-
```diff
68-
- reShakeShareEntry:
69-
- 'http://localhost:3003/independent-packages/antd/antd_mf_host.3fc92539.js',
70-
+ reShakeShareEntry:
71-
+ 'http://localhost:3003/independent-packages/antd/antd_mf_host.3fc92539.js',
72-
```
77+
6. Check the loaded lib
7378

74-
Finally, set `localStorage.setItem('calc','use')` and refresh the page.
79+
- Refresh the page and check `lib()`; it should return a much smaller export subset (e.g., only 4–5 components).
7580

76-
You will see the shared will use the re-shake shared with 5 modules export only.
81+
## Cypress E2E (no‑server Host)
82+
83+
- Run from the Host root:
84+
- `nx run shared-treeshake-no-server-host:test:e2e`
85+
86+
## Cypress E2E (with-server Host)
87+
88+
- Run from the Host root:
89+
- `nx run shared-treeshake-with-server-host:test:e2e`
90+
91+
## FAQ
92+
93+
- Ports: Provider `3002`, Host `3001`, re‑shake static server `3003`.
94+
- Keys and versions: `__FEDERATION__.__SHARE__` keys contain `:` or `-`; use bracket notation (e.g., `['mf_host:0.1.34']`, `['ui-lib']`).
95+
- Console example:
96+
- `__FEDERATION__.__SHARE__['mf_host:0.1.34'].default['antd']['6.0.1'].lib()`
97+
- If you use Nx, you can also run `nx build/serve` for the projects; commands must match actual project names.

apps/shared-treeshake/basic/App.js

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

apps/shared-treeshake/basic/package.json

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

apps/shared-treeshake/basic/project.json

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

apps/shared-treeshake/basic/webpack.config.js

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

apps/shared-treeshake/host/project.json

Lines changed: 0 additions & 61 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)