@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.
Files changed (122) hide show
  1. package/dist/LICENSE +21 -0
  2. package/dist/cjs/cli/configPlugin.js +381 -0
  3. package/dist/cjs/cli/configPlugin.spec.js +114 -0
  4. package/dist/cjs/cli/index.js +95 -0
  5. package/dist/cjs/cli/mfRuntimePlugins/auto-fetch-data.js +98 -0
  6. package/dist/cjs/cli/mfRuntimePlugins/inject-node-fetch.js +44 -0
  7. package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +82 -0
  8. package/dist/cjs/cli/mfRuntimePlugins/shared-strategy.js +43 -0
  9. package/dist/cjs/cli/server/data-fetch-server-plugin.js +189 -0
  10. package/dist/cjs/cli/ssrPlugin.js +236 -0
  11. package/dist/cjs/cli/utils.js +90 -0
  12. package/dist/cjs/constant.js +78 -0
  13. package/dist/cjs/interfaces/bundler.js +16 -0
  14. package/dist/cjs/interfaces/global.js +16 -0
  15. package/dist/cjs/logger.js +27 -0
  16. package/dist/cjs/runtime/AwaitDataFetch.js +144 -0
  17. package/dist/cjs/runtime/createRemoteComponent.js +327 -0
  18. package/dist/cjs/runtime/index.js +50 -0
  19. package/dist/cjs/runtime/wrapNoSSR.js +35 -0
  20. package/dist/cjs/server/fileCache.js +84 -0
  21. package/dist/cjs/server/fileCache.spec.js +28 -0
  22. package/dist/cjs/server/index.js +58 -0
  23. package/dist/cjs/server/staticMiddleware.js +77 -0
  24. package/dist/cjs/server/staticMiddleware.spec.js +185 -0
  25. package/dist/cjs/ssr-runtime/SSRLiveReload.js +43 -0
  26. package/dist/cjs/ssr-runtime/devPlugin.js +71 -0
  27. package/dist/cjs/ssr-runtime/downgrade.js +114 -0
  28. package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +136 -0
  29. package/dist/cjs/types/index.js +16 -0
  30. package/dist/cjs/utils/dataFetch.js +211 -0
  31. package/dist/cjs/utils/index.js +54 -0
  32. package/dist/esm/cli/configPlugin.js +388 -0
  33. package/dist/esm/cli/configPlugin.spec.js +110 -0
  34. package/dist/esm/cli/index.js +90 -0
  35. package/dist/esm/cli/mfRuntimePlugins/auto-fetch-data.js +74 -0
  36. package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js +16 -0
  37. package/dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.js +74 -0
  38. package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +25 -0
  39. package/dist/esm/cli/server/data-fetch-server-plugin.js +237 -0
  40. package/dist/esm/cli/ssrPlugin.js +219 -0
  41. package/dist/esm/cli/utils.js +53 -0
  42. package/dist/esm/constant.js +42 -0
  43. package/dist/esm/interfaces/bundler.js +0 -0
  44. package/dist/esm/interfaces/global.js +0 -0
  45. package/dist/esm/logger.js +7 -0
  46. package/dist/esm/runtime/AwaitDataFetch.js +131 -0
  47. package/dist/esm/runtime/createRemoteComponent.js +417 -0
  48. package/dist/esm/runtime/index.js +22 -0
  49. package/dist/esm/runtime/wrapNoSSR.js +12 -0
  50. package/dist/esm/server/fileCache.js +98 -0
  51. package/dist/esm/server/fileCache.spec.js +50 -0
  52. package/dist/esm/server/index.js +36 -0
  53. package/dist/esm/server/staticMiddleware.js +81 -0
  54. package/dist/esm/server/staticMiddleware.spec.js +328 -0
  55. package/dist/esm/ssr-runtime/SSRLiveReload.js +26 -0
  56. package/dist/esm/ssr-runtime/devPlugin.js +73 -0
  57. package/dist/esm/ssr-runtime/downgrade.js +150 -0
  58. package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js +177 -0
  59. package/dist/esm/types/index.js +0 -0
  60. package/dist/esm/utils/dataFetch.js +237 -0
  61. package/dist/esm/utils/index.js +28 -0
  62. package/dist/esm-node/cli/configPlugin.js +340 -0
  63. package/dist/esm-node/cli/configPlugin.spec.js +91 -0
  64. package/dist/esm-node/cli/index.js +70 -0
  65. package/dist/esm-node/cli/mfRuntimePlugins/auto-fetch-data.js +68 -0
  66. package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.js +14 -0
  67. package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +62 -0
  68. package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +23 -0
  69. package/dist/esm-node/cli/server/data-fetch-server-plugin.js +159 -0
  70. package/dist/esm-node/cli/ssrPlugin.js +200 -0
  71. package/dist/esm-node/cli/utils.js +53 -0
  72. package/dist/esm-node/constant.js +42 -0
  73. package/dist/esm-node/interfaces/bundler.js +0 -0
  74. package/dist/esm-node/interfaces/global.js +0 -0
  75. package/dist/esm-node/logger.js +7 -0
  76. package/dist/esm-node/runtime/AwaitDataFetch.js +109 -0
  77. package/dist/esm-node/runtime/createRemoteComponent.js +291 -0
  78. package/dist/esm-node/runtime/index.js +22 -0
  79. package/dist/esm-node/runtime/wrapNoSSR.js +11 -0
  80. package/dist/esm-node/server/fileCache.js +49 -0
  81. package/dist/esm-node/server/fileCache.spec.js +27 -0
  82. package/dist/esm-node/server/index.js +34 -0
  83. package/dist/esm-node/server/staticMiddleware.js +43 -0
  84. package/dist/esm-node/server/staticMiddleware.spec.js +162 -0
  85. package/dist/esm-node/ssr-runtime/SSRLiveReload.js +19 -0
  86. package/dist/esm-node/ssr-runtime/devPlugin.js +37 -0
  87. package/dist/esm-node/ssr-runtime/downgrade.js +88 -0
  88. package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.js +101 -0
  89. package/dist/esm-node/types/index.js +0 -0
  90. package/dist/esm-node/utils/dataFetch.js +166 -0
  91. package/dist/esm-node/utils/index.js +27 -0
  92. package/dist/types/cli/configPlugin.d.ts +21 -0
  93. package/dist/types/cli/configPlugin.spec.d.ts +1 -0
  94. package/dist/types/cli/index.d.ts +6 -0
  95. package/dist/types/cli/mfRuntimePlugins/auto-fetch-data.d.ts +3 -0
  96. package/dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts +3 -0
  97. package/dist/types/cli/mfRuntimePlugins/resolve-entry-ipv4.d.ts +3 -0
  98. package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +3 -0
  99. package/dist/types/cli/server/data-fetch-server-plugin.d.ts +3 -0
  100. package/dist/types/cli/ssrPlugin.d.ts +6 -0
  101. package/dist/types/cli/utils.d.ts +6 -0
  102. package/dist/types/constant.d.ts +25 -0
  103. package/dist/types/interfaces/bundler.d.ts +18 -0
  104. package/dist/types/interfaces/global.d.ts +27 -0
  105. package/dist/types/logger.d.ts +2 -0
  106. package/dist/types/runtime/AwaitDataFetch.d.ts +19 -0
  107. package/dist/types/runtime/createRemoteComponent.d.ts +26 -0
  108. package/dist/types/runtime/index.d.ts +10 -0
  109. package/dist/types/runtime/wrapNoSSR.d.ts +9 -0
  110. package/dist/types/server/fileCache.d.ts +14 -0
  111. package/dist/types/server/fileCache.spec.d.ts +1 -0
  112. package/dist/types/server/index.d.ts +4 -0
  113. package/dist/types/server/staticMiddleware.d.ts +6 -0
  114. package/dist/types/server/staticMiddleware.spec.d.ts +1 -0
  115. package/dist/types/ssr-runtime/SSRLiveReload.d.ts +1 -0
  116. package/dist/types/ssr-runtime/devPlugin.d.ts +2 -0
  117. package/dist/types/ssr-runtime/downgrade.d.ts +4 -0
  118. package/dist/types/ssr-runtime/injectDataFetchFunctionPlugin.d.ts +7 -0
  119. package/dist/types/types/index.d.ts +24 -0
  120. package/dist/types/utils/dataFetch.d.ts +26 -0
  121. package/dist/types/utils/index.d.ts +15 -0
  122. package/package.json +37 -12
@@ -0,0 +1,327 @@
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 createRemoteComponent_exports = {};
30
+ __export(createRemoteComponent_exports, {
31
+ collectSSRAssets: () => collectSSRAssets,
32
+ createRemoteComponent: () => createRemoteComponent,
33
+ createRemoteSSRComponent: () => createRemoteSSRComponent
34
+ });
35
+ module.exports = __toCommonJS(createRemoteComponent_exports);
36
+ var import_jsx_runtime = require("react/jsx-runtime");
37
+ var import_react = __toESM(require("react"));
38
+ var import_logger = __toESM(require("../logger"));
39
+ var import_runtime = require("@module-federation/enhanced/runtime");
40
+ var import_AwaitDataFetch = require("./AwaitDataFetch");
41
+ var import_dataFetch = require("../utils/dataFetch");
42
+ var import_utils = require("../utils");
43
+ var import_constant = require("../constant");
44
+ function getTargetModuleInfo(id) {
45
+ const instance = (0, import_runtime.getInstance)();
46
+ if (!instance) {
47
+ return;
48
+ }
49
+ const loadedRemoteInfo = (0, import_utils.getLoadedRemoteInfos)(id, instance);
50
+ if (!loadedRemoteInfo) {
51
+ return;
52
+ }
53
+ const snapshot = loadedRemoteInfo.snapshot;
54
+ if (!snapshot) {
55
+ return;
56
+ }
57
+ const publicPath = "publicPath" in snapshot ? snapshot.publicPath : "getPublicPath" in snapshot ? new Function(snapshot.getPublicPath)() : "";
58
+ if (!publicPath) {
59
+ return;
60
+ }
61
+ const modules = "modules" in snapshot ? snapshot.modules : [];
62
+ const targetModule = modules.find((m) => m.modulePath === loadedRemoteInfo.expose);
63
+ if (!targetModule) {
64
+ return;
65
+ }
66
+ const remoteEntry = "remoteEntry" in snapshot ? snapshot.remoteEntry : "";
67
+ if (!remoteEntry) {
68
+ return;
69
+ }
70
+ return {
71
+ module: targetModule,
72
+ publicPath,
73
+ remoteEntry
74
+ };
75
+ }
76
+ function collectSSRAssets(options) {
77
+ const { id, injectLink = true, injectScript = false } = typeof options === "string" ? {
78
+ id: options
79
+ } : options;
80
+ const links = [];
81
+ const scripts = [];
82
+ const instance = (0, import_runtime.getInstance)();
83
+ if (!instance || !injectLink && !injectScript) {
84
+ return [
85
+ ...scripts,
86
+ ...links
87
+ ];
88
+ }
89
+ const moduleAndPublicPath = getTargetModuleInfo(id);
90
+ if (!moduleAndPublicPath) {
91
+ return [
92
+ ...scripts,
93
+ ...links
94
+ ];
95
+ }
96
+ const { module: targetModule, publicPath, remoteEntry } = moduleAndPublicPath;
97
+ if (injectLink) {
98
+ [
99
+ ...targetModule.assets.css.sync,
100
+ ...targetModule.assets.css.async
101
+ ].sort().forEach((file, index) => {
102
+ links.push(/* @__PURE__ */ (0, import_jsx_runtime.jsx)("link", {
103
+ href: `${publicPath}${file}`,
104
+ rel: "stylesheet",
105
+ type: "text/css"
106
+ }, `${file.split(".")[0]}_${index}`));
107
+ });
108
+ }
109
+ if (injectScript) {
110
+ scripts.push(/* @__PURE__ */ (0, import_jsx_runtime.jsx)("script", {
111
+ async: true,
112
+ src: `${publicPath}${remoteEntry}`,
113
+ crossOrigin: "anonymous"
114
+ }, remoteEntry.split(".")[0]));
115
+ [
116
+ ...targetModule.assets.js.sync
117
+ ].sort().forEach((file, index) => {
118
+ scripts.push(/* @__PURE__ */ (0, import_jsx_runtime.jsx)("script", {
119
+ async: true,
120
+ src: `${publicPath}${file}`,
121
+ crossOrigin: "anonymous"
122
+ }, `${file.split(".")[0]}_${index}`));
123
+ });
124
+ }
125
+ return [
126
+ ...scripts,
127
+ ...links
128
+ ];
129
+ }
130
+ function getServerNeedRemoteInfo(loadedRemoteInfo, id, noSSR) {
131
+ if (noSSR || typeof window !== "undefined" && window.location.href !== window[import_constant.FS_HREF]) {
132
+ var _dataFetchItem_;
133
+ if (!(loadedRemoteInfo === null || loadedRemoteInfo === void 0 ? void 0 : loadedRemoteInfo.version)) {
134
+ throw new Error(`${loadedRemoteInfo === null || loadedRemoteInfo === void 0 ? void 0 : loadedRemoteInfo.name} version is empty`);
135
+ }
136
+ const { snapshot } = loadedRemoteInfo;
137
+ if (!snapshot) {
138
+ throw new Error(`${loadedRemoteInfo === null || loadedRemoteInfo === void 0 ? void 0 : loadedRemoteInfo.name} snapshot is empty`);
139
+ }
140
+ const dataFetchItem = (0, import_dataFetch.getDataFetchItem)(id);
141
+ const isFetchServer = (dataFetchItem === null || dataFetchItem === void 0 ? void 0 : (_dataFetchItem_ = dataFetchItem[0]) === null || _dataFetchItem_ === void 0 ? void 0 : _dataFetchItem_[1]) === import_constant.MF_DATA_FETCH_TYPE.FETCH_SERVER;
142
+ if (isFetchServer && (!("ssrPublicPath" in snapshot) || !snapshot.ssrPublicPath)) {
143
+ throw new Error(`ssrPublicPath is required while fetching ${loadedRemoteInfo === null || loadedRemoteInfo === void 0 ? void 0 : loadedRemoteInfo.name} data in SSR project!`);
144
+ }
145
+ if (isFetchServer && (!("ssrRemoteEntry" in snapshot) || !snapshot.ssrRemoteEntry)) {
146
+ throw new Error(`ssrRemoteEntry is required while loading ${loadedRemoteInfo === null || loadedRemoteInfo === void 0 ? void 0 : loadedRemoteInfo.name} data loader in SSR project!`);
147
+ }
148
+ return {
149
+ name: loadedRemoteInfo.name,
150
+ version: loadedRemoteInfo.version,
151
+ ssrPublicPath: "ssrPublicPath" in snapshot ? snapshot.ssrPublicPath || "" : "",
152
+ ssrRemoteEntry: "ssrRemoteEntry" in snapshot ? snapshot.ssrRemoteEntry || "" : "",
153
+ globalName: loadedRemoteInfo.entryGlobalName
154
+ };
155
+ }
156
+ return;
157
+ }
158
+ function createRemoteComponent(options) {
159
+ const exportName = (options === null || options === void 0 ? void 0 : options.export) || "default";
160
+ const callLoader = async () => {
161
+ import_logger.default.debug("callLoader start", Date.now());
162
+ const m = await options.loader();
163
+ import_logger.default.debug("callLoader end", Date.now());
164
+ if (!m) {
165
+ throw new Error("load remote failed");
166
+ }
167
+ return m;
168
+ };
169
+ const getData = async (noSSR) => {
170
+ let loadedRemoteInfo;
171
+ let moduleId;
172
+ const instance = (0, import_runtime.getInstance)();
173
+ try {
174
+ const m = await callLoader();
175
+ moduleId = m && m[Symbol.for("mf_module_id")];
176
+ if (!moduleId) {
177
+ throw new Error("moduleId is empty");
178
+ }
179
+ loadedRemoteInfo = (0, import_utils.getLoadedRemoteInfos)(moduleId, instance);
180
+ if (!loadedRemoteInfo) {
181
+ throw new Error(`can not find loaded remote('${moduleId}') info!`);
182
+ }
183
+ } catch (e) {
184
+ const errMsg = `${import_constant.LOAD_REMOTE_ERROR_PREFIX}${e}`;
185
+ import_logger.default.debug(e);
186
+ throw new Error(errMsg);
187
+ }
188
+ let dataFetchMapKey;
189
+ try {
190
+ dataFetchMapKey = (0, import_dataFetch.getDataFetchMapKey)((0, import_utils.getDataFetchInfo)({
191
+ name: loadedRemoteInfo.name,
192
+ alias: loadedRemoteInfo.alias,
193
+ id: moduleId,
194
+ remoteSnapshot: loadedRemoteInfo.snapshot
195
+ }), {
196
+ name: instance.name,
197
+ version: instance === null || instance === void 0 ? void 0 : instance.options.version
198
+ });
199
+ import_logger.default.debug("getData dataFetchMapKey: ", dataFetchMapKey);
200
+ if (!dataFetchMapKey) {
201
+ return;
202
+ }
203
+ const data = await (0, import_dataFetch.fetchData)(dataFetchMapKey, {
204
+ ...options.dataFetchParams,
205
+ isDowngrade: false
206
+ }, getServerNeedRemoteInfo(loadedRemoteInfo, dataFetchMapKey, noSSR));
207
+ (0, import_utils.setDataFetchItemLoadedStatus)(dataFetchMapKey);
208
+ import_logger.default.debug("get data res: \n", data);
209
+ return data;
210
+ } catch (err) {
211
+ const errMsg = `${import_constant.DATA_FETCH_ERROR_PREFIX}${(0, import_utils.wrapDataFetchId)(dataFetchMapKey)}${err}`;
212
+ import_logger.default.debug(errMsg);
213
+ throw new Error(errMsg);
214
+ }
215
+ };
216
+ const LazyComponent = /* @__PURE__ */ import_react.default.lazy(async () => {
217
+ const m = await callLoader();
218
+ const moduleId = m && m[Symbol.for("mf_module_id")];
219
+ const instance = (0, import_runtime.getInstance)();
220
+ const loadedRemoteInfo = (0, import_utils.getLoadedRemoteInfos)(moduleId, instance);
221
+ loadedRemoteInfo === null || loadedRemoteInfo === void 0 ? void 0 : loadedRemoteInfo.snapshot;
222
+ const dataFetchMapKey = loadedRemoteInfo ? (0, import_dataFetch.getDataFetchMapKey)((0, import_utils.getDataFetchInfo)({
223
+ name: loadedRemoteInfo.name,
224
+ alias: loadedRemoteInfo.alias,
225
+ id: moduleId,
226
+ remoteSnapshot: loadedRemoteInfo.snapshot
227
+ }), {
228
+ name: instance.name,
229
+ version: instance === null || instance === void 0 ? void 0 : instance.options.version
230
+ }) : void 0;
231
+ import_logger.default.debug("LazyComponent dataFetchMapKey: ", dataFetchMapKey);
232
+ const assets = collectSSRAssets({
233
+ id: moduleId
234
+ });
235
+ const Com = m[exportName];
236
+ if (exportName in m && typeof Com === "function") {
237
+ return {
238
+ default: (props) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, {
239
+ children: [
240
+ globalThis.FEDERATION_SSR && dataFetchMapKey && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("script", {
241
+ suppressHydrationWarning: true,
242
+ dangerouslySetInnerHTML: {
243
+ __html: String.raw`
244
+ globalThis['${import_constant.DATA_FETCH_FUNCTION}'] = globalThis['${import_constant.DATA_FETCH_FUNCTION}'] || [];
245
+ globalThis['${import_constant.DATA_FETCH_FUNCTION}'].push(['${dataFetchMapKey}',${JSON.stringify(props.mfData)}]);
246
+ `
247
+ }
248
+ }),
249
+ globalThis.FEDERATION_SSR && assets,
250
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Com, {
251
+ ...props
252
+ })
253
+ ]
254
+ })
255
+ };
256
+ } else {
257
+ throw Error(`Make sure that ${moduleId} has the correct export when export is ${String(exportName)}`);
258
+ }
259
+ });
260
+ return (props) => {
261
+ const { key, ...args } = props;
262
+ if (globalThis.FEDERATION_SSR && !options.noSSR) {
263
+ const { key: key2, ...args2 } = props;
264
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_AwaitDataFetch.AwaitDataFetch, {
265
+ resolve: getData(options.noSSR),
266
+ loading: options.loading,
267
+ errorElement: options.fallback,
268
+ children: (data) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LazyComponent, {
269
+ ...args2,
270
+ mfData: data
271
+ })
272
+ });
273
+ } else {
274
+ const [data, setData] = (0, import_react.useState)(null);
275
+ const [loading, setLoading] = (0, import_react.useState)(true);
276
+ const [error, setError] = (0, import_react.useState)(null);
277
+ (0, import_react.useEffect)(() => {
278
+ let isMounted = true;
279
+ const fetchDataAsync = async () => {
280
+ try {
281
+ setLoading(true);
282
+ const result = await getData(options.noSSR);
283
+ if (isMounted) {
284
+ setData(result);
285
+ }
286
+ } catch (e) {
287
+ if (isMounted) {
288
+ setError((0, import_AwaitDataFetch.transformError)(e));
289
+ }
290
+ } finally {
291
+ if (isMounted) {
292
+ setLoading(false);
293
+ }
294
+ }
295
+ };
296
+ fetchDataAsync();
297
+ return () => {
298
+ isMounted = false;
299
+ };
300
+ }, []);
301
+ if (loading) {
302
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {
303
+ children: options.loading
304
+ });
305
+ }
306
+ if (error) {
307
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {
308
+ children: typeof options.fallback === "function" ? options.fallback(error) : options.fallback
309
+ });
310
+ }
311
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LazyComponent, {
312
+ ...args,
313
+ mfData: data
314
+ });
315
+ }
316
+ };
317
+ }
318
+ function createRemoteSSRComponent(options) {
319
+ import_logger.default.warn("createRemoteSSRComponent is deprecated, please use createRemoteComponent instead!");
320
+ return createRemoteComponent(options);
321
+ }
322
+ // Annotate the CommonJS export names for ESM import in node:
323
+ 0 && (module.exports = {
324
+ collectSSRAssets,
325
+ createRemoteComponent,
326
+ createRemoteSSRComponent
327
+ });
@@ -0,0 +1,50 @@
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 runtime_exports = {};
21
+ __export(runtime_exports, {
22
+ ERROR_TYPE: () => import_constant.ERROR_TYPE,
23
+ kit: () => kit,
24
+ setSSREnv: () => import_injectDataFetchFunctionPlugin.setSSREnv
25
+ });
26
+ module.exports = __toCommonJS(runtime_exports);
27
+ __reExport(runtime_exports, require("@module-federation/enhanced/runtime"), module.exports);
28
+ var import_constant = require("../constant");
29
+ var import_injectDataFetchFunctionPlugin = require("../ssr-runtime/injectDataFetchFunctionPlugin");
30
+ const kit = {
31
+ get createRemoteSSRComponent() {
32
+ return require("./createRemoteComponent").createRemoteSSRComponent;
33
+ },
34
+ get createRemoteComponent() {
35
+ return require("./createRemoteComponent").createRemoteComponent;
36
+ },
37
+ get collectSSRAssets() {
38
+ return require("./createRemoteComponent").collectSSRAssets;
39
+ },
40
+ get wrapNoSSR() {
41
+ return require("./wrapNoSSR").wrapNoSSR;
42
+ }
43
+ };
44
+ // Annotate the CommonJS export names for ESM import in node:
45
+ 0 && (module.exports = {
46
+ ERROR_TYPE,
47
+ kit,
48
+ setSSREnv,
49
+ ...require("@module-federation/enhanced/runtime")
50
+ });
@@ -0,0 +1,35 @@
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var wrapNoSSR_exports = {};
20
+ __export(wrapNoSSR_exports, {
21
+ wrapNoSSR: () => wrapNoSSR
22
+ });
23
+ module.exports = __toCommonJS(wrapNoSSR_exports);
24
+ function wrapNoSSR(createComponentFn) {
25
+ return (options) => {
26
+ return createComponentFn({
27
+ ...options,
28
+ noSSR: true
29
+ });
30
+ };
31
+ }
32
+ // Annotate the CommonJS export names for ESM import in node:
33
+ 0 && (module.exports = {
34
+ wrapNoSSR
35
+ });
@@ -0,0 +1,84 @@
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 fileCache_exports = {};
30
+ __export(fileCache_exports, {
31
+ FileCache: () => FileCache,
32
+ fileCache: () => fileCache
33
+ });
34
+ module.exports = __toCommonJS(fileCache_exports);
35
+ var import_fs_extra = __toESM(require("fs-extra"));
36
+ var import_lru_cache = require("lru-cache");
37
+ class FileCache {
38
+ /**
39
+ * Check if file exists and return file info
40
+ * @param filepath Path to the file
41
+ * @returns FileResult or null if file doesn't exist
42
+ */
43
+ async getFile(filepath) {
44
+ if (!await import_fs_extra.default.pathExists(filepath)) {
45
+ return null;
46
+ }
47
+ try {
48
+ const stat = await import_fs_extra.default.lstat(filepath);
49
+ const currentModified = stat.mtimeMs;
50
+ const cachedEntry = this.cache.get(filepath);
51
+ if (cachedEntry && currentModified <= cachedEntry.lastModified) {
52
+ return {
53
+ content: cachedEntry.content,
54
+ lastModified: cachedEntry.lastModified
55
+ };
56
+ }
57
+ const content = await import_fs_extra.default.readFile(filepath, "utf-8");
58
+ const newEntry = {
59
+ content,
60
+ lastModified: currentModified
61
+ };
62
+ this.cache.set(filepath, newEntry, {
63
+ size: stat.size || content.length
64
+ });
65
+ return {
66
+ content,
67
+ lastModified: currentModified
68
+ };
69
+ } catch (err) {
70
+ return null;
71
+ }
72
+ }
73
+ constructor() {
74
+ this.cache = new import_lru_cache.LRUCache({
75
+ maxSize: 200 * 1024 * 1024
76
+ });
77
+ }
78
+ }
79
+ const fileCache = new FileCache();
80
+ // Annotate the CommonJS export names for ESM import in node:
81
+ 0 && (module.exports = {
82
+ FileCache,
83
+ fileCache
84
+ });
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ var import_vitest = require("vitest");
3
+ var import_fileCache = require("./fileCache");
4
+ (0, import_vitest.beforeAll)(() => {
5
+ import_vitest.vi.mock("fs-extra", () => ({
6
+ default: {
7
+ pathExists: () => {
8
+ return true;
9
+ },
10
+ lstat: () => {
11
+ return {
12
+ mtimeMs: Date.now(),
13
+ size: 4
14
+ };
15
+ },
16
+ readFile: () => {
17
+ return "test";
18
+ }
19
+ }
20
+ }));
21
+ });
22
+ (0, import_vitest.describe)("modern serve static file cache", async () => {
23
+ (0, import_vitest.it)("should cache file", async () => {
24
+ const cache = new import_fileCache.FileCache();
25
+ const result = await cache.getFile("test.txt");
26
+ (0, import_vitest.expect)(result === null || result === void 0 ? void 0 : result.content).toBe("test");
27
+ });
28
+ });
@@ -0,0 +1,58 @@
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var server_exports = {};
20
+ __export(server_exports, {
21
+ default: () => server_default,
22
+ staticServePlugin: () => staticServePlugin
23
+ });
24
+ module.exports = __toCommonJS(server_exports);
25
+ var import_staticMiddleware = require("./staticMiddleware");
26
+ const staticServePlugin = () => ({
27
+ name: "@modern-js/module-federation/server",
28
+ setup: (api) => {
29
+ api.onPrepare(() => {
30
+ var _config_output, _config_server;
31
+ console.log(process.env.NODE_ENV);
32
+ if (process.env.NODE_ENV === "development") {
33
+ return;
34
+ }
35
+ const { middlewares } = api.getServerContext();
36
+ const config = api.getServerConfig();
37
+ const assetPrefix = ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.assetPrefix) || "";
38
+ if (!((_config_server = config.server) === null || _config_server === void 0 ? void 0 : _config_server.ssr)) {
39
+ return;
40
+ }
41
+ const context = api.getServerContext();
42
+ const pwd = context.distDirectory;
43
+ const serverStaticMiddleware = (0, import_staticMiddleware.createStaticMiddleware)({
44
+ assetPrefix,
45
+ pwd
46
+ });
47
+ middlewares.push({
48
+ name: "module-federation-serve-manifest",
49
+ handler: serverStaticMiddleware
50
+ });
51
+ });
52
+ }
53
+ });
54
+ var server_default = staticServePlugin;
55
+ // Annotate the CommonJS export names for ESM import in node:
56
+ 0 && (module.exports = {
57
+ staticServePlugin
58
+ });
@@ -0,0 +1,77 @@
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 staticMiddleware_exports = {};
30
+ __export(staticMiddleware_exports, {
31
+ createStaticMiddleware: () => createStaticMiddleware
32
+ });
33
+ module.exports = __toCommonJS(staticMiddleware_exports);
34
+ var import_fs_extra = __toESM(require("fs-extra"));
35
+ var import_node_path = __toESM(require("node:path"));
36
+ var import_fileCache = require("./fileCache");
37
+ const bundlesAssetPrefix = "/bundles";
38
+ const removeHost = (url) => {
39
+ try {
40
+ const hasProtocol = url.includes("://");
41
+ const hasDomain = hasProtocol || url.startsWith("//");
42
+ const pathname = hasDomain ? new URL(hasProtocol ? url : `http:${url}`).pathname : url;
43
+ return pathname;
44
+ } catch (e) {
45
+ return url;
46
+ }
47
+ };
48
+ const createStaticMiddleware = (options) => {
49
+ const { assetPrefix, pwd } = options;
50
+ return async (c, next) => {
51
+ const pathname = c.req.path;
52
+ if (import_node_path.default.extname(pathname) !== ".js") {
53
+ return next();
54
+ }
55
+ const prefixWithoutHost = removeHost(assetPrefix);
56
+ const prefixWithBundle = import_node_path.default.join(prefixWithoutHost, bundlesAssetPrefix);
57
+ if (!pathname.startsWith(prefixWithBundle)) {
58
+ return next();
59
+ }
60
+ const pathnameWithoutPrefix = pathname.replace(prefixWithBundle, "");
61
+ const filepath = import_node_path.default.join(pwd, bundlesAssetPrefix, pathnameWithoutPrefix);
62
+ if (!await import_fs_extra.default.pathExists(filepath)) {
63
+ return next();
64
+ }
65
+ const fileResult = await import_fileCache.fileCache.getFile(filepath);
66
+ if (!fileResult) {
67
+ return next();
68
+ }
69
+ c.header("Content-Type", "application/javascript");
70
+ c.header("Content-Length", String(fileResult.content.length));
71
+ return c.body(fileResult.content, 200);
72
+ };
73
+ };
74
+ // Annotate the CommonJS export names for ESM import in node:
75
+ 0 && (module.exports = {
76
+ createStaticMiddleware
77
+ });