@module-federation/modern-js 0.14.2 → 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.
- package/dist/LICENSE +21 -0
- package/dist/cjs/cli/configPlugin.js +381 -0
- package/dist/cjs/cli/configPlugin.spec.js +114 -0
- package/dist/cjs/cli/index.js +95 -0
- package/dist/cjs/cli/mfRuntimePlugins/auto-fetch-data.js +98 -0
- package/dist/cjs/cli/mfRuntimePlugins/inject-node-fetch.js +44 -0
- package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +82 -0
- package/dist/cjs/cli/mfRuntimePlugins/shared-strategy.js +43 -0
- package/dist/cjs/cli/server/data-fetch-server-plugin.js +189 -0
- package/dist/cjs/cli/ssrPlugin.js +236 -0
- package/dist/cjs/cli/utils.js +90 -0
- package/dist/cjs/constant.js +78 -0
- package/dist/cjs/interfaces/bundler.js +16 -0
- package/dist/cjs/interfaces/global.js +16 -0
- package/dist/cjs/logger.js +27 -0
- package/dist/cjs/runtime/AwaitDataFetch.js +144 -0
- package/dist/cjs/runtime/createRemoteComponent.js +327 -0
- package/dist/cjs/runtime/index.js +50 -0
- package/dist/cjs/runtime/wrapNoSSR.js +35 -0
- package/dist/cjs/server/fileCache.js +84 -0
- package/dist/cjs/server/fileCache.spec.js +28 -0
- package/dist/cjs/server/index.js +58 -0
- package/dist/cjs/server/staticMiddleware.js +77 -0
- package/dist/cjs/server/staticMiddleware.spec.js +185 -0
- package/dist/cjs/ssr-runtime/SSRLiveReload.js +43 -0
- package/dist/cjs/ssr-runtime/devPlugin.js +71 -0
- package/dist/cjs/ssr-runtime/downgrade.js +114 -0
- package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +136 -0
- package/dist/cjs/types/index.js +16 -0
- package/dist/cjs/utils/dataFetch.js +211 -0
- package/dist/cjs/utils/index.js +54 -0
- package/dist/esm/cli/configPlugin.js +388 -0
- package/dist/esm/cli/configPlugin.spec.js +110 -0
- package/dist/esm/cli/index.js +90 -0
- package/dist/esm/cli/mfRuntimePlugins/auto-fetch-data.js +74 -0
- package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js +16 -0
- package/dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.js +74 -0
- package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +25 -0
- package/dist/esm/cli/server/data-fetch-server-plugin.js +237 -0
- package/dist/esm/cli/ssrPlugin.js +219 -0
- package/dist/esm/cli/utils.js +53 -0
- package/dist/esm/constant.js +42 -0
- package/dist/esm/interfaces/bundler.js +0 -0
- package/dist/esm/interfaces/global.js +0 -0
- package/dist/esm/logger.js +7 -0
- package/dist/esm/runtime/AwaitDataFetch.js +131 -0
- package/dist/esm/runtime/createRemoteComponent.js +417 -0
- package/dist/esm/runtime/index.js +22 -0
- package/dist/esm/runtime/wrapNoSSR.js +12 -0
- package/dist/esm/server/fileCache.js +98 -0
- package/dist/esm/server/fileCache.spec.js +50 -0
- package/dist/esm/server/index.js +36 -0
- package/dist/esm/server/staticMiddleware.js +81 -0
- package/dist/esm/server/staticMiddleware.spec.js +328 -0
- package/dist/esm/ssr-runtime/SSRLiveReload.js +26 -0
- package/dist/esm/ssr-runtime/devPlugin.js +73 -0
- package/dist/esm/ssr-runtime/downgrade.js +150 -0
- package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js +177 -0
- package/dist/esm/types/index.js +0 -0
- package/dist/esm/utils/dataFetch.js +237 -0
- package/dist/esm/utils/index.js +28 -0
- package/dist/esm-node/cli/configPlugin.js +340 -0
- package/dist/esm-node/cli/configPlugin.spec.js +91 -0
- package/dist/esm-node/cli/index.js +70 -0
- package/dist/esm-node/cli/mfRuntimePlugins/auto-fetch-data.js +68 -0
- package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.js +14 -0
- package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +62 -0
- package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +23 -0
- package/dist/esm-node/cli/server/data-fetch-server-plugin.js +159 -0
- package/dist/esm-node/cli/ssrPlugin.js +200 -0
- package/dist/esm-node/cli/utils.js +53 -0
- package/dist/esm-node/constant.js +42 -0
- package/dist/esm-node/interfaces/bundler.js +0 -0
- package/dist/esm-node/interfaces/global.js +0 -0
- package/dist/esm-node/logger.js +7 -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 +22 -0
- package/dist/esm-node/runtime/wrapNoSSR.js +11 -0
- package/dist/esm-node/server/fileCache.js +49 -0
- package/dist/esm-node/server/fileCache.spec.js +27 -0
- package/dist/esm-node/server/index.js +34 -0
- package/dist/esm-node/server/staticMiddleware.js +43 -0
- package/dist/esm-node/server/staticMiddleware.spec.js +162 -0
- package/dist/esm-node/ssr-runtime/SSRLiveReload.js +19 -0
- package/dist/esm-node/ssr-runtime/devPlugin.js +37 -0
- package/dist/esm-node/ssr-runtime/downgrade.js +88 -0
- package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.js +101 -0
- package/dist/esm-node/types/index.js +0 -0
- package/dist/esm-node/utils/dataFetch.js +166 -0
- package/dist/esm-node/utils/index.js +27 -0
- package/dist/types/cli/configPlugin.d.ts +21 -0
- package/dist/types/cli/configPlugin.spec.d.ts +1 -0
- package/dist/types/cli/index.d.ts +6 -0
- package/dist/types/cli/mfRuntimePlugins/auto-fetch-data.d.ts +3 -0
- package/dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts +3 -0
- package/dist/types/cli/mfRuntimePlugins/resolve-entry-ipv4.d.ts +3 -0
- package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +3 -0
- package/dist/types/cli/server/data-fetch-server-plugin.d.ts +3 -0
- package/dist/types/cli/ssrPlugin.d.ts +6 -0
- package/dist/types/cli/utils.d.ts +6 -0
- package/dist/types/constant.d.ts +25 -0
- package/dist/types/interfaces/bundler.d.ts +18 -0
- package/dist/types/interfaces/global.d.ts +27 -0
- package/dist/types/logger.d.ts +2 -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 +10 -0
- package/dist/types/runtime/wrapNoSSR.d.ts +9 -0
- package/dist/types/server/fileCache.d.ts +14 -0
- package/dist/types/server/fileCache.spec.d.ts +1 -0
- package/dist/types/server/index.d.ts +4 -0
- package/dist/types/server/staticMiddleware.d.ts +6 -0
- package/dist/types/server/staticMiddleware.spec.d.ts +1 -0
- package/dist/types/ssr-runtime/SSRLiveReload.d.ts +1 -0
- package/dist/types/ssr-runtime/devPlugin.d.ts +2 -0
- package/dist/types/ssr-runtime/downgrade.d.ts +4 -0
- package/dist/types/ssr-runtime/injectDataFetchFunctionPlugin.d.ts +7 -0
- package/dist/types/types/index.d.ts +24 -0
- package/dist/types/utils/dataFetch.d.ts +26 -0
- package/dist/types/utils/index.d.ts +15 -0
- package/package.json +37 -12
|
@@ -0,0 +1,417 @@
|
|
|
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 _object_without_properties } from "@swc/helpers/_/_object_without_properties";
|
|
5
|
+
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
|
|
6
|
+
import { _ as _tagged_template_literal } from "@swc/helpers/_/_tagged_template_literal";
|
|
7
|
+
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
8
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
9
|
+
function _templateObject() {
|
|
10
|
+
var data = _tagged_template_literal([
|
|
11
|
+
"\n globalThis['",
|
|
12
|
+
"'] = globalThis['",
|
|
13
|
+
"'] || [];\n globalThis['",
|
|
14
|
+
"'].push(['",
|
|
15
|
+
"',",
|
|
16
|
+
"]);\n "
|
|
17
|
+
]);
|
|
18
|
+
_templateObject = function _templateObject2() {
|
|
19
|
+
return data;
|
|
20
|
+
};
|
|
21
|
+
return data;
|
|
22
|
+
}
|
|
23
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
24
|
+
import React, { useEffect, useState } from "react";
|
|
25
|
+
import logger from "../logger";
|
|
26
|
+
import { getInstance } from "@module-federation/enhanced/runtime";
|
|
27
|
+
import { AwaitDataFetch, transformError } from "./AwaitDataFetch";
|
|
28
|
+
import { fetchData, getDataFetchItem, getDataFetchMapKey } from "../utils/dataFetch";
|
|
29
|
+
import { getDataFetchInfo, getLoadedRemoteInfos, setDataFetchItemLoadedStatus, wrapDataFetchId } from "../utils";
|
|
30
|
+
import { DATA_FETCH_ERROR_PREFIX, DATA_FETCH_FUNCTION, FS_HREF, LOAD_REMOTE_ERROR_PREFIX, MF_DATA_FETCH_TYPE } from "../constant";
|
|
31
|
+
function getTargetModuleInfo(id) {
|
|
32
|
+
var instance = getInstance();
|
|
33
|
+
if (!instance) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
var loadedRemoteInfo = getLoadedRemoteInfos(id, instance);
|
|
37
|
+
if (!loadedRemoteInfo) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
var snapshot = loadedRemoteInfo.snapshot;
|
|
41
|
+
if (!snapshot) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
var publicPath = "publicPath" in snapshot ? snapshot.publicPath : "getPublicPath" in snapshot ? new Function(snapshot.getPublicPath)() : "";
|
|
45
|
+
if (!publicPath) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
var modules = "modules" in snapshot ? snapshot.modules : [];
|
|
49
|
+
var targetModule = modules.find(function(m) {
|
|
50
|
+
return m.modulePath === loadedRemoteInfo.expose;
|
|
51
|
+
});
|
|
52
|
+
if (!targetModule) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
var remoteEntry = "remoteEntry" in snapshot ? snapshot.remoteEntry : "";
|
|
56
|
+
if (!remoteEntry) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
module: targetModule,
|
|
61
|
+
publicPath,
|
|
62
|
+
remoteEntry
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function collectSSRAssets(options) {
|
|
66
|
+
var _ref = typeof options === "string" ? {
|
|
67
|
+
id: options
|
|
68
|
+
} : options, id = _ref.id, _ref_injectLink = _ref.injectLink, injectLink = _ref_injectLink === void 0 ? true : _ref_injectLink, _ref_injectScript = _ref.injectScript, injectScript = _ref_injectScript === void 0 ? false : _ref_injectScript;
|
|
69
|
+
var links = [];
|
|
70
|
+
var scripts = [];
|
|
71
|
+
var instance = getInstance();
|
|
72
|
+
if (!instance || !injectLink && !injectScript) {
|
|
73
|
+
return _to_consumable_array(scripts).concat(_to_consumable_array(links));
|
|
74
|
+
}
|
|
75
|
+
var moduleAndPublicPath = getTargetModuleInfo(id);
|
|
76
|
+
if (!moduleAndPublicPath) {
|
|
77
|
+
return _to_consumable_array(scripts).concat(_to_consumable_array(links));
|
|
78
|
+
}
|
|
79
|
+
var targetModule = moduleAndPublicPath.module, publicPath = moduleAndPublicPath.publicPath, remoteEntry = moduleAndPublicPath.remoteEntry;
|
|
80
|
+
if (injectLink) {
|
|
81
|
+
_to_consumable_array(targetModule.assets.css.sync).concat(_to_consumable_array(targetModule.assets.css.async)).sort().forEach(function(file, index) {
|
|
82
|
+
links.push(/* @__PURE__ */ _jsx("link", {
|
|
83
|
+
href: "".concat(publicPath).concat(file),
|
|
84
|
+
rel: "stylesheet",
|
|
85
|
+
type: "text/css"
|
|
86
|
+
}, "".concat(file.split(".")[0], "_").concat(index)));
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
if (injectScript) {
|
|
90
|
+
scripts.push(/* @__PURE__ */ _jsx("script", {
|
|
91
|
+
async: true,
|
|
92
|
+
src: "".concat(publicPath).concat(remoteEntry),
|
|
93
|
+
crossOrigin: "anonymous"
|
|
94
|
+
}, remoteEntry.split(".")[0]));
|
|
95
|
+
_to_consumable_array(targetModule.assets.js.sync).sort().forEach(function(file, index) {
|
|
96
|
+
scripts.push(/* @__PURE__ */ _jsx("script", {
|
|
97
|
+
async: true,
|
|
98
|
+
src: "".concat(publicPath).concat(file),
|
|
99
|
+
crossOrigin: "anonymous"
|
|
100
|
+
}, "".concat(file.split(".")[0], "_").concat(index)));
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
return _to_consumable_array(scripts).concat(_to_consumable_array(links));
|
|
104
|
+
}
|
|
105
|
+
function getServerNeedRemoteInfo(loadedRemoteInfo, id, noSSR) {
|
|
106
|
+
if (noSSR || typeof window !== "undefined" && window.location.href !== window[FS_HREF]) {
|
|
107
|
+
var _dataFetchItem_;
|
|
108
|
+
if (!(loadedRemoteInfo === null || loadedRemoteInfo === void 0 ? void 0 : loadedRemoteInfo.version)) {
|
|
109
|
+
throw new Error("".concat(loadedRemoteInfo === null || loadedRemoteInfo === void 0 ? void 0 : loadedRemoteInfo.name, " version is empty"));
|
|
110
|
+
}
|
|
111
|
+
var snapshot = loadedRemoteInfo.snapshot;
|
|
112
|
+
if (!snapshot) {
|
|
113
|
+
throw new Error("".concat(loadedRemoteInfo === null || loadedRemoteInfo === void 0 ? void 0 : loadedRemoteInfo.name, " snapshot is empty"));
|
|
114
|
+
}
|
|
115
|
+
var dataFetchItem = getDataFetchItem(id);
|
|
116
|
+
var isFetchServer = (dataFetchItem === null || dataFetchItem === void 0 ? void 0 : (_dataFetchItem_ = dataFetchItem[0]) === null || _dataFetchItem_ === void 0 ? void 0 : _dataFetchItem_[1]) === MF_DATA_FETCH_TYPE.FETCH_SERVER;
|
|
117
|
+
if (isFetchServer && (!("ssrPublicPath" in snapshot) || !snapshot.ssrPublicPath)) {
|
|
118
|
+
throw new Error("ssrPublicPath is required while fetching ".concat(loadedRemoteInfo === null || loadedRemoteInfo === void 0 ? void 0 : loadedRemoteInfo.name, " data in SSR project!"));
|
|
119
|
+
}
|
|
120
|
+
if (isFetchServer && (!("ssrRemoteEntry" in snapshot) || !snapshot.ssrRemoteEntry)) {
|
|
121
|
+
throw new Error("ssrRemoteEntry is required while loading ".concat(loadedRemoteInfo === null || loadedRemoteInfo === void 0 ? void 0 : loadedRemoteInfo.name, " data loader in SSR project!"));
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
name: loadedRemoteInfo.name,
|
|
125
|
+
version: loadedRemoteInfo.version,
|
|
126
|
+
ssrPublicPath: "ssrPublicPath" in snapshot ? snapshot.ssrPublicPath || "" : "",
|
|
127
|
+
ssrRemoteEntry: "ssrRemoteEntry" in snapshot ? snapshot.ssrRemoteEntry || "" : "",
|
|
128
|
+
globalName: loadedRemoteInfo.entryGlobalName
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
function createRemoteComponent(options) {
|
|
134
|
+
var exportName = (options === null || options === void 0 ? void 0 : options.export) || "default";
|
|
135
|
+
var callLoader = function() {
|
|
136
|
+
var _ref = _async_to_generator(function() {
|
|
137
|
+
var m;
|
|
138
|
+
return _ts_generator(this, function(_state) {
|
|
139
|
+
switch (_state.label) {
|
|
140
|
+
case 0:
|
|
141
|
+
logger.debug("callLoader start", Date.now());
|
|
142
|
+
return [
|
|
143
|
+
4,
|
|
144
|
+
options.loader()
|
|
145
|
+
];
|
|
146
|
+
case 1:
|
|
147
|
+
m = _state.sent();
|
|
148
|
+
logger.debug("callLoader end", Date.now());
|
|
149
|
+
if (!m) {
|
|
150
|
+
throw new Error("load remote failed");
|
|
151
|
+
}
|
|
152
|
+
return [
|
|
153
|
+
2,
|
|
154
|
+
m
|
|
155
|
+
];
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
return function callLoader2() {
|
|
160
|
+
return _ref.apply(this, arguments);
|
|
161
|
+
};
|
|
162
|
+
}();
|
|
163
|
+
var getData = function() {
|
|
164
|
+
var _ref = _async_to_generator(function(noSSR) {
|
|
165
|
+
var loadedRemoteInfo, moduleId, instance, m, e, errMsg, dataFetchMapKey, data, err, errMsg1;
|
|
166
|
+
return _ts_generator(this, function(_state) {
|
|
167
|
+
switch (_state.label) {
|
|
168
|
+
case 0:
|
|
169
|
+
instance = getInstance();
|
|
170
|
+
_state.label = 1;
|
|
171
|
+
case 1:
|
|
172
|
+
_state.trys.push([
|
|
173
|
+
1,
|
|
174
|
+
3,
|
|
175
|
+
,
|
|
176
|
+
4
|
|
177
|
+
]);
|
|
178
|
+
return [
|
|
179
|
+
4,
|
|
180
|
+
callLoader()
|
|
181
|
+
];
|
|
182
|
+
case 2:
|
|
183
|
+
m = _state.sent();
|
|
184
|
+
moduleId = m && m[Symbol.for("mf_module_id")];
|
|
185
|
+
if (!moduleId) {
|
|
186
|
+
throw new Error("moduleId is empty");
|
|
187
|
+
}
|
|
188
|
+
loadedRemoteInfo = getLoadedRemoteInfos(moduleId, instance);
|
|
189
|
+
if (!loadedRemoteInfo) {
|
|
190
|
+
throw new Error("can not find loaded remote('".concat(moduleId, "') info!"));
|
|
191
|
+
}
|
|
192
|
+
return [
|
|
193
|
+
3,
|
|
194
|
+
4
|
|
195
|
+
];
|
|
196
|
+
case 3:
|
|
197
|
+
e = _state.sent();
|
|
198
|
+
errMsg = "".concat(LOAD_REMOTE_ERROR_PREFIX).concat(e);
|
|
199
|
+
logger.debug(e);
|
|
200
|
+
throw new Error(errMsg);
|
|
201
|
+
case 4:
|
|
202
|
+
_state.trys.push([
|
|
203
|
+
4,
|
|
204
|
+
6,
|
|
205
|
+
,
|
|
206
|
+
7
|
|
207
|
+
]);
|
|
208
|
+
dataFetchMapKey = getDataFetchMapKey(getDataFetchInfo({
|
|
209
|
+
name: loadedRemoteInfo.name,
|
|
210
|
+
alias: loadedRemoteInfo.alias,
|
|
211
|
+
id: moduleId,
|
|
212
|
+
remoteSnapshot: loadedRemoteInfo.snapshot
|
|
213
|
+
}), {
|
|
214
|
+
name: instance.name,
|
|
215
|
+
version: instance === null || instance === void 0 ? void 0 : instance.options.version
|
|
216
|
+
});
|
|
217
|
+
logger.debug("getData dataFetchMapKey: ", dataFetchMapKey);
|
|
218
|
+
if (!dataFetchMapKey) {
|
|
219
|
+
return [
|
|
220
|
+
2
|
|
221
|
+
];
|
|
222
|
+
}
|
|
223
|
+
return [
|
|
224
|
+
4,
|
|
225
|
+
fetchData(dataFetchMapKey, _object_spread_props(_object_spread({}, options.dataFetchParams), {
|
|
226
|
+
isDowngrade: false
|
|
227
|
+
}), getServerNeedRemoteInfo(loadedRemoteInfo, dataFetchMapKey, noSSR))
|
|
228
|
+
];
|
|
229
|
+
case 5:
|
|
230
|
+
data = _state.sent();
|
|
231
|
+
setDataFetchItemLoadedStatus(dataFetchMapKey);
|
|
232
|
+
logger.debug("get data res: \n", data);
|
|
233
|
+
return [
|
|
234
|
+
2,
|
|
235
|
+
data
|
|
236
|
+
];
|
|
237
|
+
case 6:
|
|
238
|
+
err = _state.sent();
|
|
239
|
+
errMsg1 = "".concat(DATA_FETCH_ERROR_PREFIX).concat(wrapDataFetchId(dataFetchMapKey)).concat(err);
|
|
240
|
+
logger.debug(errMsg1);
|
|
241
|
+
throw new Error(errMsg1);
|
|
242
|
+
case 7:
|
|
243
|
+
return [
|
|
244
|
+
2
|
|
245
|
+
];
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
return function getData2(noSSR) {
|
|
250
|
+
return _ref.apply(this, arguments);
|
|
251
|
+
};
|
|
252
|
+
}();
|
|
253
|
+
var LazyComponent = /* @__PURE__ */ React.lazy(/* @__PURE__ */ _async_to_generator(function() {
|
|
254
|
+
var m, moduleId, instance, loadedRemoteInfo, dataFetchMapKey, assets, Com;
|
|
255
|
+
return _ts_generator(this, function(_state) {
|
|
256
|
+
switch (_state.label) {
|
|
257
|
+
case 0:
|
|
258
|
+
return [
|
|
259
|
+
4,
|
|
260
|
+
callLoader()
|
|
261
|
+
];
|
|
262
|
+
case 1:
|
|
263
|
+
m = _state.sent();
|
|
264
|
+
moduleId = m && m[Symbol.for("mf_module_id")];
|
|
265
|
+
instance = getInstance();
|
|
266
|
+
loadedRemoteInfo = getLoadedRemoteInfos(moduleId, instance);
|
|
267
|
+
loadedRemoteInfo === null || loadedRemoteInfo === void 0 ? void 0 : loadedRemoteInfo.snapshot;
|
|
268
|
+
dataFetchMapKey = loadedRemoteInfo ? getDataFetchMapKey(getDataFetchInfo({
|
|
269
|
+
name: loadedRemoteInfo.name,
|
|
270
|
+
alias: loadedRemoteInfo.alias,
|
|
271
|
+
id: moduleId,
|
|
272
|
+
remoteSnapshot: loadedRemoteInfo.snapshot
|
|
273
|
+
}), {
|
|
274
|
+
name: instance.name,
|
|
275
|
+
version: instance === null || instance === void 0 ? void 0 : instance.options.version
|
|
276
|
+
}) : void 0;
|
|
277
|
+
logger.debug("LazyComponent dataFetchMapKey: ", dataFetchMapKey);
|
|
278
|
+
assets = collectSSRAssets({
|
|
279
|
+
id: moduleId
|
|
280
|
+
});
|
|
281
|
+
Com = m[exportName];
|
|
282
|
+
if (exportName in m && typeof Com === "function") {
|
|
283
|
+
return [
|
|
284
|
+
2,
|
|
285
|
+
{
|
|
286
|
+
default: function(props) {
|
|
287
|
+
return /* @__PURE__ */ _jsxs(_Fragment, {
|
|
288
|
+
children: [
|
|
289
|
+
globalThis.FEDERATION_SSR && dataFetchMapKey && /* @__PURE__ */ _jsx("script", {
|
|
290
|
+
suppressHydrationWarning: true,
|
|
291
|
+
dangerouslySetInnerHTML: {
|
|
292
|
+
__html: String.raw(_templateObject(), DATA_FETCH_FUNCTION, DATA_FETCH_FUNCTION, DATA_FETCH_FUNCTION, dataFetchMapKey, JSON.stringify(props.mfData))
|
|
293
|
+
}
|
|
294
|
+
}),
|
|
295
|
+
globalThis.FEDERATION_SSR && assets,
|
|
296
|
+
/* @__PURE__ */ _jsx(Com, _object_spread({}, props))
|
|
297
|
+
]
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
];
|
|
302
|
+
} else {
|
|
303
|
+
throw Error("Make sure that ".concat(moduleId, " has the correct export when export is ").concat(String(exportName)));
|
|
304
|
+
}
|
|
305
|
+
return [
|
|
306
|
+
2
|
|
307
|
+
];
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
}));
|
|
311
|
+
return function(props) {
|
|
312
|
+
var key = props.key, args = _object_without_properties(props, [
|
|
313
|
+
"key"
|
|
314
|
+
]);
|
|
315
|
+
if (globalThis.FEDERATION_SSR && !options.noSSR) {
|
|
316
|
+
var key1 = props.key, args1 = _object_without_properties(props, [
|
|
317
|
+
"key"
|
|
318
|
+
]);
|
|
319
|
+
return /* @__PURE__ */ _jsx(AwaitDataFetch, {
|
|
320
|
+
resolve: getData(options.noSSR),
|
|
321
|
+
loading: options.loading,
|
|
322
|
+
errorElement: options.fallback,
|
|
323
|
+
children: function(data2) {
|
|
324
|
+
return /* @__PURE__ */ _jsx(LazyComponent, _object_spread_props(_object_spread({}, args1), {
|
|
325
|
+
mfData: data2
|
|
326
|
+
}));
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
} else {
|
|
330
|
+
var _useState = _sliced_to_array(useState(null), 2), data = _useState[0], setData = _useState[1];
|
|
331
|
+
var _useState1 = _sliced_to_array(useState(true), 2), loading = _useState1[0], setLoading = _useState1[1];
|
|
332
|
+
var _useState2 = _sliced_to_array(useState(null), 2), error = _useState2[0], setError = _useState2[1];
|
|
333
|
+
useEffect(function() {
|
|
334
|
+
var isMounted = true;
|
|
335
|
+
var fetchDataAsync = function() {
|
|
336
|
+
var _ref = _async_to_generator(function() {
|
|
337
|
+
var result, e;
|
|
338
|
+
return _ts_generator(this, function(_state) {
|
|
339
|
+
switch (_state.label) {
|
|
340
|
+
case 0:
|
|
341
|
+
_state.trys.push([
|
|
342
|
+
0,
|
|
343
|
+
2,
|
|
344
|
+
3,
|
|
345
|
+
4
|
|
346
|
+
]);
|
|
347
|
+
setLoading(true);
|
|
348
|
+
return [
|
|
349
|
+
4,
|
|
350
|
+
getData(options.noSSR)
|
|
351
|
+
];
|
|
352
|
+
case 1:
|
|
353
|
+
result = _state.sent();
|
|
354
|
+
if (isMounted) {
|
|
355
|
+
setData(result);
|
|
356
|
+
}
|
|
357
|
+
return [
|
|
358
|
+
3,
|
|
359
|
+
4
|
|
360
|
+
];
|
|
361
|
+
case 2:
|
|
362
|
+
e = _state.sent();
|
|
363
|
+
if (isMounted) {
|
|
364
|
+
setError(transformError(e));
|
|
365
|
+
}
|
|
366
|
+
return [
|
|
367
|
+
3,
|
|
368
|
+
4
|
|
369
|
+
];
|
|
370
|
+
case 3:
|
|
371
|
+
if (isMounted) {
|
|
372
|
+
setLoading(false);
|
|
373
|
+
}
|
|
374
|
+
return [
|
|
375
|
+
7
|
|
376
|
+
];
|
|
377
|
+
case 4:
|
|
378
|
+
return [
|
|
379
|
+
2
|
|
380
|
+
];
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
});
|
|
384
|
+
return function fetchDataAsync2() {
|
|
385
|
+
return _ref.apply(this, arguments);
|
|
386
|
+
};
|
|
387
|
+
}();
|
|
388
|
+
fetchDataAsync();
|
|
389
|
+
return function() {
|
|
390
|
+
isMounted = false;
|
|
391
|
+
};
|
|
392
|
+
}, []);
|
|
393
|
+
if (loading) {
|
|
394
|
+
return /* @__PURE__ */ _jsx(_Fragment, {
|
|
395
|
+
children: options.loading
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
if (error) {
|
|
399
|
+
return /* @__PURE__ */ _jsx(_Fragment, {
|
|
400
|
+
children: typeof options.fallback === "function" ? options.fallback(error) : options.fallback
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
return /* @__PURE__ */ _jsx(LazyComponent, _object_spread_props(_object_spread({}, args), {
|
|
404
|
+
mfData: data
|
|
405
|
+
}));
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
function createRemoteSSRComponent(options) {
|
|
410
|
+
logger.warn("createRemoteSSRComponent is deprecated, please use createRemoteComponent instead!");
|
|
411
|
+
return createRemoteComponent(options);
|
|
412
|
+
}
|
|
413
|
+
export {
|
|
414
|
+
collectSSRAssets,
|
|
415
|
+
createRemoteComponent,
|
|
416
|
+
createRemoteSSRComponent
|
|
417
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export * from "@module-federation/enhanced/runtime";
|
|
2
|
+
import { ERROR_TYPE } from "../constant";
|
|
3
|
+
var kit = {
|
|
4
|
+
get createRemoteSSRComponent() {
|
|
5
|
+
return require("./createRemoteComponent").createRemoteSSRComponent;
|
|
6
|
+
},
|
|
7
|
+
get createRemoteComponent() {
|
|
8
|
+
return require("./createRemoteComponent").createRemoteComponent;
|
|
9
|
+
},
|
|
10
|
+
get collectSSRAssets() {
|
|
11
|
+
return require("./createRemoteComponent").collectSSRAssets;
|
|
12
|
+
},
|
|
13
|
+
get wrapNoSSR() {
|
|
14
|
+
return require("./wrapNoSSR").wrapNoSSR;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
import { setSSREnv } from "../ssr-runtime/injectDataFetchFunctionPlugin";
|
|
18
|
+
export {
|
|
19
|
+
ERROR_TYPE,
|
|
20
|
+
kit,
|
|
21
|
+
setSSREnv
|
|
22
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
2
|
+
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
3
|
+
function wrapNoSSR(createComponentFn) {
|
|
4
|
+
return function(options) {
|
|
5
|
+
return createComponentFn(_object_spread_props(_object_spread({}, options), {
|
|
6
|
+
noSSR: true
|
|
7
|
+
}));
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
export {
|
|
11
|
+
wrapNoSSR
|
|
12
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
|
|
3
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
4
|
+
import fs from "fs-extra";
|
|
5
|
+
import { LRUCache } from "lru-cache";
|
|
6
|
+
var FileCache = /* @__PURE__ */ function() {
|
|
7
|
+
"use strict";
|
|
8
|
+
function FileCache2() {
|
|
9
|
+
_class_call_check(this, FileCache2);
|
|
10
|
+
this.cache = new LRUCache({
|
|
11
|
+
maxSize: 200 * 1024 * 1024
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
var _proto = FileCache2.prototype;
|
|
15
|
+
_proto.getFile = function getFile(filepath) {
|
|
16
|
+
var _this = this;
|
|
17
|
+
return _async_to_generator(function() {
|
|
18
|
+
var stat, currentModified, cachedEntry, content, newEntry, err;
|
|
19
|
+
return _ts_generator(this, function(_state) {
|
|
20
|
+
switch (_state.label) {
|
|
21
|
+
case 0:
|
|
22
|
+
return [
|
|
23
|
+
4,
|
|
24
|
+
fs.pathExists(filepath)
|
|
25
|
+
];
|
|
26
|
+
case 1:
|
|
27
|
+
if (!_state.sent()) {
|
|
28
|
+
return [
|
|
29
|
+
2,
|
|
30
|
+
null
|
|
31
|
+
];
|
|
32
|
+
}
|
|
33
|
+
_state.label = 2;
|
|
34
|
+
case 2:
|
|
35
|
+
_state.trys.push([
|
|
36
|
+
2,
|
|
37
|
+
5,
|
|
38
|
+
,
|
|
39
|
+
6
|
|
40
|
+
]);
|
|
41
|
+
return [
|
|
42
|
+
4,
|
|
43
|
+
fs.lstat(filepath)
|
|
44
|
+
];
|
|
45
|
+
case 3:
|
|
46
|
+
stat = _state.sent();
|
|
47
|
+
currentModified = stat.mtimeMs;
|
|
48
|
+
cachedEntry = _this.cache.get(filepath);
|
|
49
|
+
if (cachedEntry && currentModified <= cachedEntry.lastModified) {
|
|
50
|
+
return [
|
|
51
|
+
2,
|
|
52
|
+
{
|
|
53
|
+
content: cachedEntry.content,
|
|
54
|
+
lastModified: cachedEntry.lastModified
|
|
55
|
+
}
|
|
56
|
+
];
|
|
57
|
+
}
|
|
58
|
+
return [
|
|
59
|
+
4,
|
|
60
|
+
fs.readFile(filepath, "utf-8")
|
|
61
|
+
];
|
|
62
|
+
case 4:
|
|
63
|
+
content = _state.sent();
|
|
64
|
+
newEntry = {
|
|
65
|
+
content,
|
|
66
|
+
lastModified: currentModified
|
|
67
|
+
};
|
|
68
|
+
_this.cache.set(filepath, newEntry, {
|
|
69
|
+
size: stat.size || content.length
|
|
70
|
+
});
|
|
71
|
+
return [
|
|
72
|
+
2,
|
|
73
|
+
{
|
|
74
|
+
content,
|
|
75
|
+
lastModified: currentModified
|
|
76
|
+
}
|
|
77
|
+
];
|
|
78
|
+
case 5:
|
|
79
|
+
err = _state.sent();
|
|
80
|
+
return [
|
|
81
|
+
2,
|
|
82
|
+
null
|
|
83
|
+
];
|
|
84
|
+
case 6:
|
|
85
|
+
return [
|
|
86
|
+
2
|
|
87
|
+
];
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
})();
|
|
91
|
+
};
|
|
92
|
+
return FileCache2;
|
|
93
|
+
}();
|
|
94
|
+
var fileCache = new FileCache();
|
|
95
|
+
export {
|
|
96
|
+
FileCache,
|
|
97
|
+
fileCache
|
|
98
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
3
|
+
import { it, expect, describe, vi, beforeAll } from "vitest";
|
|
4
|
+
import { FileCache } from "./fileCache";
|
|
5
|
+
beforeAll(function() {
|
|
6
|
+
vi.mock("fs-extra", function() {
|
|
7
|
+
return {
|
|
8
|
+
default: {
|
|
9
|
+
pathExists: function() {
|
|
10
|
+
return true;
|
|
11
|
+
},
|
|
12
|
+
lstat: function() {
|
|
13
|
+
return {
|
|
14
|
+
mtimeMs: Date.now(),
|
|
15
|
+
size: 4
|
|
16
|
+
};
|
|
17
|
+
},
|
|
18
|
+
readFile: function() {
|
|
19
|
+
return "test";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
describe("modern serve static file cache", /* @__PURE__ */ _async_to_generator(function() {
|
|
26
|
+
return _ts_generator(this, function(_state) {
|
|
27
|
+
it("should cache file", /* @__PURE__ */ _async_to_generator(function() {
|
|
28
|
+
var cache, result;
|
|
29
|
+
return _ts_generator(this, function(_state2) {
|
|
30
|
+
switch (_state2.label) {
|
|
31
|
+
case 0:
|
|
32
|
+
cache = new FileCache();
|
|
33
|
+
return [
|
|
34
|
+
4,
|
|
35
|
+
cache.getFile("test.txt")
|
|
36
|
+
];
|
|
37
|
+
case 1:
|
|
38
|
+
result = _state2.sent();
|
|
39
|
+
expect(result === null || result === void 0 ? void 0 : result.content).toBe("test");
|
|
40
|
+
return [
|
|
41
|
+
2
|
|
42
|
+
];
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}));
|
|
46
|
+
return [
|
|
47
|
+
2
|
|
48
|
+
];
|
|
49
|
+
});
|
|
50
|
+
}));
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { createStaticMiddleware } from "./staticMiddleware";
|
|
2
|
+
var staticServePlugin = function() {
|
|
3
|
+
return {
|
|
4
|
+
name: "@modern-js/module-federation/server",
|
|
5
|
+
setup: function(api) {
|
|
6
|
+
api.onPrepare(function() {
|
|
7
|
+
var _config_output, _config_server;
|
|
8
|
+
console.log(process.env.NODE_ENV);
|
|
9
|
+
if (process.env.NODE_ENV === "development") {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
var middlewares = api.getServerContext().middlewares;
|
|
13
|
+
var config = api.getServerConfig();
|
|
14
|
+
var assetPrefix = ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.assetPrefix) || "";
|
|
15
|
+
if (!((_config_server = config.server) === null || _config_server === void 0 ? void 0 : _config_server.ssr)) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
var context = api.getServerContext();
|
|
19
|
+
var pwd = context.distDirectory;
|
|
20
|
+
var serverStaticMiddleware = createStaticMiddleware({
|
|
21
|
+
assetPrefix,
|
|
22
|
+
pwd
|
|
23
|
+
});
|
|
24
|
+
middlewares.push({
|
|
25
|
+
name: "module-federation-serve-manifest",
|
|
26
|
+
handler: serverStaticMiddleware
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
var server_default = staticServePlugin;
|
|
33
|
+
export {
|
|
34
|
+
server_default as default,
|
|
35
|
+
staticServePlugin
|
|
36
|
+
};
|