@module-federation/modern-js 0.14.3 → 0.16.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 +18 -14
  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 +100 -0
  5. package/dist/cjs/cli/server/data-fetch-server-plugin.js +189 -0
  6. package/dist/cjs/cli/ssrPlugin.js +33 -14
  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 +19 -15
  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 +76 -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 +19 -15
  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 +70 -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,136 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var injectDataFetchFunctionPlugin_exports = {};
30
+ __export(injectDataFetchFunctionPlugin_exports, {
31
+ injectDataFetchFunctionPlugin: () => injectDataFetchFunctionPlugin,
32
+ setSSREnv: () => setSSREnv
33
+ });
34
+ module.exports = __toCommonJS(injectDataFetchFunctionPlugin_exports);
35
+ var import_constant = require("../constant");
36
+ var import_logger = __toESM(require("../logger"));
37
+ var import_utils = require("../utils");
38
+ var import_downgrade = require("./downgrade");
39
+ function setSSREnv({ fetchServerQuery }) {
40
+ globalThis.FEDERATION_SSR = true;
41
+ globalThis.FEDERATION_SERVER_QUERY = fetchServerQuery;
42
+ }
43
+ const injectDataFetchFunctionPlugin = ({ fetchServerQuery }) => ({
44
+ name: "@module-federation/inject-data-fetch-function-plugin",
45
+ setup: (api) => {
46
+ api.onBeforeRender(async () => {
47
+ var _globalThis, _DATA_FETCH_FUNCTION;
48
+ setSSREnv({
49
+ fetchServerQuery
50
+ });
51
+ if (typeof window === "undefined") {
52
+ return;
53
+ }
54
+ const dataFetchFunction = async function(options) {
55
+ const [id, data, downgrade] = options;
56
+ import_logger.default.debug("==========call data fetch function!");
57
+ if (data) {
58
+ if (!id) {
59
+ throw new Error("id is required!");
60
+ }
61
+ if (!(0, import_utils.getDataFetchMap)()) {
62
+ (0, import_utils.initDataFetchMap)();
63
+ }
64
+ const dataFetchItem = (0, import_utils.getDataFetchItem)(id);
65
+ if (dataFetchItem) {
66
+ var _dataFetchItem__, _dataFetchItem_;
67
+ (_dataFetchItem_ = dataFetchItem[1]) === null || _dataFetchItem_ === void 0 ? void 0 : (_dataFetchItem__ = _dataFetchItem_[1]) === null || _dataFetchItem__ === void 0 ? void 0 : _dataFetchItem__.call(_dataFetchItem_, data);
68
+ dataFetchItem[2] = import_constant.MF_DATA_FETCH_STATUS.LOADED;
69
+ return;
70
+ }
71
+ if (!dataFetchItem) {
72
+ const dataFetchMap = (0, import_utils.getDataFetchMap)();
73
+ let res;
74
+ let rej;
75
+ const p = new Promise((resolve, reject) => {
76
+ res = resolve;
77
+ rej = reject;
78
+ });
79
+ dataFetchMap[id] = [
80
+ [
81
+ async () => async () => {
82
+ return "";
83
+ },
84
+ import_constant.MF_DATA_FETCH_TYPE.FETCH_SERVER
85
+ ],
86
+ [
87
+ p,
88
+ res,
89
+ rej
90
+ ],
91
+ import_constant.MF_DATA_FETCH_STATUS.LOADED
92
+ ];
93
+ res(data);
94
+ return;
95
+ }
96
+ }
97
+ if (downgrade) {
98
+ const mfDowngrade2 = (0, import_downgrade.getDowngradeTag)();
99
+ if (!mfDowngrade2) {
100
+ globalThis[import_constant.DOWNGRADE_KEY] = id ? [
101
+ id
102
+ ] : true;
103
+ } else if (Array.isArray(mfDowngrade2) && id && !mfDowngrade2.includes(id)) {
104
+ mfDowngrade2.push(id);
105
+ }
106
+ }
107
+ const mfDowngrade = (0, import_downgrade.getDowngradeTag)();
108
+ if (typeof mfDowngrade === "boolean") {
109
+ return (0, import_downgrade.callAllDowngrade)();
110
+ }
111
+ if (Array.isArray(mfDowngrade)) {
112
+ if (!id) {
113
+ globalThis[import_constant.DOWNGRADE_KEY] = true;
114
+ return (0, import_downgrade.callAllDowngrade)();
115
+ }
116
+ if (!mfDowngrade.includes(id)) {
117
+ mfDowngrade.push(id);
118
+ }
119
+ return (0, import_downgrade.callDowngrade)(id);
120
+ }
121
+ };
122
+ globalThis[import_constant.FS_HREF] = window.location.href;
123
+ (_globalThis = globalThis)[_DATA_FETCH_FUNCTION = import_constant.DATA_FETCH_FUNCTION] || (_globalThis[_DATA_FETCH_FUNCTION] = []);
124
+ const dataFetch = globalThis[import_constant.DATA_FETCH_FUNCTION];
125
+ await Promise.all(dataFetch.map(async (options) => {
126
+ await dataFetchFunction(options);
127
+ }));
128
+ dataFetch.push = dataFetchFunction;
129
+ });
130
+ }
131
+ });
132
+ // Annotate the CommonJS export names for ESM import in node:
133
+ 0 && (module.exports = {
134
+ injectDataFetchFunctionPlugin,
135
+ setSSREnv
136
+ });
@@ -0,0 +1,211 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var dataFetch_exports = {};
30
+ __export(dataFetch_exports, {
31
+ fetchData: () => fetchData,
32
+ flushDataFetch: () => flushDataFetch,
33
+ getDataFetchIdWithErrorMsgs: () => getDataFetchIdWithErrorMsgs,
34
+ getDataFetchInfo: () => getDataFetchInfo,
35
+ getDataFetchItem: () => getDataFetchItem,
36
+ getDataFetchMap: () => getDataFetchMap,
37
+ getDataFetchMapKey: () => getDataFetchMapKey,
38
+ initDataFetchMap: () => initDataFetchMap,
39
+ isDataLoaderExpose: () => isDataLoaderExpose,
40
+ loadDataFetchModule: () => loadDataFetchModule,
41
+ setDataFetchItemLoadedStatus: () => setDataFetchItemLoadedStatus,
42
+ wrapDataFetchId: () => wrapDataFetchId
43
+ });
44
+ module.exports = __toCommonJS(dataFetch_exports);
45
+ var import_sdk = require("@module-federation/sdk");
46
+ var import_utils = require("../utils");
47
+ var import_logger = __toESM(require("../logger"));
48
+ var import_downgrade = require("../ssr-runtime/downgrade");
49
+ var import_constant = require("../constant");
50
+ var import_constant2 = require("@module-federation/rsbuild-plugin/constant");
51
+ const getDataFetchInfo = ({ name, alias, id, remoteSnapshot }) => {
52
+ if (!remoteSnapshot) {
53
+ return;
54
+ }
55
+ if (!("modules" in remoteSnapshot)) {
56
+ return;
57
+ }
58
+ const regex = new RegExp(`^${name}(/[^/].*|)$`);
59
+ const nameOrAlias = regex.test(id) ? name : alias || name;
60
+ const expose = id.replace(nameOrAlias, "");
61
+ let dataFetchName = "";
62
+ let dataFetchId = "";
63
+ if (expose.startsWith("/")) {
64
+ dataFetchName = `${expose.slice(1)}.${import_constant2.DATA_FETCH_IDENTIFIER}`;
65
+ dataFetchId = `${id}.${import_constant2.DATA_FETCH_IDENTIFIER}`;
66
+ } else if (expose === "") {
67
+ dataFetchName = import_constant2.DATA_FETCH_IDENTIFIER;
68
+ dataFetchId = `${id}/${import_constant2.DATA_FETCH_IDENTIFIER}`;
69
+ } else {
70
+ return;
71
+ }
72
+ if (!dataFetchName || !dataFetchId) {
73
+ return;
74
+ }
75
+ if (!remoteSnapshot.modules.find((module2) => module2.moduleName === dataFetchName)) {
76
+ return;
77
+ }
78
+ return {
79
+ dataFetchName,
80
+ dataFetchId
81
+ };
82
+ };
83
+ function initDataFetchMap() {
84
+ var _globalThis;
85
+ (_globalThis = globalThis).__MF_DATA_FETCH_MAP__ || (_globalThis.__MF_DATA_FETCH_MAP__ = {});
86
+ }
87
+ function getDataFetchItem(id) {
88
+ var _globalThis___MF_DATA_FETCH_MAP__;
89
+ 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];
90
+ }
91
+ function getDataFetchMap() {
92
+ return globalThis.__MF_DATA_FETCH_MAP__;
93
+ }
94
+ const flushDataFetch = () => {
95
+ globalThis.__MF_DATA_FETCH_MAP__ = {};
96
+ globalThis[import_constant.DOWNGRADE_KEY] = void 0;
97
+ };
98
+ function setDataFetchItemLoadedStatus(id) {
99
+ const dataFetchItem = getDataFetchItem(id);
100
+ if (!dataFetchItem) {
101
+ return;
102
+ }
103
+ dataFetchItem[2] = import_constant.MF_DATA_FETCH_STATUS.LOADED;
104
+ }
105
+ const wrapDataFetchId = (id) => {
106
+ return `${import_constant.WRAP_DATA_FETCH_ID_IDENTIFIER}${id}${import_constant.WRAP_DATA_FETCH_ID_IDENTIFIER}`;
107
+ };
108
+ const getDataFetchIdWithErrorMsgs = (errMsgs) => {
109
+ const firstIdentifierIndex = errMsgs.indexOf(import_constant.WRAP_DATA_FETCH_ID_IDENTIFIER);
110
+ if (firstIdentifierIndex === -1) {
111
+ return void 0;
112
+ }
113
+ const secondIdentifierIndex = errMsgs.indexOf(import_constant.WRAP_DATA_FETCH_ID_IDENTIFIER, firstIdentifierIndex + import_constant.WRAP_DATA_FETCH_ID_IDENTIFIER.length);
114
+ if (secondIdentifierIndex === -1) {
115
+ return void 0;
116
+ }
117
+ return errMsgs.substring(firstIdentifierIndex + import_constant.WRAP_DATA_FETCH_ID_IDENTIFIER.length, secondIdentifierIndex);
118
+ };
119
+ async function fetchData(id, params, remoteInfo) {
120
+ const callFetchData = async () => {
121
+ const item = getDataFetchItem(id);
122
+ if (!item) {
123
+ return;
124
+ }
125
+ const [fetchDataFnArr, ..._rest] = item;
126
+ const fetchDataFn = await fetchDataFnArr[2];
127
+ if (!fetchDataFn) {
128
+ return;
129
+ }
130
+ return fetchDataFn(params);
131
+ };
132
+ if ((0, import_sdk.isBrowserEnv)()) {
133
+ var _dataFetchItem_;
134
+ const dataFetchItem = getDataFetchItem(id);
135
+ if (!dataFetchItem) {
136
+ throw new Error(`dataFetchItem not found, id: ${id}`);
137
+ }
138
+ if ((_dataFetchItem_ = dataFetchItem[1]) === null || _dataFetchItem_ === void 0 ? void 0 : _dataFetchItem_[0]) {
139
+ return dataFetchItem[1][0];
140
+ }
141
+ if ((0, import_utils.isCSROnly)()) {
142
+ import_logger.default.debug("==========csr only!");
143
+ return callFetchData();
144
+ }
145
+ if (remoteInfo) {
146
+ return (0, import_downgrade.callDowngrade)(id, params, remoteInfo);
147
+ }
148
+ const mfDowngrade = (0, import_downgrade.getDowngradeTag)();
149
+ if (mfDowngrade) {
150
+ if (typeof mfDowngrade === "boolean") {
151
+ return (0, import_downgrade.callDowngrade)(id, {
152
+ ...params,
153
+ isDowngrade: true
154
+ });
155
+ }
156
+ if (mfDowngrade.includes(id)) {
157
+ return (0, import_downgrade.callDowngrade)(id, {
158
+ ...params,
159
+ isDowngrade: true
160
+ });
161
+ }
162
+ }
163
+ let res;
164
+ let rej;
165
+ const p = new Promise((resolve, reject) => {
166
+ res = resolve;
167
+ rej = reject;
168
+ });
169
+ dataFetchItem[1] = [
170
+ p,
171
+ res,
172
+ rej
173
+ ];
174
+ dataFetchItem[2] = import_constant.MF_DATA_FETCH_STATUS.AWAIT;
175
+ return dataFetchItem[1][0];
176
+ }
177
+ return callFetchData();
178
+ }
179
+ function getDataFetchMapKey(dataFetchInfo, hostInfo) {
180
+ if (!dataFetchInfo || !hostInfo) {
181
+ return;
182
+ }
183
+ const { dataFetchId } = dataFetchInfo;
184
+ return (0, import_sdk.composeKeyWithSeparator)(dataFetchId, hostInfo.name, hostInfo.version);
185
+ }
186
+ async function loadDataFetchModule(instance, id) {
187
+ return instance.loadRemote(id).then((m) => {
188
+ if (m && typeof m === "object" && "fetchData" in m && typeof m.fetchData === "function") {
189
+ return m.fetchData;
190
+ }
191
+ throw new Error(`fetchData not found in remote ${id}, ${JSON.stringify(m)}`);
192
+ });
193
+ }
194
+ function isDataLoaderExpose(exposeKey) {
195
+ return exposeKey.endsWith(import_constant2.DATA_FETCH_IDENTIFIER) || exposeKey.endsWith(import_constant2.DATA_FETCH_CLIENT_SUFFIX);
196
+ }
197
+ // Annotate the CommonJS export names for ESM import in node:
198
+ 0 && (module.exports = {
199
+ fetchData,
200
+ flushDataFetch,
201
+ getDataFetchIdWithErrorMsgs,
202
+ getDataFetchInfo,
203
+ getDataFetchItem,
204
+ getDataFetchMap,
205
+ getDataFetchMapKey,
206
+ initDataFetchMap,
207
+ isDataLoaderExpose,
208
+ loadDataFetchModule,
209
+ setDataFetchItemLoadedStatus,
210
+ wrapDataFetchId
211
+ });
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+ var utils_exports = {};
21
+ __export(utils_exports, {
22
+ getLoadedRemoteInfos: () => getLoadedRemoteInfos,
23
+ isCSROnly: () => isCSROnly
24
+ });
25
+ module.exports = __toCommonJS(utils_exports);
26
+ __reExport(utils_exports, require("./dataFetch"), module.exports);
27
+ function getLoadedRemoteInfos(id, instance) {
28
+ if (!instance) {
29
+ return;
30
+ }
31
+ const { name, expose } = instance.remoteHandler.idToRemoteMap[id] || {};
32
+ if (!name) {
33
+ return;
34
+ }
35
+ const module2 = instance.moduleCache.get(name);
36
+ if (!module2) {
37
+ return;
38
+ }
39
+ const { remoteSnapshot } = instance.snapshotHandler.getGlobalRemoteInfo(module2.remoteInfo);
40
+ return {
41
+ ...module2.remoteInfo,
42
+ snapshot: remoteSnapshot,
43
+ expose
44
+ };
45
+ }
46
+ function isCSROnly() {
47
+ return window._SSR_DATA === void 0;
48
+ }
49
+ // Annotate the CommonJS export names for ESM import in node:
50
+ 0 && (module.exports = {
51
+ getLoadedRemoteInfos,
52
+ isCSROnly,
53
+ ...require("./dataFetch")
54
+ });
@@ -9,10 +9,10 @@ import { getIPV4, isWebTarget, skipByTarget } from "./utils";
9
9
  import { encodeName } from "@module-federation/sdk";
10
10
  import { bundle } from "@modern-js/node-bundle-require";
11
11
  import { LOCALHOST, PLUGIN_IDENTIFIER } from "../constant";
12
- import { autoDeleteSplitChunkCacheGroups } from "@module-federation/rsbuild-plugin/utils";
13
- import logger from "./logger";
12
+ import { autoDeleteSplitChunkCacheGroups, addDataFetchExposes } from "@module-federation/rsbuild-plugin/utils";
13
+ import logger from "../logger";
14
+ import { isDev } from "./utils";
14
15
  var defaultPath = path.resolve(process.cwd(), "module-federation.config.ts");
15
- var isDev = process.env.NODE_ENV === "development";
16
16
  function setEnv(enableSSR) {
17
17
  if (enableSSR) {
18
18
  process.env["MF_DISABLE_EMIT_STATS"] = "true";
@@ -126,8 +126,9 @@ var patchDTSConfig = function(mfConfig, isServer) {
126
126
  }
127
127
  }
128
128
  };
129
- var patchMFConfig = function(mfConfig, isServer, remoteIpStrategy) {
129
+ var patchMFConfig = function(mfConfig, isServer, remoteIpStrategy, enableSSR) {
130
130
  replaceRemoteUrl(mfConfig, remoteIpStrategy);
131
+ addDataFetchExposes(mfConfig.exposes, isServer);
131
132
  if (mfConfig.remoteType === void 0) {
132
133
  mfConfig.remoteType = "script";
133
134
  }
@@ -137,12 +138,13 @@ var patchMFConfig = function(mfConfig, isServer, remoteIpStrategy) {
137
138
  var runtimePlugins = _to_consumable_array(mfConfig.runtimePlugins || []);
138
139
  patchDTSConfig(mfConfig, isServer);
139
140
  injectRuntimePlugins(require.resolve("@module-federation/modern-js/shared-strategy"), runtimePlugins);
140
- if (isDev) {
141
+ injectRuntimePlugins(require.resolve("@module-federation/modern-js/auto-fetch-data"), runtimePlugins);
142
+ if (enableSSR && isDev()) {
141
143
  injectRuntimePlugins(require.resolve("@module-federation/modern-js/resolve-entry-ipv4"), runtimePlugins);
142
144
  }
143
145
  if (isServer) {
144
146
  injectRuntimePlugins(require.resolve("@module-federation/node/runtimePlugin"), runtimePlugins);
145
- if (isDev) {
147
+ if (isDev()) {
146
148
  injectRuntimePlugins(require.resolve("@module-federation/node/record-dynamic-remote-entry-hash-plugin"), runtimePlugins);
147
149
  }
148
150
  injectRuntimePlugins(require.resolve("@module-federation/modern-js/inject-node-fetch"), runtimePlugins);
@@ -240,7 +242,7 @@ function patchBundlerConfig(options) {
240
242
  splitChunkConfig.chunks = "async";
241
243
  logger.warn('splitChunks.chunks = async is not allowed with stream SSR mode, it will auto changed to "async"');
242
244
  }
243
- if (isDev && chain.output.get("publicPath") === "auto") {
245
+ if (isDev() && chain.output.get("publicPath") === "auto") {
244
246
  var _modernjsConfig_dev, _modernjsConfig_server;
245
247
  var 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;
246
248
  var publicPath = "http://localhost:".concat(port, "/");
@@ -250,11 +252,11 @@ function patchBundlerConfig(options) {
250
252
  var uniqueName = mfConfig.name || chain.output.get("uniqueName");
251
253
  var chunkFileName = chain.output.get("chunkFilename");
252
254
  if (typeof chunkFileName === "string" && uniqueName && !chunkFileName.includes(uniqueName)) {
253
- var suffix = "".concat(encodeName(uniqueName), "-[chunkhash].js");
255
+ var suffix = "".concat(encodeName(uniqueName), "-[contenthash].js");
254
256
  chain.output.chunkFilename(chunkFileName.replace(".js", suffix));
255
257
  }
256
258
  }
257
- if (isDev && enableSSR && !isServer) {
259
+ if (isDev() && enableSSR && !isServer) {
258
260
  chain.resolve.fallback.set("crypto", false).set("stream", false).set("vm", false);
259
261
  }
260
262
  if (((_modernjsConfig_deploy = modernjsConfig.deploy) === null || _modernjsConfig_deploy === void 0 ? void 0 : _modernjsConfig_deploy.microFrontend) && Object.keys(mfConfig.exposes || {}).length) {
@@ -296,9 +298,8 @@ var moduleFederationConfigPlugin = function(userConfig) {
296
298
  var isWeb = isWebTarget(target);
297
299
  addMyTypes2Ignored(chain, !isWeb ? ssrConfig : csrConfig);
298
300
  var targetMFConfig = !isWeb ? ssrConfig : csrConfig;
299
- patchMFConfig(targetMFConfig, !isWeb, userConfig.remoteIpStrategy || "ipv4");
301
+ patchMFConfig(targetMFConfig, !isWeb, userConfig.remoteIpStrategy || "ipv4", enableSSR);
300
302
  patchBundlerConfig({
301
- // @ts-expect-error chain type is not correct
302
303
  chain,
303
304
  isServer: !isWeb,
304
305
  modernjsConfig,
@@ -335,6 +336,12 @@ var moduleFederationConfigPlugin = function(userConfig) {
335
336
  "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
336
337
  "Access-Control-Allow-Headers": "*"
337
338
  } : void 0;
339
+ var defineConfig = {
340
+ REMOTE_IP_STRATEGY: JSON.stringify(userConfig.remoteIpStrategy)
341
+ };
342
+ if (enableSSR && isDev()) {
343
+ defineConfig["FEDERATION_IPV4"] = JSON.stringify(ipv4);
344
+ }
338
345
  var _modernjsConfig_source_enableAsyncEntry;
339
346
  return {
340
347
  tools: {
@@ -347,10 +354,7 @@ var moduleFederationConfigPlugin = function(userConfig) {
347
354
  // TODO: deprecated
348
355
  "@modern-js/runtime/mf": require.resolve("@module-federation/modern-js/runtime")
349
356
  },
350
- define: {
351
- FEDERATION_IPV4: JSON.stringify(ipv4),
352
- REMOTE_IP_STRATEGY: JSON.stringify(userConfig.remoteIpStrategy)
353
- },
357
+ define: defineConfig,
354
358
  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
355
359
  },
356
360
  dev: {
@@ -43,6 +43,7 @@ describe("patchMFConfig", /* @__PURE__ */ _async_to_generator(function() {
43
43
  remoteType: "script",
44
44
  runtimePlugins: [
45
45
  require.resolve("@module-federation/modern-js/shared-strategy"),
46
+ require.resolve("@module-federation/modern-js/auto-fetch-data"),
46
47
  require.resolve("@module-federation/node/runtimePlugin"),
47
48
  require.resolve("@module-federation/modern-js/inject-node-fetch")
48
49
  ],
@@ -76,7 +77,8 @@ describe("patchMFConfig", /* @__PURE__ */ _async_to_generator(function() {
76
77
  },
77
78
  remoteType: "script",
78
79
  runtimePlugins: [
79
- require.resolve("@module-federation/modern-js/shared-strategy")
80
+ require.resolve("@module-federation/modern-js/shared-strategy"),
81
+ require.resolve("@module-federation/modern-js/auto-fetch-data")
80
82
  ],
81
83
  shared: {
82
84
  react: {
@@ -8,6 +8,7 @@ import { moduleFederationSSRPlugin } from "./ssrPlugin";
8
8
  import { isWebTarget } from "./utils";
9
9
  var moduleFederationPlugin = function() {
10
10
  var userConfig = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
11
+ var _userConfig_fetchServerQuery;
11
12
  var internalModernPluginOptions = {
12
13
  csrConfig: void 0,
13
14
  ssrConfig: void 0,
@@ -16,7 +17,8 @@ var moduleFederationPlugin = function() {
16
17
  distOutputDir: "",
17
18
  originPluginOptions: userConfig,
18
19
  remoteIpStrategy: userConfig === null || userConfig === void 0 ? void 0 : userConfig.remoteIpStrategy,
19
- userConfig: userConfig || {}
20
+ userConfig: userConfig || {},
21
+ fetchServerQuery: (_userConfig_fetchServerQuery = userConfig.fetchServerQuery) !== null && _userConfig_fetchServerQuery !== void 0 ? _userConfig_fetchServerQuery : void 0
20
22
  };
21
23
  return {
22
24
  name: "@modern-js/plugin-module-federation",
@@ -0,0 +1,76 @@
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
+ var autoFetchData = function() {
7
+ return {
8
+ name: "auto-fetch-data-plugin",
9
+ beforeInit: function beforeInit(args) {
10
+ initDataFetchMap();
11
+ return args;
12
+ },
13
+ afterLoadSnapshot: function afterLoadSnapshot(args) {
14
+ var id = args.id, moduleInfo = args.moduleInfo, remoteSnapshot = args.remoteSnapshot, host = args.host;
15
+ if (typeof id === "string" && isDataLoaderExpose(id)) {
16
+ return args;
17
+ }
18
+ if (!remoteSnapshot || !id || !("modules" in remoteSnapshot)) {
19
+ return args;
20
+ }
21
+ var name = moduleInfo.name, alias = moduleInfo.alias;
22
+ var dataFetchInfo = getDataFetchInfo({
23
+ name,
24
+ alias,
25
+ id,
26
+ remoteSnapshot
27
+ });
28
+ if (!dataFetchInfo) {
29
+ return args;
30
+ }
31
+ var dataFetchId = dataFetchInfo.dataFetchId, dataFetchName = dataFetchInfo.dataFetchName;
32
+ var dataFetchMapKey = getDataFetchMapKey(dataFetchInfo, {
33
+ name: host.name,
34
+ version: host.options.version
35
+ });
36
+ logger.debug("======= auto fetch plugin dataFetchMapKey: ", dataFetchMapKey);
37
+ if (!dataFetchMapKey) {
38
+ return args;
39
+ }
40
+ var dataFetchItem = getDataFetchItem(dataFetchMapKey);
41
+ if (dataFetchItem) {
42
+ return args;
43
+ }
44
+ var dataFetchMap = getDataFetchMap();
45
+ var hasSSRAsset = Boolean(remoteSnapshot.ssrRemoteEntry);
46
+ var hasDataFetchClient = Boolean(remoteSnapshot.modules.find(function(module) {
47
+ return module.moduleName === "".concat(dataFetchName).concat(DATA_FETCH_CLIENT_SUFFIX);
48
+ }));
49
+ var downgradeType = hasDataFetchClient ? MF_DATA_FETCH_TYPE.FETCH_CLIENT : hasSSRAsset ? MF_DATA_FETCH_TYPE.FETCH_SERVER : MF_DATA_FETCH_TYPE.FETCH_CLIENT;
50
+ var finalDataFetchId = dataFetchId;
51
+ if (typeof window !== "undefined") {
52
+ finalDataFetchId = downgradeType === MF_DATA_FETCH_TYPE.FETCH_CLIENT ? hasDataFetchClient ? "".concat(dataFetchId).concat(DATA_FETCH_CLIENT_SUFFIX) : dataFetchId : dataFetchId;
53
+ }
54
+ var getDataFetchGetter = function() {
55
+ return loadDataFetchModule(host, finalDataFetchId);
56
+ };
57
+ var dataFetchFnItem = [
58
+ getDataFetchGetter,
59
+ downgradeType
60
+ ];
61
+ if (typeof window === "undefined" || isCSROnly()) {
62
+ dataFetchFnItem.push(getDataFetchGetter());
63
+ }
64
+ dataFetchMap[dataFetchMapKey] = [
65
+ dataFetchFnItem,
66
+ void 0,
67
+ MF_DATA_FETCH_STATUS.AWAIT
68
+ ];
69
+ return args;
70
+ }
71
+ };
72
+ };
73
+ var auto_fetch_data_default = autoFetchData;
74
+ export {
75
+ auto_fetch_data_default as default
76
+ };