@module-federation/modern-js 0.0.0-next-20241016071233 → 0.0.0-next-20241016082124

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 (103) hide show
  1. package/dist/cjs/cli/dataLoader/ast/constant.js +49 -0
  2. package/dist/cjs/cli/dataLoader/ast/generateRoutes.js +143 -0
  3. package/dist/cjs/cli/{constant.js → dataLoader/ast/index.js} +6 -6
  4. package/dist/cjs/cli/dataLoader/clearMFCache.js +57 -0
  5. package/dist/cjs/cli/dataLoader/generateRouteFile.js +148 -0
  6. package/dist/cjs/cli/dataLoader/patchMFConfig.js +160 -0
  7. package/dist/cjs/cli/dataLoader/plugin.js +219 -0
  8. package/dist/cjs/cli/index.js +5 -1
  9. package/dist/cjs/cli/mfRuntimePlugins/{inject-node-fetch.js → node-fetch.js} +6 -6
  10. package/dist/cjs/cli/mfRuntimePlugins/node.js +41 -0
  11. package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +40 -38
  12. package/dist/cjs/cli/server/dataLoaderPlugin.js +81 -0
  13. package/dist/cjs/cli/server/fetchRemoteFullRoutesMap.js +72 -0
  14. package/dist/cjs/cli/ssrPlugin.js +5 -2
  15. package/dist/cjs/cli/utils.js +11 -6
  16. package/dist/cjs/cli/utils.spec.js +5 -6
  17. package/dist/cjs/constant.js +35 -2
  18. package/dist/cjs/interfaces/route.js +16 -0
  19. package/dist/cjs/runtime/dataLoader/plugin-inject-assets.js +92 -0
  20. package/dist/cjs/runtime/dataLoader/plugin.js +75 -0
  21. package/dist/cjs/runtime/dataLoader/utils.js +115 -0
  22. package/dist/cjs/runtime/index.js +4 -1
  23. package/dist/cjs/{cli/mfRuntimePlugins/shared-strategy.js → runtime/withMFRouteId.js} +11 -23
  24. package/dist/cjs/ssr-runtime/plugin.js +7 -8
  25. package/dist/cjs/types/modern.js +16 -0
  26. package/dist/esm/cli/dataLoader/ast/constant.js +18 -0
  27. package/dist/esm/cli/dataLoader/ast/generateRoutes.js +110 -0
  28. package/dist/esm/cli/dataLoader/ast/index.js +4 -0
  29. package/dist/esm/cli/dataLoader/clearMFCache.js +34 -0
  30. package/dist/esm/cli/dataLoader/generateRouteFile.js +308 -0
  31. package/dist/esm/cli/dataLoader/patchMFConfig.js +124 -0
  32. package/dist/esm/cli/dataLoader/plugin.js +341 -0
  33. package/dist/esm/cli/index.js +4 -1
  34. package/dist/esm/cli/mfRuntimePlugins/{inject-node-fetch.js → node-fetch.js} +3 -3
  35. package/dist/esm/cli/mfRuntimePlugins/node.js +13 -0
  36. package/dist/esm/cli/server/dataLoaderPlugin.js +104 -0
  37. package/dist/esm/cli/server/fetchRemoteFullRoutesMap.js +93 -0
  38. package/dist/esm/cli/ssrPlugin.js +6 -3
  39. package/dist/esm/cli/utils.js +9 -5
  40. package/dist/esm/cli/utils.spec.js +5 -6
  41. package/dist/esm/constant.js +23 -1
  42. package/dist/esm/interfaces/route.js +0 -0
  43. package/dist/esm/runtime/dataLoader/plugin-inject-assets.js +71 -0
  44. package/dist/esm/runtime/dataLoader/plugin.js +97 -0
  45. package/dist/esm/runtime/dataLoader/utils.js +86 -0
  46. package/dist/esm/runtime/index.js +3 -1
  47. package/dist/esm/runtime/withMFRouteId.js +7 -0
  48. package/dist/esm/ssr-runtime/plugin.js +8 -11
  49. package/dist/esm/types/modern.js +0 -0
  50. package/dist/esm-node/cli/dataLoader/ast/constant.js +18 -0
  51. package/dist/esm-node/cli/dataLoader/ast/generateRoutes.js +109 -0
  52. package/dist/esm-node/cli/dataLoader/ast/index.js +4 -0
  53. package/dist/esm-node/cli/dataLoader/clearMFCache.js +33 -0
  54. package/dist/esm-node/cli/dataLoader/generateRouteFile.js +113 -0
  55. package/dist/esm-node/cli/dataLoader/patchMFConfig.js +126 -0
  56. package/dist/esm-node/cli/dataLoader/plugin.js +184 -0
  57. package/dist/esm-node/cli/index.js +5 -1
  58. package/dist/esm-node/cli/mfRuntimePlugins/{inject-node-fetch.js → node-fetch.js} +3 -3
  59. package/dist/esm-node/cli/mfRuntimePlugins/node.js +11 -0
  60. package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +40 -38
  61. package/dist/esm-node/cli/server/dataLoaderPlugin.js +61 -0
  62. package/dist/esm-node/cli/server/fetchRemoteFullRoutesMap.js +47 -0
  63. package/dist/esm-node/cli/ssrPlugin.js +5 -2
  64. package/dist/esm-node/cli/utils.js +9 -5
  65. package/dist/esm-node/cli/utils.spec.js +5 -6
  66. package/dist/esm-node/constant.js +23 -1
  67. package/dist/esm-node/interfaces/route.js +0 -0
  68. package/dist/esm-node/runtime/dataLoader/plugin-inject-assets.js +68 -0
  69. package/dist/esm-node/runtime/dataLoader/plugin.js +51 -0
  70. package/dist/esm-node/runtime/dataLoader/utils.js +86 -0
  71. package/dist/esm-node/runtime/index.js +3 -1
  72. package/dist/esm-node/runtime/withMFRouteId.js +7 -0
  73. package/dist/esm-node/ssr-runtime/plugin.js +7 -8
  74. package/dist/esm-node/types/modern.js +0 -0
  75. package/dist/types/cli/dataLoader/ast/constant.d.ts +8 -0
  76. package/dist/types/cli/dataLoader/ast/generateRoutes.d.ts +5 -0
  77. package/dist/types/cli/dataLoader/ast/index.d.ts +1 -0
  78. package/dist/types/cli/dataLoader/clearMFCache.d.ts +1 -0
  79. package/dist/types/cli/dataLoader/generateRouteFile.d.ts +10 -0
  80. package/dist/types/cli/dataLoader/patchMFConfig.d.ts +10 -0
  81. package/dist/types/cli/dataLoader/plugin.d.ts +6 -0
  82. package/dist/types/cli/mfRuntimePlugins/node.d.ts +3 -0
  83. package/dist/types/cli/server/dataLoaderPlugin.d.ts +10 -0
  84. package/dist/types/cli/server/fetchRemoteFullRoutesMap.d.ts +11 -0
  85. package/dist/types/cli/utils.d.ts +2 -0
  86. package/dist/types/constant.d.ts +11 -0
  87. package/dist/types/interfaces/route.d.ts +12 -0
  88. package/dist/types/runtime/dataLoader/plugin-inject-assets.d.ts +4 -0
  89. package/dist/types/runtime/dataLoader/plugin.d.ts +5 -0
  90. package/dist/types/runtime/dataLoader/utils.d.ts +22 -0
  91. package/dist/types/runtime/index.d.ts +1 -0
  92. package/dist/types/runtime/withMFRouteId.d.ts +1 -0
  93. package/dist/types/ssr-runtime/plugin.d.ts +3 -1
  94. package/dist/types/types/index.d.ts +8 -0
  95. package/dist/types/types/modern.d.ts +12 -0
  96. package/package.json +42 -5
  97. package/dist/esm/cli/constant.js +0 -4
  98. package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +0 -25
  99. package/dist/esm-node/cli/constant.js +0 -4
  100. package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +0 -23
  101. package/dist/types/cli/constant.d.ts +0 -1
  102. package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +0 -3
  103. /package/dist/types/cli/mfRuntimePlugins/{inject-node-fetch.d.ts → node-fetch.d.ts} +0 -0
@@ -0,0 +1,341 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
3
+ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
4
+ import { _ as _type_of } from "@swc/helpers/_/_type_of";
5
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
6
+ import { fs } from "@modern-js/utils";
7
+ import { transformName2Prefix } from "../../runtime/dataLoader/utils";
8
+ import { DEFAULT_ENTRY, ROUTE_ID } from "../../constant";
9
+ import { META_NAME } from "../../constant";
10
+ import { generateRoutes } from "./ast";
11
+ import { generateRouteFile } from "./generateRouteFile";
12
+ import { patchMFConfig } from "./patchMFConfig";
13
+ import { clearMFCache } from "./clearMFCache";
14
+ import path from "path";
15
+ function _transformRuntimeOptions(buildOptions) {
16
+ var remotes = buildOptions.remotes || {};
17
+ var runtimeRemotes = Object.entries(remotes).map(function(remote) {
18
+ var _remote = _sliced_to_array(remote, 2), alias = _remote[0], nameAndEntry = _remote[1];
19
+ var _nameAndEntry_split = _sliced_to_array(nameAndEntry.split("@"), 2), name = _nameAndEntry_split[0], entry = _nameAndEntry_split[1];
20
+ return {
21
+ name,
22
+ entry,
23
+ alias
24
+ };
25
+ });
26
+ return {
27
+ name: buildOptions.name,
28
+ remotes: runtimeRemotes
29
+ };
30
+ }
31
+ var moduleFederationDataLoaderPlugin = function(enable, internalOptions, userConfig) {
32
+ return {
33
+ name: "@modern-js/plugin-module-federation-data-loader",
34
+ pre: [
35
+ "@modern-js/plugin-module-federation-config"
36
+ ],
37
+ post: [
38
+ "@modern-js/plugin-router",
39
+ "@modern-js/plugin-module-federation"
40
+ ],
41
+ setup: function() {
42
+ var _ref = _async_to_generator(function(param) {
43
+ var useAppContext, _userConfig_serverPlugin, serverPlugin, transformRuntimeOptions, appContext, name, _internalOptions_metaName, metaName, internalDirectory, transformRuntimeFn, entries, remotePathMap, ssrByRouteIdsMap, csrConfig, _ref2, scanEntries, scanRemotePathMap, dataLoaderRemotes, serverPluginOptions, hasDataLoaderRemotes;
44
+ return _ts_generator(this, function(_state) {
45
+ switch (_state.label) {
46
+ case 0:
47
+ useAppContext = param.useAppContext;
48
+ if (!enable) {
49
+ return [
50
+ 2
51
+ ];
52
+ }
53
+ _userConfig_serverPlugin = userConfig.serverPlugin, serverPlugin = _userConfig_serverPlugin === void 0 ? "@module-federation/modern-js/data-loader-server" : _userConfig_serverPlugin, transformRuntimeOptions = userConfig.transformRuntimeOptions;
54
+ appContext = useAppContext();
55
+ name = internalOptions.csrConfig.name;
56
+ _internalOptions_metaName = internalOptions.metaName, metaName = _internalOptions_metaName === void 0 ? META_NAME : _internalOptions_metaName;
57
+ internalDirectory = appContext.internalDirectory.replace(META_NAME, metaName || META_NAME);
58
+ transformRuntimeFn = transformRuntimeOptions || _transformRuntimeOptions;
59
+ entries = /* @__PURE__ */ new Set();
60
+ remotePathMap = {};
61
+ ssrByRouteIdsMap = {};
62
+ csrConfig = internalOptions.csrConfig;
63
+ if (!(_type_of(csrConfig.remotes) === "object" && Object.keys(csrConfig.remotes).length))
64
+ return [
65
+ 3,
66
+ 2
67
+ ];
68
+ return [
69
+ 4,
70
+ generateRouteFile({
71
+ appDirectory: appContext.appDirectory
72
+ })
73
+ ];
74
+ case 1:
75
+ _ref2 = _state.sent(), scanEntries = _ref2.entries, scanRemotePathMap = _ref2.remotePathMap;
76
+ scanEntries.forEach(function(e) {
77
+ return entries.add(e);
78
+ });
79
+ remotePathMap = scanRemotePathMap;
80
+ return [
81
+ 3,
82
+ 3
83
+ ];
84
+ case 2:
85
+ entries.add(DEFAULT_ENTRY);
86
+ _state.label = 3;
87
+ case 3:
88
+ dataLoaderRemotes = _to_consumable_array(new Set(Object.values(remotePathMap).map(function(i) {
89
+ return i.name;
90
+ })));
91
+ serverPluginOptions = {
92
+ runtimeOptions: transformRuntimeFn(internalOptions.csrConfig),
93
+ dataLoaderRemotes,
94
+ ssrByRouteIdsMap
95
+ };
96
+ hasDataLoaderRemotes = function() {
97
+ return Boolean(serverPluginOptions.dataLoaderRemotes.length);
98
+ };
99
+ return [
100
+ 2,
101
+ {
102
+ _internalRuntimePlugins: function(param2) {
103
+ var entrypoint = param2.entrypoint, plugins = param2.plugins;
104
+ if (!hasDataLoaderRemotes()) {
105
+ return {
106
+ entrypoint,
107
+ plugins
108
+ };
109
+ }
110
+ plugins.push({
111
+ name: "ssrDataLoader",
112
+ path: "@module-federation/modern-js/data-loader",
113
+ config: {
114
+ metaName,
115
+ dataLoaderRemotes
116
+ }
117
+ });
118
+ plugins.push({
119
+ name: "ssrDataLoaderInjectAssets",
120
+ path: "@module-federation/modern-js/data-loader-inject-assets",
121
+ config: {
122
+ metaName,
123
+ dataLoaderRemotes
124
+ }
125
+ });
126
+ return {
127
+ entrypoint,
128
+ plugins
129
+ };
130
+ },
131
+ _internalServerPlugins: function _internalServerPlugins(param2) {
132
+ var plugins = param2.plugins;
133
+ if (!hasDataLoaderRemotes()) {
134
+ return {
135
+ plugins
136
+ };
137
+ }
138
+ plugins.push({
139
+ name: serverPlugin,
140
+ options: serverPluginOptions
141
+ });
142
+ return {
143
+ plugins
144
+ };
145
+ },
146
+ modifyFileSystemRoutes: function modifyFileSystemRoutes(options) {
147
+ return _async_to_generator(function() {
148
+ var routes, entrypoint, entryName, prefix, modifyRouteIds;
149
+ return _ts_generator(this, function(_state2) {
150
+ switch (_state2.label) {
151
+ case 0:
152
+ routes = options.routes, entrypoint = options.entrypoint;
153
+ entryName = entrypoint.entryName;
154
+ prefix = transformName2Prefix(internalOptions.csrConfig.name);
155
+ modifyRouteIds = function() {
156
+ var _ref3 = _async_to_generator(function(route, pathName) {
157
+ var currentPathName, originalId, remoteInfo, _$path, name2, remoteIdPrefix, isLayout, newId, content, _tmp;
158
+ return _ts_generator(this, function(_state3) {
159
+ switch (_state3.label) {
160
+ case 0:
161
+ currentPathName = route.path ? path.join("/", pathName, route.path) : pathName;
162
+ if (!route.id)
163
+ return [
164
+ 3,
165
+ 6
166
+ ];
167
+ originalId = route.id;
168
+ route.originalId = originalId;
169
+ remoteInfo = remotePathMap[currentPathName];
170
+ if (!remoteInfo)
171
+ return [
172
+ 3,
173
+ 2
174
+ ];
175
+ _$path = remoteInfo.path, name2 = remoteInfo.name;
176
+ remoteIdPrefix = transformName2Prefix(name2);
177
+ isLayout = Boolean(route.children);
178
+ newId = "".concat(remoteIdPrefix).concat(isLayout ? "layout" : "page");
179
+ ssrByRouteIdsMap[originalId] = newId;
180
+ content = fs.readFileSync(_$path, "utf-8");
181
+ return [
182
+ 4,
183
+ fs.writeFile(_$path, content.replace(ROUTE_ID, originalId))
184
+ ];
185
+ case 1:
186
+ _state3.sent();
187
+ route.id = newId;
188
+ return [
189
+ 3,
190
+ 3
191
+ ];
192
+ case 2:
193
+ route.id = "".concat(prefix).concat(route.id);
194
+ _state3.label = 3;
195
+ case 3:
196
+ _tmp = route.children;
197
+ if (!_tmp)
198
+ return [
199
+ 3,
200
+ 5
201
+ ];
202
+ return [
203
+ 4,
204
+ Promise.all(route.children.map(function() {
205
+ var _ref4 = _async_to_generator(function(r) {
206
+ return _ts_generator(this, function(_state4) {
207
+ switch (_state4.label) {
208
+ case 0:
209
+ return [
210
+ 4,
211
+ modifyRouteIds(r, currentPathName)
212
+ ];
213
+ case 1:
214
+ return [
215
+ 2,
216
+ _state4.sent()
217
+ ];
218
+ }
219
+ });
220
+ });
221
+ return function(r) {
222
+ return _ref4.apply(this, arguments);
223
+ };
224
+ }()))
225
+ ];
226
+ case 4:
227
+ _tmp = _state3.sent();
228
+ _state3.label = 5;
229
+ case 5:
230
+ _tmp;
231
+ _state3.label = 6;
232
+ case 6:
233
+ return [
234
+ 2
235
+ ];
236
+ }
237
+ });
238
+ });
239
+ return function modifyRouteIds2(route, pathName) {
240
+ return _ref3.apply(this, arguments);
241
+ };
242
+ }();
243
+ return [
244
+ 4,
245
+ Promise.all(routes.map(function() {
246
+ var _ref3 = _async_to_generator(function(route) {
247
+ return _ts_generator(this, function(_state3) {
248
+ switch (_state3.label) {
249
+ case 0:
250
+ return [
251
+ 4,
252
+ modifyRouteIds(route, entryName === DEFAULT_ENTRY ? "" : entryName)
253
+ ];
254
+ case 1:
255
+ _state3.sent();
256
+ return [
257
+ 2
258
+ ];
259
+ }
260
+ });
261
+ });
262
+ return function(route) {
263
+ return _ref3.apply(this, arguments);
264
+ };
265
+ }()))
266
+ ];
267
+ case 1:
268
+ _state2.sent();
269
+ return [
270
+ 2,
271
+ options
272
+ ];
273
+ }
274
+ });
275
+ })();
276
+ },
277
+ config: /* @__PURE__ */ _async_to_generator(function() {
278
+ return _ts_generator(this, function(_state2) {
279
+ return [
280
+ 2,
281
+ {
282
+ tools: {
283
+ // bundlerChain can not keep target order
284
+ rspack: function rspack(_config, param2) {
285
+ var isServer = param2.isServer;
286
+ patchMFConfig({
287
+ mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
288
+ metaName,
289
+ isServer,
290
+ internalDirectory,
291
+ entries
292
+ });
293
+ },
294
+ // bundlerChain can not keep target order
295
+ webpack: function webpack(_config, param2) {
296
+ var isServer = param2.isServer;
297
+ patchMFConfig({
298
+ mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
299
+ metaName,
300
+ isServer,
301
+ internalDirectory,
302
+ entries
303
+ });
304
+ }
305
+ },
306
+ source: {
307
+ define: {
308
+ MODERN_ROUTER_ID_PREFIX: JSON.stringify(transformName2Prefix(name))
309
+ }
310
+ }
311
+ }
312
+ ];
313
+ });
314
+ }),
315
+ afterDev: function afterDev() {
316
+ return _async_to_generator(function() {
317
+ return _ts_generator(this, function(_state2) {
318
+ clearMFCache();
319
+ return [
320
+ 2
321
+ ];
322
+ });
323
+ })();
324
+ }
325
+ }
326
+ ];
327
+ }
328
+ });
329
+ });
330
+ return function(_) {
331
+ return _ref.apply(this, arguments);
332
+ };
333
+ }()
334
+ };
335
+ };
336
+ var plugin_default = moduleFederationDataLoaderPlugin;
337
+ export {
338
+ plugin_default as default,
339
+ generateRoutes,
340
+ moduleFederationDataLoaderPlugin
341
+ };
@@ -1,10 +1,12 @@
1
1
  import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
3
  import { _ as _type_of } from "@swc/helpers/_/_type_of";
3
4
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
5
  import { ModuleFederationPlugin as WebpackModuleFederationPlugin, AsyncBoundaryPlugin } from "@module-federation/enhanced";
5
6
  import { ModuleFederationPlugin as RspackModuleFederationPlugin } from "@module-federation/enhanced/rspack";
6
7
  import { moduleFederationConfigPlugin } from "./configPlugin";
7
8
  import { moduleFederationSSRPlugin } from "./ssrPlugin";
9
+ import { moduleFederationDataLoaderPlugin } from "./dataLoader/plugin";
8
10
  var moduleFederationPlugin = function() {
9
11
  var userConfig = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
10
12
  var internalModernPluginOptions = {
@@ -80,7 +82,8 @@ var moduleFederationPlugin = function() {
80
82
  }(),
81
83
  usePlugins: [
82
84
  moduleFederationConfigPlugin(internalModernPluginOptions),
83
- moduleFederationSSRPlugin(internalModernPluginOptions)
85
+ moduleFederationSSRPlugin(internalModernPluginOptions),
86
+ moduleFederationDataLoaderPlugin(Boolean(userConfig.dataLoader), internalModernPluginOptions, _object_spread({}, typeof userConfig.dataLoader === "boolean" ? {} : userConfig.dataLoader))
84
87
  ]
85
88
  };
86
89
  };
@@ -1,7 +1,7 @@
1
1
  import nodeFetch from "node-fetch";
2
2
  var injectNodeFetchPlugin = function() {
3
3
  return {
4
- name: "inject-node-fetch-plugin",
4
+ name: "node-fetch-plugin",
5
5
  beforeInit: function beforeInit(args) {
6
6
  if (!globalThis.fetch) {
7
7
  globalThis.fetch = nodeFetch;
@@ -10,7 +10,7 @@ var injectNodeFetchPlugin = function() {
10
10
  }
11
11
  };
12
12
  };
13
- var inject_node_fetch_default = injectNodeFetchPlugin;
13
+ var node_fetch_default = injectNodeFetchPlugin;
14
14
  export {
15
- inject_node_fetch_default as default
15
+ node_fetch_default as default
16
16
  };
@@ -0,0 +1,13 @@
1
+ import nodeRuntimePlugin from "@module-federation/node/runtimePlugin";
2
+ var isContainer = typeof __filename === "string" ? __filename.includes("remote") ? true : false : false;
3
+ var pluginName = "node-plugin-".concat(isContainer);
4
+ var nodePlugin = function() {
5
+ return {
6
+ name: pluginName,
7
+ beforeInit: nodeRuntimePlugin().beforeInit
8
+ };
9
+ };
10
+ var node_default = nodePlugin;
11
+ export {
12
+ node_default as default
13
+ };
@@ -0,0 +1,104 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
3
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
+ import { init } from "@module-federation/enhanced/runtime";
5
+ import { isBrowserEnv } from "@module-federation/sdk";
6
+ import { fetchRemoteFullRoutesMap, getSSRByRouteIds } from "./fetchRemoteFullRoutesMap";
7
+ import { injectRemoteRoutes } from "../../runtime/dataLoader/utils";
8
+ import { MF_ROUTES, DEFAULT_ENTRY, MODERN_JS_ROUTE_SERVER_LOADER } from "../../constant";
9
+ function dataLoaderPlugin_default(param) {
10
+ var runtimeOptions = param.runtimeOptions, dataLoaderRemotes = param.dataLoaderRemotes, ssrByRouteIdsMap = param.ssrByRouteIdsMap;
11
+ return {
12
+ name: "MFDataLoaderServerPlugin",
13
+ pre: [
14
+ "@modern-js/plugin-inject-resource"
15
+ ],
16
+ setup: function setup(api) {
17
+ var remotes = runtimeOptions.remotes, name = runtimeOptions.name;
18
+ var isHandled = false;
19
+ return {
20
+ prepare: function prepare() {
21
+ var middlewares = api.useAppContext().middlewares;
22
+ middlewares.push({
23
+ name: "MFPatchRouteMiddleWare",
24
+ handler: function() {
25
+ var _ref = _async_to_generator(function(c, next) {
26
+ var serverManifest, loaderBundles, nestedRoutesJson, _api_useConfigContext_server, instance, remoteFullRoutesMap, originalSSRByRouteIds, remoteSSRByRouteIds, ssrByRouteIds;
27
+ return _ts_generator(this, function(_state) {
28
+ switch (_state.label) {
29
+ case 0:
30
+ serverManifest = c.get("serverManifest");
31
+ loaderBundles = serverManifest.loaderBundles, nestedRoutesJson = serverManifest.nestedRoutesJson;
32
+ if (!(isHandled && !globalThis.FORCE_MF_REFRESH))
33
+ return [
34
+ 3,
35
+ 2
36
+ ];
37
+ return [
38
+ 4,
39
+ next()
40
+ ];
41
+ case 1:
42
+ _state.sent();
43
+ return [
44
+ 3,
45
+ 5
46
+ ];
47
+ case 2:
48
+ instance = init({
49
+ name,
50
+ remotes
51
+ });
52
+ return [
53
+ 4,
54
+ fetchRemoteFullRoutesMap({
55
+ instance,
56
+ remotePath: isBrowserEnv() ? "".concat(DEFAULT_ENTRY, "/").concat(MF_ROUTES) : "".concat(DEFAULT_ENTRY, "/").concat(MODERN_JS_ROUTE_SERVER_LOADER),
57
+ dataLoaderRemotes
58
+ })
59
+ ];
60
+ case 3:
61
+ remoteFullRoutesMap = _state.sent();
62
+ originalSSRByRouteIds = (_api_useConfigContext_server = api.useConfigContext().server) === null || _api_useConfigContext_server === void 0 ? void 0 : _api_useConfigContext_server.ssrByRouteIds;
63
+ if (originalSSRByRouteIds && dataLoaderRemotes.length) {
64
+ remoteSSRByRouteIds = getSSRByRouteIds(dataLoaderRemotes, remoteFullRoutesMap) || [];
65
+ ssrByRouteIds = new Set(_to_consumable_array(originalSSRByRouteIds).concat(_to_consumable_array(remoteSSRByRouteIds.map(function(id) {
66
+ return ssrByRouteIdsMap[id] || id;
67
+ }))));
68
+ api.useConfigContext().server.ssrByRouteIds = Array.from(ssrByRouteIds);
69
+ }
70
+ injectRemoteRoutes(loaderBundles, remoteFullRoutesMap);
71
+ injectRemoteRoutes(nestedRoutesJson, remoteFullRoutesMap);
72
+ isHandled = true;
73
+ globalThis.FORCE_MF_REFRESH = false;
74
+ return [
75
+ 4,
76
+ next()
77
+ ];
78
+ case 4:
79
+ _state.sent();
80
+ _state.label = 5;
81
+ case 5:
82
+ return [
83
+ 2
84
+ ];
85
+ }
86
+ });
87
+ });
88
+ return function(c, next) {
89
+ return _ref.apply(this, arguments);
90
+ };
91
+ }(),
92
+ before: [
93
+ "render"
94
+ ]
95
+ });
96
+ }
97
+ };
98
+ }
99
+ };
100
+ }
101
+ ;
102
+ export {
103
+ dataLoaderPlugin_default as default
104
+ };
@@ -0,0 +1,93 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
3
+ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
4
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
5
+ function fetchRemoteFullRoutesMap(options) {
6
+ return _fetchRemoteFullRoutesMap.apply(this, arguments);
7
+ }
8
+ function _fetchRemoteFullRoutesMap() {
9
+ _fetchRemoteFullRoutesMap = _async_to_generator(function(options) {
10
+ var instance, remotePath, dataLoaderRemotes, remotes, remoteRoutesMap;
11
+ return _ts_generator(this, function(_state) {
12
+ switch (_state.label) {
13
+ case 0:
14
+ instance = options.instance, remotePath = options.remotePath, dataLoaderRemotes = options.dataLoaderRemotes;
15
+ remotes = instance.options.remotes;
16
+ remoteRoutesMap = {};
17
+ if (!remotes.length) {
18
+ return [
19
+ 2,
20
+ remoteRoutesMap
21
+ ];
22
+ }
23
+ return [
24
+ 4,
25
+ Promise.all(remotes.map(function() {
26
+ var _ref = _async_to_generator(function(remote) {
27
+ var remoteId, routes;
28
+ return _ts_generator(this, function(_state2) {
29
+ switch (_state2.label) {
30
+ case 0:
31
+ if (!dataLoaderRemotes.includes(remote.name)) {
32
+ return [
33
+ 2
34
+ ];
35
+ }
36
+ remoteId = "".concat(remote.name, "/").concat(remotePath);
37
+ return [
38
+ 4,
39
+ instance.loadRemote(remoteId)
40
+ ];
41
+ case 1:
42
+ routes = _state2.sent().routes;
43
+ remoteRoutesMap[remote.name] = {
44
+ routes
45
+ };
46
+ return [
47
+ 2
48
+ ];
49
+ }
50
+ });
51
+ });
52
+ return function(remote) {
53
+ return _ref.apply(this, arguments);
54
+ };
55
+ }()))
56
+ ];
57
+ case 1:
58
+ _state.sent();
59
+ return [
60
+ 2,
61
+ remoteRoutesMap
62
+ ];
63
+ }
64
+ });
65
+ });
66
+ return _fetchRemoteFullRoutesMap.apply(this, arguments);
67
+ }
68
+ function getSSRByRouteIds(dataLoaderRemotes, remoteRoutesMap) {
69
+ var remoteProviderRouteIds = /* @__PURE__ */ new Set();
70
+ var collectIds = function(route) {
71
+ remoteProviderRouteIds.add(route.id);
72
+ if (route.children) {
73
+ route.children.forEach(function(r) {
74
+ collectIds(r);
75
+ });
76
+ }
77
+ };
78
+ Object.entries(remoteRoutesMap).forEach(function(item) {
79
+ var _item = _sliced_to_array(item, 2), remoteName = _item[0], routesObj = _item[1];
80
+ if (!dataLoaderRemotes.includes(remoteName)) {
81
+ return;
82
+ }
83
+ var routes = routesObj.routes;
84
+ routes.forEach(function(route) {
85
+ collectIds(route);
86
+ });
87
+ });
88
+ return _to_consumable_array(remoteProviderRouteIds);
89
+ }
90
+ export {
91
+ fetchRemoteFullRoutesMap,
92
+ getSSRByRouteIds
93
+ };
@@ -6,7 +6,7 @@ import { ModuleFederationPlugin } from "@module-federation/enhanced/webpack";
6
6
  import { ModuleFederationPlugin as RspackModuleFederationPlugin } from "@module-federation/enhanced/rspack";
7
7
  import UniverseEntryChunkTrackerPlugin from "@module-federation/node/universe-entry-chunk-tracker-plugin";
8
8
  import { updateStatsAndManifest } from "./manifest";
9
- import { isDev } from "./constant";
9
+ import { isDev, META_NAME } from "../constant";
10
10
  function setEnv() {
11
11
  process.env["MF_DISABLE_EMIT_STATS"] = "true";
12
12
  process.env["MF_SSR_PRJ"] = "true";
@@ -20,10 +20,11 @@ var moduleFederationSSRPlugin = function(userConfig) {
20
20
  ],
21
21
  setup: function() {
22
22
  var _ref = _async_to_generator(function(param) {
23
- var useConfigContext, useAppContext, _modernjsConfig_server, modernjsConfig, enableSSR;
23
+ var useConfigContext, useAppContext, _modernjsConfig_server, modernjsConfig, _userConfig_metaName, metaName, enableSSR;
24
24
  return _ts_generator(this, function(_state) {
25
25
  useConfigContext = param.useConfigContext, useAppContext = param.useAppContext;
26
26
  modernjsConfig = useConfigContext();
27
+ _userConfig_metaName = userConfig.metaName, metaName = _userConfig_metaName === void 0 ? META_NAME : _userConfig_metaName;
27
28
  enableSSR = Boolean(modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
28
29
  if (!enableSSR) {
29
30
  return [
@@ -46,7 +47,9 @@ var moduleFederationSSRPlugin = function(userConfig) {
46
47
  plugins.push({
47
48
  name: "mfSSR",
48
49
  path: "@module-federation/modern-js/ssr-runtime",
49
- config: {}
50
+ config: {
51
+ metaName
52
+ }
50
53
  });
51
54
  return {
52
55
  entrypoint,
@@ -80,7 +80,9 @@ var replaceRemoteUrl = function(mfConfig, remoteIpStrategy) {
80
80
  if (typeof remoteObject === "string") {
81
81
  return;
82
82
  }
83
- handleRemoteObject(remoteObject);
83
+ if ("entry" in remoteObject && typeof remoteObject.entry === "string") {
84
+ remoteObject.entry = remoteObject.entry.replace(LOCALHOST, ipv4);
85
+ }
84
86
  });
85
87
  } else if (typeof mfConfig.remotes !== "string") {
86
88
  handleRemoteObject(mfConfig.remotes);
@@ -127,17 +129,18 @@ var patchMFConfig = function(mfConfig, isServer, remoteIpStrategy) {
127
129
  throw new Error("".concat(PLUGIN_IDENTIFIER, " mfConfig.name can not be empty!"));
128
130
  }
129
131
  var runtimePlugins = _to_consumable_array(mfConfig.runtimePlugins || []);
132
+ var _mfConfig_shareStrategy;
133
+ mfConfig.shareStrategy = (_mfConfig_shareStrategy = mfConfig.shareStrategy) !== null && _mfConfig_shareStrategy !== void 0 ? _mfConfig_shareStrategy : "loaded-first";
130
134
  patchDTSConfig(mfConfig, isServer);
131
- injectRuntimePlugins(path.resolve(__dirname, "./mfRuntimePlugins/shared-strategy.js"), runtimePlugins);
132
135
  if (isDev) {
133
136
  injectRuntimePlugins(path.resolve(__dirname, "./mfRuntimePlugins/resolve-entry-ipv4.js"), runtimePlugins);
134
137
  }
135
138
  if (isServer) {
136
- injectRuntimePlugins(require.resolve("@module-federation/node/runtimePlugin"), runtimePlugins);
139
+ injectRuntimePlugins(path.resolve(__dirname, "./mfRuntimePlugins/node.js"), runtimePlugins);
137
140
  if (isDev) {
138
141
  injectRuntimePlugins(require.resolve("@module-federation/node/record-dynamic-remote-entry-hash-plugin"), runtimePlugins);
139
142
  }
140
- injectRuntimePlugins(path.resolve(__dirname, "./mfRuntimePlugins/inject-node-fetch.js"), runtimePlugins);
143
+ injectRuntimePlugins(path.resolve(__dirname, "./mfRuntimePlugins/node-fetch.js"), runtimePlugins);
141
144
  if (!mfConfig.library) {
142
145
  mfConfig.library = {
143
146
  type: "commonjs-module",
@@ -340,5 +343,6 @@ export {
340
343
  getMFConfig,
341
344
  patchBundlerConfig,
342
345
  patchIgnoreWarning,
343
- patchMFConfig
346
+ patchMFConfig,
347
+ replaceRemoteUrl
344
348
  };