@module-federation/modern-js 0.0.0-next-20250526074234 → 0.0.0-next-20250527094526

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 (109) hide show
  1. package/dist/cjs/cli/configPlugin.js +6 -8
  2. package/dist/cjs/cli/configPlugin.spec.js +1 -3
  3. package/dist/cjs/{runtime/wrapNoSSR.js → cli/constant.js} +6 -13
  4. package/dist/cjs/cli/index.js +9 -3
  5. package/dist/cjs/{logger.js → cli/logger.js} +1 -1
  6. package/dist/cjs/cli/manifest.js +3 -3
  7. package/dist/cjs/cli/ssrPlugin.js +15 -34
  8. package/dist/cjs/cli/utils.js +0 -5
  9. package/dist/cjs/constant.js +2 -49
  10. package/dist/cjs/runtime/createRemoteSSRComponent.js +201 -0
  11. package/dist/cjs/runtime/index.js +3 -15
  12. package/dist/cjs/server/fileCache.js +84 -0
  13. package/dist/cjs/server/fileCache.spec.js +28 -0
  14. package/dist/cjs/server/index.js +58 -0
  15. package/dist/cjs/server/staticMiddleware.js +77 -0
  16. package/dist/cjs/server/staticMiddleware.spec.js +185 -0
  17. package/dist/cjs/{interfaces/global.js → ssr-runtime/index.js} +8 -2
  18. package/dist/cjs/ssr-runtime/{devPlugin.js → plugin.js} +6 -9
  19. package/dist/esm/cli/configPlugin.js +7 -9
  20. package/dist/esm/cli/configPlugin.spec.js +1 -3
  21. package/dist/esm/cli/constant.js +4 -0
  22. package/dist/esm/cli/index.js +10 -3
  23. package/dist/esm/{logger.js → cli/logger.js} +1 -1
  24. package/dist/esm/cli/manifest.js +1 -1
  25. package/dist/esm/cli/ssrPlugin.js +8 -28
  26. package/dist/esm/cli/utils.js +0 -4
  27. package/dist/esm/constant.js +1 -37
  28. package/dist/esm/runtime/createRemoteSSRComponent.js +191 -0
  29. package/dist/esm/runtime/index.js +3 -13
  30. package/dist/esm/server/fileCache.js +98 -0
  31. package/dist/esm/server/fileCache.spec.js +50 -0
  32. package/dist/esm/server/index.js +36 -0
  33. package/dist/esm/server/staticMiddleware.js +81 -0
  34. package/dist/esm/server/staticMiddleware.spec.js +328 -0
  35. package/dist/esm/ssr-runtime/index.js +1 -0
  36. package/dist/esm/ssr-runtime/{devPlugin.js → plugin.js} +2 -5
  37. package/dist/esm-node/cli/configPlugin.js +7 -9
  38. package/dist/esm-node/cli/configPlugin.spec.js +1 -3
  39. package/dist/esm-node/cli/constant.js +4 -0
  40. package/dist/esm-node/cli/index.js +9 -3
  41. package/dist/esm-node/{logger.js → cli/logger.js} +1 -1
  42. package/dist/esm-node/cli/manifest.js +1 -1
  43. package/dist/esm-node/cli/ssrPlugin.js +8 -27
  44. package/dist/esm-node/cli/utils.js +0 -4
  45. package/dist/esm-node/constant.js +1 -37
  46. package/dist/esm-node/runtime/createRemoteSSRComponent.js +166 -0
  47. package/dist/esm-node/runtime/index.js +3 -13
  48. package/dist/esm-node/server/fileCache.js +49 -0
  49. package/dist/esm-node/server/fileCache.spec.js +27 -0
  50. package/dist/esm-node/server/index.js +34 -0
  51. package/dist/esm-node/server/staticMiddleware.js +43 -0
  52. package/dist/esm-node/server/staticMiddleware.spec.js +162 -0
  53. package/dist/esm-node/ssr-runtime/index.js +1 -0
  54. package/dist/esm-node/ssr-runtime/{devPlugin.js → plugin.js} +2 -5
  55. package/dist/types/cli/constant.d.ts +1 -0
  56. package/dist/types/cli/index.d.ts +0 -1
  57. package/dist/types/cli/utils.d.ts +0 -1
  58. package/dist/types/constant.d.ts +0 -23
  59. package/dist/types/runtime/createRemoteSSRComponent.d.ts +18 -0
  60. package/dist/types/runtime/index.d.ts +2 -7
  61. package/dist/types/server/fileCache.d.ts +14 -0
  62. package/dist/types/server/fileCache.spec.d.ts +1 -0
  63. package/dist/types/server/index.d.ts +4 -0
  64. package/dist/types/server/staticMiddleware.d.ts +6 -0
  65. package/dist/types/server/staticMiddleware.spec.d.ts +1 -0
  66. package/dist/types/ssr-runtime/index.d.ts +1 -0
  67. package/dist/types/ssr-runtime/plugin.d.ts +2 -0
  68. package/dist/types/types/index.d.ts +0 -2
  69. package/package.json +23 -39
  70. package/dist/cjs/cli/mfRuntimePlugins/auto-fetch-data.js +0 -98
  71. package/dist/cjs/cli/server/data-fetch-server-plugin.js +0 -192
  72. package/dist/cjs/runtime/AwaitDataFetch.js +0 -144
  73. package/dist/cjs/runtime/createRemoteComponent.js +0 -327
  74. package/dist/cjs/ssr-runtime/downgrade.js +0 -114
  75. package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -136
  76. package/dist/cjs/utils/dataFetch.js +0 -211
  77. package/dist/cjs/utils/index.js +0 -54
  78. package/dist/esm/cli/mfRuntimePlugins/auto-fetch-data.js +0 -74
  79. package/dist/esm/cli/server/data-fetch-server-plugin.js +0 -240
  80. package/dist/esm/interfaces/global.js +0 -0
  81. package/dist/esm/runtime/AwaitDataFetch.js +0 -131
  82. package/dist/esm/runtime/createRemoteComponent.js +0 -417
  83. package/dist/esm/runtime/wrapNoSSR.js +0 -12
  84. package/dist/esm/ssr-runtime/downgrade.js +0 -150
  85. package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -177
  86. package/dist/esm/utils/dataFetch.js +0 -237
  87. package/dist/esm/utils/index.js +0 -28
  88. package/dist/esm-node/cli/mfRuntimePlugins/auto-fetch-data.js +0 -68
  89. package/dist/esm-node/cli/server/data-fetch-server-plugin.js +0 -162
  90. package/dist/esm-node/interfaces/global.js +0 -0
  91. package/dist/esm-node/runtime/AwaitDataFetch.js +0 -109
  92. package/dist/esm-node/runtime/createRemoteComponent.js +0 -291
  93. package/dist/esm-node/runtime/wrapNoSSR.js +0 -11
  94. package/dist/esm-node/ssr-runtime/downgrade.js +0 -88
  95. package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -101
  96. package/dist/esm-node/utils/dataFetch.js +0 -166
  97. package/dist/esm-node/utils/index.js +0 -27
  98. package/dist/types/cli/mfRuntimePlugins/auto-fetch-data.d.ts +0 -3
  99. package/dist/types/cli/server/data-fetch-server-plugin.d.ts +0 -3
  100. package/dist/types/interfaces/global.d.ts +0 -27
  101. package/dist/types/runtime/AwaitDataFetch.d.ts +0 -19
  102. package/dist/types/runtime/createRemoteComponent.d.ts +0 -26
  103. package/dist/types/runtime/wrapNoSSR.d.ts +0 -9
  104. package/dist/types/ssr-runtime/devPlugin.d.ts +0 -2
  105. package/dist/types/ssr-runtime/downgrade.d.ts +0 -4
  106. package/dist/types/ssr-runtime/injectDataFetchFunctionPlugin.d.ts +0 -7
  107. package/dist/types/utils/dataFetch.d.ts +0 -26
  108. package/dist/types/utils/index.d.ts +0 -15
  109. /package/dist/types/{logger.d.ts → cli/logger.d.ts} +0 -0
@@ -1,211 +0,0 @@
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
- });
@@ -1,54 +0,0 @@
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
- });
@@ -1,74 +0,0 @@
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 downgradeType = remoteSnapshot.modules.find(function(module) {
46
- return module.moduleName === "".concat(dataFetchName).concat(DATA_FETCH_CLIENT_SUFFIX);
47
- }) ? MF_DATA_FETCH_TYPE.FETCH_CLIENT : MF_DATA_FETCH_TYPE.FETCH_SERVER;
48
- var finalDataFetchId = dataFetchId;
49
- if (typeof window !== "undefined") {
50
- finalDataFetchId = downgradeType === MF_DATA_FETCH_TYPE.FETCH_CLIENT ? "".concat(dataFetchId).concat(DATA_FETCH_CLIENT_SUFFIX) : dataFetchId;
51
- }
52
- var getDataFetchGetter = function() {
53
- return loadDataFetchModule(host, finalDataFetchId);
54
- };
55
- var dataFetchFnItem = [
56
- getDataFetchGetter,
57
- downgradeType
58
- ];
59
- if (typeof window === "undefined" || isCSROnly()) {
60
- dataFetchFnItem.push(getDataFetchGetter());
61
- }
62
- dataFetchMap[dataFetchMapKey] = [
63
- dataFetchFnItem,
64
- void 0,
65
- MF_DATA_FETCH_STATUS.AWAIT
66
- ];
67
- return args;
68
- }
69
- };
70
- };
71
- var auto_fetch_data_default = autoFetchData;
72
- export {
73
- auto_fetch_data_default as default
74
- };
@@ -1,240 +0,0 @@
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 _ts_generator } from "@swc/helpers/_/_ts_generator";
5
- import { DATA_FETCH_QUERY, MF_DATA_FETCH_STATUS } from "../../constant";
6
- import logger from "../../logger";
7
- import { getDataFetchMap } from "../../utils";
8
- import { fetchData, initDataFetchMap, loadDataFetchModule } from "../../utils/dataFetch";
9
- import { SEPARATOR, MANIFEST_EXT } from "@module-federation/sdk";
10
- function wrapSetTimeout(targetPromise) {
11
- var delay = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 2e4, id = arguments.length > 2 ? arguments[2] : void 0;
12
- if (targetPromise && typeof targetPromise.then === "function") {
13
- return new Promise(function(resolve, reject) {
14
- var timeoutId = setTimeout(function() {
15
- logger.warn("Data fetch for ID ".concat(id, " timed out after 20 seconds."));
16
- reject(new Error("Data fetch for ID ".concat(id, " timed out after 20 seconds")));
17
- }, delay);
18
- targetPromise.then(function(value) {
19
- clearTimeout(timeoutId);
20
- resolve(value);
21
- }).catch(function(err) {
22
- clearTimeout(timeoutId);
23
- reject(err);
24
- });
25
- });
26
- }
27
- }
28
- function addProtocol(url) {
29
- if (url.startsWith("//")) {
30
- return "https:" + url;
31
- }
32
- return url;
33
- }
34
- var getDecodeQuery = function(url, name) {
35
- var res = url.searchParams.get(name);
36
- if (!res) {
37
- return null;
38
- }
39
- return decodeURIComponent(res);
40
- };
41
- var middleware = function() {
42
- var _ref = _async_to_generator(function(ctx, next) {
43
- var _globalThis___FEDERATION__, url, dataFetchId, params, remoteInfo, remoteInfoQuery, _dataFetchMap_dataFetchId, _dataFetchMap_dataFetchId1, dataFetchMap, fetchDataPromise, targetPromise, wrappedPromise, res, hostInstance, remoteEntry, remote, _hostInstance_snapshotHandler_getGlobalRemoteInfo, hostGlobalSnapshot, remoteSnapshot, dataFetchItem, callFetchDataPromise, wrappedPromise1, res1, remoteId, hostInstance1, dataFetchFn, data, e;
44
- return _ts_generator(this, function(_state) {
45
- switch (_state.label) {
46
- case 0:
47
- try {
48
- url = new URL(ctx.req.url);
49
- dataFetchId = getDecodeQuery(url, DATA_FETCH_QUERY);
50
- params = JSON.parse(getDecodeQuery(url, "params") || "{}");
51
- remoteInfoQuery = getDecodeQuery(url, "remoteInfo");
52
- remoteInfo = remoteInfoQuery ? JSON.parse(remoteInfoQuery) : null;
53
- } catch (e2) {
54
- console.log("fetch data from server, error: ", e2);
55
- console.error(e2);
56
- return [
57
- 2,
58
- next()
59
- ];
60
- }
61
- if (!dataFetchId) {
62
- return [
63
- 2,
64
- next()
65
- ];
66
- }
67
- logger.log("fetch data from server, dataFetchId: ", dataFetchId);
68
- console.log("fetch data from server, moduleInfo: ", (_globalThis___FEDERATION__ = globalThis.__FEDERATION__) === null || _globalThis___FEDERATION__ === void 0 ? void 0 : _globalThis___FEDERATION__.moduleInfo);
69
- _state.label = 1;
70
- case 1:
71
- _state.trys.push([
72
- 1,
73
- 9,
74
- ,
75
- 10
76
- ]);
77
- dataFetchMap = getDataFetchMap();
78
- if (!dataFetchMap) {
79
- initDataFetchMap();
80
- }
81
- fetchDataPromise = (_dataFetchMap_dataFetchId = dataFetchMap[dataFetchId]) === null || _dataFetchMap_dataFetchId === void 0 ? void 0 : _dataFetchMap_dataFetchId[1];
82
- console.log("fetch data from server, fetchDataPromise: ", fetchDataPromise);
83
- if (!(fetchDataPromise && ((_dataFetchMap_dataFetchId1 = dataFetchMap[dataFetchId]) === null || _dataFetchMap_dataFetchId1 === void 0 ? void 0 : _dataFetchMap_dataFetchId1[2]) !== MF_DATA_FETCH_STATUS.ERROR))
84
- return [
85
- 3,
86
- 4
87
- ];
88
- logger.log("fetch data from server, fetchDataPromise: ", fetchDataPromise);
89
- targetPromise = fetchDataPromise[0];
90
- wrappedPromise = wrapSetTimeout(targetPromise, 2e4, dataFetchId);
91
- if (!wrappedPromise)
92
- return [
93
- 3,
94
- 3
95
- ];
96
- return [
97
- 4,
98
- wrappedPromise
99
- ];
100
- case 2:
101
- res = _state.sent();
102
- logger.log("fetch data from server, fetchDataPromise res: ", res);
103
- return [
104
- 2,
105
- ctx.json(res)
106
- ];
107
- case 3:
108
- logger.error("Expected a Promise from fetchDataPromise[0] for dataFetchId ".concat(dataFetchId, ", but received:"), targetPromise, "Will try call new dataFetch again...");
109
- _state.label = 4;
110
- case 4:
111
- if (remoteInfo) {
112
- try {
113
- hostInstance = globalThis.__FEDERATION__.__INSTANCES__[0];
114
- remoteEntry = "".concat(addProtocol(remoteInfo.ssrPublicPath) + remoteInfo.ssrRemoteEntry);
115
- if (!hostInstance) {
116
- throw new Error("host instance not found!");
117
- }
118
- remote = hostInstance.options.remotes.find(function(remote2) {
119
- return remote2.name === remoteInfo.name;
120
- });
121
- logger.debug("find remote: ", JSON.stringify(remote));
122
- if (!remote) {
123
- hostInstance.registerRemotes([
124
- {
125
- name: remoteInfo.name,
126
- entry: remoteEntry,
127
- entryGlobalName: remoteInfo.globalName
128
- }
129
- ]);
130
- } else if (!("entry" in remote) || !remote.entry.includes(MANIFEST_EXT)) {
131
- _hostInstance_snapshotHandler_getGlobalRemoteInfo = hostInstance.snapshotHandler.getGlobalRemoteInfo(remoteInfo), hostGlobalSnapshot = _hostInstance_snapshotHandler_getGlobalRemoteInfo.hostGlobalSnapshot, remoteSnapshot = _hostInstance_snapshotHandler_getGlobalRemoteInfo.remoteSnapshot;
132
- logger.debug("find hostGlobalSnapshot: ", JSON.stringify(hostGlobalSnapshot));
133
- logger.debug("find remoteSnapshot: ", JSON.stringify(remoteSnapshot));
134
- if (!hostGlobalSnapshot || !remoteSnapshot) {
135
- if ("version" in remote) {
136
- delete remote.version;
137
- }
138
- remote.entry = remoteEntry;
139
- remote.entryGlobalName = remoteInfo.globalName;
140
- }
141
- }
142
- } catch (e2) {
143
- ctx.status(500);
144
- return [
145
- 2,
146
- ctx.text("failed to fetch ".concat(remoteInfo.name, " data, error:\n ").concat(e2))
147
- ];
148
- }
149
- }
150
- dataFetchItem = dataFetchMap[dataFetchId];
151
- console.log("fetch data from server, dataFetchItem: ", dataFetchItem);
152
- if (!dataFetchItem)
153
- return [
154
- 3,
155
- 6
156
- ];
157
- logger.log("fetch data from server, dataFetchItem: ", dataFetchItem);
158
- callFetchDataPromise = fetchData(dataFetchId, _object_spread_props(_object_spread({}, params), {
159
- isDowngrade: !remoteInfo
160
- }));
161
- wrappedPromise1 = wrapSetTimeout(callFetchDataPromise, 2e4, dataFetchId);
162
- if (!wrappedPromise1)
163
- return [
164
- 3,
165
- 6
166
- ];
167
- return [
168
- 4,
169
- wrappedPromise1
170
- ];
171
- case 5:
172
- res1 = _state.sent();
173
- logger.log("fetch data from server, dataFetchItem res: ", res1);
174
- return [
175
- 2,
176
- ctx.json(res1)
177
- ];
178
- case 6:
179
- remoteId = dataFetchId.split(SEPARATOR)[0];
180
- hostInstance1 = globalThis.__FEDERATION__.__INSTANCES__[0];
181
- if (!hostInstance1) {
182
- throw new Error("host instance not found!");
183
- }
184
- return [
185
- 4,
186
- loadDataFetchModule(hostInstance1, remoteId)
187
- ];
188
- case 7:
189
- dataFetchFn = _state.sent();
190
- return [
191
- 4,
192
- dataFetchFn(_object_spread_props(_object_spread({}, params), {
193
- isDowngrade: !remoteInfo
194
- }))
195
- ];
196
- case 8:
197
- data = _state.sent();
198
- logger.log("fetch data from server, loadDataFetchModule res: ", data);
199
- return [
200
- 2,
201
- ctx.json(data)
202
- ];
203
- case 9:
204
- e = _state.sent();
205
- console.log("server plugin data fetch error: ", e);
206
- ctx.status(500);
207
- return [
208
- 2,
209
- ctx.text("failed to fetch ".concat(remoteInfo.name, " data, error:\n ").concat(e))
210
- ];
211
- case 10:
212
- return [
213
- 2
214
- ];
215
- }
216
- });
217
- });
218
- return function middleware2(ctx, next) {
219
- return _ref.apply(this, arguments);
220
- };
221
- }();
222
- var dataFetchServePlugin = function() {
223
- return {
224
- name: "mf-data-fetch-server-plugin",
225
- setup: function(api) {
226
- api.onPrepare(function() {
227
- var middlewares = api.getServerContext().middlewares;
228
- middlewares.push({
229
- name: "module-federation-serve-manifest",
230
- // @ts-ignore type error
231
- handler: middleware
232
- });
233
- });
234
- }
235
- };
236
- };
237
- var data_fetch_server_plugin_default = dataFetchServePlugin;
238
- export {
239
- data_fetch_server_plugin_default as default
240
- };
File without changes