Skip to content

Commit 2f2844f

Browse files
committed
chore: wip
1 parent b0e7c8c commit 2f2844f

File tree

6 files changed

+71
-33
lines changed

6 files changed

+71
-33
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime';
2+
3+
const autoFetchData: () => FederationRuntimePlugin = () => ({
4+
name: 'auto-fetch-data-plugin',
5+
loadSnapshot(args) {
6+
const { id, moduleInfo, remoteSnapshot } = args;
7+
8+
if (!('modules' in moduleInfo) || !id) {
9+
return args;
10+
}
11+
12+
if (hasData(remoteSnapshot)) {
13+
mfModule.data = fetchData();
14+
}
15+
16+
return args;
17+
},
18+
});
19+
export default autoFetchData;

packages/modernjs/src/runtime/createRemoteSSRComponent.tsx

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@ type IProps = {
1616

1717
type ReactKey = { key?: React.Key | null };
1818

19+
async function fetchData(id: string) {
20+
const instance = getInstance();
21+
if (!instance) {
22+
return;
23+
}
24+
const { name } = instance.remoteHandler.idToRemoteMap[id] || {};
25+
if (!name) {
26+
return;
27+
}
28+
const module = instance.moduleCache.get(name);
29+
if (!module) {
30+
return;
31+
}
32+
const dataFetch = module.dataFetchMap[id];
33+
if (!dataFetch) {
34+
return;
35+
}
36+
return dataFetch;
37+
}
38+
1939
function getLoadedRemoteInfos(instance: FederationHost, id: string) {
2040
const { name, expose } = instance.remoteHandler.idToRemoteMap[id] || {};
2141
if (!name) {
@@ -140,9 +160,9 @@ export function createRemoteSSRComponent<T, E extends keyof T>(info: {
140160
}) {
141161
type ComponentType = T[E] extends (...args: any) => any
142162
? Parameters<T[E]>[0] extends undefined
143-
? ReactKey
144-
: Parameters<T[E]>[0] & ReactKey
145-
: ReactKey;
163+
? ReactKey & { _mf_data?: any }
164+
: Parameters<T[E]>[0] & ReactKey & { _mfData?: any }
165+
: ReactKey & { _mfData?: any };
146166
const exportName = info?.export || 'default';
147167

148168
const LazyComponent = React.lazy(async () => {
@@ -154,17 +174,19 @@ export function createRemoteSSRComponent<T, E extends keyof T>(info: {
154174
}
155175
const moduleId = m && m[Symbol.for('mf_module_id')];
156176

177+
const data = await fetchData(moduleId);
178+
157179
const assets = collectSSRAssets({
158180
id: moduleId,
159181
});
160182

161-
const Com = m[exportName] as React.FC;
183+
const Com = m[exportName] as React.FC<ComponentType>;
162184
if (exportName in m && typeof Com === 'function') {
163185
return {
164186
default: (props: Omit<ComponentType, 'key'>) => (
165187
<>
166188
{assets}
167-
<Com {...props} />
189+
<Com {...props} _mfData={data} />
168190
</>
169191
),
170192
};

packages/runtime-core/src/global.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface Federation {
2424
__SHARE__: GlobalShareScopeMap;
2525
__MANIFEST_LOADING__: Record<string, Promise<ModuleInfo>>;
2626
__PRELOADED_MAP__: Map<string, boolean>;
27+
__DATA_FETCH_MAP__: Map<string, Promise<unknown>>;
2728
}
2829
export const CurrentGlobal =
2930
typeof globalThis === 'object' ? globalThis : window;
@@ -91,6 +92,7 @@ function setGlobalDefaultVal(target: typeof CurrentGlobal) {
9192
__SHARE__: {},
9293
__MANIFEST_LOADING__: {},
9394
__PRELOADED_MAP__: new Map(),
95+
__DATA_FETCH_MAP__: new Map(),
9496
});
9597

9698
definePropertyGlobalVal(target, '__VMOK__', target.__FEDERATION__);
@@ -102,6 +104,7 @@ function setGlobalDefaultVal(target: typeof CurrentGlobal) {
102104
target.__FEDERATION__.__SHARE__ ??= {};
103105
target.__FEDERATION__.__MANIFEST_LOADING__ ??= {};
104106
target.__FEDERATION__.__PRELOADED_MAP__ ??= new Map();
107+
target.__FEDERATION__.__DATA_FETCH_MAP__ ??= new Map();
105108
}
106109

107110
setGlobalDefaultVal(CurrentGlobal);

packages/runtime-core/src/plugins/snapshot/SnapshotHandler.ts

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ export class SnapshotHandler {
8282
void
8383
>('beforeLoadRemoteSnapshot'),
8484
loadSnapshot: new AsyncWaterfallHook<{
85+
id?: string;
86+
expose?: string;
8587
options: Options;
8688
moduleInfo: Remote;
8789
hostGlobalSnapshot: GlobalModuleInfo[string] | undefined;
@@ -111,32 +113,16 @@ export class SnapshotHandler {
111113
this.loaderHook = HostInstance.loaderHook;
112114
}
113115

114-
async loadSnapshot(moduleInfo: Remote): Promise<{
115-
remoteSnapshot: GlobalModuleInfo[string] | undefined;
116-
globalSnapshot: ReturnType<typeof getGlobalSnapshot>;
117-
}> {
118-
const { options } = this.HostInstance;
119-
const { hostGlobalSnapshot, remoteSnapshot, globalSnapshot } =
120-
this.getGlobalRemoteInfo(moduleInfo);
121-
122-
const {
123-
remoteSnapshot: globalRemoteSnapshot,
124-
globalSnapshot: globalSnapshotRes,
125-
} = await this.hooks.lifecycle.loadSnapshot.emit({
126-
options,
127-
moduleInfo,
128-
hostGlobalSnapshot,
129-
remoteSnapshot,
130-
globalSnapshot,
131-
});
132-
return {
133-
remoteSnapshot: globalRemoteSnapshot,
134-
globalSnapshot: globalSnapshotRes,
135-
};
136-
}
137-
138116
// eslint-disable-next-line max-lines-per-function
139-
async loadRemoteSnapshotInfo(moduleInfo: Remote):
117+
async loadRemoteSnapshotInfo({
118+
moduleInfo,
119+
id,
120+
expose,
121+
}: {
122+
moduleInfo: Remote;
123+
id?: string;
124+
expose?: string;
125+
}):
140126
| Promise<{
141127
remoteSnapshot: ModuleInfo;
142128
globalSnapshot: GlobalModuleInfo;
@@ -189,6 +175,8 @@ export class SnapshotHandler {
189175
remoteSnapshot: globalRemoteSnapshot,
190176
globalSnapshot: globalSnapshotRes,
191177
} = await this.hooks.lifecycle.loadSnapshot.emit({
178+
id,
179+
expose,
192180
options,
193181
moduleInfo,
194182
hostGlobalSnapshot,

packages/runtime-core/src/plugins/snapshot/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,15 @@ export function snapshotPlugin(): FederationRuntimePlugin {
4141
return {
4242
name: 'snapshot-plugin',
4343
async afterResolve(args) {
44-
const { remote, pkgNameOrAlias, expose, origin, remoteInfo } = args;
44+
const { remote, pkgNameOrAlias, expose, origin, remoteInfo, id } = args;
4545

4646
if (!isRemoteInfoWithEntry(remote) || !isPureRemoteEntry(remote)) {
4747
const { remoteSnapshot, globalSnapshot } =
48-
await origin.snapshotHandler.loadRemoteSnapshotInfo(remote);
48+
await origin.snapshotHandler.loadRemoteSnapshotInfo({
49+
moduleInfo: remote,
50+
id,
51+
expose,
52+
});
4953

5054
assignRemoteInfo(remoteInfo, remoteSnapshot);
5155
// preloading assets

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,9 @@ export class RemoteHandler {
287287
const { remote } = ops;
288288
const remoteInfo = getRemoteInfo(remote);
289289
const { globalSnapshot, remoteSnapshot } =
290-
await host.snapshotHandler.loadRemoteSnapshotInfo(remote);
290+
await host.snapshotHandler.loadRemoteSnapshotInfo({
291+
moduleInfo: remote,
292+
});
291293

292294
const assets = await this.hooks.lifecycle.generatePreloadAssets.emit({
293295
origin: host,

0 commit comments

Comments
 (0)