@module-federation/modern-js 0.0.0-next-20250708121428 → 0.0.0-next-20250709032753
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
|
@@ -148,6 +148,7 @@ const patchMFConfig = (mfConfig, isServer, remoteIpStrategy, enableSSR) => {
|
|
|
148
148
|
];
|
|
149
149
|
patchDTSConfig(mfConfig, isServer);
|
|
150
150
|
injectRuntimePlugins(require.resolve("@module-federation/modern-js/shared-strategy"), runtimePlugins);
|
|
151
|
+
injectRuntimePlugins(require.resolve("@module-federation/modern-js/auto-fetch-data"), runtimePlugins);
|
|
151
152
|
if (enableSSR && (0, import_utils3.isDev)()) {
|
|
152
153
|
injectRuntimePlugins(require.resolve("@module-federation/modern-js/resolve-entry-ipv4"), runtimePlugins);
|
|
153
154
|
}
|
|
@@ -61,6 +61,7 @@ const mfConfig = {
|
|
|
61
61
|
remoteType: "script",
|
|
62
62
|
runtimePlugins: [
|
|
63
63
|
require.resolve("@module-federation/modern-js/shared-strategy"),
|
|
64
|
+
require.resolve("@module-federation/modern-js/auto-fetch-data"),
|
|
64
65
|
require.resolve("@module-federation/node/runtimePlugin"),
|
|
65
66
|
require.resolve("@module-federation/modern-js/inject-node-fetch")
|
|
66
67
|
],
|
|
@@ -89,7 +90,7 @@ const mfConfig = {
|
|
|
89
90
|
remoteType: "script",
|
|
90
91
|
runtimePlugins: [
|
|
91
92
|
require.resolve("@module-federation/modern-js/shared-strategy"),
|
|
92
|
-
require.resolve("@module-federation/
|
|
93
|
+
require.resolve("@module-federation/modern-js/auto-fetch-data")
|
|
93
94
|
],
|
|
94
95
|
shared: {
|
|
95
96
|
react: {
|
|
@@ -0,0 +1,100 @@
|
|
|
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 auto_fetch_data_exports = {};
|
|
30
|
+
__export(auto_fetch_data_exports, {
|
|
31
|
+
default: () => auto_fetch_data_default
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(auto_fetch_data_exports);
|
|
34
|
+
var import_utils = require("../../utils");
|
|
35
|
+
var import_logger = __toESM(require("../../logger"));
|
|
36
|
+
var import_dataFetch = require("../../utils/dataFetch");
|
|
37
|
+
var import_constant = require("../../constant");
|
|
38
|
+
var import_constant2 = require("@module-federation/rsbuild-plugin/constant");
|
|
39
|
+
const autoFetchData = () => ({
|
|
40
|
+
name: "auto-fetch-data-plugin",
|
|
41
|
+
beforeInit(args) {
|
|
42
|
+
(0, import_utils.initDataFetchMap)();
|
|
43
|
+
return args;
|
|
44
|
+
},
|
|
45
|
+
afterLoadSnapshot(args) {
|
|
46
|
+
const { id, moduleInfo, remoteSnapshot, host } = args;
|
|
47
|
+
if (typeof id === "string" && (0, import_dataFetch.isDataLoaderExpose)(id)) {
|
|
48
|
+
return args;
|
|
49
|
+
}
|
|
50
|
+
if (!remoteSnapshot || !id || !("modules" in remoteSnapshot)) {
|
|
51
|
+
return args;
|
|
52
|
+
}
|
|
53
|
+
const { name, alias } = moduleInfo;
|
|
54
|
+
const dataFetchInfo = (0, import_utils.getDataFetchInfo)({
|
|
55
|
+
name,
|
|
56
|
+
alias,
|
|
57
|
+
id,
|
|
58
|
+
remoteSnapshot
|
|
59
|
+
});
|
|
60
|
+
if (!dataFetchInfo) {
|
|
61
|
+
return args;
|
|
62
|
+
}
|
|
63
|
+
const { dataFetchId, dataFetchName } = dataFetchInfo;
|
|
64
|
+
const dataFetchMapKey = (0, import_dataFetch.getDataFetchMapKey)(dataFetchInfo, {
|
|
65
|
+
name: host.name,
|
|
66
|
+
version: host.options.version
|
|
67
|
+
});
|
|
68
|
+
import_logger.default.debug("======= auto fetch plugin dataFetchMapKey: ", dataFetchMapKey);
|
|
69
|
+
if (!dataFetchMapKey) {
|
|
70
|
+
return args;
|
|
71
|
+
}
|
|
72
|
+
const dataFetchItem = (0, import_utils.getDataFetchItem)(dataFetchMapKey);
|
|
73
|
+
if (dataFetchItem) {
|
|
74
|
+
return args;
|
|
75
|
+
}
|
|
76
|
+
const dataFetchMap = (0, import_utils.getDataFetchMap)();
|
|
77
|
+
const hasSSRAsset = Boolean(remoteSnapshot.ssrRemoteEntry);
|
|
78
|
+
const hasDataFetchClient = Boolean(remoteSnapshot.modules.find((module2) => module2.moduleName === `${dataFetchName}${import_constant2.DATA_FETCH_CLIENT_SUFFIX}`));
|
|
79
|
+
const downgradeType = hasDataFetchClient ? import_constant.MF_DATA_FETCH_TYPE.FETCH_CLIENT : hasSSRAsset ? import_constant.MF_DATA_FETCH_TYPE.FETCH_SERVER : import_constant.MF_DATA_FETCH_TYPE.FETCH_CLIENT;
|
|
80
|
+
let finalDataFetchId = dataFetchId;
|
|
81
|
+
if (typeof window !== "undefined") {
|
|
82
|
+
finalDataFetchId = downgradeType === import_constant.MF_DATA_FETCH_TYPE.FETCH_CLIENT ? hasDataFetchClient ? `${dataFetchId}${import_constant2.DATA_FETCH_CLIENT_SUFFIX}` : dataFetchId : dataFetchId;
|
|
83
|
+
}
|
|
84
|
+
const getDataFetchGetter = () => (0, import_dataFetch.loadDataFetchModule)(host, finalDataFetchId);
|
|
85
|
+
const dataFetchFnItem = [
|
|
86
|
+
getDataFetchGetter,
|
|
87
|
+
downgradeType
|
|
88
|
+
];
|
|
89
|
+
if (typeof window === "undefined" || (0, import_utils.isCSROnly)()) {
|
|
90
|
+
dataFetchFnItem.push(getDataFetchGetter());
|
|
91
|
+
}
|
|
92
|
+
dataFetchMap[dataFetchMapKey] = [
|
|
93
|
+
dataFetchFnItem,
|
|
94
|
+
void 0,
|
|
95
|
+
import_constant.MF_DATA_FETCH_STATUS.AWAIT
|
|
96
|
+
];
|
|
97
|
+
return args;
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
var auto_fetch_data_default = autoFetchData;
|
|
@@ -31,7 +31,148 @@ __export(data_fetch_server_plugin_exports, {
|
|
|
31
31
|
default: () => data_fetch_server_plugin_default
|
|
32
32
|
});
|
|
33
33
|
module.exports = __toCommonJS(data_fetch_server_plugin_exports);
|
|
34
|
-
var
|
|
34
|
+
var import_constant = require("../../constant");
|
|
35
|
+
var import_logger = __toESM(require("../../logger"));
|
|
36
|
+
var import_utils = require("../../utils");
|
|
37
|
+
var import_dataFetch = require("../../utils/dataFetch");
|
|
38
|
+
var import_sdk = require("@module-federation/sdk");
|
|
39
|
+
function wrapSetTimeout(targetPromise, delay = 2e4, id) {
|
|
40
|
+
if (targetPromise && typeof targetPromise.then === "function") {
|
|
41
|
+
return new Promise((resolve, reject) => {
|
|
42
|
+
const timeoutId = setTimeout(() => {
|
|
43
|
+
import_logger.default.warn(`Data fetch for ID ${id} timed out after 20 seconds.`);
|
|
44
|
+
reject(new Error(`Data fetch for ID ${id} timed out after 20 seconds`));
|
|
45
|
+
}, delay);
|
|
46
|
+
targetPromise.then((value) => {
|
|
47
|
+
clearTimeout(timeoutId);
|
|
48
|
+
resolve(value);
|
|
49
|
+
}).catch((err) => {
|
|
50
|
+
clearTimeout(timeoutId);
|
|
51
|
+
reject(err);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function addProtocol(url) {
|
|
57
|
+
if (url.startsWith("//")) {
|
|
58
|
+
return "https:" + url;
|
|
59
|
+
}
|
|
60
|
+
return url;
|
|
61
|
+
}
|
|
62
|
+
const getDecodeQuery = (url, name) => {
|
|
63
|
+
const res = url.searchParams.get(name);
|
|
64
|
+
if (!res) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
return decodeURIComponent(res);
|
|
68
|
+
};
|
|
69
|
+
const middleware = async (ctx, next) => {
|
|
70
|
+
var _globalThis___FEDERATION__;
|
|
71
|
+
let url;
|
|
72
|
+
let dataFetchId;
|
|
73
|
+
let params;
|
|
74
|
+
let remoteInfo;
|
|
75
|
+
try {
|
|
76
|
+
url = new URL(ctx.req.url);
|
|
77
|
+
dataFetchId = getDecodeQuery(url, import_constant.DATA_FETCH_QUERY);
|
|
78
|
+
params = JSON.parse(getDecodeQuery(url, "params") || "{}");
|
|
79
|
+
const remoteInfoQuery = getDecodeQuery(url, "remoteInfo");
|
|
80
|
+
remoteInfo = remoteInfoQuery ? JSON.parse(remoteInfoQuery) : null;
|
|
81
|
+
} catch (e) {
|
|
82
|
+
import_logger.default.error("fetch data from server, error: ", e);
|
|
83
|
+
return next();
|
|
84
|
+
}
|
|
85
|
+
if (!dataFetchId) {
|
|
86
|
+
return next();
|
|
87
|
+
}
|
|
88
|
+
import_logger.default.log("fetch data from server, dataFetchId: ", dataFetchId);
|
|
89
|
+
import_logger.default.debug("fetch data from server, moduleInfo: ", (_globalThis___FEDERATION__ = globalThis.__FEDERATION__) === null || _globalThis___FEDERATION__ === void 0 ? void 0 : _globalThis___FEDERATION__.moduleInfo);
|
|
90
|
+
try {
|
|
91
|
+
var _dataFetchMap_dataFetchId, _dataFetchMap_dataFetchId1;
|
|
92
|
+
const dataFetchMap = (0, import_utils.getDataFetchMap)();
|
|
93
|
+
if (!dataFetchMap) {
|
|
94
|
+
(0, import_dataFetch.initDataFetchMap)();
|
|
95
|
+
}
|
|
96
|
+
const fetchDataPromise = (_dataFetchMap_dataFetchId = dataFetchMap[dataFetchId]) === null || _dataFetchMap_dataFetchId === void 0 ? void 0 : _dataFetchMap_dataFetchId[1];
|
|
97
|
+
import_logger.default.debug("fetch data from server, fetchDataPromise: ", fetchDataPromise);
|
|
98
|
+
if (fetchDataPromise && ((_dataFetchMap_dataFetchId1 = dataFetchMap[dataFetchId]) === null || _dataFetchMap_dataFetchId1 === void 0 ? void 0 : _dataFetchMap_dataFetchId1[2]) !== import_constant.MF_DATA_FETCH_STATUS.ERROR) {
|
|
99
|
+
const targetPromise = fetchDataPromise[0];
|
|
100
|
+
const wrappedPromise = wrapSetTimeout(targetPromise, 2e4, dataFetchId);
|
|
101
|
+
if (wrappedPromise) {
|
|
102
|
+
const res = await wrappedPromise;
|
|
103
|
+
import_logger.default.log("fetch data from server, fetchDataPromise res: ", res);
|
|
104
|
+
return ctx.json(res);
|
|
105
|
+
}
|
|
106
|
+
import_logger.default.error(`Expected a Promise from fetchDataPromise[0] for dataFetchId ${dataFetchId}, but received:`, targetPromise, "Will try call new dataFetch again...");
|
|
107
|
+
}
|
|
108
|
+
if (remoteInfo) {
|
|
109
|
+
try {
|
|
110
|
+
const hostInstance2 = globalThis.__FEDERATION__.__INSTANCES__[0];
|
|
111
|
+
const remoteEntry = `${addProtocol(remoteInfo.ssrPublicPath) + remoteInfo.ssrRemoteEntry}`;
|
|
112
|
+
if (!hostInstance2) {
|
|
113
|
+
throw new Error("host instance not found!");
|
|
114
|
+
}
|
|
115
|
+
const remote = hostInstance2.options.remotes.find((remote2) => remote2.name === remoteInfo.name);
|
|
116
|
+
import_logger.default.debug("find remote: ", JSON.stringify(remote));
|
|
117
|
+
if (!remote) {
|
|
118
|
+
hostInstance2.registerRemotes([
|
|
119
|
+
{
|
|
120
|
+
name: remoteInfo.name,
|
|
121
|
+
entry: remoteEntry,
|
|
122
|
+
entryGlobalName: remoteInfo.globalName
|
|
123
|
+
}
|
|
124
|
+
]);
|
|
125
|
+
} else if (!("entry" in remote) || !remote.entry.includes(import_sdk.MANIFEST_EXT)) {
|
|
126
|
+
const { hostGlobalSnapshot, remoteSnapshot } = hostInstance2.snapshotHandler.getGlobalRemoteInfo(remoteInfo);
|
|
127
|
+
import_logger.default.debug("find hostGlobalSnapshot: ", JSON.stringify(hostGlobalSnapshot));
|
|
128
|
+
import_logger.default.debug("find remoteSnapshot: ", JSON.stringify(remoteSnapshot));
|
|
129
|
+
if (!hostGlobalSnapshot || !remoteSnapshot) {
|
|
130
|
+
if ("version" in remote) {
|
|
131
|
+
delete remote.version;
|
|
132
|
+
}
|
|
133
|
+
remote.entry = remoteEntry;
|
|
134
|
+
remote.entryGlobalName = remoteInfo.globalName;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
} catch (e) {
|
|
138
|
+
ctx.status(500);
|
|
139
|
+
return ctx.text(`failed to fetch ${remoteInfo.name} data, error:
|
|
140
|
+
${e}`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const dataFetchItem = dataFetchMap[dataFetchId];
|
|
144
|
+
import_logger.default.debug("fetch data from server, dataFetchItem: ", dataFetchItem);
|
|
145
|
+
if (dataFetchItem) {
|
|
146
|
+
const callFetchDataPromise = (0, import_dataFetch.fetchData)(dataFetchId, {
|
|
147
|
+
...params,
|
|
148
|
+
isDowngrade: !remoteInfo
|
|
149
|
+
});
|
|
150
|
+
const wrappedPromise = wrapSetTimeout(callFetchDataPromise, 2e4, dataFetchId);
|
|
151
|
+
if (wrappedPromise) {
|
|
152
|
+
const res = await wrappedPromise;
|
|
153
|
+
import_logger.default.log("fetch data from server, dataFetchItem res: ", res);
|
|
154
|
+
return ctx.json(res);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const remoteId = dataFetchId.split(import_sdk.SEPARATOR)[0];
|
|
158
|
+
const hostInstance = globalThis.__FEDERATION__.__INSTANCES__[0];
|
|
159
|
+
if (!hostInstance) {
|
|
160
|
+
throw new Error("host instance not found!");
|
|
161
|
+
}
|
|
162
|
+
const dataFetchFn = await (0, import_dataFetch.loadDataFetchModule)(hostInstance, remoteId);
|
|
163
|
+
const data = await dataFetchFn({
|
|
164
|
+
...params,
|
|
165
|
+
isDowngrade: !remoteInfo
|
|
166
|
+
});
|
|
167
|
+
import_logger.default.log("fetch data from server, loadDataFetchModule res: ", data);
|
|
168
|
+
return ctx.json(data);
|
|
169
|
+
} catch (e) {
|
|
170
|
+
import_logger.default.error("server plugin data fetch error: ", e);
|
|
171
|
+
ctx.status(500);
|
|
172
|
+
return ctx.text(`failed to fetch ${remoteInfo.name} data, error:
|
|
173
|
+
${e}`);
|
|
174
|
+
}
|
|
175
|
+
};
|
|
35
176
|
const dataFetchServePlugin = () => ({
|
|
36
177
|
name: "mf-data-fetch-server-plugin",
|
|
37
178
|
setup: (api) => {
|
|
@@ -40,7 +181,7 @@ const dataFetchServePlugin = () => ({
|
|
|
40
181
|
middlewares.push({
|
|
41
182
|
name: "module-federation-serve-manifest",
|
|
42
183
|
// @ts-ignore type error
|
|
43
|
-
handler:
|
|
184
|
+
handler: middleware
|
|
44
185
|
});
|
|
45
186
|
});
|
|
46
187
|
}
|
package/dist/cjs/constant.js
CHANGED
|
@@ -18,14 +18,61 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
var constant_exports = {};
|
|
20
20
|
__export(constant_exports, {
|
|
21
|
+
DATA_FETCH_ERROR_PREFIX: () => DATA_FETCH_ERROR_PREFIX,
|
|
22
|
+
DATA_FETCH_FUNCTION: () => DATA_FETCH_FUNCTION,
|
|
23
|
+
DATA_FETCH_MAP_KEY: () => DATA_FETCH_MAP_KEY,
|
|
24
|
+
DATA_FETCH_QUERY: () => DATA_FETCH_QUERY,
|
|
25
|
+
DOWNGRADE_KEY: () => DOWNGRADE_KEY,
|
|
26
|
+
ERROR_TYPE: () => ERROR_TYPE,
|
|
27
|
+
FS_HREF: () => FS_HREF,
|
|
28
|
+
LOAD_REMOTE_ERROR_PREFIX: () => LOAD_REMOTE_ERROR_PREFIX,
|
|
21
29
|
LOCALHOST: () => LOCALHOST,
|
|
22
|
-
|
|
30
|
+
MF_DATA_FETCH_STATUS: () => MF_DATA_FETCH_STATUS,
|
|
31
|
+
MF_DATA_FETCH_TYPE: () => MF_DATA_FETCH_TYPE,
|
|
32
|
+
PLUGIN_IDENTIFIER: () => PLUGIN_IDENTIFIER,
|
|
33
|
+
WRAP_DATA_FETCH_ID_IDENTIFIER: () => WRAP_DATA_FETCH_ID_IDENTIFIER
|
|
23
34
|
});
|
|
24
35
|
module.exports = __toCommonJS(constant_exports);
|
|
25
36
|
const LOCALHOST = "localhost";
|
|
26
37
|
const PLUGIN_IDENTIFIER = "[ Modern.js Module Federation ]";
|
|
38
|
+
const DATA_FETCH_QUERY = "x-mf-data-fetch";
|
|
39
|
+
const DATA_FETCH_ERROR_PREFIX = "caught the following error during dataFetch: ";
|
|
40
|
+
const LOAD_REMOTE_ERROR_PREFIX = "caught the following error during loadRemote: ";
|
|
41
|
+
const DOWNGRADE_KEY = "_mfSSRDowngrade";
|
|
42
|
+
const DATA_FETCH_MAP_KEY = "__MF_DATA_FETCH_MAP__";
|
|
43
|
+
const DATA_FETCH_FUNCTION = "_mfDataFetch";
|
|
44
|
+
const FS_HREF = "_mfFSHref";
|
|
45
|
+
const ERROR_TYPE = {
|
|
46
|
+
DATA_FETCH: 1,
|
|
47
|
+
LOAD_REMOTE: 2,
|
|
48
|
+
UNKNOWN: 3
|
|
49
|
+
};
|
|
50
|
+
const WRAP_DATA_FETCH_ID_IDENTIFIER = "wrap_dfip_identifier";
|
|
51
|
+
var MF_DATA_FETCH_TYPE;
|
|
52
|
+
(function(MF_DATA_FETCH_TYPE2) {
|
|
53
|
+
MF_DATA_FETCH_TYPE2[MF_DATA_FETCH_TYPE2["FETCH_SERVER"] = 1] = "FETCH_SERVER";
|
|
54
|
+
MF_DATA_FETCH_TYPE2[MF_DATA_FETCH_TYPE2["FETCH_CLIENT"] = 2] = "FETCH_CLIENT";
|
|
55
|
+
})(MF_DATA_FETCH_TYPE || (MF_DATA_FETCH_TYPE = {}));
|
|
56
|
+
var MF_DATA_FETCH_STATUS;
|
|
57
|
+
(function(MF_DATA_FETCH_STATUS2) {
|
|
58
|
+
MF_DATA_FETCH_STATUS2[MF_DATA_FETCH_STATUS2["LOADED"] = 1] = "LOADED";
|
|
59
|
+
MF_DATA_FETCH_STATUS2[MF_DATA_FETCH_STATUS2["LOADING"] = 2] = "LOADING";
|
|
60
|
+
MF_DATA_FETCH_STATUS2[MF_DATA_FETCH_STATUS2["AWAIT"] = 0] = "AWAIT";
|
|
61
|
+
MF_DATA_FETCH_STATUS2[MF_DATA_FETCH_STATUS2["ERROR"] = 3] = "ERROR";
|
|
62
|
+
})(MF_DATA_FETCH_STATUS || (MF_DATA_FETCH_STATUS = {}));
|
|
27
63
|
// Annotate the CommonJS export names for ESM import in node:
|
|
28
64
|
0 && (module.exports = {
|
|
65
|
+
DATA_FETCH_ERROR_PREFIX,
|
|
66
|
+
DATA_FETCH_FUNCTION,
|
|
67
|
+
DATA_FETCH_MAP_KEY,
|
|
68
|
+
DATA_FETCH_QUERY,
|
|
69
|
+
DOWNGRADE_KEY,
|
|
70
|
+
ERROR_TYPE,
|
|
71
|
+
FS_HREF,
|
|
72
|
+
LOAD_REMOTE_ERROR_PREFIX,
|
|
29
73
|
LOCALHOST,
|
|
30
|
-
|
|
74
|
+
MF_DATA_FETCH_STATUS,
|
|
75
|
+
MF_DATA_FETCH_TYPE,
|
|
76
|
+
PLUGIN_IDENTIFIER,
|
|
77
|
+
WRAP_DATA_FETCH_ID_IDENTIFIER
|
|
31
78
|
});
|
|
@@ -0,0 +1,16 @@
|
|
|
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 __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
var global_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(global_exports);
|
|
@@ -0,0 +1,144 @@
|
|
|
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 AwaitDataFetch_exports = {};
|
|
30
|
+
__export(AwaitDataFetch_exports, {
|
|
31
|
+
AwaitDataFetch: () => AwaitDataFetch,
|
|
32
|
+
transformError: () => transformError
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(AwaitDataFetch_exports);
|
|
35
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
36
|
+
var import_react = require("react");
|
|
37
|
+
var import_logger = __toESM(require("../logger"));
|
|
38
|
+
var import_constant = require("../constant");
|
|
39
|
+
var import_utils = require("../utils");
|
|
40
|
+
function isPromise(obj) {
|
|
41
|
+
return !!obj && (typeof obj === "object" || typeof obj === "function") && typeof obj.then === "function";
|
|
42
|
+
}
|
|
43
|
+
const AWAIT_ERROR_PREFIX = "<Await /> caught the following error during render: ";
|
|
44
|
+
const transformError = (err) => {
|
|
45
|
+
const errMsg = err instanceof Error ? err.message : err;
|
|
46
|
+
const originalMsg = errMsg.replace(AWAIT_ERROR_PREFIX, "");
|
|
47
|
+
const dataFetchMapKey = (0, import_utils.getDataFetchIdWithErrorMsgs)(originalMsg);
|
|
48
|
+
if (originalMsg.indexOf(import_constant.DATA_FETCH_ERROR_PREFIX) === 0) {
|
|
49
|
+
return {
|
|
50
|
+
error: new Error(originalMsg.replace(import_constant.DATA_FETCH_ERROR_PREFIX, "").replace((0, import_utils.wrapDataFetchId)(dataFetchMapKey), "")),
|
|
51
|
+
errorType: import_constant.ERROR_TYPE.DATA_FETCH,
|
|
52
|
+
dataFetchMapKey
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
if (originalMsg.indexOf(import_constant.LOAD_REMOTE_ERROR_PREFIX) === 0) {
|
|
56
|
+
return {
|
|
57
|
+
error: new Error(originalMsg.replace(import_constant.LOAD_REMOTE_ERROR_PREFIX, "").replace((0, import_utils.wrapDataFetchId)(dataFetchMapKey), "")),
|
|
58
|
+
errorType: import_constant.ERROR_TYPE.LOAD_REMOTE,
|
|
59
|
+
dataFetchMapKey
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
error: new Error(originalMsg.replace((0, import_utils.wrapDataFetchId)(dataFetchMapKey), "")),
|
|
64
|
+
errorType: import_constant.ERROR_TYPE.UNKNOWN,
|
|
65
|
+
dataFetchMapKey
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
const DefaultLoading = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {});
|
|
69
|
+
const DefaultErrorElement = (_data) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
|
|
70
|
+
children: "Error"
|
|
71
|
+
});
|
|
72
|
+
function AwaitDataFetch({ resolve, loading = DefaultLoading, errorElement = DefaultErrorElement, children, params }) {
|
|
73
|
+
const dataRef = (0, import_react.useRef)(void 0);
|
|
74
|
+
const data = dataRef.current || resolve;
|
|
75
|
+
const getData = isPromise(data) ? fetchData(data, dataRef) : () => data;
|
|
76
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AwaitSuspense, {
|
|
77
|
+
params,
|
|
78
|
+
loading,
|
|
79
|
+
errorElement,
|
|
80
|
+
// @ts-ignore
|
|
81
|
+
resolve: getData,
|
|
82
|
+
children
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
function AwaitSuspense({ resolve, children, loading = DefaultLoading, errorElement = DefaultErrorElement }) {
|
|
86
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react.Suspense, {
|
|
87
|
+
fallback: loading,
|
|
88
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ResolveAwait, {
|
|
89
|
+
resolve,
|
|
90
|
+
errorElement,
|
|
91
|
+
children
|
|
92
|
+
})
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
function ResolveAwait({ children, resolve, errorElement, params }) {
|
|
96
|
+
const data = resolve();
|
|
97
|
+
import_logger.default.debug("resolve data: ", data);
|
|
98
|
+
if (typeof data === "string" && data.indexOf(AWAIT_ERROR_PREFIX) === 0) {
|
|
99
|
+
const transformedError = transformError(data);
|
|
100
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {
|
|
101
|
+
children: typeof errorElement === "function" ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, {
|
|
102
|
+
children: [
|
|
103
|
+
globalThis.FEDERATION_SSR && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("script", {
|
|
104
|
+
suppressHydrationWarning: true,
|
|
105
|
+
dangerouslySetInnerHTML: {
|
|
106
|
+
__html: String.raw`
|
|
107
|
+
globalThis['${import_constant.DATA_FETCH_FUNCTION}'] = globalThis['${import_constant.DATA_FETCH_FUNCTION}'] || []
|
|
108
|
+
globalThis['${import_constant.DATA_FETCH_FUNCTION}'].push([${transformedError.dataFetchMapKey ? `'${transformedError.dataFetchMapKey}'` : ""},${params ? JSON.stringify(params) : null},true]);`
|
|
109
|
+
}
|
|
110
|
+
}),
|
|
111
|
+
errorElement(transformedError)
|
|
112
|
+
]
|
|
113
|
+
}) : errorElement
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
const toRender = typeof children === "function" ? children(data) : children;
|
|
117
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {
|
|
118
|
+
children: toRender
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
const fetchData = (promise, ref) => {
|
|
122
|
+
let data;
|
|
123
|
+
let status = "pending";
|
|
124
|
+
const suspender = promise.then((res) => {
|
|
125
|
+
status = "success";
|
|
126
|
+
data = res;
|
|
127
|
+
ref.current = res;
|
|
128
|
+
}).catch((e) => {
|
|
129
|
+
status = "success";
|
|
130
|
+
console.warn(e);
|
|
131
|
+
data = AWAIT_ERROR_PREFIX + e;
|
|
132
|
+
});
|
|
133
|
+
return () => {
|
|
134
|
+
if (status === "pending") {
|
|
135
|
+
throw suspender;
|
|
136
|
+
}
|
|
137
|
+
return data;
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
141
|
+
0 && (module.exports = {
|
|
142
|
+
AwaitDataFetch,
|
|
143
|
+
transformError
|
|
144
|
+
});
|