1
1
import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime' ;
2
- import helpers from '@module-federation/runtime/helpers' ;
3
- import { getDataFetchInfo } from '../../runtime/utils' ;
2
+ import {
3
+ getDataFetchInfo ,
4
+ initDataFetchMap ,
5
+ getDataFetchItem ,
6
+ getDataFetchMap ,
7
+ isCSROnly ,
8
+ isSSRDowngrade ,
9
+ } from '../../runtime/utils' ;
4
10
import logger from '../../runtime/logger' ;
5
11
import { getDataFetchMapKey } from '../../runtime/dataFetch' ;
12
+ import type { MF_DATA_FETCH_MAP_VALUE } from '../../interfaces/global' ;
6
13
7
14
const autoFetchData : ( ) => FederationRuntimePlugin = ( ) => ( {
8
15
name : 'auto-fetch-data-plugin' ,
16
+ beforeInit ( args ) {
17
+ initDataFetchMap ( ) ;
18
+ return args ;
19
+ } ,
9
20
afterLoadSnapshot ( args ) {
10
- if ( typeof window !== 'undefined' ) {
11
- return args ;
12
- }
21
+ // if (typeof window !== 'undefined' && !(isCSROnly() || isSSRDowngrade() )) {
22
+ // if(!globalThis._MF__DATA_FETCH_ID_MAP__['_mfSSRDowngrade']){
23
+ // return args;
24
+ // }
25
+ // }
13
26
14
27
const { id, moduleInfo, remoteSnapshot, host } = args ;
15
28
if ( typeof id === 'string' && id . includes ( 'data' ) ) {
@@ -30,12 +43,20 @@ const autoFetchData: () => FederationRuntimePlugin = () => ({
30
43
}
31
44
const { dataFetchId, dataFetchName } = dataFetchInfo ;
32
45
46
+ if (
47
+ ! remoteSnapshot . modules . find (
48
+ ( module ) => module . moduleName === dataFetchName ,
49
+ )
50
+ ) {
51
+ return args ;
52
+ }
53
+
33
54
const { modules, version } = remoteSnapshot ;
34
55
35
- const dataFetchMapKey = getDataFetchMapKey (
36
- { name, version } ,
37
- dataFetchInfo ,
38
- ) ;
56
+ const dataFetchMapKey = getDataFetchMapKey ( dataFetchInfo , {
57
+ name : host . name ,
58
+ version : host . options . version ,
59
+ } ) ;
39
60
logger . debug (
40
61
'======= auto fetch plugin dataFetchMapKey: ' ,
41
62
dataFetchMapKey ,
@@ -45,11 +66,8 @@ const autoFetchData: () => FederationRuntimePlugin = () => ({
45
66
return args ;
46
67
}
47
68
48
- if (
49
- helpers . global . nativeGlobal . __FEDERATION__ . __DATA_FETCH_MAP__ [
50
- dataFetchMapKey
51
- ]
52
- ) {
69
+ const dataFetchItem = getDataFetchItem ( dataFetchMapKey ) ;
70
+ if ( dataFetchItem ) {
53
71
return args ;
54
72
}
55
73
@@ -62,9 +80,10 @@ const autoFetchData: () => FederationRuntimePlugin = () => ({
62
80
return args ;
63
81
}
64
82
65
- const fetchData = host
66
- . loadRemote ( dataFetchId )
67
- . then ( ( m ) => {
83
+ const dataFetchMap = getDataFetchMap ( ) ;
84
+
85
+ const getDataFetchGetter = ( ) =>
86
+ host . loadRemote ( dataFetchId ) . then ( ( m ) => {
68
87
if (
69
88
m &&
70
89
typeof m === 'object' &&
@@ -73,16 +92,23 @@ const autoFetchData: () => FederationRuntimePlugin = () => ({
73
92
) {
74
93
return m . fetchData as ( ) => Promise < unknown > ;
75
94
}
76
- } )
77
- . catch ( ( e ) => {
78
- console . log ( '======= auto fetch plugin fetchData error' , e ) ;
79
- return undefined ;
95
+ throw new Error (
96
+ `fetchData not found in remote ${ dataFetchId } , ${ JSON . stringify ( m ) } ` ,
97
+ ) ;
80
98
} ) ;
99
+ // .catch((e) => {
100
+ // console.log('======= auto fetch plugin fetchData error', e);
101
+ // return ()=>`${e}`;
102
+ // });
81
103
82
- helpers . global . nativeGlobal . __FEDERATION__ . __DATA_FETCH_MAP__ . set (
83
- dataFetchMapKey ,
84
- fetchData ,
85
- ) ;
104
+ const dataFetchFnItem : MF_DATA_FETCH_MAP_VALUE [ 0 ] = [ getDataFetchGetter ] ;
105
+
106
+ // server client must execute
107
+ if ( typeof window === 'undefined' || isCSROnly ( ) ) {
108
+ dataFetchFnItem . push ( getDataFetchGetter ( ) ) ;
109
+ }
110
+
111
+ dataFetchMap . set ( dataFetchMapKey , [ dataFetchFnItem ] ) ;
86
112
87
113
return args ;
88
114
} ,
0 commit comments