|
1 | | -# shared-treeshake |
| 1 | +# Shared Treeshake Demos |
2 | 2 |
|
3 | | -## How to start the demos ? |
| 3 | +Two Module Federation demo apps that showcase tree‑shaking for shared dependencies: |
4 | 4 |
|
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. |
6 | 7 |
|
7 | | -1. Build host and provider |
| 8 | +## Project Layout |
8 | 9 |
|
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 |
12 | 14 |
|
13 | | -pnpm i -g serve |
| 15 | + |
14 | 16 |
|
15 | | -nx build modern-js-plugin |
| 17 | +## Quick Start (no‑server) |
16 | 18 |
|
17 | | -nx build shared-treeshake-host |
| 19 | +1. Install dependencies (at repo root) |
18 | 20 |
|
19 | | -nx build shared-treeshake-provider |
| 21 | +- `pnpm i` |
20 | 22 |
|
21 | | -``` |
22 | | -2. Serve host and provider |
| 23 | +2. Build and serve the Provider (start the remote first) |
23 | 24 |
|
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/` |
26 | 28 |
|
27 | | -serve apps/shared-treeshake/provider/dist -C -p 3002 |
28 | | -``` |
| 29 | +3. Build and serve the Host (local dev) |
29 | 30 |
|
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/` |
31 | 34 |
|
32 | | -open http://localhost:3001 , it will render success. |
| 35 | +4. Verify the page and the shared dependency |
33 | 36 |
|
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`). |
35 | 42 |
|
36 | | -It will show all antd components (fallback resources). |
| 43 | +5. Simulate a “full fallback” (no treeshake) |
37 | 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). |
38 | 47 |
|
39 | | -<!-- 4. Set localStorage to mock snapshot |
| 48 | +## Advanced Flow (with‑server) |
40 | 49 |
|
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. |
44 | 51 |
|
45 | | -It will use the fallback resources. |
| 52 | +1. Produce re‑shake artifacts for the Host |
46 | 53 |
|
47 | | -5. Refresh the page (Use fallback) |
| 54 | +- `nx run shared-treeshake-with-server-host:build-re-shake` |
48 | 55 |
|
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 |
50 | 57 |
|
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` |
52 | 60 |
|
53 | | -### Advanced |
| 61 | +3. Configure the snapshot entry in the Host |
54 | 62 |
|
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` |
56 | 66 |
|
57 | | -In this demo , you can set `localStorage.setItem('calc','use')` to mock snapshot. |
| 67 | +4. Build and serve the Provider |
58 | 68 |
|
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`) |
60 | 71 |
|
61 | | -```bash |
62 | | -nx build:re-shake shared-treeshake-host |
63 | | -``` |
| 72 | +5. Build and serve the Host |
64 | 73 |
|
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`) |
66 | 76 |
|
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 |
73 | 78 |
|
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). |
75 | 80 |
|
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. |
0 commit comments