Skip to content

Commit b90fa7d

Browse files
authored
feat: add shareStrategy option (#2870)
1 parent 14d499e commit b90fa7d

File tree

25 files changed

+134
-120
lines changed

25 files changed

+134
-120
lines changed

.changeset/tasty-hotels-obey.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@module-federation/enhanced': patch
3+
'@module-federation/runtime': patch
4+
'@module-federation/sdk': patch
5+
---
6+
7+
feat: add shareStrategy option

apps/runtime-demo/3005-runtime-host/runtimePlugin.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

apps/runtime-demo/3005-runtime-host/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ module.exports = composePlugins(withNx(), withReact(), (config, context) => {
4747
dts: {
4848
tsConfigPath: path.resolve(__dirname, 'tsconfig.app.json'),
4949
},
50+
shareStrategy: 'loaded-first',
5051
shared: {
5152
lodash: {
5253
singleton: true,
@@ -73,7 +74,6 @@ module.exports = composePlugins(withNx(), withReact(), (config, context) => {
7374
requiredVersion: '^18.2.0',
7475
},
7576
},
76-
runtimePlugins: [path.join(__dirname, './runtimePlugin.ts')],
7777
}),
7878
);
7979
config.optimization.runtimeChunk = false;

apps/runtime-demo/3006-runtime-remote/runtimePlugin.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

apps/runtime-demo/3006-runtime-remote/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module.exports = composePlugins(
3535
'./WebpackSvg': './src/components/WebpackSvg',
3636
'./WebpackPng': './src/components/WebpackPng',
3737
},
38+
shareStrategy: 'loaded-first',
3839
shared: {
3940
lodash: {
4041
singleton: true,
@@ -64,7 +65,6 @@ module.exports = composePlugins(
6465
dts: {
6566
tsConfigPath: path.resolve(__dirname, 'tsconfig.app.json'),
6667
},
67-
runtimePlugins: [path.join(__dirname, './runtimePlugin.ts')],
6868
}),
6969
);
7070
// config.externals={

apps/runtime-demo/3007-runtime-remote/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ module.exports = composePlugins(
5151
requiredVersion: '^18.2.0',
5252
},
5353
},
54+
shareStrategy: 'loaded-first',
5455
dev: {
5556
disableLiveReload: true,
5657
},

apps/website-new/docs/en/configure/_meta.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,10 @@
5858
"type": "file",
5959
"name": "manifest",
6060
"label": "Manifest"
61+
},
62+
{
63+
"type": "file",
64+
"name": "shareStrategy",
65+
"label": "shareStrategy"
6166
}
6267
]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# shareStrategy
2+
3+
- Type: `'version-first' | 'loaded-first'`
4+
- Required: No
5+
- Default: `'version-first'`
6+
7+
Control the loading strategy of shared dependencies:
8+
9+
- `'version-first'`: Version priority, ensuring that the highest version of shared dependencies is used. After setting, all *remotes* entry files will be automatically loaded and **register** the corresponding shared dependencies to ensure that all shared dependency versions can be obtained. This strategy is recommended when there are strict version requirements.
10+
11+
- `'loaded-first'`: Prioritize reuse, greatly reducing redundant dependency requests. After setting, the *remotes* entry file will not be automatically loaded (it will only be loaded when needed), and the loaded shared dependencies will be reused first. This strategy is recommended when there are no strict requirements on the version and performance is required.

apps/website-new/docs/en/guide/basic/error-catalog.mdx

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,16 @@ Error: [ Federation Runtime ]:
1717
:::
1818

1919
#### Solution
20-
1. Create a shared-strategy in all hosts and remotes
21-
22-
```ts title="shared-strategy.ts"
23-
``import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime';
24-
25-
const sharedStrategy: () => FederationRuntimePlugin = () => ({
26-
name: 'shared-strategy-plugin',
27-
beforeInit(args) {
28-
const { userOptions } = args;
29-
const shared = userOptions.shared;
30-
if (shared) {
31-
Object.keys(shared).forEach((sharedKey) => {
32-
const sharedConfigs = shared[sharedKey];
33-
const arraySharedConfigs = Array.isArray(sharedConfigs)
34-
? sharedConfigs
35-
: [sharedConfigs];
36-
arraySharedConfigs.forEach((s) => {
37-
s.strategy = 'loaded-first';
38-
});
39-
});
40-
}
41-
return args;
42-
},
43-
});
44-
export default sharedStrategy;
45-
```
46-
47-
2. Add the shared-strategy to your config
20+
Set `shareStrategy` as `'loaded-first'` host
4821
```ts title="modern.config.js"
4922
{
5023
...
5124
new ModuleFederationPlugin({
5225
...,
53-
runtimePlugins: ['path/to/shared-strategy.ts'],
26+
shareStrategy: 'loaded-first',
5427
})
5528
}
5629
```
57-
5830
3. Optional: If you are running `module-federation` in a Docker environment, make sure to adapt the following fields in your configs of all `hosts` and `remotes`:
5931
```ts title="modern.config.js"
6032
{

apps/website-new/docs/zh/configure/_meta.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,10 @@
5858
"type": "file",
5959
"name": "manifest",
6060
"label": "manifest"
61+
},
62+
{
63+
"type": "file",
64+
"name": "shareStrategy",
65+
"label": "shareStrategy"
6166
}
6267
]

0 commit comments

Comments
 (0)