Skip to content

Commit cdfb10e

Browse files
committed
feat: support bundle specify exports shared
1 parent 6bddce5 commit cdfb10e

File tree

88 files changed

+5319
-1861
lines changed

Some content is hidden

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

88 files changed

+5319
-1861
lines changed

apps/shared-treeshake/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# shared-treeshake
2+
3+
## How to start the demos ?
4+
5+
### Basic
6+
7+
1. Build host and provider
8+
9+
```bash
10+
# Root directory
11+
pnpm i
12+
13+
pnpm i -g serve
14+
15+
nx build modern-js-plugin
16+
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 render success.
33+
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.
35+
36+
It will show all antd components (fallback resources).
37+
38+
39+
<!-- 4. Set localStorage to mock snapshot
40+
41+
```bash
42+
localStorage.setItem('calc','no-use')
43+
```
44+
45+
It will use the fallback resources.
46+
47+
5. Refresh the page (Use fallback)
48+
49+
Execute `__FEDERATION__.__SHARE__["mf_host:0.1.34"].default.antd["4.24.15"].lib()` in browser console.
50+
51+
It will show export all components . -->
52+
53+
### Advanced
54+
55+
This is combined with deploy server , which can calculate the snapshot of shared resources.
56+
57+
In this demo , you can set `localStorage.setItem('calc','use')` to mock snapshot.
58+
59+
First, need to re-shake the asset:
60+
61+
```bash
62+
nx build:re-shake shared-treeshake-host
63+
```
64+
65+
Second, serve it(`serve apps/shared-treeshake/host/dist-test -C -p 3003` ) and update the `reShakeShareEntry` with real url in `runtimePlugin.ts`
66+
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+
```
73+
74+
Finally, set `localStorage.setItem('calc','use')` and refresh the page.
75+
76+
You will see the shared will use the re-shake shared with 5 modules export only.

apps/shared-treeshake/host/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
strict-peer-dependencies=false
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# modernjs-ssr-nested-remote
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { appTools, defineConfig } from '@modern-js/app-tools';
2+
import { serverPlugin } from '@modern-js/plugin-server';
3+
import {
4+
// DependencyReferencExportPlugin,
5+
IndependentSharePlugin,
6+
ModuleFederationPlugin,
7+
} from '@module-federation/enhanced';
8+
import mfConfig from './module-federation.config';
9+
import path from 'path';
10+
11+
const isReShake = process.env.RE_SHAKE;
12+
if (isReShake) {
13+
process.env.MF_CUSTOM_REFERENCED_EXPORTS = JSON.stringify({
14+
antd: ['Divider', 'Space', 'Switch', 'Button', 'Badge'],
15+
});
16+
}
17+
18+
const webpackConfig = {
19+
cache: false,
20+
};
21+
22+
if (isReShake) {
23+
// @ts-ignore
24+
webpackConfig.entry = {
25+
main: 'data:application/node;base64,',
26+
};
27+
// @ts-ignore
28+
webpackConfig.output = {
29+
path: path.resolve(__dirname, 'dist-test'),
30+
};
31+
}
32+
33+
const publicPath = 'http://localhost:3001/';
34+
35+
// https://modernjs.dev/en/configure/app/usage
36+
export default defineConfig({
37+
runtime: {
38+
router: true,
39+
},
40+
dev: {
41+
assetPrefix: publicPath,
42+
},
43+
output: {
44+
assetPrefix: publicPath,
45+
polyfill: 'off',
46+
disableTsChecker: true,
47+
},
48+
server: {
49+
port: 3001,
50+
},
51+
plugins: [
52+
appTools({
53+
bundler: 'webpack', // Set to 'webpack' to enable webpack
54+
}),
55+
serverPlugin(),
56+
],
57+
performance: {
58+
chunkSplit: {
59+
strategy: 'split-by-module',
60+
},
61+
},
62+
source: {
63+
enableAsyncEntry: true,
64+
transformImport: false,
65+
},
66+
tools: {
67+
webpack: webpackConfig,
68+
bundlerChain(chain) {
69+
chain.optimization.moduleIds('named');
70+
chain.optimization.chunkIds('named');
71+
chain.optimization.mangleExports(false);
72+
// enable in dev
73+
chain.optimization.usedExports(true);
74+
// chain.optimization.minimize(false)
75+
chain.optimization.runtimeChunk(false);
76+
if (isReShake) {
77+
chain.plugin('IndependentSharePlugin').use(IndependentSharePlugin, [
78+
{
79+
// @ts-ignore
80+
mfConfig,
81+
outputDir: 'independent-packages',
82+
treeshake: true,
83+
},
84+
]);
85+
} else {
86+
chain.plugin('MF').use(ModuleFederationPlugin, [mfConfig]);
87+
}
88+
},
89+
},
90+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { createModuleFederationConfig } from '@module-federation/enhanced';
2+
3+
export default createModuleFederationConfig({
4+
name: 'mf_host',
5+
remotes: {
6+
mf_remote: 'mf_remote@http://localhost:3002/mf-manifest.json',
7+
},
8+
shared: {
9+
antd: { singleton: true, treeshake: true },
10+
react: {},
11+
'react-dom': {},
12+
},
13+
// shareStrategy: 'loaded-first',
14+
dts: false,
15+
runtimePlugins: [require.resolve('./runtimePlugin.ts')],
16+
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"name": "shared-treeshake-host",
3+
"private": true,
4+
"version": "0.1.34",
5+
"scripts": {
6+
"reset": "npx rimraf ./**/node_modules",
7+
"dev": "modern dev",
8+
"build": "modern build",
9+
"build:re-shake": "RE_SHAKE=true modern build",
10+
"start": "modern start",
11+
"serve": "modern serve",
12+
"new": "modern new",
13+
"lint": "modern lint",
14+
"upgrade": "modern upgrade",
15+
"inspect": "modern inspect"
16+
},
17+
"engines": {
18+
"node": ">=16.18.1"
19+
},
20+
"lint-staged": {
21+
"*.{js,jsx,ts,tsx,mjs,cjs}": [
22+
"node --max_old_space_size=8192 ./node_modules/eslint/bin/eslint.js --fix --color --cache --quiet"
23+
]
24+
},
25+
"eslintIgnore": [
26+
"node_modules/",
27+
"dist/"
28+
],
29+
"dependencies": {
30+
"@babel/runtime": "7.28.2",
31+
"@modern-js/runtime": "2.68.0",
32+
"@module-federation/enhanced": "workspace:*",
33+
"antd": "4.24.15",
34+
"react": "~18.3.1",
35+
"react-dom": "~18.3.1"
36+
},
37+
"devDependencies": {
38+
"@modern-js-app/eslint-config": "2.59.0",
39+
"@modern-js/app-tools": "2.68.0",
40+
"@modern-js/eslint-config": "2.59.0",
41+
"@modern-js/tsconfig": "2.68.0",
42+
"@modern-js/server-runtime": "2.68.0",
43+
"@modern-js/plugin-server": "2.68.0",
44+
"@types/jest": "~29.5.0",
45+
"@types/node": "~16.11.7",
46+
"@types/react": "~18.2.0",
47+
"@types/react-dom": "~18.3.0",
48+
"lint-staged": "~13.1.0",
49+
"prettier": "~3.3.3",
50+
"rimraf": "~3.0.2",
51+
"typescript": "~5.0.4",
52+
"fs-extra": "^11.1.1",
53+
"ts-node": "~10.8.1",
54+
"tsconfig-paths": "~3.14.1"
55+
}
56+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"name": "shared-treeshake-host",
3+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
4+
"sourceRoot": "apps/shared-treeshake/host/src",
5+
"projectType": "application",
6+
"tags": [],
7+
"implicitDependencies": ["typescript"],
8+
"targets": {
9+
"build": {
10+
"executor": "nx:run-commands",
11+
"options": {
12+
"dependsOn": [
13+
{
14+
"target": "build",
15+
"dependencies": true
16+
}
17+
],
18+
"commands": [
19+
{
20+
"command": "cd apps/shared-treeshake/host; pnpm run build",
21+
"forwardAllArgs": true
22+
}
23+
]
24+
}
25+
},
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+
},
43+
"serve": {
44+
"executor": "nx:run-commands",
45+
"options": {
46+
"dependsOn": [
47+
{
48+
"target": "build",
49+
"dependencies": true
50+
}
51+
],
52+
"commands": [
53+
{
54+
"command": "cd apps/shared-treeshake/host; pnpm run serve",
55+
"forwardAllArgs": true
56+
}
57+
]
58+
}
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)