@module-federation/modern-js 0.14.3 → 0.15.0

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.
Files changed (91) hide show
  1. package/dist/cjs/cli/configPlugin.js +17 -13
  2. package/dist/cjs/cli/configPlugin.spec.js +3 -1
  3. package/dist/cjs/cli/index.js +3 -1
  4. package/dist/cjs/cli/mfRuntimePlugins/auto-fetch-data.js +98 -0
  5. package/dist/cjs/cli/server/data-fetch-server-plugin.js +189 -0
  6. package/dist/cjs/cli/ssrPlugin.js +31 -12
  7. package/dist/cjs/cli/utils.js +5 -0
  8. package/dist/cjs/constant.js +49 -2
  9. package/dist/cjs/{ssr-runtime/index.js → interfaces/global.js} +2 -8
  10. package/dist/cjs/{cli/logger.js → logger.js} +1 -1
  11. package/dist/cjs/runtime/AwaitDataFetch.js +144 -0
  12. package/dist/cjs/runtime/createRemoteComponent.js +327 -0
  13. package/dist/cjs/runtime/index.js +15 -3
  14. package/dist/cjs/{cli/constant.js → runtime/wrapNoSSR.js} +13 -6
  15. package/dist/cjs/ssr-runtime/{plugin.js → devPlugin.js} +9 -6
  16. package/dist/cjs/ssr-runtime/downgrade.js +114 -0
  17. package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +136 -0
  18. package/dist/cjs/utils/dataFetch.js +211 -0
  19. package/dist/cjs/utils/index.js +54 -0
  20. package/dist/esm/cli/configPlugin.js +18 -14
  21. package/dist/esm/cli/configPlugin.spec.js +3 -1
  22. package/dist/esm/cli/index.js +3 -1
  23. package/dist/esm/cli/mfRuntimePlugins/auto-fetch-data.js +74 -0
  24. package/dist/esm/cli/server/data-fetch-server-plugin.js +237 -0
  25. package/dist/esm/cli/ssrPlugin.js +28 -8
  26. package/dist/esm/cli/utils.js +4 -0
  27. package/dist/esm/constant.js +37 -1
  28. package/dist/esm/interfaces/global.js +0 -0
  29. package/dist/esm/{cli/logger.js → logger.js} +1 -1
  30. package/dist/esm/runtime/AwaitDataFetch.js +131 -0
  31. package/dist/esm/runtime/createRemoteComponent.js +417 -0
  32. package/dist/esm/runtime/index.js +13 -3
  33. package/dist/esm/runtime/wrapNoSSR.js +12 -0
  34. package/dist/esm/ssr-runtime/{plugin.js → devPlugin.js} +5 -2
  35. package/dist/esm/ssr-runtime/downgrade.js +150 -0
  36. package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js +177 -0
  37. package/dist/esm/utils/dataFetch.js +237 -0
  38. package/dist/esm/utils/index.js +28 -0
  39. package/dist/esm-node/cli/configPlugin.js +18 -14
  40. package/dist/esm-node/cli/configPlugin.spec.js +3 -1
  41. package/dist/esm-node/cli/index.js +3 -1
  42. package/dist/esm-node/cli/mfRuntimePlugins/auto-fetch-data.js +68 -0
  43. package/dist/esm-node/cli/server/data-fetch-server-plugin.js +159 -0
  44. package/dist/esm-node/cli/ssrPlugin.js +27 -8
  45. package/dist/esm-node/cli/utils.js +4 -0
  46. package/dist/esm-node/constant.js +37 -1
  47. package/dist/esm-node/interfaces/global.js +0 -0
  48. package/dist/esm-node/{cli/logger.js → logger.js} +1 -1
  49. package/dist/esm-node/runtime/AwaitDataFetch.js +109 -0
  50. package/dist/esm-node/runtime/createRemoteComponent.js +291 -0
  51. package/dist/esm-node/runtime/index.js +13 -3
  52. package/dist/esm-node/runtime/wrapNoSSR.js +11 -0
  53. package/dist/esm-node/ssr-runtime/{plugin.js → devPlugin.js} +5 -2
  54. package/dist/esm-node/ssr-runtime/downgrade.js +88 -0
  55. package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.js +101 -0
  56. package/dist/esm-node/utils/dataFetch.js +166 -0
  57. package/dist/esm-node/utils/index.js +27 -0
  58. package/dist/types/cli/configPlugin.d.ts +1 -1
  59. package/dist/types/cli/index.d.ts +1 -0
  60. package/dist/types/cli/mfRuntimePlugins/auto-fetch-data.d.ts +3 -0
  61. package/dist/types/cli/server/data-fetch-server-plugin.d.ts +3 -0
  62. package/dist/types/cli/utils.d.ts +1 -0
  63. package/dist/types/constant.d.ts +23 -0
  64. package/dist/types/interfaces/global.d.ts +27 -0
  65. package/dist/types/runtime/AwaitDataFetch.d.ts +19 -0
  66. package/dist/types/runtime/createRemoteComponent.d.ts +26 -0
  67. package/dist/types/runtime/index.d.ts +7 -2
  68. package/dist/types/runtime/wrapNoSSR.d.ts +9 -0
  69. package/dist/types/ssr-runtime/devPlugin.d.ts +2 -0
  70. package/dist/types/ssr-runtime/downgrade.d.ts +4 -0
  71. package/dist/types/ssr-runtime/injectDataFetchFunctionPlugin.d.ts +7 -0
  72. package/dist/types/types/index.d.ts +2 -0
  73. package/dist/types/utils/dataFetch.d.ts +26 -0
  74. package/dist/types/utils/index.d.ts +15 -0
  75. package/package.json +37 -12
  76. package/dist/cjs/cli/manifest.js +0 -75
  77. package/dist/cjs/runtime/createRemoteSSRComponent.js +0 -201
  78. package/dist/esm/cli/constant.js +0 -4
  79. package/dist/esm/cli/manifest.js +0 -41
  80. package/dist/esm/runtime/createRemoteSSRComponent.js +0 -191
  81. package/dist/esm/ssr-runtime/index.js +0 -1
  82. package/dist/esm-node/cli/constant.js +0 -4
  83. package/dist/esm-node/cli/manifest.js +0 -41
  84. package/dist/esm-node/runtime/createRemoteSSRComponent.js +0 -166
  85. package/dist/esm-node/ssr-runtime/index.js +0 -1
  86. package/dist/types/cli/constant.d.ts +0 -1
  87. package/dist/types/cli/manifest.d.ts +0 -2
  88. package/dist/types/runtime/createRemoteSSRComponent.d.ts +0 -18
  89. package/dist/types/ssr-runtime/index.d.ts +0 -1
  90. package/dist/types/ssr-runtime/plugin.d.ts +0 -2
  91. /package/dist/types/{cli/logger.d.ts → logger.d.ts} +0 -0
@@ -0,0 +1,177 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
3
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
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
+ }
13
+ var injectDataFetchFunctionPlugin = function(param) {
14
+ var fetchServerQuery = param.fetchServerQuery;
15
+ return {
16
+ name: "@module-federation/inject-data-fetch-function-plugin",
17
+ setup: function(api) {
18
+ api.onBeforeRender(/* @__PURE__ */ _async_to_generator(function() {
19
+ var _globalThis, _DATA_FETCH_FUNCTION, dataFetchFunction, dataFetch;
20
+ return _ts_generator(this, function(_state) {
21
+ switch (_state.label) {
22
+ case 0:
23
+ setSSREnv({
24
+ fetchServerQuery
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];
138
+ return [
139
+ 4,
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
+ }()))
161
+ ];
162
+ case 1:
163
+ _state.sent();
164
+ dataFetch.push = dataFetchFunction;
165
+ return [
166
+ 2
167
+ ];
168
+ }
169
+ });
170
+ }));
171
+ }
172
+ };
173
+ };
174
+ export {
175
+ injectDataFetchFunctionPlugin,
176
+ setSSREnv
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
+ };
@@ -3,10 +3,10 @@ import { getIPV4, isWebTarget, skipByTarget } from "./utils";
3
3
  import { encodeName } from "@module-federation/sdk";
4
4
  import { bundle } from "@modern-js/node-bundle-require";
5
5
  import { LOCALHOST, PLUGIN_IDENTIFIER } from "../constant";
6
- import { autoDeleteSplitChunkCacheGroups } from "@module-federation/rsbuild-plugin/utils";
7
- import logger from "./logger";
6
+ import { autoDeleteSplitChunkCacheGroups, addDataFetchExposes } from "@module-federation/rsbuild-plugin/utils";
7
+ import logger from "../logger";
8
+ import { isDev } from "./utils";
8
9
  const defaultPath = path.resolve(process.cwd(), "module-federation.config.ts");
9
- const isDev = process.env.NODE_ENV === "development";
10
10
  function setEnv(enableSSR) {
11
11
  if (enableSSR) {
12
12
  process.env["MF_DISABLE_EMIT_STATS"] = "true";
@@ -93,8 +93,9 @@ const patchDTSConfig = (mfConfig, isServer) => {
93
93
  }
94
94
  }
95
95
  };
96
- const patchMFConfig = (mfConfig, isServer, remoteIpStrategy) => {
96
+ const patchMFConfig = (mfConfig, isServer, remoteIpStrategy, enableSSR) => {
97
97
  replaceRemoteUrl(mfConfig, remoteIpStrategy);
98
+ addDataFetchExposes(mfConfig.exposes, isServer);
98
99
  if (mfConfig.remoteType === void 0) {
99
100
  mfConfig.remoteType = "script";
100
101
  }
@@ -106,12 +107,13 @@ const patchMFConfig = (mfConfig, isServer, remoteIpStrategy) => {
106
107
  ];
107
108
  patchDTSConfig(mfConfig, isServer);
108
109
  injectRuntimePlugins(require.resolve("@module-federation/modern-js/shared-strategy"), runtimePlugins);
109
- if (isDev) {
110
+ injectRuntimePlugins(require.resolve("@module-federation/modern-js/auto-fetch-data"), runtimePlugins);
111
+ if (enableSSR && isDev()) {
110
112
  injectRuntimePlugins(require.resolve("@module-federation/modern-js/resolve-entry-ipv4"), runtimePlugins);
111
113
  }
112
114
  if (isServer) {
113
115
  injectRuntimePlugins(require.resolve("@module-federation/node/runtimePlugin"), runtimePlugins);
114
- if (isDev) {
116
+ if (isDev()) {
115
117
  injectRuntimePlugins(require.resolve("@module-federation/node/record-dynamic-remote-entry-hash-plugin"), runtimePlugins);
116
118
  }
117
119
  injectRuntimePlugins(require.resolve("@module-federation/modern-js/inject-node-fetch"), runtimePlugins);
@@ -211,7 +213,7 @@ function patchBundlerConfig(options) {
211
213
  splitChunkConfig.chunks = "async";
212
214
  logger.warn(`splitChunks.chunks = async is not allowed with stream SSR mode, it will auto changed to "async"`);
213
215
  }
214
- if (isDev && chain.output.get("publicPath") === "auto") {
216
+ if (isDev() && chain.output.get("publicPath") === "auto") {
215
217
  var _modernjsConfig_dev, _modernjsConfig_server;
216
218
  const port = ((_modernjsConfig_dev = modernjsConfig.dev) === null || _modernjsConfig_dev === void 0 ? void 0 : _modernjsConfig_dev.port) || ((_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.port) || 8080;
217
219
  const publicPath = `http://localhost:${port}/`;
@@ -225,7 +227,7 @@ function patchBundlerConfig(options) {
225
227
  chain.output.chunkFilename(chunkFileName.replace(".js", suffix));
226
228
  }
227
229
  }
228
- if (isDev && enableSSR && !isServer) {
230
+ if (isDev() && enableSSR && !isServer) {
229
231
  chain.resolve.fallback.set("crypto", false).set("stream", false).set("vm", false);
230
232
  }
231
233
  if (((_modernjsConfig_deploy = modernjsConfig.deploy) === null || _modernjsConfig_deploy === void 0 ? void 0 : _modernjsConfig_deploy.microFrontend) && Object.keys(mfConfig.exposes || {}).length) {
@@ -258,9 +260,8 @@ const moduleFederationConfigPlugin = (userConfig) => ({
258
260
  const isWeb = isWebTarget(target);
259
261
  addMyTypes2Ignored(chain, !isWeb ? ssrConfig : csrConfig);
260
262
  const targetMFConfig = !isWeb ? ssrConfig : csrConfig;
261
- patchMFConfig(targetMFConfig, !isWeb, userConfig.remoteIpStrategy || "ipv4");
263
+ patchMFConfig(targetMFConfig, !isWeb, userConfig.remoteIpStrategy || "ipv4", enableSSR);
262
264
  patchBundlerConfig({
263
- // @ts-expect-error chain type is not correct
264
265
  chain,
265
266
  isServer: !isWeb,
266
267
  modernjsConfig,
@@ -297,6 +298,12 @@ const moduleFederationConfigPlugin = (userConfig) => ({
297
298
  "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
298
299
  "Access-Control-Allow-Headers": "*"
299
300
  } : void 0;
301
+ const defineConfig = {
302
+ REMOTE_IP_STRATEGY: JSON.stringify(userConfig.remoteIpStrategy)
303
+ };
304
+ if (enableSSR && isDev()) {
305
+ defineConfig["FEDERATION_IPV4"] = JSON.stringify(ipv4);
306
+ }
300
307
  var _modernjsConfig_source_enableAsyncEntry;
301
308
  return {
302
309
  tools: {
@@ -309,10 +316,7 @@ const moduleFederationConfigPlugin = (userConfig) => ({
309
316
  // TODO: deprecated
310
317
  "@modern-js/runtime/mf": require.resolve("@module-federation/modern-js/runtime")
311
318
  },
312
- define: {
313
- FEDERATION_IPV4: JSON.stringify(ipv4),
314
- REMOTE_IP_STRATEGY: JSON.stringify(userConfig.remoteIpStrategy)
315
- },
319
+ define: defineConfig,
316
320
  enableAsyncEntry: bundlerType === "rspack" ? (_modernjsConfig_source_enableAsyncEntry = (_modernjsConfig_source = modernjsConfig.source) === null || _modernjsConfig_source === void 0 ? void 0 : _modernjsConfig_source.enableAsyncEntry) !== null && _modernjsConfig_source_enableAsyncEntry !== void 0 ? _modernjsConfig_source_enableAsyncEntry : true : (_modernjsConfig_source1 = modernjsConfig.source) === null || _modernjsConfig_source1 === void 0 ? void 0 : _modernjsConfig_source1.enableAsyncEntry
317
321
  },
318
322
  dev: {
@@ -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
  ],
@@ -65,7 +66,8 @@ describe("patchMFConfig", async () => {
65
66
  },
66
67
  remoteType: "script",
67
68
  runtimePlugins: [
68
- require.resolve("@module-federation/modern-js/shared-strategy")
69
+ require.resolve("@module-federation/modern-js/shared-strategy"),
70
+ require.resolve("@module-federation/modern-js/auto-fetch-data")
69
71
  ],
70
72
  shared: {
71
73
  react: {
@@ -4,6 +4,7 @@ import { moduleFederationConfigPlugin } from "./configPlugin";
4
4
  import { moduleFederationSSRPlugin } from "./ssrPlugin";
5
5
  import { isWebTarget } from "./utils";
6
6
  const moduleFederationPlugin = (userConfig = {}) => {
7
+ var _userConfig_fetchServerQuery;
7
8
  const internalModernPluginOptions = {
8
9
  csrConfig: void 0,
9
10
  ssrConfig: void 0,
@@ -12,7 +13,8 @@ const moduleFederationPlugin = (userConfig = {}) => {
12
13
  distOutputDir: "",
13
14
  originPluginOptions: userConfig,
14
15
  remoteIpStrategy: userConfig === null || userConfig === void 0 ? void 0 : userConfig.remoteIpStrategy,
15
- userConfig: userConfig || {}
16
+ userConfig: userConfig || {},
17
+ fetchServerQuery: (_userConfig_fetchServerQuery = userConfig.fetchServerQuery) !== null && _userConfig_fetchServerQuery !== void 0 ? _userConfig_fetchServerQuery : void 0
16
18
  };
17
19
  return {
18
20
  name: "@modern-js/plugin-module-federation",
@@ -0,0 +1,68 @@
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 downgradeType = remoteSnapshot.modules.find((module) => module.moduleName === `${dataFetchName}${DATA_FETCH_CLIENT_SUFFIX}`) ? MF_DATA_FETCH_TYPE.FETCH_CLIENT : MF_DATA_FETCH_TYPE.FETCH_SERVER;
45
+ let finalDataFetchId = dataFetchId;
46
+ if (typeof window !== "undefined") {
47
+ finalDataFetchId = downgradeType === MF_DATA_FETCH_TYPE.FETCH_CLIENT ? `${dataFetchId}${DATA_FETCH_CLIENT_SUFFIX}` : dataFetchId;
48
+ }
49
+ const getDataFetchGetter = () => loadDataFetchModule(host, finalDataFetchId);
50
+ const dataFetchFnItem = [
51
+ getDataFetchGetter,
52
+ downgradeType
53
+ ];
54
+ if (typeof window === "undefined" || isCSROnly()) {
55
+ dataFetchFnItem.push(getDataFetchGetter());
56
+ }
57
+ dataFetchMap[dataFetchMapKey] = [
58
+ dataFetchFnItem,
59
+ void 0,
60
+ MF_DATA_FETCH_STATUS.AWAIT
61
+ ];
62
+ return args;
63
+ }
64
+ });
65
+ var auto_fetch_data_default = autoFetchData;
66
+ export {
67
+ auto_fetch_data_default as default
68
+ };