@module-federation/modern-js 0.0.0-next-20250708134245 → 0.0.0-next-20250709075526
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/cli/configPlugin.js +1 -0
- package/dist/cjs/cli/configPlugin.spec.js +2 -1
- package/dist/cjs/cli/mfRuntimePlugins/auto-fetch-data.js +100 -0
- package/dist/cjs/cli/server/data-fetch-server-plugin.js +143 -2
- package/dist/cjs/constant.js +49 -2
- package/dist/cjs/interfaces/global.js +16 -0
- package/dist/cjs/runtime/AwaitDataFetch.js +144 -0
- package/dist/cjs/runtime/createRemoteComponent.js +327 -0
- package/dist/cjs/runtime/index.js +28 -0
- package/dist/cjs/runtime/{plugin.js → wrapNoSSR.js} +12 -20
- package/dist/cjs/ssr-runtime/devPlugin.js +2 -2
- package/dist/cjs/ssr-runtime/downgrade.js +114 -0
- package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +102 -6
- package/dist/cjs/utils/dataFetch.js +211 -0
- package/dist/cjs/utils/index.js +54 -0
- package/dist/esm/cli/configPlugin.js +1 -0
- package/dist/esm/cli/configPlugin.spec.js +2 -1
- package/dist/esm/cli/mfRuntimePlugins/auto-fetch-data.js +76 -0
- package/dist/esm/cli/server/data-fetch-server-plugin.js +219 -2
- package/dist/esm/constant.js +37 -1
- package/dist/esm/interfaces/global.js +0 -0
- package/dist/esm/runtime/AwaitDataFetch.js +131 -0
- package/dist/esm/runtime/createRemoteComponent.js +417 -0
- package/dist/esm/runtime/index.js +21 -0
- package/dist/esm/runtime/wrapNoSSR.js +12 -0
- package/dist/esm/ssr-runtime/devPlugin.js +1 -1
- package/dist/esm/ssr-runtime/downgrade.js +150 -0
- package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js +147 -4
- package/dist/esm/utils/dataFetch.js +237 -0
- package/dist/esm/utils/index.js +28 -0
- package/dist/esm-node/cli/configPlugin.js +1 -0
- package/dist/esm-node/cli/configPlugin.spec.js +2 -1
- package/dist/esm-node/cli/mfRuntimePlugins/auto-fetch-data.js +70 -0
- package/dist/esm-node/cli/server/data-fetch-server-plugin.js +143 -2
- package/dist/esm-node/constant.js +37 -1
- package/dist/esm-node/interfaces/global.js +0 -0
- package/dist/esm-node/runtime/AwaitDataFetch.js +109 -0
- package/dist/esm-node/runtime/createRemoteComponent.js +291 -0
- package/dist/esm-node/runtime/index.js +21 -0
- package/dist/esm-node/runtime/wrapNoSSR.js +11 -0
- package/dist/esm-node/ssr-runtime/devPlugin.js +1 -1
- package/dist/esm-node/ssr-runtime/downgrade.js +88 -0
- package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.js +89 -4
- package/dist/esm-node/utils/dataFetch.js +166 -0
- package/dist/esm-node/utils/index.js +27 -0
- package/dist/types/cli/mfRuntimePlugins/auto-fetch-data.d.ts +3 -0
- package/dist/types/constant.d.ts +23 -0
- package/dist/types/interfaces/global.d.ts +27 -0
- package/dist/types/runtime/AwaitDataFetch.d.ts +19 -0
- package/dist/types/runtime/createRemoteComponent.d.ts +26 -0
- package/dist/types/runtime/index.d.ts +9 -0
- package/dist/types/runtime/wrapNoSSR.d.ts +9 -0
- package/dist/types/ssr-runtime/SSRLiveReload.d.ts +1 -1
- package/dist/types/ssr-runtime/downgrade.d.ts +4 -0
- package/dist/types/ssr-runtime/injectDataFetchFunctionPlugin.d.ts +3 -0
- package/dist/types/utils/dataFetch.d.ts +26 -0
- package/dist/types/utils/index.d.ts +15 -0
- package/package.json +18 -23
- package/dist/cjs/react/index.js +0 -44
- package/dist/esm/react/index.js +0 -12
- package/dist/esm/runtime/plugin.js +0 -19
- package/dist/esm-node/react/index.js +0 -12
- package/dist/esm-node/runtime/plugin.js +0 -19
- package/dist/types/react/index.d.ts +0 -2
- package/dist/types/runtime/plugin.d.ts +0 -2
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
3
|
+
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
4
|
+
import { _ as _type_of } from "@swc/helpers/_/_type_of";
|
|
5
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
6
|
+
import { DATA_FETCH_QUERY, DOWNGRADE_KEY } from "../constant";
|
|
7
|
+
import { MF_DATA_FETCH_STATUS, MF_DATA_FETCH_TYPE } from "../constant";
|
|
8
|
+
import { getDataFetchMap } from "../utils";
|
|
9
|
+
function getDowngradeTag() {
|
|
10
|
+
return globalThis[DOWNGRADE_KEY];
|
|
11
|
+
}
|
|
12
|
+
function callAllDowngrade() {
|
|
13
|
+
var dataFetchMap = getDataFetchMap();
|
|
14
|
+
if (!dataFetchMap) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
Object.keys(dataFetchMap).forEach(function(key) {
|
|
18
|
+
callDowngrade(key);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function callDowngrade(id, params, remoteInfo) {
|
|
22
|
+
return _callDowngrade.apply(this, arguments);
|
|
23
|
+
}
|
|
24
|
+
function _callDowngrade() {
|
|
25
|
+
_callDowngrade = _async_to_generator(function(id, params, remoteInfo) {
|
|
26
|
+
var dataFetchMap, mfDataFetch, promise, res, rej, dataFetchType, currentUrl, fetchServerQuery, fetchUrl, data, e;
|
|
27
|
+
return _ts_generator(this, function(_state) {
|
|
28
|
+
switch (_state.label) {
|
|
29
|
+
case 0:
|
|
30
|
+
dataFetchMap = getDataFetchMap();
|
|
31
|
+
if (!dataFetchMap) {
|
|
32
|
+
return [
|
|
33
|
+
2
|
|
34
|
+
];
|
|
35
|
+
}
|
|
36
|
+
mfDataFetch = dataFetchMap[id];
|
|
37
|
+
if (!((mfDataFetch === null || mfDataFetch === void 0 ? void 0 : mfDataFetch[2]) === MF_DATA_FETCH_STATUS.AWAIT))
|
|
38
|
+
return [
|
|
39
|
+
3,
|
|
40
|
+
6
|
|
41
|
+
];
|
|
42
|
+
mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADING;
|
|
43
|
+
if (mfDataFetch[1]) {
|
|
44
|
+
promise = mfDataFetch[1][0];
|
|
45
|
+
res = mfDataFetch[1][1];
|
|
46
|
+
rej = mfDataFetch[1][2];
|
|
47
|
+
} else {
|
|
48
|
+
promise = new Promise(function(resolve, reject) {
|
|
49
|
+
res = resolve;
|
|
50
|
+
rej = reject;
|
|
51
|
+
});
|
|
52
|
+
mfDataFetch[1] = [
|
|
53
|
+
promise,
|
|
54
|
+
res,
|
|
55
|
+
rej
|
|
56
|
+
];
|
|
57
|
+
}
|
|
58
|
+
dataFetchType = mfDataFetch[0][1];
|
|
59
|
+
if (!(dataFetchType === MF_DATA_FETCH_TYPE.FETCH_CLIENT))
|
|
60
|
+
return [
|
|
61
|
+
3,
|
|
62
|
+
1
|
|
63
|
+
];
|
|
64
|
+
try {
|
|
65
|
+
mfDataFetch[0][0]().then(function(getDataFetchFn) {
|
|
66
|
+
return getDataFetchFn(_object_spread_props(_object_spread({}, params), {
|
|
67
|
+
isDowngrade: true
|
|
68
|
+
})).then(function(data2) {
|
|
69
|
+
mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADED;
|
|
70
|
+
res(data2);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
} catch (e2) {
|
|
74
|
+
mfDataFetch[2] = MF_DATA_FETCH_STATUS.ERROR;
|
|
75
|
+
rej(e2);
|
|
76
|
+
}
|
|
77
|
+
return [
|
|
78
|
+
3,
|
|
79
|
+
5
|
|
80
|
+
];
|
|
81
|
+
case 1:
|
|
82
|
+
if (!(dataFetchType === MF_DATA_FETCH_TYPE.FETCH_SERVER))
|
|
83
|
+
return [
|
|
84
|
+
3,
|
|
85
|
+
5
|
|
86
|
+
];
|
|
87
|
+
_state.label = 2;
|
|
88
|
+
case 2:
|
|
89
|
+
_state.trys.push([
|
|
90
|
+
2,
|
|
91
|
+
4,
|
|
92
|
+
,
|
|
93
|
+
5
|
|
94
|
+
]);
|
|
95
|
+
currentUrl = new URL(window.location.href);
|
|
96
|
+
currentUrl.searchParams.set(DATA_FETCH_QUERY, encodeURIComponent(id));
|
|
97
|
+
if (params) {
|
|
98
|
+
currentUrl.searchParams.set("params", encodeURIComponent(JSON.stringify(params)));
|
|
99
|
+
}
|
|
100
|
+
if (remoteInfo) {
|
|
101
|
+
currentUrl.searchParams.set("remoteInfo", encodeURIComponent(JSON.stringify(remoteInfo)));
|
|
102
|
+
}
|
|
103
|
+
fetchServerQuery = globalThis.FEDERATION_SERVER_QUERY;
|
|
104
|
+
if (fetchServerQuery && (typeof fetchServerQuery === "undefined" ? "undefined" : _type_of(fetchServerQuery)) === "object") {
|
|
105
|
+
Object.keys(fetchServerQuery).forEach(function(key) {
|
|
106
|
+
currentUrl.searchParams.set(key, JSON.stringify(fetchServerQuery[key]));
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
fetchUrl = currentUrl.toString();
|
|
110
|
+
return [
|
|
111
|
+
4,
|
|
112
|
+
fetch(fetchUrl).then(function(res2) {
|
|
113
|
+
return res2.json();
|
|
114
|
+
})
|
|
115
|
+
];
|
|
116
|
+
case 3:
|
|
117
|
+
data = _state.sent();
|
|
118
|
+
mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADED;
|
|
119
|
+
res(data);
|
|
120
|
+
return [
|
|
121
|
+
3,
|
|
122
|
+
5
|
|
123
|
+
];
|
|
124
|
+
case 4:
|
|
125
|
+
e = _state.sent();
|
|
126
|
+
mfDataFetch[2] = MF_DATA_FETCH_STATUS.ERROR;
|
|
127
|
+
rej(e);
|
|
128
|
+
return [
|
|
129
|
+
3,
|
|
130
|
+
5
|
|
131
|
+
];
|
|
132
|
+
case 5:
|
|
133
|
+
return [
|
|
134
|
+
2,
|
|
135
|
+
promise
|
|
136
|
+
];
|
|
137
|
+
case 6:
|
|
138
|
+
return [
|
|
139
|
+
2
|
|
140
|
+
];
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
return _callDowngrade.apply(this, arguments);
|
|
145
|
+
}
|
|
146
|
+
export {
|
|
147
|
+
callAllDowngrade,
|
|
148
|
+
callDowngrade,
|
|
149
|
+
getDowngradeTag
|
|
150
|
+
};
|
|
@@ -1,25 +1,167 @@
|
|
|
1
1
|
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
|
|
2
3
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
4
|
+
import { DATA_FETCH_FUNCTION, DOWNGRADE_KEY, FS_HREF, MF_DATA_FETCH_STATUS, MF_DATA_FETCH_TYPE } from "../constant";
|
|
5
|
+
import logger from "../logger";
|
|
6
|
+
import { getDataFetchMap, getDataFetchItem, initDataFetchMap } from "../utils";
|
|
7
|
+
import { callAllDowngrade, callDowngrade, getDowngradeTag } from "./downgrade";
|
|
8
|
+
function setSSREnv(param) {
|
|
9
|
+
var fetchServerQuery = param.fetchServerQuery;
|
|
10
|
+
globalThis.FEDERATION_SSR = true;
|
|
11
|
+
globalThis.FEDERATION_SERVER_QUERY = fetchServerQuery;
|
|
12
|
+
}
|
|
5
13
|
var injectDataFetchFunctionPlugin = function(param) {
|
|
6
14
|
var fetchServerQuery = param.fetchServerQuery;
|
|
7
15
|
return {
|
|
8
16
|
name: "@module-federation/inject-data-fetch-function-plugin",
|
|
9
17
|
setup: function(api) {
|
|
10
18
|
api.onBeforeRender(/* @__PURE__ */ _async_to_generator(function() {
|
|
19
|
+
var _globalThis, _DATA_FETCH_FUNCTION, dataFetchFunction, dataFetch;
|
|
11
20
|
return _ts_generator(this, function(_state) {
|
|
12
21
|
switch (_state.label) {
|
|
13
22
|
case 0:
|
|
14
23
|
setSSREnv({
|
|
15
24
|
fetchServerQuery
|
|
16
25
|
});
|
|
26
|
+
if (typeof window === "undefined") {
|
|
27
|
+
return [
|
|
28
|
+
2
|
|
29
|
+
];
|
|
30
|
+
}
|
|
31
|
+
dataFetchFunction = function() {
|
|
32
|
+
var _ref = _async_to_generator(function(options) {
|
|
33
|
+
var _options, id, data, downgrade, dataFetchItem, _dataFetchItem__, _dataFetchItem_, dataFetchMap, res, rej, p, mfDowngrade, mfDowngrade1;
|
|
34
|
+
return _ts_generator(this, function(_state2) {
|
|
35
|
+
_options = _sliced_to_array(options, 3), id = _options[0], data = _options[1], downgrade = _options[2];
|
|
36
|
+
logger.debug("==========call data fetch function!");
|
|
37
|
+
if (data) {
|
|
38
|
+
if (!id) {
|
|
39
|
+
throw new Error("id is required!");
|
|
40
|
+
}
|
|
41
|
+
if (!getDataFetchMap()) {
|
|
42
|
+
initDataFetchMap();
|
|
43
|
+
}
|
|
44
|
+
dataFetchItem = getDataFetchItem(id);
|
|
45
|
+
if (dataFetchItem) {
|
|
46
|
+
;
|
|
47
|
+
(_dataFetchItem_ = dataFetchItem[1]) === null || _dataFetchItem_ === void 0 ? void 0 : (_dataFetchItem__ = _dataFetchItem_[1]) === null || _dataFetchItem__ === void 0 ? void 0 : _dataFetchItem__.call(_dataFetchItem_, data);
|
|
48
|
+
dataFetchItem[2] = MF_DATA_FETCH_STATUS.LOADED;
|
|
49
|
+
return [
|
|
50
|
+
2
|
|
51
|
+
];
|
|
52
|
+
}
|
|
53
|
+
if (!dataFetchItem) {
|
|
54
|
+
dataFetchMap = getDataFetchMap();
|
|
55
|
+
;
|
|
56
|
+
;
|
|
57
|
+
p = new Promise(function(resolve, reject) {
|
|
58
|
+
res = resolve;
|
|
59
|
+
rej = reject;
|
|
60
|
+
});
|
|
61
|
+
dataFetchMap[id] = [
|
|
62
|
+
[
|
|
63
|
+
/* @__PURE__ */ _async_to_generator(function() {
|
|
64
|
+
return _ts_generator(this, function(_state3) {
|
|
65
|
+
return [
|
|
66
|
+
2,
|
|
67
|
+
_async_to_generator(function() {
|
|
68
|
+
return _ts_generator(this, function(_state4) {
|
|
69
|
+
return [
|
|
70
|
+
2,
|
|
71
|
+
""
|
|
72
|
+
];
|
|
73
|
+
});
|
|
74
|
+
})
|
|
75
|
+
];
|
|
76
|
+
});
|
|
77
|
+
}),
|
|
78
|
+
MF_DATA_FETCH_TYPE.FETCH_SERVER
|
|
79
|
+
],
|
|
80
|
+
[
|
|
81
|
+
p,
|
|
82
|
+
res,
|
|
83
|
+
rej
|
|
84
|
+
],
|
|
85
|
+
MF_DATA_FETCH_STATUS.LOADED
|
|
86
|
+
];
|
|
87
|
+
res(data);
|
|
88
|
+
return [
|
|
89
|
+
2
|
|
90
|
+
];
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (downgrade) {
|
|
94
|
+
mfDowngrade = getDowngradeTag();
|
|
95
|
+
if (!mfDowngrade) {
|
|
96
|
+
globalThis[DOWNGRADE_KEY] = id ? [
|
|
97
|
+
id
|
|
98
|
+
] : true;
|
|
99
|
+
} else if (Array.isArray(mfDowngrade) && id && !mfDowngrade.includes(id)) {
|
|
100
|
+
mfDowngrade.push(id);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
mfDowngrade1 = getDowngradeTag();
|
|
104
|
+
if (typeof mfDowngrade1 === "boolean") {
|
|
105
|
+
return [
|
|
106
|
+
2,
|
|
107
|
+
callAllDowngrade()
|
|
108
|
+
];
|
|
109
|
+
}
|
|
110
|
+
if (Array.isArray(mfDowngrade1)) {
|
|
111
|
+
if (!id) {
|
|
112
|
+
globalThis[DOWNGRADE_KEY] = true;
|
|
113
|
+
return [
|
|
114
|
+
2,
|
|
115
|
+
callAllDowngrade()
|
|
116
|
+
];
|
|
117
|
+
}
|
|
118
|
+
if (!mfDowngrade1.includes(id)) {
|
|
119
|
+
mfDowngrade1.push(id);
|
|
120
|
+
}
|
|
121
|
+
return [
|
|
122
|
+
2,
|
|
123
|
+
callDowngrade(id)
|
|
124
|
+
];
|
|
125
|
+
}
|
|
126
|
+
return [
|
|
127
|
+
2
|
|
128
|
+
];
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
return function dataFetchFunction2(options) {
|
|
132
|
+
return _ref.apply(this, arguments);
|
|
133
|
+
};
|
|
134
|
+
}();
|
|
135
|
+
globalThis[FS_HREF] = window.location.href;
|
|
136
|
+
(_globalThis = globalThis)[_DATA_FETCH_FUNCTION = DATA_FETCH_FUNCTION] || (_globalThis[_DATA_FETCH_FUNCTION] = []);
|
|
137
|
+
dataFetch = globalThis[DATA_FETCH_FUNCTION];
|
|
17
138
|
return [
|
|
18
139
|
4,
|
|
19
|
-
|
|
140
|
+
Promise.all(dataFetch.map(function() {
|
|
141
|
+
var _ref = _async_to_generator(function(options) {
|
|
142
|
+
return _ts_generator(this, function(_state2) {
|
|
143
|
+
switch (_state2.label) {
|
|
144
|
+
case 0:
|
|
145
|
+
return [
|
|
146
|
+
4,
|
|
147
|
+
dataFetchFunction(options)
|
|
148
|
+
];
|
|
149
|
+
case 1:
|
|
150
|
+
_state2.sent();
|
|
151
|
+
return [
|
|
152
|
+
2
|
|
153
|
+
];
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
return function(options) {
|
|
158
|
+
return _ref.apply(this, arguments);
|
|
159
|
+
};
|
|
160
|
+
}()))
|
|
20
161
|
];
|
|
21
162
|
case 1:
|
|
22
163
|
_state.sent();
|
|
164
|
+
dataFetch.push = dataFetchFunction;
|
|
23
165
|
return [
|
|
24
166
|
2
|
|
25
167
|
];
|
|
@@ -30,5 +172,6 @@ var injectDataFetchFunctionPlugin = function(param) {
|
|
|
30
172
|
};
|
|
31
173
|
};
|
|
32
174
|
export {
|
|
33
|
-
injectDataFetchFunctionPlugin
|
|
175
|
+
injectDataFetchFunctionPlugin,
|
|
176
|
+
setSSREnv
|
|
34
177
|
};
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
3
|
+
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
4
|
+
import { _ as _to_array } from "@swc/helpers/_/_to_array";
|
|
5
|
+
import { _ as _type_of } from "@swc/helpers/_/_type_of";
|
|
6
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
7
|
+
import { isBrowserEnv, composeKeyWithSeparator } from "@module-federation/sdk";
|
|
8
|
+
import { isCSROnly } from "../utils";
|
|
9
|
+
import logger from "../logger";
|
|
10
|
+
import { callDowngrade, getDowngradeTag } from "../ssr-runtime/downgrade";
|
|
11
|
+
import { DOWNGRADE_KEY, MF_DATA_FETCH_STATUS, WRAP_DATA_FETCH_ID_IDENTIFIER } from "../constant";
|
|
12
|
+
import { DATA_FETCH_CLIENT_SUFFIX, DATA_FETCH_IDENTIFIER } from "@module-federation/rsbuild-plugin/constant";
|
|
13
|
+
var getDataFetchInfo = function(param) {
|
|
14
|
+
var name = param.name, alias = param.alias, id = param.id, remoteSnapshot = param.remoteSnapshot;
|
|
15
|
+
if (!remoteSnapshot) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (!("modules" in remoteSnapshot)) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
var regex = new RegExp("^".concat(name, "(/[^/].*|)$"));
|
|
22
|
+
var nameOrAlias = regex.test(id) ? name : alias || name;
|
|
23
|
+
var expose = id.replace(nameOrAlias, "");
|
|
24
|
+
var dataFetchName = "";
|
|
25
|
+
var dataFetchId = "";
|
|
26
|
+
if (expose.startsWith("/")) {
|
|
27
|
+
dataFetchName = "".concat(expose.slice(1), ".").concat(DATA_FETCH_IDENTIFIER);
|
|
28
|
+
dataFetchId = "".concat(id, ".").concat(DATA_FETCH_IDENTIFIER);
|
|
29
|
+
} else if (expose === "") {
|
|
30
|
+
dataFetchName = DATA_FETCH_IDENTIFIER;
|
|
31
|
+
dataFetchId = "".concat(id, "/").concat(DATA_FETCH_IDENTIFIER);
|
|
32
|
+
} else {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (!dataFetchName || !dataFetchId) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (!remoteSnapshot.modules.find(function(module) {
|
|
39
|
+
return module.moduleName === dataFetchName;
|
|
40
|
+
})) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
dataFetchName,
|
|
45
|
+
dataFetchId
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
function initDataFetchMap() {
|
|
49
|
+
var _globalThis;
|
|
50
|
+
(_globalThis = globalThis).__MF_DATA_FETCH_MAP__ || (_globalThis.__MF_DATA_FETCH_MAP__ = {});
|
|
51
|
+
}
|
|
52
|
+
function getDataFetchItem(id) {
|
|
53
|
+
var _globalThis___MF_DATA_FETCH_MAP__;
|
|
54
|
+
return (_globalThis___MF_DATA_FETCH_MAP__ = globalThis.__MF_DATA_FETCH_MAP__) === null || _globalThis___MF_DATA_FETCH_MAP__ === void 0 ? void 0 : _globalThis___MF_DATA_FETCH_MAP__[id];
|
|
55
|
+
}
|
|
56
|
+
function getDataFetchMap() {
|
|
57
|
+
return globalThis.__MF_DATA_FETCH_MAP__;
|
|
58
|
+
}
|
|
59
|
+
var flushDataFetch = function() {
|
|
60
|
+
globalThis.__MF_DATA_FETCH_MAP__ = {};
|
|
61
|
+
globalThis[DOWNGRADE_KEY] = void 0;
|
|
62
|
+
};
|
|
63
|
+
function setDataFetchItemLoadedStatus(id) {
|
|
64
|
+
var dataFetchItem = getDataFetchItem(id);
|
|
65
|
+
if (!dataFetchItem) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
dataFetchItem[2] = MF_DATA_FETCH_STATUS.LOADED;
|
|
69
|
+
}
|
|
70
|
+
var wrapDataFetchId = function(id) {
|
|
71
|
+
return "".concat(WRAP_DATA_FETCH_ID_IDENTIFIER).concat(id).concat(WRAP_DATA_FETCH_ID_IDENTIFIER);
|
|
72
|
+
};
|
|
73
|
+
var getDataFetchIdWithErrorMsgs = function(errMsgs) {
|
|
74
|
+
var firstIdentifierIndex = errMsgs.indexOf(WRAP_DATA_FETCH_ID_IDENTIFIER);
|
|
75
|
+
if (firstIdentifierIndex === -1) {
|
|
76
|
+
return void 0;
|
|
77
|
+
}
|
|
78
|
+
var secondIdentifierIndex = errMsgs.indexOf(WRAP_DATA_FETCH_ID_IDENTIFIER, firstIdentifierIndex + WRAP_DATA_FETCH_ID_IDENTIFIER.length);
|
|
79
|
+
if (secondIdentifierIndex === -1) {
|
|
80
|
+
return void 0;
|
|
81
|
+
}
|
|
82
|
+
return errMsgs.substring(firstIdentifierIndex + WRAP_DATA_FETCH_ID_IDENTIFIER.length, secondIdentifierIndex);
|
|
83
|
+
};
|
|
84
|
+
function fetchData(id, params, remoteInfo) {
|
|
85
|
+
return _fetchData.apply(this, arguments);
|
|
86
|
+
}
|
|
87
|
+
function _fetchData() {
|
|
88
|
+
_fetchData = _async_to_generator(function(id, params, remoteInfo) {
|
|
89
|
+
var callFetchData, _dataFetchItem_, dataFetchItem, mfDowngrade, res, rej, p;
|
|
90
|
+
return _ts_generator(this, function(_state) {
|
|
91
|
+
callFetchData = function() {
|
|
92
|
+
var _ref = _async_to_generator(function() {
|
|
93
|
+
var item, _item, fetchDataFnArr, _rest, fetchDataFn;
|
|
94
|
+
return _ts_generator(this, function(_state2) {
|
|
95
|
+
switch (_state2.label) {
|
|
96
|
+
case 0:
|
|
97
|
+
item = getDataFetchItem(id);
|
|
98
|
+
if (!item) {
|
|
99
|
+
return [
|
|
100
|
+
2
|
|
101
|
+
];
|
|
102
|
+
}
|
|
103
|
+
_item = _to_array(item), fetchDataFnArr = _item[0], _rest = _item.slice(1);
|
|
104
|
+
return [
|
|
105
|
+
4,
|
|
106
|
+
fetchDataFnArr[2]
|
|
107
|
+
];
|
|
108
|
+
case 1:
|
|
109
|
+
fetchDataFn = _state2.sent();
|
|
110
|
+
if (!fetchDataFn) {
|
|
111
|
+
return [
|
|
112
|
+
2
|
|
113
|
+
];
|
|
114
|
+
}
|
|
115
|
+
return [
|
|
116
|
+
2,
|
|
117
|
+
fetchDataFn(params)
|
|
118
|
+
];
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
return function callFetchData2() {
|
|
123
|
+
return _ref.apply(this, arguments);
|
|
124
|
+
};
|
|
125
|
+
}();
|
|
126
|
+
if (isBrowserEnv()) {
|
|
127
|
+
;
|
|
128
|
+
dataFetchItem = getDataFetchItem(id);
|
|
129
|
+
if (!dataFetchItem) {
|
|
130
|
+
throw new Error("dataFetchItem not found, id: ".concat(id));
|
|
131
|
+
}
|
|
132
|
+
if ((_dataFetchItem_ = dataFetchItem[1]) === null || _dataFetchItem_ === void 0 ? void 0 : _dataFetchItem_[0]) {
|
|
133
|
+
return [
|
|
134
|
+
2,
|
|
135
|
+
dataFetchItem[1][0]
|
|
136
|
+
];
|
|
137
|
+
}
|
|
138
|
+
if (isCSROnly()) {
|
|
139
|
+
logger.debug("==========csr only!");
|
|
140
|
+
return [
|
|
141
|
+
2,
|
|
142
|
+
callFetchData()
|
|
143
|
+
];
|
|
144
|
+
}
|
|
145
|
+
if (remoteInfo) {
|
|
146
|
+
return [
|
|
147
|
+
2,
|
|
148
|
+
callDowngrade(id, params, remoteInfo)
|
|
149
|
+
];
|
|
150
|
+
}
|
|
151
|
+
mfDowngrade = getDowngradeTag();
|
|
152
|
+
if (mfDowngrade) {
|
|
153
|
+
if (typeof mfDowngrade === "boolean") {
|
|
154
|
+
return [
|
|
155
|
+
2,
|
|
156
|
+
callDowngrade(id, _object_spread_props(_object_spread({}, params), {
|
|
157
|
+
isDowngrade: true
|
|
158
|
+
}))
|
|
159
|
+
];
|
|
160
|
+
}
|
|
161
|
+
if (mfDowngrade.includes(id)) {
|
|
162
|
+
return [
|
|
163
|
+
2,
|
|
164
|
+
callDowngrade(id, _object_spread_props(_object_spread({}, params), {
|
|
165
|
+
isDowngrade: true
|
|
166
|
+
}))
|
|
167
|
+
];
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
;
|
|
171
|
+
;
|
|
172
|
+
p = new Promise(function(resolve, reject) {
|
|
173
|
+
res = resolve;
|
|
174
|
+
rej = reject;
|
|
175
|
+
});
|
|
176
|
+
dataFetchItem[1] = [
|
|
177
|
+
p,
|
|
178
|
+
res,
|
|
179
|
+
rej
|
|
180
|
+
];
|
|
181
|
+
dataFetchItem[2] = MF_DATA_FETCH_STATUS.AWAIT;
|
|
182
|
+
return [
|
|
183
|
+
2,
|
|
184
|
+
dataFetchItem[1][0]
|
|
185
|
+
];
|
|
186
|
+
}
|
|
187
|
+
return [
|
|
188
|
+
2,
|
|
189
|
+
callFetchData()
|
|
190
|
+
];
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
return _fetchData.apply(this, arguments);
|
|
194
|
+
}
|
|
195
|
+
function getDataFetchMapKey(dataFetchInfo, hostInfo) {
|
|
196
|
+
if (!dataFetchInfo || !hostInfo) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
var dataFetchId = dataFetchInfo.dataFetchId;
|
|
200
|
+
return composeKeyWithSeparator(dataFetchId, hostInfo.name, hostInfo.version);
|
|
201
|
+
}
|
|
202
|
+
function loadDataFetchModule(instance, id) {
|
|
203
|
+
return _loadDataFetchModule.apply(this, arguments);
|
|
204
|
+
}
|
|
205
|
+
function _loadDataFetchModule() {
|
|
206
|
+
_loadDataFetchModule = _async_to_generator(function(instance, id) {
|
|
207
|
+
return _ts_generator(this, function(_state) {
|
|
208
|
+
return [
|
|
209
|
+
2,
|
|
210
|
+
instance.loadRemote(id).then(function(m) {
|
|
211
|
+
if (m && (typeof m === "undefined" ? "undefined" : _type_of(m)) === "object" && "fetchData" in m && typeof m.fetchData === "function") {
|
|
212
|
+
return m.fetchData;
|
|
213
|
+
}
|
|
214
|
+
throw new Error("fetchData not found in remote ".concat(id, ", ").concat(JSON.stringify(m)));
|
|
215
|
+
})
|
|
216
|
+
];
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
return _loadDataFetchModule.apply(this, arguments);
|
|
220
|
+
}
|
|
221
|
+
function isDataLoaderExpose(exposeKey) {
|
|
222
|
+
return exposeKey.endsWith(DATA_FETCH_IDENTIFIER) || exposeKey.endsWith(DATA_FETCH_CLIENT_SUFFIX);
|
|
223
|
+
}
|
|
224
|
+
export {
|
|
225
|
+
fetchData,
|
|
226
|
+
flushDataFetch,
|
|
227
|
+
getDataFetchIdWithErrorMsgs,
|
|
228
|
+
getDataFetchInfo,
|
|
229
|
+
getDataFetchItem,
|
|
230
|
+
getDataFetchMap,
|
|
231
|
+
getDataFetchMapKey,
|
|
232
|
+
initDataFetchMap,
|
|
233
|
+
isDataLoaderExpose,
|
|
234
|
+
loadDataFetchModule,
|
|
235
|
+
setDataFetchItemLoadedStatus,
|
|
236
|
+
wrapDataFetchId
|
|
237
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
2
|
+
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
3
|
+
function getLoadedRemoteInfos(id, instance) {
|
|
4
|
+
if (!instance) {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
var _ref = instance.remoteHandler.idToRemoteMap[id] || {}, name = _ref.name, expose = _ref.expose;
|
|
8
|
+
if (!name) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
var module = instance.moduleCache.get(name);
|
|
12
|
+
if (!module) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
var remoteSnapshot = instance.snapshotHandler.getGlobalRemoteInfo(module.remoteInfo).remoteSnapshot;
|
|
16
|
+
return _object_spread_props(_object_spread({}, module.remoteInfo), {
|
|
17
|
+
snapshot: remoteSnapshot,
|
|
18
|
+
expose
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function isCSROnly() {
|
|
22
|
+
return window._SSR_DATA === void 0;
|
|
23
|
+
}
|
|
24
|
+
export * from "./dataFetch";
|
|
25
|
+
export {
|
|
26
|
+
getLoadedRemoteInfos,
|
|
27
|
+
isCSROnly
|
|
28
|
+
};
|
|
@@ -107,6 +107,7 @@ const patchMFConfig = (mfConfig, isServer, remoteIpStrategy, enableSSR) => {
|
|
|
107
107
|
];
|
|
108
108
|
patchDTSConfig(mfConfig, isServer);
|
|
109
109
|
injectRuntimePlugins(require.resolve("@module-federation/modern-js/shared-strategy"), runtimePlugins);
|
|
110
|
+
injectRuntimePlugins(require.resolve("@module-federation/modern-js/auto-fetch-data"), runtimePlugins);
|
|
110
111
|
if (enableSSR && isDev()) {
|
|
111
112
|
injectRuntimePlugins(require.resolve("@module-federation/modern-js/resolve-entry-ipv4"), runtimePlugins);
|
|
112
113
|
}
|
|
@@ -38,6 +38,7 @@ describe("patchMFConfig", async () => {
|
|
|
38
38
|
remoteType: "script",
|
|
39
39
|
runtimePlugins: [
|
|
40
40
|
require.resolve("@module-federation/modern-js/shared-strategy"),
|
|
41
|
+
require.resolve("@module-federation/modern-js/auto-fetch-data"),
|
|
41
42
|
require.resolve("@module-federation/node/runtimePlugin"),
|
|
42
43
|
require.resolve("@module-federation/modern-js/inject-node-fetch")
|
|
43
44
|
],
|
|
@@ -66,7 +67,7 @@ describe("patchMFConfig", async () => {
|
|
|
66
67
|
remoteType: "script",
|
|
67
68
|
runtimePlugins: [
|
|
68
69
|
require.resolve("@module-federation/modern-js/shared-strategy"),
|
|
69
|
-
require.resolve("@module-federation/
|
|
70
|
+
require.resolve("@module-federation/modern-js/auto-fetch-data")
|
|
70
71
|
],
|
|
71
72
|
shared: {
|
|
72
73
|
react: {
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { getDataFetchInfo, initDataFetchMap, getDataFetchItem, getDataFetchMap, isCSROnly } from "../../utils";
|
|
2
|
+
import logger from "../../logger";
|
|
3
|
+
import { getDataFetchMapKey, isDataLoaderExpose, loadDataFetchModule } from "../../utils/dataFetch";
|
|
4
|
+
import { MF_DATA_FETCH_TYPE, MF_DATA_FETCH_STATUS } from "../../constant";
|
|
5
|
+
import { DATA_FETCH_CLIENT_SUFFIX } from "@module-federation/rsbuild-plugin/constant";
|
|
6
|
+
const autoFetchData = () => ({
|
|
7
|
+
name: "auto-fetch-data-plugin",
|
|
8
|
+
beforeInit(args) {
|
|
9
|
+
initDataFetchMap();
|
|
10
|
+
return args;
|
|
11
|
+
},
|
|
12
|
+
afterLoadSnapshot(args) {
|
|
13
|
+
const { id, moduleInfo, remoteSnapshot, host } = args;
|
|
14
|
+
if (typeof id === "string" && isDataLoaderExpose(id)) {
|
|
15
|
+
return args;
|
|
16
|
+
}
|
|
17
|
+
if (!remoteSnapshot || !id || !("modules" in remoteSnapshot)) {
|
|
18
|
+
return args;
|
|
19
|
+
}
|
|
20
|
+
const { name, alias } = moduleInfo;
|
|
21
|
+
const dataFetchInfo = getDataFetchInfo({
|
|
22
|
+
name,
|
|
23
|
+
alias,
|
|
24
|
+
id,
|
|
25
|
+
remoteSnapshot
|
|
26
|
+
});
|
|
27
|
+
if (!dataFetchInfo) {
|
|
28
|
+
return args;
|
|
29
|
+
}
|
|
30
|
+
const { dataFetchId, dataFetchName } = dataFetchInfo;
|
|
31
|
+
const dataFetchMapKey = getDataFetchMapKey(dataFetchInfo, {
|
|
32
|
+
name: host.name,
|
|
33
|
+
version: host.options.version
|
|
34
|
+
});
|
|
35
|
+
logger.debug("======= auto fetch plugin dataFetchMapKey: ", dataFetchMapKey);
|
|
36
|
+
if (!dataFetchMapKey) {
|
|
37
|
+
return args;
|
|
38
|
+
}
|
|
39
|
+
const dataFetchItem = getDataFetchItem(dataFetchMapKey);
|
|
40
|
+
if (dataFetchItem) {
|
|
41
|
+
return args;
|
|
42
|
+
}
|
|
43
|
+
const dataFetchMap = getDataFetchMap();
|
|
44
|
+
const hasSSRAsset = Boolean(remoteSnapshot.ssrRemoteEntry);
|
|
45
|
+
const hasDataFetchClient = Boolean(remoteSnapshot.modules.find((module) => module.moduleName === `${dataFetchName}${DATA_FETCH_CLIENT_SUFFIX}`));
|
|
46
|
+
const downgradeType = hasDataFetchClient ? MF_DATA_FETCH_TYPE.FETCH_CLIENT : hasSSRAsset ? MF_DATA_FETCH_TYPE.FETCH_SERVER : MF_DATA_FETCH_TYPE.FETCH_CLIENT;
|
|
47
|
+
let finalDataFetchId = dataFetchId;
|
|
48
|
+
if (typeof window !== "undefined") {
|
|
49
|
+
finalDataFetchId = downgradeType === MF_DATA_FETCH_TYPE.FETCH_CLIENT ? hasDataFetchClient ? `${dataFetchId}${DATA_FETCH_CLIENT_SUFFIX}` : dataFetchId : dataFetchId;
|
|
50
|
+
}
|
|
51
|
+
const getDataFetchGetter = () => loadDataFetchModule(host, finalDataFetchId);
|
|
52
|
+
const dataFetchFnItem = [
|
|
53
|
+
getDataFetchGetter,
|
|
54
|
+
downgradeType
|
|
55
|
+
];
|
|
56
|
+
if (typeof window === "undefined" || isCSROnly()) {
|
|
57
|
+
dataFetchFnItem.push(getDataFetchGetter());
|
|
58
|
+
}
|
|
59
|
+
dataFetchMap[dataFetchMapKey] = [
|
|
60
|
+
dataFetchFnItem,
|
|
61
|
+
void 0,
|
|
62
|
+
MF_DATA_FETCH_STATUS.AWAIT
|
|
63
|
+
];
|
|
64
|
+
return args;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
var auto_fetch_data_default = autoFetchData;
|
|
68
|
+
export {
|
|
69
|
+
auto_fetch_data_default as default
|
|
70
|
+
};
|