|
1 | 1 | # shared-treeshake |
2 | 2 |
|
3 | | -## Running Demo |
| 3 | +## How to start the demos ? |
4 | 4 |
|
5 | | -- host: [localhost:5001](http://localhost:5001/) |
6 | | -- provider: [localhost:5002](http://localhost:5002/) |
| 5 | +### Basic |
7 | 6 |
|
8 | | -## How to start the demos ? |
| 7 | +1. Build host and provider |
9 | 8 |
|
10 | 9 | ```bash |
11 | 10 | # Root directory |
12 | 11 | pnpm i |
13 | 12 |
|
| 13 | +pnpm i -g serve |
| 14 | + |
14 | 15 | nx build modern-js-plugin |
15 | 16 |
|
16 | | -pnpm run app:shared-treeshake:dev |
| 17 | +nx build shared-treeshake-host |
| 18 | + |
| 19 | +nx build shared-treeshake-provider |
| 20 | + |
| 21 | +``` |
| 22 | +2. Serve host and provider |
| 23 | + |
| 24 | +```bash |
| 25 | +nx serve shared-treeshake-host |
| 26 | + |
| 27 | +serve apps/shared-treeshake/provider/dist -C -p 3002 |
| 28 | +``` |
| 29 | + |
| 30 | +3. Visit page |
| 31 | + |
| 32 | +open http://localhost:3001 , it will show the error: |
| 33 | + |
| 34 | +Minified React error #130 |
| 35 | + |
| 36 | +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 | + |
| 38 | +It will show only export 4 components : |
| 39 | + |
| 40 | +``` |
| 41 | +Button |
| 42 | +Divider |
| 43 | +Space |
| 44 | +Switch |
| 45 | +``` |
| 46 | + |
| 47 | +4. Set localStorage to mock snapshot |
| 48 | + |
| 49 | +```bash |
| 50 | +localStorage.setItem('calc','no-use') |
| 51 | +``` |
| 52 | + |
| 53 | +It will use the fallback resources. |
| 54 | + |
| 55 | +5. Refresh the page (Use fallback) |
| 56 | + |
| 57 | +Execute `__FEDERATION__.__SHARE__["mf_host:0.1.34"].default.antd["4.24.15"].lib()` in browser console. |
| 58 | + |
| 59 | +It will show export all components . |
| 60 | + |
| 61 | +### Advanced |
17 | 62 |
|
18 | | -open http://localhost:5001/ |
| 63 | +This is combined with deploy server , which can calculate the snapshot of shared resources. |
| 64 | + |
| 65 | +In this demo , you can set `localStorage.setItem('calc','use')` to mock snapshot. |
| 66 | + |
| 67 | +First, need to re-shake the asset: |
| 68 | + |
| 69 | +```bash |
| 70 | +nx build:re-shake shared-treeshake-host |
19 | 71 | ``` |
| 72 | + |
| 73 | +Second, serve it(`serve apps/shared-treeshake/host/dist-test -C -p 3003` ) and update the `reShakeShareEntry` with real url in `runtimePlugin.ts` |
| 74 | + |
| 75 | +```diff |
| 76 | +- reShakeShareEntry: |
| 77 | +- 'http://localhost:3003/independent-packages/antd/antd_mf_host.3fc92539.js', |
| 78 | ++ reShakeShareEntry: |
| 79 | ++ 'http://localhost:3003/independent-packages/antd/antd_mf_host.3fc92539.js', |
| 80 | +``` |
| 81 | + |
| 82 | +Finally, set `localStorage.setItem('calc','use')` and refresh the page. |
| 83 | + |
| 84 | +You will see the shared will use the re-shake shared with 5 modules export only. |
0 commit comments