Skip to content

Commit 121af12

Browse files
committed
chore: add runtime plugin
1 parent 2f2844f commit 121af12

File tree

10 files changed

+132
-25
lines changed

10 files changed

+132
-25
lines changed

packages/modernjs/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
"import": "./dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.js",
5252
"require": "./dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.js"
5353
},
54+
"./auto-fetch-data": {
55+
"types": "./dist/types/cli/mfRuntimePlugins/auto-fetch-data.d.ts",
56+
"import": "./dist/esm/cli/mfRuntimePlugins/auto-fetch-data.js",
57+
"require": "./dist/esm/cli/mfRuntimePlugins/auto-fetch-data.js"
58+
},
5459
"./inject-node-fetch": {
5560
"types": "./dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts",
5661
"import": "./dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js",
@@ -77,6 +82,9 @@
7782
"resolve-entry-ipv4": [
7883
"./dist/types/cli/mfRuntimePlugins/resolve-entry-ipv4.d.ts"
7984
],
85+
"auto-fetch-data": [
86+
"./dist/types/cli/mfRuntimePlugins/auto-fetch-data.d.ts"
87+
],
8088
"inject-node-fetch": [
8189
"./dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts"
8290
]
@@ -91,6 +99,7 @@
9199
"@module-federation/rsbuild-plugin": "workspace:*",
92100
"@modern-js/utils": "2.65.1",
93101
"@module-federation/enhanced": "workspace:*",
102+
"@module-federation/runtime": "workspace:*",
94103
"@module-federation/node": "workspace:*",
95104
"@module-federation/sdk": "workspace:*",
96105
"@swc/helpers": "0.5.13",

packages/modernjs/src/cli/mfRuntimePlugins/auto-fetch-data.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,51 @@
11
import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime';
2+
import helpers from '@module-federation/runtime/helpers';
3+
import { getDataFetchInfo } from '../../runtime/utils';
24

35
const autoFetchData: () => FederationRuntimePlugin = () => ({
46
name: 'auto-fetch-data-plugin',
5-
loadSnapshot(args) {
6-
const { id, moduleInfo, remoteSnapshot } = args;
7+
afterLoadSnapshot(args) {
8+
const { id, moduleInfo, remoteSnapshot, host } = args;
79

8-
if (!('modules' in moduleInfo) || !id) {
10+
if (!remoteSnapshot || !id || !('modules' in remoteSnapshot)) {
911
return args;
1012
}
1113

12-
if (hasData(remoteSnapshot)) {
13-
mfModule.data = fetchData();
14+
const { name, alias } = moduleInfo;
15+
const dataFetchInfo = getDataFetchInfo({
16+
name,
17+
alias,
18+
id,
19+
});
20+
if (!dataFetchInfo) {
21+
return args;
22+
}
23+
const { dataFetchId, dataFetchName } = dataFetchInfo;
24+
25+
const { modules, version } = remoteSnapshot;
26+
27+
const key = `${name}@${version}@${dataFetchName}`;
28+
if (helpers.global.nativeGlobal.__FEDERATION__.__DATA_FETCH_MAP__[key]) {
29+
return args;
1430
}
1531

32+
if (modules.find((module) => module.moduleName === dataFetchName)) {
33+
return args;
34+
}
35+
36+
helpers.global.nativeGlobal.__FEDERATION__.__DATA_FETCH_MAP__[key] = host
37+
.loadRemote(dataFetchId)
38+
.then((m) => {
39+
if (
40+
m &&
41+
typeof m === 'object' &&
42+
'prefetch' in m &&
43+
typeof m.prefetch === 'function'
44+
) {
45+
return m.prefetch();
46+
}
47+
});
48+
1649
return args;
1750
},
1851
});

packages/modernjs/src/cli/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ export const patchMFConfig = (
152152
runtimePlugins,
153153
);
154154

155+
injectRuntimePlugins(
156+
require.resolve('@module-federation/modern-js/auto-fetch-data'),
157+
runtimePlugins,
158+
);
159+
155160
if (isDev) {
156161
injectRuntimePlugins(
157162
require.resolve('@module-federation/modern-js/resolve-entry-ipv4'),

packages/modernjs/src/runtime/createRemoteSSRComponent.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import helpers from '@module-federation/runtime/helpers';
23
import {
34
getInstance,
45
type FederationHost,
@@ -7,6 +8,7 @@ import {
78
ErrorBoundary,
89
ErrorBoundaryPropsWithComponent,
910
} from 'react-error-boundary';
11+
import { getDataFetchInfo } from './utils';
1012

1113
type IProps = {
1214
id: string;
@@ -16,7 +18,7 @@ type IProps = {
1618

1719
type ReactKey = { key?: React.Key | null };
1820

19-
async function fetchData(id: string) {
21+
async function fetchData(id: string): Promise<unknown | undefined> {
2022
const instance = getInstance();
2123
if (!instance) {
2224
return;
@@ -29,11 +31,16 @@ async function fetchData(id: string) {
2931
if (!module) {
3032
return;
3133
}
32-
const dataFetch = module.dataFetchMap[id];
33-
if (!dataFetch) {
34+
const dataFetchInfo = getDataFetchInfo({
35+
id,
36+
name,
37+
alias: module.remoteInfo.alias,
38+
});
39+
if (!dataFetchInfo) {
3440
return;
3541
}
36-
return dataFetch;
42+
const key = `${name}@${module.remoteInfo.version}@${dataFetchInfo.dataFetchName}`;
43+
return helpers.global.nativeGlobal.__FEDERATION__.__DATA_FETCH_MAP__[key];
3744
}
3845

3946
function getLoadedRemoteInfos(instance: FederationHost, id: string) {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export const getDataFetchInfo = ({
2+
name,
3+
alias,
4+
id,
5+
}: {
6+
id: string;
7+
name: string;
8+
alias?: string;
9+
}) => {
10+
const regex = new RegExp(`^${name}(/[^/].*|)$`);
11+
const nameOrAlias = regex.test(id) ? name : alias || name;
12+
const DATA_FETCH = 'mf-data';
13+
14+
const expose = id.replace(nameOrAlias, '');
15+
let dataFetchName = '';
16+
let dataFetchId = '';
17+
if (expose.startsWith('/')) {
18+
dataFetchName = `${expose.slice(1)}.${DATA_FETCH}`;
19+
dataFetchId = `${id}.${DATA_FETCH}`;
20+
} else if (expose === '') {
21+
dataFetchName = DATA_FETCH;
22+
dataFetchId = `${id}/${DATA_FETCH}`;
23+
} else {
24+
return;
25+
}
26+
27+
if (!dataFetchName || !dataFetchId) {
28+
return;
29+
}
30+
31+
return {
32+
dataFetchName,
33+
dataFetchId,
34+
};
35+
};

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ export class SnapshotHandler {
8282
void
8383
>('beforeLoadRemoteSnapshot'),
8484
loadSnapshot: new AsyncWaterfallHook<{
85-
id?: string;
86-
expose?: string;
8785
options: Options;
8886
moduleInfo: Remote;
8987
hostGlobalSnapshot: GlobalModuleInfo[string] | undefined;
@@ -99,6 +97,8 @@ export class SnapshotHandler {
9997
from: 'global' | 'manifest';
10098
}>('loadRemoteSnapshot'),
10199
afterLoadSnapshot: new AsyncWaterfallHook<{
100+
id?: string;
101+
host: FederationHost;
102102
options: Options;
103103
moduleInfo: Remote;
104104
remoteSnapshot: ModuleInfo;
@@ -175,8 +175,6 @@ export class SnapshotHandler {
175175
remoteSnapshot: globalRemoteSnapshot,
176176
globalSnapshot: globalSnapshotRes,
177177
} = await this.hooks.lifecycle.loadSnapshot.emit({
178-
id,
179-
expose,
180178
options,
181179
moduleInfo,
182180
hostGlobalSnapshot,
@@ -257,6 +255,8 @@ export class SnapshotHandler {
257255
}
258256

259257
await this.hooks.lifecycle.afterLoadSnapshot.emit({
258+
id,
259+
host: this.HostInstance,
260260
options,
261261
moduleInfo,
262262
remoteSnapshot: mSnapshot,

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export function snapshotPlugin(): FederationRuntimePlugin {
4848
await origin.snapshotHandler.loadRemoteSnapshotInfo({
4949
moduleInfo: remote,
5050
id,
51-
expose,
5251
});
5352

5453
assignRemoteInfo(remoteInfo, remoteSnapshot);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export type LoadShareExtraOptions = {
3131
};
3232

3333
export interface RemoteInfo {
34+
alias?: string;
3435
name: string;
3536
version?: string;
3637
buildVersion?: string;

packages/sdk/src/generateSnapshotFromManifest.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
ManifestProvider,
88
} from './types';
99
import { MANIFEST_EXT } from './constant';
10-
import { isBrowserEnv } from './env';
1110

1211
interface IOptions {
1312
remotes?: Record<string, string>;

pnpm-lock.yaml

Lines changed: 29 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)