@module-federation/modern-js 0.14.3 → 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 (91) hide show
  1. package/dist/cjs/cli/configPlugin.js +17 -13
  2. package/dist/cjs/cli/configPlugin.spec.js +3 -1
  3. package/dist/cjs/cli/index.js +3 -1
  4. package/dist/cjs/cli/mfRuntimePlugins/auto-fetch-data.js +98 -0
  5. package/dist/cjs/cli/server/data-fetch-server-plugin.js +189 -0
  6. package/dist/cjs/cli/ssrPlugin.js +31 -12
  7. package/dist/cjs/cli/utils.js +5 -0
  8. package/dist/cjs/constant.js +49 -2
  9. package/dist/cjs/{ssr-runtime/index.js → interfaces/global.js} +2 -8
  10. package/dist/cjs/{cli/logger.js → logger.js} +1 -1
  11. package/dist/cjs/runtime/AwaitDataFetch.js +144 -0
  12. package/dist/cjs/runtime/createRemoteComponent.js +327 -0
  13. package/dist/cjs/runtime/index.js +15 -3
  14. package/dist/cjs/{cli/constant.js → runtime/wrapNoSSR.js} +13 -6
  15. package/dist/cjs/ssr-runtime/{plugin.js → devPlugin.js} +9 -6
  16. package/dist/cjs/ssr-runtime/downgrade.js +114 -0
  17. package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +136 -0
  18. package/dist/cjs/utils/dataFetch.js +211 -0
  19. package/dist/cjs/utils/index.js +54 -0
  20. package/dist/esm/cli/configPlugin.js +18 -14
  21. package/dist/esm/cli/configPlugin.spec.js +3 -1
  22. package/dist/esm/cli/index.js +3 -1
  23. package/dist/esm/cli/mfRuntimePlugins/auto-fetch-data.js +74 -0
  24. package/dist/esm/cli/server/data-fetch-server-plugin.js +237 -0
  25. package/dist/esm/cli/ssrPlugin.js +28 -8
  26. package/dist/esm/cli/utils.js +4 -0
  27. package/dist/esm/constant.js +37 -1
  28. package/dist/esm/interfaces/global.js +0 -0
  29. package/dist/esm/{cli/logger.js → logger.js} +1 -1
  30. package/dist/esm/runtime/AwaitDataFetch.js +131 -0
  31. package/dist/esm/runtime/createRemoteComponent.js +417 -0
  32. package/dist/esm/runtime/index.js +13 -3
  33. package/dist/esm/runtime/wrapNoSSR.js +12 -0
  34. package/dist/esm/ssr-runtime/{plugin.js → devPlugin.js} +5 -2
  35. package/dist/esm/ssr-runtime/downgrade.js +150 -0
  36. package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js +177 -0
  37. package/dist/esm/utils/dataFetch.js +237 -0
  38. package/dist/esm/utils/index.js +28 -0
  39. package/dist/esm-node/cli/configPlugin.js +18 -14
  40. package/dist/esm-node/cli/configPlugin.spec.js +3 -1
  41. package/dist/esm-node/cli/index.js +3 -1
  42. package/dist/esm-node/cli/mfRuntimePlugins/auto-fetch-data.js +68 -0
  43. package/dist/esm-node/cli/server/data-fetch-server-plugin.js +159 -0
  44. package/dist/esm-node/cli/ssrPlugin.js +27 -8
  45. package/dist/esm-node/cli/utils.js +4 -0
  46. package/dist/esm-node/constant.js +37 -1
  47. package/dist/esm-node/interfaces/global.js +0 -0
  48. package/dist/esm-node/{cli/logger.js → logger.js} +1 -1
  49. package/dist/esm-node/runtime/AwaitDataFetch.js +109 -0
  50. package/dist/esm-node/runtime/createRemoteComponent.js +291 -0
  51. package/dist/esm-node/runtime/index.js +13 -3
  52. package/dist/esm-node/runtime/wrapNoSSR.js +11 -0
  53. package/dist/esm-node/ssr-runtime/{plugin.js → devPlugin.js} +5 -2
  54. package/dist/esm-node/ssr-runtime/downgrade.js +88 -0
  55. package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.js +101 -0
  56. package/dist/esm-node/utils/dataFetch.js +166 -0
  57. package/dist/esm-node/utils/index.js +27 -0
  58. package/dist/types/cli/configPlugin.d.ts +1 -1
  59. package/dist/types/cli/index.d.ts +1 -0
  60. package/dist/types/cli/mfRuntimePlugins/auto-fetch-data.d.ts +3 -0
  61. package/dist/types/cli/server/data-fetch-server-plugin.d.ts +3 -0
  62. package/dist/types/cli/utils.d.ts +1 -0
  63. package/dist/types/constant.d.ts +23 -0
  64. package/dist/types/interfaces/global.d.ts +27 -0
  65. package/dist/types/runtime/AwaitDataFetch.d.ts +19 -0
  66. package/dist/types/runtime/createRemoteComponent.d.ts +26 -0
  67. package/dist/types/runtime/index.d.ts +7 -2
  68. package/dist/types/runtime/wrapNoSSR.d.ts +9 -0
  69. package/dist/types/ssr-runtime/devPlugin.d.ts +2 -0
  70. package/dist/types/ssr-runtime/downgrade.d.ts +4 -0
  71. package/dist/types/ssr-runtime/injectDataFetchFunctionPlugin.d.ts +7 -0
  72. package/dist/types/types/index.d.ts +2 -0
  73. package/dist/types/utils/dataFetch.d.ts +26 -0
  74. package/dist/types/utils/index.d.ts +15 -0
  75. package/package.json +37 -12
  76. package/dist/cjs/cli/manifest.js +0 -75
  77. package/dist/cjs/runtime/createRemoteSSRComponent.js +0 -201
  78. package/dist/esm/cli/constant.js +0 -4
  79. package/dist/esm/cli/manifest.js +0 -41
  80. package/dist/esm/runtime/createRemoteSSRComponent.js +0 -191
  81. package/dist/esm/ssr-runtime/index.js +0 -1
  82. package/dist/esm-node/cli/constant.js +0 -4
  83. package/dist/esm-node/cli/manifest.js +0 -41
  84. package/dist/esm-node/runtime/createRemoteSSRComponent.js +0 -166
  85. package/dist/esm-node/ssr-runtime/index.js +0 -1
  86. package/dist/types/cli/constant.d.ts +0 -1
  87. package/dist/types/cli/manifest.d.ts +0 -2
  88. package/dist/types/runtime/createRemoteSSRComponent.d.ts +0 -18
  89. package/dist/types/ssr-runtime/index.d.ts +0 -1
  90. package/dist/types/ssr-runtime/plugin.d.ts +0 -2
  91. /package/dist/types/{cli/logger.d.ts → logger.d.ts} +0 -0
@@ -1,201 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var createRemoteSSRComponent_exports = {};
30
- __export(createRemoteSSRComponent_exports, {
31
- collectSSRAssets: () => collectSSRAssets,
32
- createRemoteSSRComponent: () => createRemoteSSRComponent
33
- });
34
- module.exports = __toCommonJS(createRemoteSSRComponent_exports);
35
- var import_jsx_runtime = require("react/jsx-runtime");
36
- var import_react = __toESM(require("react"));
37
- var import_runtime = require("@module-federation/enhanced/runtime");
38
- var import_react_error_boundary = require("react-error-boundary");
39
- function getLoadedRemoteInfos(instance, id) {
40
- const { name, expose } = instance.remoteHandler.idToRemoteMap[id] || {};
41
- if (!name) {
42
- return;
43
- }
44
- const module2 = instance.moduleCache.get(name);
45
- if (!module2) {
46
- return;
47
- }
48
- const { remoteSnapshot } = instance.snapshotHandler.getGlobalRemoteInfo(module2.remoteInfo);
49
- return {
50
- ...module2.remoteInfo,
51
- snapshot: remoteSnapshot,
52
- expose
53
- };
54
- }
55
- function getTargetModuleInfo(id) {
56
- const instance = (0, import_runtime.getInstance)();
57
- if (!instance) {
58
- return;
59
- }
60
- const loadedRemoteInfo = getLoadedRemoteInfos(instance, id);
61
- if (!loadedRemoteInfo) {
62
- return;
63
- }
64
- const snapshot = loadedRemoteInfo.snapshot;
65
- if (!snapshot) {
66
- return;
67
- }
68
- const publicPath = "publicPath" in snapshot ? snapshot.publicPath : "getPublicPath" in snapshot ? new Function(snapshot.getPublicPath)() : "";
69
- if (!publicPath) {
70
- return;
71
- }
72
- const modules = "modules" in snapshot ? snapshot.modules : [];
73
- const targetModule = modules.find((m) => m.modulePath === loadedRemoteInfo.expose);
74
- if (!targetModule) {
75
- return;
76
- }
77
- const remoteEntry = "remoteEntry" in snapshot ? snapshot.remoteEntry : "";
78
- if (!remoteEntry) {
79
- return;
80
- }
81
- return {
82
- module: targetModule,
83
- publicPath,
84
- remoteEntry
85
- };
86
- }
87
- function collectSSRAssets(options) {
88
- const { id, injectLink = true, injectScript = true } = typeof options === "string" ? {
89
- id: options
90
- } : options;
91
- const links = [];
92
- const scripts = [];
93
- const instance = (0, import_runtime.getInstance)();
94
- if (!instance || !injectLink && !injectScript) {
95
- return [
96
- ...scripts,
97
- ...links
98
- ];
99
- }
100
- const moduleAndPublicPath = getTargetModuleInfo(id);
101
- if (!moduleAndPublicPath) {
102
- return [
103
- ...scripts,
104
- ...links
105
- ];
106
- }
107
- const { module: targetModule, publicPath, remoteEntry } = moduleAndPublicPath;
108
- if (injectLink) {
109
- [
110
- ...targetModule.assets.css.sync,
111
- ...targetModule.assets.css.async
112
- ].forEach((file, index) => {
113
- links.push(/* @__PURE__ */ (0, import_jsx_runtime.jsx)("link", {
114
- href: `${publicPath}${file}`,
115
- rel: "stylesheet",
116
- type: "text/css"
117
- }, `${file.split(".")[0]}_${index}`));
118
- });
119
- }
120
- if (injectScript) {
121
- scripts.push(/* @__PURE__ */ (0, import_jsx_runtime.jsx)("script", {
122
- async: true,
123
- src: `${publicPath}${remoteEntry}`,
124
- crossOrigin: "anonymous"
125
- }, remoteEntry.split(".")[0]));
126
- [
127
- ...targetModule.assets.js.sync
128
- ].forEach((file, index) => {
129
- scripts.push(/* @__PURE__ */ (0, import_jsx_runtime.jsx)("script", {
130
- async: true,
131
- src: `${publicPath}${file}`,
132
- crossOrigin: "anonymous"
133
- }, `${file.split(".")[0]}_${index}`));
134
- });
135
- }
136
- return [
137
- ...scripts,
138
- ...links
139
- ];
140
- }
141
- function createRemoteSSRComponent(info) {
142
- const exportName = (info === null || info === void 0 ? void 0 : info.export) || "default";
143
- const LazyComponent = /* @__PURE__ */ import_react.default.lazy(async () => {
144
- try {
145
- const m = await info.loader();
146
- if (!m) {
147
- throw new Error("load remote failed");
148
- }
149
- const moduleId = m && m[Symbol.for("mf_module_id")];
150
- const assets = collectSSRAssets({
151
- id: moduleId
152
- });
153
- const Com = m[exportName];
154
- if (exportName in m && typeof Com === "function") {
155
- return {
156
- default: (props) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, {
157
- children: [
158
- assets,
159
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Com, {
160
- ...props
161
- })
162
- ]
163
- })
164
- };
165
- } else {
166
- throw Error(`Make sure that ${moduleId} has the correct export when export is ${String(exportName)}`);
167
- }
168
- } catch (err) {
169
- if (!info.fallback) {
170
- throw err;
171
- }
172
- const FallbackFunctionComponent = info.fallback;
173
- const FallbackNode = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FallbackFunctionComponent, {
174
- error: err,
175
- resetErrorBoundary: () => {
176
- console.log('SSR mode not support "resetErrorBoundary" !');
177
- }
178
- });
179
- return {
180
- default: () => FallbackNode
181
- };
182
- }
183
- });
184
- return (props) => {
185
- const { key, ...args } = props;
186
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_error_boundary.ErrorBoundary, {
187
- FallbackComponent: info.fallback,
188
- children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react.default.Suspense, {
189
- fallback: info.loading,
190
- children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LazyComponent, {
191
- ...args
192
- })
193
- })
194
- });
195
- };
196
- }
197
- // Annotate the CommonJS export names for ESM import in node:
198
- 0 && (module.exports = {
199
- collectSSRAssets,
200
- createRemoteSSRComponent
201
- });
@@ -1,4 +0,0 @@
1
- var isDev = process.env.NODE_ENV === "development";
2
- export {
3
- isDev
4
- };
@@ -1,41 +0,0 @@
1
- import path from "path";
2
- import fs from "fs-extra";
3
- function mergeStats(browserStats, nodeStats) {
4
- var ssrRemoteEntry = nodeStats.metaData.remoteEntry;
5
- browserStats.metaData.ssrRemoteEntry = ssrRemoteEntry;
6
- if ("publicPath" in browserStats.metaData) {
7
- browserStats.metaData.ssrPublicPath = nodeStats.metaData.publicPath;
8
- }
9
- return browserStats;
10
- }
11
- function mergeManifest(browserManifest, nodeManifest) {
12
- var ssrRemoteEntry = nodeManifest.metaData.remoteEntry;
13
- browserManifest.metaData.ssrRemoteEntry = ssrRemoteEntry;
14
- if ("publicPath" in browserManifest.metaData) {
15
- browserManifest.metaData.ssrPublicPath = nodeManifest.metaData.publicPath;
16
- }
17
- return browserManifest;
18
- }
19
- function mergeStatsAndManifest(nodePlugin, browserPlugin) {
20
- var nodeResourceInfo = nodePlugin.statsResourceInfo;
21
- var browserResourceInfo = browserPlugin.statsResourceInfo;
22
- if (!browserResourceInfo || !nodeResourceInfo || !browserResourceInfo.stats || !nodeResourceInfo.stats || !browserResourceInfo.manifest || !nodeResourceInfo.manifest) {
23
- throw new Error("can not get browserResourceInfo or nodeResourceInfo");
24
- }
25
- var mergedStats = mergeStats(browserResourceInfo.stats.stats, nodeResourceInfo.stats.stats);
26
- var mergedManifest = mergeManifest(browserResourceInfo.manifest.manifest, nodeResourceInfo.manifest.manifest);
27
- return {
28
- mergedStats,
29
- mergedStatsFilePath: browserResourceInfo.stats.filename,
30
- mergedManifest,
31
- mergedManifestFilePath: browserResourceInfo.manifest.filename
32
- };
33
- }
34
- function updateStatsAndManifest(nodePlugin, browserPlugin, outputDir) {
35
- var _mergeStatsAndManifest = mergeStatsAndManifest(nodePlugin, browserPlugin), mergedStats = _mergeStatsAndManifest.mergedStats, mergedStatsFilePath = _mergeStatsAndManifest.mergedStatsFilePath, mergedManifest = _mergeStatsAndManifest.mergedManifest, mergedManifestFilePath = _mergeStatsAndManifest.mergedManifestFilePath;
36
- fs.writeFileSync(path.resolve(outputDir, mergedStatsFilePath), JSON.stringify(mergedStats, null, 2));
37
- fs.writeFileSync(path.resolve(outputDir, mergedManifestFilePath), JSON.stringify(mergedManifest, null, 2));
38
- }
39
- export {
40
- updateStatsAndManifest
41
- };
@@ -1,191 +0,0 @@
1
- import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
- import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
3
- import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
4
- import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
5
- import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
6
- import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
7
- import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
8
- import React from "react";
9
- import { getInstance } from "@module-federation/enhanced/runtime";
10
- import { ErrorBoundary } from "react-error-boundary";
11
- function getLoadedRemoteInfos(instance, id) {
12
- var _ref = instance.remoteHandler.idToRemoteMap[id] || {}, name = _ref.name, expose = _ref.expose;
13
- if (!name) {
14
- return;
15
- }
16
- var module = instance.moduleCache.get(name);
17
- if (!module) {
18
- return;
19
- }
20
- var remoteSnapshot = instance.snapshotHandler.getGlobalRemoteInfo(module.remoteInfo).remoteSnapshot;
21
- return _object_spread_props(_object_spread({}, module.remoteInfo), {
22
- snapshot: remoteSnapshot,
23
- expose
24
- });
25
- }
26
- function getTargetModuleInfo(id) {
27
- var instance = getInstance();
28
- if (!instance) {
29
- return;
30
- }
31
- var loadedRemoteInfo = getLoadedRemoteInfos(instance, id);
32
- if (!loadedRemoteInfo) {
33
- return;
34
- }
35
- var snapshot = loadedRemoteInfo.snapshot;
36
- if (!snapshot) {
37
- return;
38
- }
39
- var publicPath = "publicPath" in snapshot ? snapshot.publicPath : "getPublicPath" in snapshot ? new Function(snapshot.getPublicPath)() : "";
40
- if (!publicPath) {
41
- return;
42
- }
43
- var modules = "modules" in snapshot ? snapshot.modules : [];
44
- var targetModule = modules.find(function(m) {
45
- return m.modulePath === loadedRemoteInfo.expose;
46
- });
47
- if (!targetModule) {
48
- return;
49
- }
50
- var remoteEntry = "remoteEntry" in snapshot ? snapshot.remoteEntry : "";
51
- if (!remoteEntry) {
52
- return;
53
- }
54
- return {
55
- module: targetModule,
56
- publicPath,
57
- remoteEntry
58
- };
59
- }
60
- function collectSSRAssets(options) {
61
- var _ref = typeof options === "string" ? {
62
- id: options
63
- } : 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 ? true : _ref_injectScript;
64
- var links = [];
65
- var scripts = [];
66
- var instance = getInstance();
67
- if (!instance || !injectLink && !injectScript) {
68
- return _to_consumable_array(scripts).concat(_to_consumable_array(links));
69
- }
70
- var moduleAndPublicPath = getTargetModuleInfo(id);
71
- if (!moduleAndPublicPath) {
72
- return _to_consumable_array(scripts).concat(_to_consumable_array(links));
73
- }
74
- var targetModule = moduleAndPublicPath.module, publicPath = moduleAndPublicPath.publicPath, remoteEntry = moduleAndPublicPath.remoteEntry;
75
- if (injectLink) {
76
- _to_consumable_array(targetModule.assets.css.sync).concat(_to_consumable_array(targetModule.assets.css.async)).forEach(function(file, index) {
77
- links.push(/* @__PURE__ */ _jsx("link", {
78
- href: "".concat(publicPath).concat(file),
79
- rel: "stylesheet",
80
- type: "text/css"
81
- }, "".concat(file.split(".")[0], "_").concat(index)));
82
- });
83
- }
84
- if (injectScript) {
85
- scripts.push(/* @__PURE__ */ _jsx("script", {
86
- async: true,
87
- src: "".concat(publicPath).concat(remoteEntry),
88
- crossOrigin: "anonymous"
89
- }, remoteEntry.split(".")[0]));
90
- _to_consumable_array(targetModule.assets.js.sync).forEach(function(file, index) {
91
- scripts.push(/* @__PURE__ */ _jsx("script", {
92
- async: true,
93
- src: "".concat(publicPath).concat(file),
94
- crossOrigin: "anonymous"
95
- }, "".concat(file.split(".")[0], "_").concat(index)));
96
- });
97
- }
98
- return _to_consumable_array(scripts).concat(_to_consumable_array(links));
99
- }
100
- function createRemoteSSRComponent(info) {
101
- var exportName = (info === null || info === void 0 ? void 0 : info.export) || "default";
102
- var LazyComponent = /* @__PURE__ */ React.lazy(/* @__PURE__ */ _async_to_generator(function() {
103
- var m, moduleId, assets, Com, err, FallbackFunctionComponent, FallbackNode;
104
- return _ts_generator(this, function(_state) {
105
- switch (_state.label) {
106
- case 0:
107
- _state.trys.push([
108
- 0,
109
- 2,
110
- ,
111
- 3
112
- ]);
113
- return [
114
- 4,
115
- info.loader()
116
- ];
117
- case 1:
118
- m = _state.sent();
119
- if (!m) {
120
- throw new Error("load remote failed");
121
- }
122
- moduleId = m && m[Symbol.for("mf_module_id")];
123
- assets = collectSSRAssets({
124
- id: moduleId
125
- });
126
- Com = m[exportName];
127
- if (exportName in m && typeof Com === "function") {
128
- return [
129
- 2,
130
- {
131
- default: function(props) {
132
- return /* @__PURE__ */ _jsxs(_Fragment, {
133
- children: [
134
- assets,
135
- /* @__PURE__ */ _jsx(Com, _object_spread({}, props))
136
- ]
137
- });
138
- }
139
- }
140
- ];
141
- } else {
142
- throw Error("Make sure that ".concat(moduleId, " has the correct export when export is ").concat(String(exportName)));
143
- }
144
- return [
145
- 3,
146
- 3
147
- ];
148
- case 2:
149
- err = _state.sent();
150
- if (!info.fallback) {
151
- throw err;
152
- }
153
- FallbackFunctionComponent = info.fallback;
154
- FallbackNode = /* @__PURE__ */ _jsx(FallbackFunctionComponent, {
155
- error: err,
156
- resetErrorBoundary: function() {
157
- console.log('SSR mode not support "resetErrorBoundary" !');
158
- }
159
- });
160
- return [
161
- 2,
162
- {
163
- default: function() {
164
- return FallbackNode;
165
- }
166
- }
167
- ];
168
- case 3:
169
- return [
170
- 2
171
- ];
172
- }
173
- });
174
- }));
175
- return function(props) {
176
- var key = props.key, args = _object_without_properties(props, [
177
- "key"
178
- ]);
179
- return /* @__PURE__ */ _jsx(ErrorBoundary, {
180
- FallbackComponent: info.fallback,
181
- children: /* @__PURE__ */ _jsx(React.Suspense, {
182
- fallback: info.loading,
183
- children: /* @__PURE__ */ _jsx(LazyComponent, _object_spread({}, args))
184
- })
185
- });
186
- };
187
- }
188
- export {
189
- collectSSRAssets,
190
- createRemoteSSRComponent
191
- };
@@ -1 +0,0 @@
1
- export * from "./plugin";
@@ -1,4 +0,0 @@
1
- const isDev = process.env.NODE_ENV === "development";
2
- export {
3
- isDev
4
- };
@@ -1,41 +0,0 @@
1
- import path from "path";
2
- import fs from "fs-extra";
3
- function mergeStats(browserStats, nodeStats) {
4
- const ssrRemoteEntry = nodeStats.metaData.remoteEntry;
5
- browserStats.metaData.ssrRemoteEntry = ssrRemoteEntry;
6
- if ("publicPath" in browserStats.metaData) {
7
- browserStats.metaData.ssrPublicPath = nodeStats.metaData.publicPath;
8
- }
9
- return browserStats;
10
- }
11
- function mergeManifest(browserManifest, nodeManifest) {
12
- const ssrRemoteEntry = nodeManifest.metaData.remoteEntry;
13
- browserManifest.metaData.ssrRemoteEntry = ssrRemoteEntry;
14
- if ("publicPath" in browserManifest.metaData) {
15
- browserManifest.metaData.ssrPublicPath = nodeManifest.metaData.publicPath;
16
- }
17
- return browserManifest;
18
- }
19
- function mergeStatsAndManifest(nodePlugin, browserPlugin) {
20
- const nodeResourceInfo = nodePlugin.statsResourceInfo;
21
- const browserResourceInfo = browserPlugin.statsResourceInfo;
22
- if (!browserResourceInfo || !nodeResourceInfo || !browserResourceInfo.stats || !nodeResourceInfo.stats || !browserResourceInfo.manifest || !nodeResourceInfo.manifest) {
23
- throw new Error("can not get browserResourceInfo or nodeResourceInfo");
24
- }
25
- const mergedStats = mergeStats(browserResourceInfo.stats.stats, nodeResourceInfo.stats.stats);
26
- const mergedManifest = mergeManifest(browserResourceInfo.manifest.manifest, nodeResourceInfo.manifest.manifest);
27
- return {
28
- mergedStats,
29
- mergedStatsFilePath: browserResourceInfo.stats.filename,
30
- mergedManifest,
31
- mergedManifestFilePath: browserResourceInfo.manifest.filename
32
- };
33
- }
34
- function updateStatsAndManifest(nodePlugin, browserPlugin, outputDir) {
35
- const { mergedStats, mergedStatsFilePath, mergedManifest, mergedManifestFilePath } = mergeStatsAndManifest(nodePlugin, browserPlugin);
36
- fs.writeFileSync(path.resolve(outputDir, mergedStatsFilePath), JSON.stringify(mergedStats, null, 2));
37
- fs.writeFileSync(path.resolve(outputDir, mergedManifestFilePath), JSON.stringify(mergedManifest, null, 2));
38
- }
39
- export {
40
- updateStatsAndManifest
41
- };
@@ -1,166 +0,0 @@
1
- import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
- import React from "react";
3
- import { getInstance } from "@module-federation/enhanced/runtime";
4
- import { ErrorBoundary } from "react-error-boundary";
5
- function getLoadedRemoteInfos(instance, id) {
6
- const { name, expose } = instance.remoteHandler.idToRemoteMap[id] || {};
7
- if (!name) {
8
- return;
9
- }
10
- const module = instance.moduleCache.get(name);
11
- if (!module) {
12
- return;
13
- }
14
- const { remoteSnapshot } = instance.snapshotHandler.getGlobalRemoteInfo(module.remoteInfo);
15
- return {
16
- ...module.remoteInfo,
17
- snapshot: remoteSnapshot,
18
- expose
19
- };
20
- }
21
- function getTargetModuleInfo(id) {
22
- const instance = getInstance();
23
- if (!instance) {
24
- return;
25
- }
26
- const loadedRemoteInfo = getLoadedRemoteInfos(instance, id);
27
- if (!loadedRemoteInfo) {
28
- return;
29
- }
30
- const snapshot = loadedRemoteInfo.snapshot;
31
- if (!snapshot) {
32
- return;
33
- }
34
- const publicPath = "publicPath" in snapshot ? snapshot.publicPath : "getPublicPath" in snapshot ? new Function(snapshot.getPublicPath)() : "";
35
- if (!publicPath) {
36
- return;
37
- }
38
- const modules = "modules" in snapshot ? snapshot.modules : [];
39
- const targetModule = modules.find((m) => m.modulePath === loadedRemoteInfo.expose);
40
- if (!targetModule) {
41
- return;
42
- }
43
- const remoteEntry = "remoteEntry" in snapshot ? snapshot.remoteEntry : "";
44
- if (!remoteEntry) {
45
- return;
46
- }
47
- return {
48
- module: targetModule,
49
- publicPath,
50
- remoteEntry
51
- };
52
- }
53
- function collectSSRAssets(options) {
54
- const { id, injectLink = true, injectScript = true } = typeof options === "string" ? {
55
- id: options
56
- } : options;
57
- const links = [];
58
- const scripts = [];
59
- const instance = getInstance();
60
- if (!instance || !injectLink && !injectScript) {
61
- return [
62
- ...scripts,
63
- ...links
64
- ];
65
- }
66
- const moduleAndPublicPath = getTargetModuleInfo(id);
67
- if (!moduleAndPublicPath) {
68
- return [
69
- ...scripts,
70
- ...links
71
- ];
72
- }
73
- const { module: targetModule, publicPath, remoteEntry } = moduleAndPublicPath;
74
- if (injectLink) {
75
- [
76
- ...targetModule.assets.css.sync,
77
- ...targetModule.assets.css.async
78
- ].forEach((file, index) => {
79
- links.push(/* @__PURE__ */ _jsx("link", {
80
- href: `${publicPath}${file}`,
81
- rel: "stylesheet",
82
- type: "text/css"
83
- }, `${file.split(".")[0]}_${index}`));
84
- });
85
- }
86
- if (injectScript) {
87
- scripts.push(/* @__PURE__ */ _jsx("script", {
88
- async: true,
89
- src: `${publicPath}${remoteEntry}`,
90
- crossOrigin: "anonymous"
91
- }, remoteEntry.split(".")[0]));
92
- [
93
- ...targetModule.assets.js.sync
94
- ].forEach((file, index) => {
95
- scripts.push(/* @__PURE__ */ _jsx("script", {
96
- async: true,
97
- src: `${publicPath}${file}`,
98
- crossOrigin: "anonymous"
99
- }, `${file.split(".")[0]}_${index}`));
100
- });
101
- }
102
- return [
103
- ...scripts,
104
- ...links
105
- ];
106
- }
107
- function createRemoteSSRComponent(info) {
108
- const exportName = (info === null || info === void 0 ? void 0 : info.export) || "default";
109
- const LazyComponent = /* @__PURE__ */ React.lazy(async () => {
110
- try {
111
- const m = await info.loader();
112
- if (!m) {
113
- throw new Error("load remote failed");
114
- }
115
- const moduleId = m && m[Symbol.for("mf_module_id")];
116
- const assets = collectSSRAssets({
117
- id: moduleId
118
- });
119
- const Com = m[exportName];
120
- if (exportName in m && typeof Com === "function") {
121
- return {
122
- default: (props) => /* @__PURE__ */ _jsxs(_Fragment, {
123
- children: [
124
- assets,
125
- /* @__PURE__ */ _jsx(Com, {
126
- ...props
127
- })
128
- ]
129
- })
130
- };
131
- } else {
132
- throw Error(`Make sure that ${moduleId} has the correct export when export is ${String(exportName)}`);
133
- }
134
- } catch (err) {
135
- if (!info.fallback) {
136
- throw err;
137
- }
138
- const FallbackFunctionComponent = info.fallback;
139
- const FallbackNode = /* @__PURE__ */ _jsx(FallbackFunctionComponent, {
140
- error: err,
141
- resetErrorBoundary: () => {
142
- console.log('SSR mode not support "resetErrorBoundary" !');
143
- }
144
- });
145
- return {
146
- default: () => FallbackNode
147
- };
148
- }
149
- });
150
- return (props) => {
151
- const { key, ...args } = props;
152
- return /* @__PURE__ */ _jsx(ErrorBoundary, {
153
- FallbackComponent: info.fallback,
154
- children: /* @__PURE__ */ _jsx(React.Suspense, {
155
- fallback: info.loading,
156
- children: /* @__PURE__ */ _jsx(LazyComponent, {
157
- ...args
158
- })
159
- })
160
- });
161
- };
162
- }
163
- export {
164
- collectSSRAssets,
165
- createRemoteSSRComponent
166
- };
@@ -1 +0,0 @@
1
- export * from "./plugin";
@@ -1 +0,0 @@
1
- export declare const isDev: boolean;
@@ -1,2 +0,0 @@
1
- import { BundlerPlugin } from '../types';
2
- export declare function updateStatsAndManifest(nodePlugin: BundlerPlugin, browserPlugin: BundlerPlugin, outputDir: string): void;
@@ -1,18 +0,0 @@
1
- import React from 'react';
2
- import { ErrorBoundaryPropsWithComponent } from 'react-error-boundary';
3
- type IProps = {
4
- id: string;
5
- injectScript?: boolean;
6
- injectLink?: boolean;
7
- };
8
- type ReactKey = {
9
- key?: React.Key | null;
10
- };
11
- export declare function collectSSRAssets(options: IProps): React.ReactNode[];
12
- export declare function createRemoteSSRComponent<T, E extends keyof T>(info: {
13
- loader: () => Promise<T>;
14
- loading: React.ReactNode;
15
- fallback: ErrorBoundaryPropsWithComponent['FallbackComponent'];
16
- export?: E;
17
- }): (props: T[E] extends (...args: any) => any ? Parameters<T[E]>[0] extends undefined ? ReactKey : Parameters<T[E]>[0] & ReactKey : ReactKey) => React.JSX.Element;
18
- export {};
@@ -1 +0,0 @@
1
- export * from './plugin';
@@ -1,2 +0,0 @@
1
- import type { RuntimePluginFuture } from '@modern-js/runtime';
2
- export declare const mfSSRPlugin: () => RuntimePluginFuture;