|
| 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. |
0 commit comments