@module-federation/modern-js 0.0.0-next-20240918063302 → 0.0.0-next-20240918073359

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 (99) hide show
  1. package/dist/cjs/cli/dataLoader/ast/constant.js +49 -0
  2. package/dist/cjs/cli/dataLoader/ast/generateRoutes.js +141 -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 +147 -0
  6. package/dist/cjs/cli/dataLoader/patchMFConfig.js +137 -0
  7. package/dist/cjs/cli/dataLoader/plugin.js +208 -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 +86 -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 +94 -0
  20. package/dist/cjs/runtime/dataLoader/plugin.js +77 -0
  21. package/dist/cjs/runtime/dataLoader/utils.js +106 -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 +9 -22
  25. package/dist/esm/cli/dataLoader/ast/constant.js +18 -0
  26. package/dist/esm/cli/dataLoader/ast/generateRoutes.js +108 -0
  27. package/dist/esm/cli/dataLoader/ast/index.js +4 -0
  28. package/dist/esm/cli/dataLoader/clearMFCache.js +34 -0
  29. package/dist/esm/cli/dataLoader/generateRouteFile.js +307 -0
  30. package/dist/esm/cli/dataLoader/patchMFConfig.js +103 -0
  31. package/dist/esm/cli/dataLoader/plugin.js +328 -0
  32. package/dist/esm/cli/index.js +6 -2
  33. package/dist/esm/cli/mfRuntimePlugins/{inject-node-fetch.js → node-fetch.js} +3 -3
  34. package/dist/esm/cli/mfRuntimePlugins/node.js +13 -0
  35. package/dist/esm/cli/server/dataLoaderPlugin.js +109 -0
  36. package/dist/esm/cli/server/fetchRemoteFullRoutesMap.js +93 -0
  37. package/dist/esm/cli/ssrPlugin.js +6 -3
  38. package/dist/esm/cli/utils.js +13 -8
  39. package/dist/esm/cli/utils.spec.js +5 -6
  40. package/dist/esm/constant.js +23 -1
  41. package/dist/esm/interfaces/route.js +0 -0
  42. package/dist/esm/runtime/dataLoader/plugin-inject-assets.js +73 -0
  43. package/dist/esm/runtime/dataLoader/plugin.js +99 -0
  44. package/dist/esm/runtime/dataLoader/utils.js +77 -0
  45. package/dist/esm/runtime/index.js +3 -1
  46. package/dist/esm/runtime/withMFRouteId.js +7 -0
  47. package/dist/esm/ssr-runtime/plugin.js +10 -26
  48. package/dist/esm-node/cli/dataLoader/ast/constant.js +18 -0
  49. package/dist/esm-node/cli/dataLoader/ast/generateRoutes.js +107 -0
  50. package/dist/esm-node/cli/dataLoader/ast/index.js +4 -0
  51. package/dist/esm-node/cli/dataLoader/clearMFCache.js +33 -0
  52. package/dist/esm-node/cli/dataLoader/generateRouteFile.js +112 -0
  53. package/dist/esm-node/cli/dataLoader/patchMFConfig.js +103 -0
  54. package/dist/esm-node/cli/dataLoader/plugin.js +173 -0
  55. package/dist/esm-node/cli/index.js +5 -1
  56. package/dist/esm-node/cli/mfRuntimePlugins/{inject-node-fetch.js → node-fetch.js} +3 -3
  57. package/dist/esm-node/cli/mfRuntimePlugins/node.js +11 -0
  58. package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +40 -38
  59. package/dist/esm-node/cli/server/dataLoaderPlugin.js +66 -0
  60. package/dist/esm-node/cli/server/fetchRemoteFullRoutesMap.js +47 -0
  61. package/dist/esm-node/cli/ssrPlugin.js +5 -2
  62. package/dist/esm-node/cli/utils.js +9 -5
  63. package/dist/esm-node/cli/utils.spec.js +5 -6
  64. package/dist/esm-node/constant.js +23 -1
  65. package/dist/esm-node/interfaces/route.js +0 -0
  66. package/dist/esm-node/runtime/dataLoader/plugin-inject-assets.js +70 -0
  67. package/dist/esm-node/runtime/dataLoader/plugin.js +53 -0
  68. package/dist/esm-node/runtime/dataLoader/utils.js +77 -0
  69. package/dist/esm-node/runtime/index.js +3 -1
  70. package/dist/esm-node/runtime/withMFRouteId.js +7 -0
  71. package/dist/esm-node/ssr-runtime/plugin.js +9 -22
  72. package/dist/types/cli/dataLoader/ast/constant.d.ts +8 -0
  73. package/dist/types/cli/dataLoader/ast/generateRoutes.d.ts +5 -0
  74. package/dist/types/cli/dataLoader/ast/index.d.ts +1 -0
  75. package/dist/types/cli/dataLoader/clearMFCache.d.ts +1 -0
  76. package/dist/types/cli/dataLoader/generateRouteFile.d.ts +10 -0
  77. package/dist/types/cli/dataLoader/patchMFConfig.d.ts +10 -0
  78. package/dist/types/cli/dataLoader/plugin.d.ts +6 -0
  79. package/dist/types/cli/mfRuntimePlugins/node.d.ts +3 -0
  80. package/dist/types/cli/server/dataLoaderPlugin.d.ts +10 -0
  81. package/dist/types/cli/server/fetchRemoteFullRoutesMap.d.ts +11 -0
  82. package/dist/types/cli/utils.d.ts +2 -0
  83. package/dist/types/constant.d.ts +11 -0
  84. package/dist/types/interfaces/route.d.ts +12 -0
  85. package/dist/types/runtime/dataLoader/plugin-inject-assets.d.ts +4 -0
  86. package/dist/types/runtime/dataLoader/plugin.d.ts +5 -0
  87. package/dist/types/runtime/dataLoader/utils.d.ts +22 -0
  88. package/dist/types/runtime/index.d.ts +1 -0
  89. package/dist/types/runtime/withMFRouteId.d.ts +1 -0
  90. package/dist/types/ssr-runtime/plugin.d.ts +3 -1
  91. package/dist/types/types/index.d.ts +8 -0
  92. package/package.json +48 -11
  93. package/dist/esm/cli/constant.js +0 -4
  94. package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +0 -25
  95. package/dist/esm-node/cli/constant.js +0 -4
  96. package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +0 -23
  97. package/dist/types/cli/constant.d.ts +0 -1
  98. package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +0 -3
  99. /package/dist/types/cli/mfRuntimePlugins/{inject-node-fetch.d.ts → node-fetch.d.ts} +0 -0
@@ -0,0 +1,328 @@
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;
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
+ return [
97
+ 2,
98
+ {
99
+ _internalRuntimePlugins: function(param2) {
100
+ var entrypoint = param2.entrypoint, plugins = param2.plugins;
101
+ plugins.push({
102
+ name: "ssrDataLoader",
103
+ path: "@module-federation/modern-js/data-loader",
104
+ config: {
105
+ metaName,
106
+ dataLoaderRemotes
107
+ }
108
+ });
109
+ plugins.push({
110
+ name: "ssrDataLoaderInjectAssets",
111
+ path: "@module-federation/modern-js/data-loader-inject-assets",
112
+ config: {
113
+ metaName,
114
+ dataLoaderRemotes
115
+ }
116
+ });
117
+ return {
118
+ entrypoint,
119
+ plugins
120
+ };
121
+ },
122
+ _internalServerPlugins: function _internalServerPlugins(param2) {
123
+ var plugins = param2.plugins;
124
+ plugins.push({
125
+ name: serverPlugin,
126
+ options: serverPluginOptions
127
+ });
128
+ return {
129
+ plugins
130
+ };
131
+ },
132
+ modifyFileSystemRoutes: function modifyFileSystemRoutes(options) {
133
+ return _async_to_generator(function() {
134
+ var routes, entrypoint, entryName, prefix, modifyRouteIds;
135
+ return _ts_generator(this, function(_state2) {
136
+ switch (_state2.label) {
137
+ case 0:
138
+ routes = options.routes, entrypoint = options.entrypoint;
139
+ entryName = entrypoint.entryName;
140
+ prefix = transformName2Prefix(internalOptions.csrConfig.name);
141
+ modifyRouteIds = function() {
142
+ var _ref3 = _async_to_generator(function(route, pathName) {
143
+ var currentPathName, originalId, remoteInfo, _$path, name2, remoteIdPrefix, isLayout, newId, content, _tmp;
144
+ return _ts_generator(this, function(_state3) {
145
+ switch (_state3.label) {
146
+ case 0:
147
+ currentPathName = route.path ? path.join(pathName, route.path) : pathName;
148
+ if (!route.id)
149
+ return [
150
+ 3,
151
+ 6
152
+ ];
153
+ originalId = route.id;
154
+ route.originalId = originalId;
155
+ remoteInfo = remotePathMap[currentPathName];
156
+ if (!remoteInfo)
157
+ return [
158
+ 3,
159
+ 2
160
+ ];
161
+ _$path = remoteInfo.path, name2 = remoteInfo.name;
162
+ remoteIdPrefix = transformName2Prefix(name2);
163
+ isLayout = Boolean(route.children);
164
+ newId = "".concat(remoteIdPrefix).concat(isLayout ? "layout" : "page");
165
+ ssrByRouteIdsMap[originalId] = newId;
166
+ content = fs.readFileSync(_$path, "utf-8");
167
+ return [
168
+ 4,
169
+ fs.writeFile(_$path, content.replace(ROUTE_ID, originalId))
170
+ ];
171
+ case 1:
172
+ _state3.sent();
173
+ route.id = newId;
174
+ return [
175
+ 3,
176
+ 3
177
+ ];
178
+ case 2:
179
+ route.id = "".concat(prefix).concat(route.id);
180
+ _state3.label = 3;
181
+ case 3:
182
+ _tmp = route.children;
183
+ if (!_tmp)
184
+ return [
185
+ 3,
186
+ 5
187
+ ];
188
+ return [
189
+ 4,
190
+ Promise.all(route.children.map(function() {
191
+ var _ref4 = _async_to_generator(function(r) {
192
+ return _ts_generator(this, function(_state4) {
193
+ switch (_state4.label) {
194
+ case 0:
195
+ return [
196
+ 4,
197
+ modifyRouteIds(r, currentPathName)
198
+ ];
199
+ case 1:
200
+ return [
201
+ 2,
202
+ _state4.sent()
203
+ ];
204
+ }
205
+ });
206
+ });
207
+ return function(r) {
208
+ return _ref4.apply(this, arguments);
209
+ };
210
+ }()))
211
+ ];
212
+ case 4:
213
+ _tmp = _state3.sent();
214
+ _state3.label = 5;
215
+ case 5:
216
+ _tmp;
217
+ _state3.label = 6;
218
+ case 6:
219
+ return [
220
+ 2
221
+ ];
222
+ }
223
+ });
224
+ });
225
+ return function modifyRouteIds2(route, pathName) {
226
+ return _ref3.apply(this, arguments);
227
+ };
228
+ }();
229
+ return [
230
+ 4,
231
+ Promise.all(routes.map(function() {
232
+ var _ref3 = _async_to_generator(function(route) {
233
+ return _ts_generator(this, function(_state3) {
234
+ switch (_state3.label) {
235
+ case 0:
236
+ return [
237
+ 4,
238
+ modifyRouteIds(route, entryName)
239
+ ];
240
+ case 1:
241
+ _state3.sent();
242
+ return [
243
+ 2
244
+ ];
245
+ }
246
+ });
247
+ });
248
+ return function(route) {
249
+ return _ref3.apply(this, arguments);
250
+ };
251
+ }()))
252
+ ];
253
+ case 1:
254
+ _state2.sent();
255
+ return [
256
+ 2,
257
+ options
258
+ ];
259
+ }
260
+ });
261
+ })();
262
+ },
263
+ config: /* @__PURE__ */ _async_to_generator(function() {
264
+ return _ts_generator(this, function(_state2) {
265
+ return [
266
+ 2,
267
+ {
268
+ tools: {
269
+ // bundlerChain can not keep target order
270
+ rspack: function rspack(_config, param2) {
271
+ var isServer = param2.isServer;
272
+ patchMFConfig({
273
+ mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
274
+ metaName,
275
+ isServer,
276
+ internalDirectory,
277
+ entries
278
+ });
279
+ },
280
+ // bundlerChain can not keep target order
281
+ webpack: function webpack(_config, param2) {
282
+ var isServer = param2.isServer;
283
+ patchMFConfig({
284
+ mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
285
+ metaName,
286
+ isServer,
287
+ internalDirectory,
288
+ entries
289
+ });
290
+ }
291
+ },
292
+ source: {
293
+ define: {
294
+ MODERN_ROUTER_ID_PREFIX: JSON.stringify(transformName2Prefix(name))
295
+ }
296
+ }
297
+ }
298
+ ];
299
+ });
300
+ }),
301
+ afterDev: function afterDev() {
302
+ return _async_to_generator(function() {
303
+ return _ts_generator(this, function(_state2) {
304
+ clearMFCache();
305
+ console.log("afterDev", globalThis.__VMOK__);
306
+ return [
307
+ 2
308
+ ];
309
+ });
310
+ })();
311
+ }
312
+ }
313
+ ];
314
+ }
315
+ });
316
+ });
317
+ return function(_) {
318
+ return _ref.apply(this, arguments);
319
+ };
320
+ }()
321
+ };
322
+ };
323
+ var plugin_default = moduleFederationDataLoaderPlugin;
324
+ export {
325
+ plugin_default as default,
326
+ generateRoutes,
327
+ moduleFederationDataLoaderPlugin
328
+ };
@@ -1,9 +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";
3
+ import { _ as _type_of } from "@swc/helpers/_/_type_of";
2
4
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
5
  import { ModuleFederationPlugin as WebpackModuleFederationPlugin, AsyncBoundaryPlugin } from "@module-federation/enhanced";
4
6
  import { ModuleFederationPlugin as RspackModuleFederationPlugin } from "@module-federation/enhanced/rspack";
5
7
  import { moduleFederationConfigPlugin } from "./configPlugin";
6
8
  import { moduleFederationSSRPlugin } from "./ssrPlugin";
9
+ import { moduleFederationDataLoaderPlugin } from "./dataLoader/plugin";
7
10
  var moduleFederationPlugin = function() {
8
11
  var userConfig = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
9
12
  var internalModernPluginOptions = {
@@ -53,7 +56,7 @@ var moduleFederationPlugin = function() {
53
56
  var enableAsyncEntry = (_modernjsConfig_source = modernjsConfig.source) === null || _modernjsConfig_source === void 0 ? void 0 : _modernjsConfig_source.enableAsyncEntry;
54
57
  if (!enableAsyncEntry && browserPluginOptions.async !== false) {
55
58
  var _config_plugins1;
56
- var asyncBoundaryPluginOptions = typeof browserPluginOptions.async === "object" ? browserPluginOptions.async : {
59
+ var asyncBoundaryPluginOptions = _type_of(browserPluginOptions.async) === "object" ? browserPluginOptions.async : {
57
60
  eager: function(module) {
58
61
  return module && /\.federation/.test((module === null || module === void 0 ? void 0 : module.request) || "");
59
62
  },
@@ -79,7 +82,8 @@ var moduleFederationPlugin = function() {
79
82
  }(),
80
83
  usePlugins: [
81
84
  moduleFederationConfigPlugin(internalModernPluginOptions),
82
- moduleFederationSSRPlugin(internalModernPluginOptions)
85
+ moduleFederationSSRPlugin(internalModernPluginOptions),
86
+ moduleFederationDataLoaderPlugin(Boolean(userConfig.dataLoader), internalModernPluginOptions, _object_spread({}, typeof userConfig.dataLoader === "boolean" ? {} : userConfig.dataLoader))
83
87
  ]
84
88
  };
85
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,109 @@
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
+ console.log("server ", dataLoaderRemotes);
19
+ if (!dataLoaderRemotes.length) {
20
+ return {};
21
+ }
22
+ var isHandled = false;
23
+ return {
24
+ prepare: function prepare() {
25
+ var middlewares = api.useAppContext().middlewares;
26
+ middlewares.push({
27
+ name: "MFDataLoaderServerPlugin",
28
+ handler: function() {
29
+ var _ref = _async_to_generator(function(c, next) {
30
+ var serverManifest, loaderBundles, nestedRoutesJson, _api_useConfigContext_server, instance, remoteFullRoutesMap, originalSSRByRouteIds, remoteSSRByRouteIds, ssrByRouteIds;
31
+ return _ts_generator(this, function(_state) {
32
+ switch (_state.label) {
33
+ case 0:
34
+ serverManifest = c.get("serverManifest");
35
+ loaderBundles = serverManifest.loaderBundles, nestedRoutesJson = serverManifest.nestedRoutesJson;
36
+ console.log("server plugin ", isHandled, loaderBundles);
37
+ if (!(isHandled && !globalThis.FORCE_MF_REFRESH))
38
+ return [
39
+ 3,
40
+ 2
41
+ ];
42
+ return [
43
+ 4,
44
+ next()
45
+ ];
46
+ case 1:
47
+ _state.sent();
48
+ return [
49
+ 3,
50
+ 5
51
+ ];
52
+ case 2:
53
+ instance = init({
54
+ name,
55
+ remotes
56
+ });
57
+ return [
58
+ 4,
59
+ fetchRemoteFullRoutesMap({
60
+ instance,
61
+ remotePath: isBrowserEnv() ? "".concat(DEFAULT_ENTRY, "/").concat(MF_ROUTES) : "".concat(DEFAULT_ENTRY, "/").concat(MODERN_JS_ROUTE_SERVER_LOADER),
62
+ dataLoaderRemotes
63
+ })
64
+ ];
65
+ case 3:
66
+ remoteFullRoutesMap = _state.sent();
67
+ originalSSRByRouteIds = (_api_useConfigContext_server = api.useConfigContext().server) === null || _api_useConfigContext_server === void 0 ? void 0 : _api_useConfigContext_server.ssrByRouteIds;
68
+ if (originalSSRByRouteIds && dataLoaderRemotes.length) {
69
+ remoteSSRByRouteIds = getSSRByRouteIds(dataLoaderRemotes, remoteFullRoutesMap) || [];
70
+ ssrByRouteIds = new Set(_to_consumable_array(originalSSRByRouteIds).concat(_to_consumable_array(remoteSSRByRouteIds.map(function(id) {
71
+ return ssrByRouteIdsMap[id] || id;
72
+ }))));
73
+ api.useConfigContext().server.ssrByRouteIds = Array.from(ssrByRouteIds);
74
+ }
75
+ injectRemoteRoutes(loaderBundles, remoteFullRoutesMap);
76
+ injectRemoteRoutes(nestedRoutesJson, remoteFullRoutesMap);
77
+ isHandled = true;
78
+ globalThis.FORCE_MF_REFRESH = false;
79
+ return [
80
+ 4,
81
+ next()
82
+ ];
83
+ case 4:
84
+ _state.sent();
85
+ _state.label = 5;
86
+ case 5:
87
+ return [
88
+ 2
89
+ ];
90
+ }
91
+ });
92
+ });
93
+ return function(c, next) {
94
+ return _ref.apply(this, arguments);
95
+ };
96
+ }(),
97
+ before: [
98
+ "render"
99
+ ]
100
+ });
101
+ }
102
+ };
103
+ }
104
+ };
105
+ }
106
+ ;
107
+ export {
108
+ dataLoaderPlugin_default as default
109
+ };
@@ -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,