Skip to content

Commit 2c46728

Browse files
committed
feat: support version-first strategy
1 parent 48243f7 commit 2c46728

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed

apps/shared-treeshake/README.md

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,14 @@ serve apps/shared-treeshake/provider/dist -C -p 3002
2929

3030
3. Visit page
3131

32-
open http://localhost:3001 , it will show the error:
33-
34-
Minified React error #130
32+
open http://localhost:3001 , it will render success.
3533

3634
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.
3735

38-
It will show only export 4 components :
36+
It will show all antd components (fallback resources).
3937

40-
```
41-
Button
42-
Divider
43-
Space
44-
Switch
45-
```
4638

47-
4. Set localStorage to mock snapshot
39+
<!-- 4. Set localStorage to mock snapshot
4840
4941
```bash
5042
localStorage.setItem('calc','no-use')
@@ -56,7 +48,7 @@ It will use the fallback resources.
5648
5749
Execute `__FEDERATION__.__SHARE__["mf_host:0.1.34"].default.antd["4.24.15"].lib()` in browser console.
5850
59-
It will show export all components .
51+
It will show export all components . -->
6052

6153
### Advanced
6254

apps/shared-treeshake/host/module-federation.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default createModuleFederationConfig({
1010
react: {},
1111
'react-dom': {},
1212
},
13-
shareStrategy: 'loaded-first',
13+
// shareStrategy: 'loaded-first',
1414
dts: false,
1515
runtimePlugins: [require.resolve('./runtimePlugin.ts')],
1616
});

packages/runtime-core/src/shared/index.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
RUNTIME_006,
55
runtimeDescMap,
66
} from '@module-federation/error-codes';
7+
import { TreeshakeStatus, isDebugMode } from '@module-federation/sdk';
78
import { Federation } from '../global';
89
import {
910
Options,
@@ -16,6 +17,7 @@ import {
1617
InitScope,
1718
InitTokens,
1819
CallFrom,
20+
NoMatchedUsedExportsItem,
1921
} from '../type';
2022
import { ModuleFederation } from '../core';
2123
import {
@@ -100,6 +102,19 @@ export class SharedHandler {
100102
shared.scope.forEach((scope) => {
101103
if (!this.shareScopeMap[scope]?.[sharedKey]?.[shared.version]) {
102104
registered = false;
105+
} else if (sharedVal.usedExports) {
106+
const registeredShared =
107+
this.shareScopeMap[scope][sharedKey][shared.version];
108+
if (
109+
registeredShared.treeshakeStatus === TreeshakeStatus.UNKNOWN &&
110+
registeredShared.usedExports &&
111+
sharedVal.usedExports.some(
112+
(exportName) =>
113+
!registeredShared.usedExports?.includes(exportName),
114+
)
115+
) {
116+
registeredShared.treeshakeStatus = TreeshakeStatus.NO_USE;
117+
}
103118
}
104119
});
105120

@@ -469,6 +484,34 @@ export class SharedHandler {
469484
extraOptions: { hostShareScopeMap?: ShareScopeMap } = {},
470485
): void {
471486
const { host } = this;
487+
const existedShareScope = this.shareScopeMap[scopeName];
488+
Object.entries(shareScope).forEach(([pkgName, newVersions]) => {
489+
const existedShareMap = existedShareScope[pkgName];
490+
if (!existedShareMap) {
491+
return;
492+
}
493+
Object.entries(existedShareMap).forEach(([version, existedShared]) => {
494+
const newShared = newVersions[version];
495+
if (
496+
newShared &&
497+
newShared.treeshakeStatus === TreeshakeStatus.UNKNOWN &&
498+
newShared.usedExports &&
499+
existedShared.usedExports &&
500+
existedShared.usedExports.some(
501+
(exportName) => !newShared.usedExports?.includes(exportName),
502+
)
503+
) {
504+
newShared.treeshakeStatus = TreeshakeStatus.NO_USE;
505+
newShared._noMatchedUsedExports =
506+
existedShared._noMatchedUsedExports || [];
507+
const item: NoMatchedUsedExportsItem = [existedShared.from];
508+
if (isDebugMode() && existedShared.usedExports) {
509+
item.push(existedShared.usedExports);
510+
}
511+
newShared._noMatchedUsedExports.push(item);
512+
}
513+
});
514+
});
472515
this.shareScopeMap[scopeName] = shareScope;
473516
this.hooks.lifecycle.initContainerShareScopeMap.emit({
474517
shareScope,

packages/runtime-core/src/type/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ export type ShareArgs =
7777
| (SharedBaseArgs & { lib: () => Module })
7878
| SharedBaseArgs;
7979
export type ShareStrategy = 'version-first' | 'loaded-first';
80+
81+
export type NoMatchedUsedExportsItem = [from: string, usedExports?: string[]];
8082
export type Shared = {
8183
version: string;
8284
get: SharedGetter;
@@ -98,6 +100,7 @@ export type Shared = {
98100
fallback?: SharedGetter;
99101
reShakeGet?: SharedGetter;
100102
treeshakeStatus: TreeshakeStatus;
103+
_noMatchedUsedExports?: NoMatchedUsedExportsItem[];
101104
};
102105

103106
export type ShareScopeMap = {

0 commit comments

Comments
 (0)