Skip to content

Commit 876a4ff

Browse files
authored
feat: align with shared import:false (#2330)
1 parent f9b8af7 commit 876a4ff

File tree

5 files changed

+79
-6
lines changed

5 files changed

+79
-6
lines changed

.changeset/angry-ways-return.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@module-federation/runtime': patch
3+
'@module-federation/enhanced': patch
4+
---
5+
6+
feat: support config shared import:false in runtime

packages/runtime/__tests__/share.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,35 @@ export const arrayShared = {
122122
export const arraySharedInfos = {
123123
'react-dom': arrayShared.shared['react-dom'],
124124
};
125+
126+
export const shareInfoWithoutLibAndGetConsumer = {
127+
name: '@federation/shared-config-consumer',
128+
remotes: [],
129+
shared: {
130+
'react-dom': {
131+
scope: ['default'],
132+
shareConfig: {
133+
singleton: true,
134+
requiredVersion: '^16.0.0',
135+
eager: false,
136+
},
137+
},
138+
},
139+
};
140+
141+
export const shareInfoWithoutLibAndGetProvider = {
142+
name: '@federation/shared-config-provider',
143+
remotes: [],
144+
shared: {
145+
'react-dom': {
146+
version: '16.0.0',
147+
scope: ['default'],
148+
get: () =>
149+
Promise.resolve(() => ({
150+
default: 'react-dom',
151+
version: '16.0.0',
152+
from: '@federation/shared-config-provider',
153+
})),
154+
},
155+
},
156+
};

packages/runtime/__tests__/shares.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
localMergeShareInfos,
88
arrayShared,
99
arraySharedInfos,
10+
shareInfoWithoutLibAndGetConsumer,
11+
shareInfoWithoutLibAndGetProvider,
1012
} from './share';
1113
// import { assert } from '../src/utils/logger';
1214
import { FederationHost } from '../src/core';
@@ -35,6 +37,29 @@ describe('shared', () => {
3537
expect(FederationInstance.options.shared).toMatchObject(arraySharedInfos);
3638
});
3739

40+
it('init shared without lib and get', async () => {
41+
const provider = init(shareInfoWithoutLibAndGetProvider);
42+
43+
await provider.loadShare<{
44+
version: string;
45+
from: string;
46+
}>('react-dom');
47+
48+
const consumer = init(shareInfoWithoutLibAndGetConsumer);
49+
consumer.initShareScopeMap('default', provider.shareScopeMap['default']);
50+
51+
debugger;
52+
const reactDomInstance = await consumer.loadShare<{
53+
version: string;
54+
from: string;
55+
}>('react-dom');
56+
assert(reactDomInstance);
57+
const reactDomInstanceRes = reactDomInstance();
58+
assert(reactDomInstanceRes, "reactInstance can't be undefined");
59+
expect(reactDomInstanceRes.from).toBe('@federation/shared-config-provider');
60+
expect(reactDomInstanceRes.version).toBe('16.0.0');
61+
});
62+
3863
it('loadShare singleton', async () => {
3964
const gmConfig1 = {
4065
name: '@federation/loadShare',

packages/runtime/src/type/config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export interface SharedConfig {
5353
}
5454

5555
type SharedBaseArgs = {
56-
version: string;
56+
version?: string;
5757
shareConfig?: SharedConfig;
5858
scope?: string | Array<string>;
5959
deps?: Array<string>;
@@ -64,7 +64,8 @@ export type SharedGetter = (() => () => Module) | (() => Promise<() => Module>);
6464

6565
export type ShareArgs =
6666
| (SharedBaseArgs & { get: SharedGetter })
67-
| (SharedBaseArgs & { lib: () => Module });
67+
| (SharedBaseArgs & { lib: () => Module })
68+
| SharedBaseArgs;
6869

6970
export type Shared = {
7071
version: string;

packages/runtime/src/utils/share.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,22 @@ import { satisfy } from './semver';
1414
import { SyncWaterfallHook } from './hooks';
1515
import { arrayOptions } from './tool';
1616

17-
export function formatShare(shareArgs: ShareArgs, from: string): Shared {
17+
export function formatShare(
18+
shareArgs: ShareArgs,
19+
from: string,
20+
name: string,
21+
): Shared {
1822
let get: Shared['get'];
1923
if ('get' in shareArgs) {
2024
// eslint-disable-next-line prefer-destructuring
2125
get = shareArgs.get;
22-
} else {
23-
// @ts-ignore ignore
26+
} else if ('lib' in shareArgs) {
2427
get = () => Promise.resolve(shareArgs.lib);
28+
} else {
29+
get = () =>
30+
Promise.resolve(() => {
31+
throw new Error(`Can not get shared '${name}'!`);
32+
});
2533
}
2634
return {
2735
deps: [],
@@ -38,6 +46,7 @@ export function formatShare(shareArgs: ShareArgs, from: string): Shared {
3846
},
3947
get,
4048
loaded: 'lib' in shareArgs ? true : undefined,
49+
version: shareArgs.version ?? '0',
4150
scope: Array.isArray(shareArgs.scope) ? shareArgs.scope : ['default'],
4251
strategy: shareArgs.strategy || 'version-first',
4352
};
@@ -54,7 +63,7 @@ export function formatShareConfigs(
5463
const arrayShareArgs = arrayOptions(shareArgs[pkgName]);
5564
res[pkgName] = res[pkgName] || [];
5665
arrayShareArgs.forEach((shareConfig) => {
57-
res[pkgName].push(formatShare(shareConfig, from));
66+
res[pkgName].push(formatShare(shareConfig, from, pkgName));
5867
});
5968
return res;
6069
}, {} as ShareInfos);

0 commit comments

Comments
 (0)