@module-federation/modern-js 0.0.0-next-20240623084034 → 0.0.0-next-20240625025206

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.
@@ -5,7 +5,7 @@ import { fs } from "@modern-js/utils";
5
5
  import { ModuleFederationPlugin as WebpackModuleFederationPlugin, AsyncBoundaryPlugin } from "@module-federation/enhanced";
6
6
  import { ModuleFederationPlugin as RspackModuleFederationPlugin } from "@module-federation/enhanced/rspack";
7
7
  import { StreamingTargetPlugin, EntryChunkTrackerPlugin } from "@module-federation/node";
8
- import { getMFConfig, getTargetEnvConfig, patchWebpackConfig, lookupIpv4 } from "./utils";
8
+ import { getMFConfig, getTargetEnvConfig, patchWebpackConfig, getIPV4 } from "./utils";
9
9
  import { updateStatsAndManifest } from "./manifest";
10
10
  import { MODERN_JS_SERVER_DIR } from "../constant";
11
11
  var SSR_PLUGIN_IDENTIFIER = "mfPluginSSR";
@@ -36,135 +36,130 @@ var moduleFederationPlugin = function() {
36
36
  config: /* @__PURE__ */ _async_to_generator(function() {
37
37
  var _modernjsConfig_dev, bundlerType, WebpackPluginConstructor, RspackPluginConstructor, MFBundlerPlugin, modifyBundlerConfig, ipv4;
38
38
  return _ts_generator(this, function(_state2) {
39
- switch (_state2.label) {
40
- case 0:
41
- bundlerType = useAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
42
- WebpackPluginConstructor = userConfig.webpackPluginImplementation || WebpackModuleFederationPlugin;
43
- RspackPluginConstructor = userConfig.rspackPluginImplementation || RspackModuleFederationPlugin;
44
- MFBundlerPlugin = bundlerType === "rspack" ? RspackPluginConstructor : WebpackPluginConstructor;
45
- if (enableSSR) {
46
- process.env["MF_DISABLE_EMIT_STATS"] = "true";
47
- process.env["MF_SSR_PRJ"] = "true";
39
+ bundlerType = useAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
40
+ WebpackPluginConstructor = userConfig.webpackPluginImplementation || WebpackModuleFederationPlugin;
41
+ RspackPluginConstructor = userConfig.rspackPluginImplementation || RspackModuleFederationPlugin;
42
+ MFBundlerPlugin = bundlerType === "rspack" ? RspackPluginConstructor : WebpackPluginConstructor;
43
+ if (enableSSR) {
44
+ process.env["MF_DISABLE_EMIT_STATS"] = "true";
45
+ process.env["MF_SSR_PRJ"] = "true";
46
+ }
47
+ modifyBundlerConfig = function(config, isServer) {
48
+ var envConfig = getTargetEnvConfig(mfConfig, isServer);
49
+ if (isServer) {
50
+ var _config_plugins, _config_plugins1;
51
+ nodePlugin = new MFBundlerPlugin(envConfig);
52
+ (_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(nodePlugin);
53
+ (_config_plugins1 = config.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(new StreamingTargetPlugin(envConfig));
54
+ if (isDev) {
55
+ var _config_plugins2;
56
+ (_config_plugins2 = config.plugins) === null || _config_plugins2 === void 0 ? void 0 : _config_plugins2.push(new EntryChunkTrackerPlugin());
48
57
  }
49
- modifyBundlerConfig = function(config, isServer) {
50
- var envConfig = getTargetEnvConfig(mfConfig, isServer);
51
- if (isServer) {
52
- var _config_plugins, _config_plugins1;
53
- nodePlugin = new MFBundlerPlugin(envConfig);
54
- (_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(nodePlugin);
55
- (_config_plugins1 = config.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(new StreamingTargetPlugin(envConfig));
56
- if (isDev) {
57
- var _config_plugins2;
58
- (_config_plugins2 = config.plugins) === null || _config_plugins2 === void 0 ? void 0 : _config_plugins2.push(new EntryChunkTrackerPlugin());
58
+ } else {
59
+ var _config_output, _config_plugins3;
60
+ outputDir = ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.path) || path.resolve(process.cwd(), "dist");
61
+ browserPlugin = new MFBundlerPlugin(envConfig);
62
+ (_config_plugins3 = config.plugins) === null || _config_plugins3 === void 0 ? void 0 : _config_plugins3.push(browserPlugin);
63
+ }
64
+ patchWebpackConfig({
65
+ bundlerConfig: config,
66
+ isServer,
67
+ modernjsConfig,
68
+ mfConfig: envConfig
69
+ });
70
+ };
71
+ ipv4 = getIPV4();
72
+ return [
73
+ 2,
74
+ {
75
+ tools: {
76
+ rspack: function rspack(config) {
77
+ if (enableSSR) {
78
+ throw new Error("@module-federation/modern-js not support ssr for rspack bundler yet!");
59
79
  }
60
- } else {
61
- var _config_output, _config_plugins3;
62
- outputDir = ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.path) || path.resolve(process.cwd(), "dist");
63
- browserPlugin = new MFBundlerPlugin(envConfig);
64
- (_config_plugins3 = config.plugins) === null || _config_plugins3 === void 0 ? void 0 : _config_plugins3.push(browserPlugin);
65
- }
66
- patchWebpackConfig({
67
- bundlerConfig: config,
68
- isServer,
69
- modernjsConfig,
70
- mfConfig: envConfig
71
- });
72
- };
73
- return [
74
- 4,
75
- lookupIpv4()
76
- ];
77
- case 1:
78
- ipv4 = _state2.sent();
79
- return [
80
- 2,
81
- {
82
- tools: {
83
- rspack: function rspack(config) {
84
- modifyBundlerConfig(config, false);
85
- },
86
- webpack: function webpack(config, param2) {
87
- var isServer = param2.isServer;
88
- var _modernjsConfig_source;
89
- modifyBundlerConfig(config, isServer);
90
- var enableAsyncEntry = (_modernjsConfig_source = modernjsConfig.source) === null || _modernjsConfig_source === void 0 ? void 0 : _modernjsConfig_source.enableAsyncEntry;
91
- if (!enableAsyncEntry && mfConfig.async !== false) {
92
- var _config_plugins;
93
- var asyncBoundaryPluginOptions = typeof mfConfig.async === "object" ? mfConfig.async : {
94
- eager: function(module) {
95
- return module && /\.federation/.test((module === null || module === void 0 ? void 0 : module.request) || "");
96
- },
97
- excludeChunk: function(chunk) {
98
- return chunk.name === mfConfig.name;
99
- }
100
- };
101
- (_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(new AsyncBoundaryPlugin(asyncBoundaryPluginOptions));
102
- }
103
- config.ignoreWarnings = config.ignoreWarnings || [];
104
- config.ignoreWarnings.push(function(warning) {
105
- if (warning.message.includes("external script")) {
106
- return true;
107
- }
108
- return false;
109
- });
110
- },
111
- devServer: {
112
- headers: {
113
- "Access-Control-Allow-Origin": "*",
114
- "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
115
- "Access-Control-Allow-Headers": "X-Requested-With, content-type, Authorization"
80
+ modifyBundlerConfig(config, false);
81
+ },
82
+ webpack: function webpack(config, param2) {
83
+ var isServer = param2.isServer;
84
+ var _modernjsConfig_source;
85
+ modifyBundlerConfig(config, isServer);
86
+ var enableAsyncEntry = (_modernjsConfig_source = modernjsConfig.source) === null || _modernjsConfig_source === void 0 ? void 0 : _modernjsConfig_source.enableAsyncEntry;
87
+ if (!enableAsyncEntry && mfConfig.async !== false) {
88
+ var _config_plugins;
89
+ var asyncBoundaryPluginOptions = typeof mfConfig.async === "object" ? mfConfig.async : {
90
+ eager: function(module) {
91
+ return module && /\.federation/.test((module === null || module === void 0 ? void 0 : module.request) || "");
116
92
  },
117
- before: [
118
- function(req, res, next) {
119
- if (!enableSSR) {
120
- next();
121
- return;
122
- }
123
- try {
124
- var _req_url, _req_url1, _req_url2;
125
- var SERVER_PREFIX = "/".concat(MODERN_JS_SERVER_DIR);
126
- if (((_req_url = req.url) === null || _req_url === void 0 ? void 0 : _req_url.startsWith(SERVER_PREFIX)) || ((_req_url1 = req.url) === null || _req_url1 === void 0 ? void 0 : _req_url1.includes(".json")) && !((_req_url2 = req.url) === null || _req_url2 === void 0 ? void 0 : _req_url2.includes("hot-update"))) {
127
- var filepath = path.join(process.cwd(), "dist".concat(req.url));
128
- fs.statSync(filepath);
129
- res.setHeader("Access-Control-Allow-Origin", "*");
130
- res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS");
131
- res.setHeader("Access-Control-Allow-Headers", "X-Requested-With, content-type, Authorization");
132
- fs.createReadStream(filepath).pipe(res);
133
- } else {
134
- next();
135
- }
136
- } catch (err) {
137
- if (process.env.FEDERATION_DEBUG) {
138
- console.error(err);
139
- }
140
- next();
141
- }
142
- }
143
- ]
144
- },
145
- bundlerChain: function bundlerChain(chain, param2) {
146
- var isServer = param2.isServer;
147
- if (isDev && !isServer) {
148
- chain.externals({
149
- "@module-federation/node/utils": "NOT_USED_IN_BROWSER"
150
- });
93
+ excludeChunk: function(chunk) {
94
+ return chunk.name === mfConfig.name;
151
95
  }
96
+ };
97
+ (_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(new AsyncBoundaryPlugin(asyncBoundaryPluginOptions));
98
+ }
99
+ config.ignoreWarnings = config.ignoreWarnings || [];
100
+ config.ignoreWarnings.push(function(warning) {
101
+ if (warning.message.includes("external script")) {
102
+ return true;
152
103
  }
104
+ return false;
105
+ });
106
+ },
107
+ devServer: {
108
+ headers: {
109
+ "Access-Control-Allow-Origin": "*",
110
+ "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
111
+ "Access-Control-Allow-Headers": "X-Requested-With, content-type, Authorization"
153
112
  },
154
- source: {
155
- alias: {
156
- "@modern-js/runtime/mf": require.resolve("@module-federation/modern-js/runtime")
157
- },
158
- define: {
159
- FEDERATION_IPV4: JSON.stringify(ipv4)
113
+ before: [
114
+ function(req, res, next) {
115
+ if (!enableSSR) {
116
+ next();
117
+ return;
118
+ }
119
+ try {
120
+ var _req_url, _req_url1, _req_url2;
121
+ var SERVER_PREFIX = "/".concat(MODERN_JS_SERVER_DIR);
122
+ if (((_req_url = req.url) === null || _req_url === void 0 ? void 0 : _req_url.startsWith(SERVER_PREFIX)) || ((_req_url1 = req.url) === null || _req_url1 === void 0 ? void 0 : _req_url1.includes(".json")) && !((_req_url2 = req.url) === null || _req_url2 === void 0 ? void 0 : _req_url2.includes("hot-update"))) {
123
+ var filepath = path.join(process.cwd(), "dist".concat(req.url));
124
+ fs.statSync(filepath);
125
+ res.setHeader("Access-Control-Allow-Origin", "*");
126
+ res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS");
127
+ res.setHeader("Access-Control-Allow-Headers", "X-Requested-With, content-type, Authorization");
128
+ fs.createReadStream(filepath).pipe(res);
129
+ } else {
130
+ next();
131
+ }
132
+ } catch (err) {
133
+ if (process.env.FEDERATION_DEBUG) {
134
+ console.error(err);
135
+ }
136
+ next();
137
+ }
160
138
  }
161
- },
162
- dev: {
163
- assetPrefix: (modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_dev = modernjsConfig.dev) === null || _modernjsConfig_dev === void 0 ? void 0 : _modernjsConfig_dev.assetPrefix) ? modernjsConfig.dev.assetPrefix : true
139
+ ]
140
+ },
141
+ bundlerChain: function bundlerChain(chain, param2) {
142
+ var isServer = param2.isServer;
143
+ if (isDev && !isServer) {
144
+ chain.externals({
145
+ "@module-federation/node/utils": "NOT_USED_IN_BROWSER"
146
+ });
164
147
  }
165
148
  }
166
- ];
167
- }
149
+ },
150
+ source: {
151
+ alias: {
152
+ "@modern-js/runtime/mf": require.resolve("@module-federation/modern-js/runtime")
153
+ },
154
+ define: {
155
+ FEDERATION_IPV4: JSON.stringify(ipv4)
156
+ }
157
+ },
158
+ dev: {
159
+ assetPrefix: (modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_dev = modernjsConfig.dev) === null || _modernjsConfig_dev === void 0 ? void 0 : _modernjsConfig_dev.assetPrefix) ? modernjsConfig.dev.assetPrefix : true
160
+ }
161
+ }
162
+ ];
168
163
  });
169
164
  }),
170
165
  modifyEntryImports: function modifyEntryImports(param2) {
@@ -5,8 +5,8 @@ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
5
5
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
6
6
  import { encodeName } from "@module-federation/sdk";
7
7
  import path from "path";
8
+ import os from "os";
8
9
  import { bundle } from "@modern-js/node-bundle-require";
9
- import dns from "dns";
10
10
  import { LOCALHOST } from "../constant";
11
11
  var defaultPath = path.resolve(process.cwd(), "module-federation.config.ts");
12
12
  var getMFConfig = function() {
@@ -64,47 +64,39 @@ var replaceRemoteUrl = function() {
64
64
  var _ref = _async_to_generator(function(mfConfig) {
65
65
  var ipv4, handleRemoteObject;
66
66
  return _ts_generator(this, function(_state) {
67
- switch (_state.label) {
68
- case 0:
69
- if (!mfConfig.remotes) {
70
- return [
71
- 2
72
- ];
67
+ if (!mfConfig.remotes) {
68
+ return [
69
+ 2
70
+ ];
71
+ }
72
+ ipv4 = getIPV4();
73
+ handleRemoteObject = function(remoteObject) {
74
+ Object.keys(remoteObject).forEach(function(remoteKey) {
75
+ var remote = remoteObject[remoteKey];
76
+ if (Array.isArray(remote)) {
77
+ return;
73
78
  }
74
- return [
75
- 4,
76
- lookupIpv4()
77
- ];
78
- case 1:
79
- ipv4 = _state.sent();
80
- handleRemoteObject = function(remoteObject) {
81
- Object.keys(remoteObject).forEach(function(remoteKey) {
82
- var remote = remoteObject[remoteKey];
83
- if (Array.isArray(remote)) {
84
- return;
85
- }
86
- if (typeof remote === "string" && remote.includes(LOCALHOST)) {
87
- remoteObject[remoteKey] = remote.replace(LOCALHOST, ipv4);
88
- }
89
- if (typeof remote === "object" && !Array.isArray(remote.external) && remote.external.includes(LOCALHOST)) {
90
- remote.external = remote.external.replace(LOCALHOST, ipv4);
91
- }
92
- });
93
- };
94
- if (Array.isArray(mfConfig.remotes)) {
95
- mfConfig.remotes.forEach(function(remoteObject) {
96
- if (typeof remoteObject === "string") {
97
- return;
98
- }
99
- handleRemoteObject(remoteObject);
100
- });
101
- } else if (typeof mfConfig.remotes !== "string") {
102
- handleRemoteObject(mfConfig.remotes);
79
+ if (typeof remote === "string" && remote.includes(LOCALHOST)) {
80
+ remoteObject[remoteKey] = remote.replace(LOCALHOST, ipv4);
103
81
  }
104
- return [
105
- 2
106
- ];
82
+ if (typeof remote === "object" && !Array.isArray(remote.external) && remote.external.includes(LOCALHOST)) {
83
+ remote.external = remote.external.replace(LOCALHOST, ipv4);
84
+ }
85
+ });
86
+ };
87
+ if (Array.isArray(mfConfig.remotes)) {
88
+ mfConfig.remotes.forEach(function(remoteObject) {
89
+ if (typeof remoteObject === "string") {
90
+ return;
91
+ }
92
+ handleRemoteObject(remoteObject);
93
+ });
94
+ } else if (typeof mfConfig.remotes !== "string") {
95
+ handleRemoteObject(mfConfig.remotes);
107
96
  }
97
+ return [
98
+ 2
99
+ ];
108
100
  });
109
101
  });
110
102
  return function replaceRemoteUrl2(mfConfig) {
@@ -114,6 +106,33 @@ var replaceRemoteUrl = function() {
114
106
  var patchMFConfig = function(mfConfig, isServer) {
115
107
  var isDev = process.env.NODE_ENV === "development";
116
108
  var runtimePlugins = _to_consumable_array(mfConfig.runtimePlugins || []);
109
+ var ModernJSRuntime = "@modern-js/runtime/mf";
110
+ if (mfConfig.dts !== false) {
111
+ var _mfConfig_dts, _mfConfig_dts1;
112
+ if (typeof mfConfig.dts === "boolean" || mfConfig.dts === void 0) {
113
+ mfConfig.dts = {
114
+ consumeTypes: {
115
+ runtimePkgs: [
116
+ ModernJSRuntime
117
+ ]
118
+ }
119
+ };
120
+ } else if (((_mfConfig_dts = mfConfig.dts) === null || _mfConfig_dts === void 0 ? void 0 : _mfConfig_dts.consumeTypes) || ((_mfConfig_dts1 = mfConfig.dts) === null || _mfConfig_dts1 === void 0 ? void 0 : _mfConfig_dts1.consumeTypes) === void 0) {
121
+ var _mfConfig_dts2;
122
+ if (typeof mfConfig.dts.consumeTypes === "boolean" || ((_mfConfig_dts2 = mfConfig.dts) === null || _mfConfig_dts2 === void 0 ? void 0 : _mfConfig_dts2.consumeTypes) === void 0) {
123
+ mfConfig.dts.consumeTypes = {
124
+ runtimePkgs: [
125
+ ModernJSRuntime
126
+ ]
127
+ };
128
+ } else {
129
+ mfConfig.dts.consumeTypes.runtimePkgs = mfConfig.dts.consumeTypes.runtimePkgs || [];
130
+ if (!mfConfig.dts.consumeTypes.runtimePkgs.includes(ModernJSRuntime)) {
131
+ mfConfig.dts.consumeTypes.runtimePkgs.push(ModernJSRuntime);
132
+ }
133
+ }
134
+ }
135
+ }
117
136
  injectRuntimePlugins(path.resolve(__dirname, "./mfRuntimePlugins/shared-strategy.js"), runtimePlugins);
118
137
  if (isDev) {
119
138
  injectRuntimePlugins(path.resolve(__dirname, "./mfRuntimePlugins/resolve-entry-ipv4.js"), runtimePlugins);
@@ -191,47 +210,31 @@ function patchWebpackConfig(options) {
191
210
  });
192
211
  }
193
212
  }
194
- var lookupIpv4 = function() {
195
- var _ref = _async_to_generator(function() {
196
- var res, err;
197
- return _ts_generator(this, function(_state) {
198
- switch (_state.label) {
199
- case 0:
200
- _state.trys.push([
201
- 0,
202
- 2,
203
- ,
204
- 3
205
- ]);
206
- return [
207
- 4,
208
- dns.promises.lookup(LOCALHOST, {
209
- family: 4
210
- })
211
- ];
212
- case 1:
213
- res = _state.sent();
214
- return [
215
- 2,
216
- res.address
217
- ];
218
- case 2:
219
- err = _state.sent();
220
- return [
221
- 2,
222
- "127.0.0.1"
223
- ];
224
- case 3:
225
- return [
226
- 2
227
- ];
228
- }
213
+ var localIpv4 = "127.0.0.1";
214
+ var getIpv4Interfaces = function() {
215
+ try {
216
+ var interfaces = os.networkInterfaces();
217
+ var ipv4Interfaces = [];
218
+ Object.values(interfaces).forEach(function(detail) {
219
+ detail === null || detail === void 0 ? void 0 : detail.forEach(function(detail2) {
220
+ var familyV4Value = typeof detail2.family === "string" ? "IPv4" : 4;
221
+ if (detail2.family === familyV4Value && detail2.address !== localIpv4) {
222
+ ipv4Interfaces.push(detail2);
223
+ }
224
+ });
229
225
  });
230
- });
231
- return function lookupIpv42() {
232
- return _ref.apply(this, arguments);
226
+ return ipv4Interfaces;
227
+ } catch (_err) {
228
+ return [];
229
+ }
230
+ };
231
+ var getIPV4 = function() {
232
+ var ipv4Interfaces = getIpv4Interfaces();
233
+ var ipv4Interface = ipv4Interfaces[0] || {
234
+ address: localIpv4
233
235
  };
234
- }();
236
+ return ipv4Interface.address;
237
+ };
235
238
  var SPLIT_CHUNK_MAP = {
236
239
  REACT: "lib-react",
237
240
  ROUTER: "lib-router",
@@ -291,9 +294,9 @@ function autoDeleteSplitChunkCacheGroups(mfConfig, bundlerConfig) {
291
294
  }
292
295
  }
293
296
  export {
297
+ getIPV4,
294
298
  getMFConfig,
295
299
  getTargetEnvConfig,
296
- lookupIpv4,
297
300
  patchMFConfig,
298
301
  patchWebpackConfig
299
302
  };
@@ -80,6 +80,13 @@ describe("getTargetEnvConfig", /* @__PURE__ */ _async_to_generator(function() {
80
80
  eager: true,
81
81
  singleton: true
82
82
  }
83
+ },
84
+ dts: {
85
+ consumeTypes: {
86
+ runtimePkgs: [
87
+ "@modern-js/runtime/mf"
88
+ ]
89
+ }
83
90
  }
84
91
  });
85
92
  return [
@@ -10,7 +10,7 @@ function _templateObject() {
10
10
  return data;
11
11
  }
12
12
  import { jsx as _jsx } from "react/jsx-runtime";
13
- function LiveReload() {
13
+ function SSRLiveReload() {
14
14
  if (process.env.NODE_ENV !== "development") {
15
15
  return null;
16
16
  }
@@ -22,5 +22,5 @@ function LiveReload() {
22
22
  });
23
23
  }
24
24
  export {
25
- LiveReload
25
+ SSRLiveReload
26
26
  };
@@ -1,9 +1,11 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
1
2
  import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
3
  import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
3
4
  import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
5
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
6
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
5
7
  import React from "react";
6
- import { loadRemote, getInstance } from "@module-federation/enhanced/runtime";
8
+ import { getInstance } from "@module-federation/enhanced/runtime";
7
9
  import { ErrorBoundary } from "react-error-boundary";
8
10
  function getLoadedRemoteInfos(instance, id) {
9
11
  var _ref = instance.remoteHandler.idToRemoteMap[id] || {}, name = _ref.name, expose = _ref.expose;
@@ -54,7 +56,7 @@ function getTargetModuleInfo(id) {
54
56
  remoteEntry
55
57
  };
56
58
  }
57
- function collectAssets(options) {
59
+ function collectSSRAssets(options) {
58
60
  var _ref = typeof options === "string" ? {
59
61
  id: options
60
62
  } : 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;
@@ -94,52 +96,94 @@ function collectAssets(options) {
94
96
  }
95
97
  return _to_consumable_array(scripts).concat(_to_consumable_array(links));
96
98
  }
97
- function MFReactComponent(props) {
98
- var _props_loading = props.loading, loading = _props_loading === void 0 ? "loading..." : _props_loading, id = props.id, _props_remoteProps = props.remoteProps, remoteProps = _props_remoteProps === void 0 ? {} : _props_remoteProps, fallback = props.fallback;
99
- var Component = /* @__PURE__ */ React.lazy(function() {
100
- return loadRemote(id).then(function(mod) {
101
- var assets = collectAssets(props);
102
- if (!mod) {
103
- throw new Error("load remote failed");
104
- }
105
- var Com = typeof mod === "object" ? "default" in mod ? mod.default : mod : mod;
106
- return {
107
- default: function() {
108
- return /* @__PURE__ */ _jsxs(_Fragment, {
109
- children: [
110
- assets,
111
- /* @__PURE__ */ _jsx(Com, _object_spread({}, remoteProps))
112
- ]
113
- });
114
- }
115
- };
116
- }).catch(function(err) {
117
- if (!fallback) {
118
- throw err;
119
- }
120
- var FallbackFunctionComponent = fallback;
121
- var FallbackNode = /* @__PURE__ */ _jsx(FallbackFunctionComponent, {
122
- error: err,
123
- resetErrorBoundary: function() {
124
- console.log('SSR mode not support "resetErrorBoundary" !');
99
+ function createRemoteSSRComponent(info) {
100
+ return function(props) {
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
+ injectLink: info.injectLink,
126
+ injectScript: info.injectScript
127
+ });
128
+ Com = m[exportName];
129
+ if (exportName in m && typeof Com === "function") {
130
+ return [
131
+ 2,
132
+ {
133
+ default: function() {
134
+ return /* @__PURE__ */ _jsxs(_Fragment, {
135
+ children: [
136
+ assets,
137
+ /* @__PURE__ */ _jsx(Com, _object_spread({}, props))
138
+ ]
139
+ });
140
+ }
141
+ }
142
+ ];
143
+ } else {
144
+ throw Error("Make sure that ".concat(moduleId, " has the correct export when export is ").concat(String(exportName)));
145
+ }
146
+ return [
147
+ 3,
148
+ 3
149
+ ];
150
+ case 2:
151
+ err = _state.sent();
152
+ if (!info.fallback) {
153
+ throw err;
154
+ }
155
+ FallbackFunctionComponent = info.fallback;
156
+ FallbackNode = /* @__PURE__ */ _jsx(FallbackFunctionComponent, {
157
+ error: err,
158
+ resetErrorBoundary: function() {
159
+ console.log('SSR mode not support "resetErrorBoundary" !');
160
+ }
161
+ });
162
+ return [
163
+ 2,
164
+ {
165
+ default: function() {
166
+ return FallbackNode;
167
+ }
168
+ }
169
+ ];
170
+ case 3:
171
+ return [
172
+ 2
173
+ ];
125
174
  }
126
175
  });
127
- return {
128
- default: function() {
129
- return FallbackNode;
130
- }
131
- };
176
+ }));
177
+ return /* @__PURE__ */ _jsx(ErrorBoundary, {
178
+ FallbackComponent: info.fallback,
179
+ children: /* @__PURE__ */ _jsx(React.Suspense, {
180
+ fallback: info.loading,
181
+ children: /* @__PURE__ */ _jsx(LazyComponent, {})
182
+ })
132
183
  });
133
- });
134
- return /* @__PURE__ */ _jsx(ErrorBoundary, {
135
- FallbackComponent: fallback,
136
- children: /* @__PURE__ */ _jsx(React.Suspense, {
137
- fallback: loading,
138
- children: /* @__PURE__ */ _jsx(Component, {})
139
- })
140
- });
184
+ };
141
185
  }
142
186
  export {
143
- MFReactComponent,
144
- collectAssets
187
+ collectSSRAssets,
188
+ createRemoteSSRComponent
145
189
  };