Skip to content

Commit 48243f7

Browse files
committed
chore: add README
1 parent d1e1d9a commit 48243f7

File tree

8 files changed

+419
-22
lines changed

8 files changed

+419
-22
lines changed

apps/shared-treeshake/README.md

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,84 @@
11
# shared-treeshake
22

3-
## Running Demo
3+
## How to start the demos ?
44

5-
- host: [localhost:5001](http://localhost:5001/)
6-
- provider: [localhost:5002](http://localhost:5002/)
5+
### Basic
76

8-
## How to start the demos ?
7+
1. Build host and provider
98

109
```bash
1110
# Root directory
1211
pnpm i
1312

13+
pnpm i -g serve
14+
1415
nx build modern-js-plugin
1516

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
1762

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
1971
```
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.

apps/shared-treeshake/host/modern.config.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { appTools, defineConfig } from '@modern-js/app-tools';
2+
import { serverPlugin } from '@modern-js/plugin-server';
23
import {
34
// DependencyReferencExportPlugin,
45
IndependentSharePlugin,
56
ModuleFederationPlugin,
67
} from '@module-federation/enhanced';
78
import mfConfig from './module-federation.config';
9+
import path from 'path';
10+
811
const isReShake = process.env.RE_SHAKE;
912
if (isReShake) {
1013
process.env.MF_CUSTOM_REFERENCED_EXPORTS = JSON.stringify({
@@ -21,17 +24,24 @@ if (isReShake) {
2124
webpackConfig.entry = {
2225
main: 'data:application/node;base64,',
2326
};
27+
// @ts-ignore
28+
webpackConfig.output = {
29+
path: path.resolve(__dirname, 'dist-test'),
30+
};
2431
}
32+
33+
const publicPath = 'http://localhost:3001/';
34+
2535
// https://modernjs.dev/en/configure/app/usage
2636
export default defineConfig({
2737
runtime: {
2838
router: true,
2939
},
3040
dev: {
31-
assetPrefix: 'http://localhost:3001/',
41+
assetPrefix: publicPath,
3242
},
3343
output: {
34-
assetPrefix: 'http://localhost:3001/',
44+
assetPrefix: publicPath,
3545
polyfill: 'off',
3646
disableTsChecker: true,
3747
},
@@ -42,6 +52,7 @@ export default defineConfig({
4252
appTools({
4353
bundler: 'webpack', // Set to 'webpack' to enable webpack
4454
}),
55+
serverPlugin(),
4556
],
4657
performance: {
4758
chunkSplit: {

apps/shared-treeshake/host/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,18 @@
3939
"@modern-js/app-tools": "2.68.0",
4040
"@modern-js/eslint-config": "2.59.0",
4141
"@modern-js/tsconfig": "2.68.0",
42+
"@modern-js/server-runtime": "2.68.0",
43+
"@modern-js/plugin-server": "2.68.0",
4244
"@types/jest": "~29.5.0",
4345
"@types/node": "~16.11.7",
4446
"@types/react": "~18.2.0",
4547
"@types/react-dom": "~18.3.0",
4648
"lint-staged": "~13.1.0",
4749
"prettier": "~3.3.3",
4850
"rimraf": "~3.0.2",
49-
"typescript": "~5.0.4"
51+
"typescript": "~5.0.4",
52+
"fs-extra": "^11.1.1",
53+
"ts-node": "~10.8.1",
54+
"tsconfig-paths": "~3.14.1"
5055
}
5156
}

apps/shared-treeshake/host/project.json

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@
2323
]
2424
}
2525
},
26+
"build:re-shake": {
27+
"executor": "nx:run-commands",
28+
"options": {
29+
"dependsOn": [
30+
{
31+
"target": "build",
32+
"dependencies": true
33+
}
34+
],
35+
"commands": [
36+
{
37+
"command": "cd apps/shared-treeshake/host; pnpm run build:re-shake",
38+
"forwardAllArgs": true
39+
}
40+
]
41+
}
42+
},
2643
"serve": {
2744
"executor": "nx:run-commands",
2845
"options": {
@@ -34,8 +51,8 @@
3451
],
3552
"commands": [
3653
{
37-
"command": "cd apps/shared-treeshake/host; pnpm run dev",
38-
"forwardAllArgs": false
54+
"command": "cd apps/shared-treeshake/host; pnpm run serve",
55+
"forwardAllArgs": true
3956
}
4057
]
4158
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { defineServerConfig } from '@modern-js/server-runtime';
2+
import path from 'path';
3+
import fs from 'fs';
4+
import { readFile } from 'fs/promises';
5+
6+
import type { MiddlewareHandler } from '@modern-js/server-runtime';
7+
8+
const handler: MiddlewareHandler = async (c, next) => {
9+
try {
10+
console.log('req.url', c.req.url);
11+
if (c.req.url?.includes('independent-packages')) {
12+
const filepath = path.join(
13+
process.cwd(),
14+
c.req.url.replace('http://localhost:3001/', 'dist/'),
15+
);
16+
console.log('filepath: ', filepath);
17+
18+
fs.statSync(filepath);
19+
c.res.headers.set('Access-Control-Allow-Origin', '*');
20+
c.res.headers.set(
21+
'Access-Control-Allow-Methods',
22+
'GET, POST, PUT, DELETE, PATCH, OPTIONS',
23+
);
24+
c.res.headers.set('Access-Control-Allow-Headers', '*');
25+
const content = await readFile(filepath, 'utf-8');
26+
return c.text(content); // 返回文件内容
27+
} else {
28+
await next();
29+
}
30+
} catch (err) {
31+
await next();
32+
}
33+
};
34+
35+
export default defineServerConfig({
36+
middlewares: [
37+
{
38+
name: 'proxy-fallback-shared',
39+
handler,
40+
},
41+
],
42+
renderMiddlewares: [],
43+
plugins: [],
44+
});

apps/shared-treeshake/host/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
"*": ["./@mf-types/*"]
1111
}
1212
},
13-
"include": ["src", "shared", "config", "modern.config.ts"],
13+
"include": ["src", "shared", "config", "modern.config.ts", "server"],
1414
"exclude": ["**/node_modules"]
1515
}

apps/shared-treeshake/provider/project.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
],
3535
"commands": [
3636
{
37-
"command": "cd apps/shared-treeshake/provider; pnpm run dev",
38-
"forwardAllArgs": false
37+
"command": "cd apps/shared-treeshake/provider; pnpm run serve",
38+
"forwardAllArgs": true
3939
}
4040
]
4141
}

0 commit comments

Comments
 (0)