@module-federation/modern-js 0.0.0-next-20240909062831 → 0.0.0-next-20240909083238

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 +142 -0
  3. package/dist/cjs/cli/dataLoader/ast/generateSelfRoutes.js +48 -0
  4. package/dist/cjs/cli/dataLoader/ast/generateSerializableRoutes.js +90 -0
  5. package/dist/cjs/cli/dataLoader/ast/generateSlimRoutes.js +104 -0
  6. package/dist/cjs/cli/dataLoader/ast/index.js +37 -0
  7. package/dist/cjs/cli/dataLoader/generateRouteFile.js +143 -0
  8. package/dist/cjs/cli/dataLoader/patchMFConfig.js +137 -0
  9. package/dist/cjs/cli/dataLoader/plugin.js +205 -0
  10. package/dist/cjs/cli/index.js +6 -1
  11. package/dist/cjs/cli/mfRuntimePlugins/inject-node-fetch.js +7 -10
  12. package/dist/cjs/cli/mfRuntimePlugins/node.js +44 -0
  13. package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +40 -38
  14. package/dist/cjs/cli/server/dataLoaderPlugin.js +82 -0
  15. package/dist/cjs/cli/server/fetchRemoteFullRoutesMap.js +68 -0
  16. package/dist/cjs/cli/ssrPlugin.js +1 -1
  17. package/dist/cjs/cli/utils.js +8 -4
  18. package/dist/cjs/cli/utils.spec.js +1 -1
  19. package/dist/cjs/constant.js +32 -2
  20. package/dist/cjs/interfaces/route.js +16 -0
  21. package/dist/cjs/runtime/dataLoader/plugin.js +73 -0
  22. package/dist/cjs/runtime/dataLoader/utils.js +89 -0
  23. package/dist/cjs/runtime/index.js +4 -1
  24. package/dist/cjs/runtime/utils.js +38 -0
  25. package/dist/cjs/{cli/constant.js → runtime/withMFRouteId.js} +9 -6
  26. package/dist/cjs/ssr-runtime/plugin.js +7 -7
  27. package/dist/esm/cli/dataLoader/ast/constant.js +18 -0
  28. package/dist/esm/cli/dataLoader/ast/generateRoutes.js +109 -0
  29. package/dist/esm/cli/dataLoader/ast/generateSelfRoutes.js +17 -0
  30. package/dist/esm/cli/dataLoader/ast/generateSerializableRoutes.js +57 -0
  31. package/dist/esm/cli/dataLoader/ast/generateSlimRoutes.js +73 -0
  32. package/dist/esm/cli/dataLoader/ast/index.js +10 -0
  33. package/dist/esm/cli/dataLoader/generateRouteFile.js +293 -0
  34. package/dist/esm/cli/dataLoader/patchMFConfig.js +103 -0
  35. package/dist/esm/cli/dataLoader/plugin.js +216 -0
  36. package/dist/esm/cli/index.js +6 -1
  37. package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js +7 -10
  38. package/dist/esm/cli/mfRuntimePlugins/node.js +16 -0
  39. package/dist/esm/cli/server/dataLoaderPlugin.js +105 -0
  40. package/dist/esm/cli/server/fetchRemoteFullRoutesMap.js +86 -0
  41. package/dist/esm/cli/ssrPlugin.js +1 -1
  42. package/dist/esm/cli/utils.js +6 -3
  43. package/dist/esm/cli/utils.spec.js +1 -1
  44. package/dist/esm/constant.js +21 -1
  45. package/dist/esm/interfaces/route.js +0 -0
  46. package/dist/esm/runtime/dataLoader/plugin.js +92 -0
  47. package/dist/esm/runtime/dataLoader/utils.js +63 -0
  48. package/dist/esm/runtime/index.js +3 -1
  49. package/dist/esm/runtime/utils.js +13 -0
  50. package/dist/esm/runtime/withMFRouteId.js +7 -0
  51. package/dist/esm/ssr-runtime/plugin.js +7 -10
  52. package/dist/esm-node/cli/dataLoader/ast/constant.js +18 -0
  53. package/dist/esm-node/cli/dataLoader/ast/generateRoutes.js +108 -0
  54. package/dist/esm-node/cli/dataLoader/ast/generateSelfRoutes.js +14 -0
  55. package/dist/esm-node/cli/dataLoader/ast/generateSerializableRoutes.js +56 -0
  56. package/dist/esm-node/cli/dataLoader/ast/generateSlimRoutes.js +70 -0
  57. package/dist/esm-node/cli/dataLoader/ast/index.js +10 -0
  58. package/dist/esm-node/cli/dataLoader/generateRouteFile.js +108 -0
  59. package/dist/esm-node/cli/dataLoader/patchMFConfig.js +103 -0
  60. package/dist/esm-node/cli/dataLoader/plugin.js +169 -0
  61. package/dist/esm-node/cli/index.js +6 -1
  62. package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.js +7 -10
  63. package/dist/esm-node/cli/mfRuntimePlugins/node.js +14 -0
  64. package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +40 -38
  65. package/dist/esm-node/cli/server/dataLoaderPlugin.js +62 -0
  66. package/dist/esm-node/cli/server/fetchRemoteFullRoutesMap.js +43 -0
  67. package/dist/esm-node/cli/ssrPlugin.js +1 -1
  68. package/dist/esm-node/cli/utils.js +6 -3
  69. package/dist/esm-node/cli/utils.spec.js +1 -1
  70. package/dist/esm-node/constant.js +21 -1
  71. package/dist/esm-node/interfaces/route.js +0 -0
  72. package/dist/esm-node/runtime/dataLoader/plugin.js +49 -0
  73. package/dist/esm-node/runtime/dataLoader/utils.js +63 -0
  74. package/dist/esm-node/runtime/index.js +3 -1
  75. package/dist/esm-node/runtime/utils.js +13 -0
  76. package/dist/esm-node/runtime/withMFRouteId.js +7 -0
  77. package/dist/esm-node/ssr-runtime/plugin.js +7 -7
  78. package/dist/types/cli/dataLoader/ast/constant.d.ts +8 -0
  79. package/dist/types/cli/dataLoader/ast/generateRoutes.d.ts +7 -0
  80. package/dist/types/cli/dataLoader/ast/generateSelfRoutes.d.ts +7 -0
  81. package/dist/types/cli/dataLoader/ast/generateSerializableRoutes.d.ts +5 -0
  82. package/dist/types/cli/dataLoader/ast/generateSlimRoutes.d.ts +7 -0
  83. package/dist/types/cli/dataLoader/ast/index.d.ts +4 -0
  84. package/dist/types/cli/dataLoader/generateRouteFile.d.ts +7 -0
  85. package/dist/types/cli/dataLoader/patchMFConfig.d.ts +11 -0
  86. package/dist/types/cli/dataLoader/plugin.d.ts +6 -0
  87. package/dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts +2 -2
  88. package/dist/types/cli/mfRuntimePlugins/node.d.ts +3 -0
  89. package/dist/types/cli/server/dataLoaderPlugin.d.ts +10 -0
  90. package/dist/types/cli/server/fetchRemoteFullRoutesMap.d.ts +10 -0
  91. package/dist/types/cli/utils.d.ts +2 -0
  92. package/dist/types/constant.d.ts +10 -0
  93. package/dist/types/interfaces/route.d.ts +13 -0
  94. package/dist/types/runtime/dataLoader/plugin.d.ts +2 -0
  95. package/dist/types/runtime/dataLoader/utils.d.ts +19 -0
  96. package/dist/types/runtime/index.d.ts +1 -0
  97. package/dist/types/runtime/utils.d.ts +2 -0
  98. package/dist/types/runtime/withMFRouteId.d.ts +1 -0
  99. package/dist/types/types/index.d.ts +10 -0
  100. package/package.json +40 -11
  101. package/dist/esm/cli/constant.js +0 -4
  102. package/dist/esm-node/cli/constant.js +0 -4
  103. package/dist/types/cli/constant.d.ts +0 -1
@@ -0,0 +1,105 @@
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, partialSSRRemotes = param.partialSSRRemotes, 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
+ if (!remotes.length) {
19
+ return {};
20
+ }
21
+ var isHandled = false;
22
+ return {
23
+ prepare: function prepare() {
24
+ var middlewares = api.useAppContext().middlewares;
25
+ middlewares.push({
26
+ name: "MFDataLoaderServerPlugin",
27
+ handler: function() {
28
+ var _ref = _async_to_generator(function(c, next) {
29
+ var serverManifest, loaderBundles, nestedRoutesJson, _api_useConfigContext_server, instance, remoteFullRoutesMap, originalSSRByRouteIds, remoteSSRByRouteIds, ssrByRouteIds;
30
+ return _ts_generator(this, function(_state) {
31
+ switch (_state.label) {
32
+ case 0:
33
+ serverManifest = c.get("serverManifest");
34
+ loaderBundles = serverManifest.loaderBundles, nestedRoutesJson = serverManifest.nestedRoutesJson;
35
+ if (!isHandled)
36
+ return [
37
+ 3,
38
+ 2
39
+ ];
40
+ return [
41
+ 4,
42
+ next()
43
+ ];
44
+ case 1:
45
+ _state.sent();
46
+ return [
47
+ 3,
48
+ 5
49
+ ];
50
+ case 2:
51
+ instance = init({
52
+ name,
53
+ remotes
54
+ });
55
+ return [
56
+ 4,
57
+ fetchRemoteFullRoutesMap({
58
+ instance,
59
+ remotePath: isBrowserEnv() ? "".concat(DEFAULT_ENTRY, "/").concat(MF_ROUTES) : "".concat(DEFAULT_ENTRY, "/").concat(MODERN_JS_ROUTE_SERVER_LOADER)
60
+ })
61
+ ];
62
+ case 3:
63
+ remoteFullRoutesMap = _state.sent();
64
+ originalSSRByRouteIds = (_api_useConfigContext_server = api.useConfigContext().server) === null || _api_useConfigContext_server === void 0 ? void 0 : _api_useConfigContext_server.ssrByRouteIds;
65
+ if (originalSSRByRouteIds && partialSSRRemotes) {
66
+ remoteSSRByRouteIds = getSSRByRouteIds(partialSSRRemotes, remoteFullRoutesMap) || [];
67
+ ssrByRouteIds = new Set(_to_consumable_array(originalSSRByRouteIds).concat(_to_consumable_array(remoteSSRByRouteIds.map(function(id) {
68
+ return ssrByRouteIdsMap[id] || id;
69
+ }))));
70
+ api.useConfigContext().server.ssrByRouteIds = Array.from(ssrByRouteIds);
71
+ }
72
+ injectRemoteRoutes(loaderBundles, remoteFullRoutesMap);
73
+ injectRemoteRoutes(nestedRoutesJson, remoteFullRoutesMap);
74
+ isHandled = true;
75
+ return [
76
+ 4,
77
+ next()
78
+ ];
79
+ case 4:
80
+ _state.sent();
81
+ _state.label = 5;
82
+ case 5:
83
+ return [
84
+ 2
85
+ ];
86
+ }
87
+ });
88
+ });
89
+ return function(c, next) {
90
+ return _ref.apply(this, arguments);
91
+ };
92
+ }(),
93
+ before: [
94
+ "render"
95
+ ]
96
+ });
97
+ }
98
+ };
99
+ }
100
+ };
101
+ }
102
+ ;
103
+ export {
104
+ dataLoaderPlugin_default as default
105
+ };
@@ -0,0 +1,86 @@
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
+ function fetchRemoteFullRoutesMap(options) {
5
+ return _fetchRemoteFullRoutesMap.apply(this, arguments);
6
+ }
7
+ function _fetchRemoteFullRoutesMap() {
8
+ _fetchRemoteFullRoutesMap = _async_to_generator(function(options) {
9
+ var instance, remotePath, remotes, remoteRoutesMap;
10
+ return _ts_generator(this, function(_state) {
11
+ switch (_state.label) {
12
+ case 0:
13
+ instance = options.instance, remotePath = options.remotePath;
14
+ remotes = instance.options.remotes;
15
+ remoteRoutesMap = {};
16
+ if (!remotes.length) {
17
+ return [
18
+ 2,
19
+ remoteRoutesMap
20
+ ];
21
+ }
22
+ return [
23
+ 4,
24
+ Promise.all(remotes.map(function() {
25
+ var _ref = _async_to_generator(function(remote) {
26
+ var remoteId, routes;
27
+ return _ts_generator(this, function(_state2) {
28
+ switch (_state2.label) {
29
+ case 0:
30
+ remoteId = "".concat(remote.name, "/").concat(remotePath);
31
+ return [
32
+ 4,
33
+ instance.loadRemote(remoteId)
34
+ ];
35
+ case 1:
36
+ routes = _state2.sent().routes;
37
+ remoteRoutesMap[remote.name] = {
38
+ routes
39
+ };
40
+ return [
41
+ 2
42
+ ];
43
+ }
44
+ });
45
+ });
46
+ return function(remote) {
47
+ return _ref.apply(this, arguments);
48
+ };
49
+ }()))
50
+ ];
51
+ case 1:
52
+ _state.sent();
53
+ return [
54
+ 2,
55
+ remoteRoutesMap
56
+ ];
57
+ }
58
+ });
59
+ });
60
+ return _fetchRemoteFullRoutesMap.apply(this, arguments);
61
+ }
62
+ function getSSRByRouteIds(partialSSRRemotes, remoteRoutesMap) {
63
+ if (!partialSSRRemotes.length) {
64
+ return void 0;
65
+ }
66
+ var remoteProviderRouteIds = /* @__PURE__ */ new Set();
67
+ var collectIds = function(route) {
68
+ remoteProviderRouteIds.add(route.id);
69
+ if (route.children) {
70
+ route.children.forEach(function(r) {
71
+ collectIds(r);
72
+ });
73
+ }
74
+ };
75
+ Object.values(remoteRoutesMap).forEach(function(item) {
76
+ var routes = item.routes;
77
+ routes.forEach(function(route) {
78
+ collectIds(route);
79
+ });
80
+ });
81
+ return _to_consumable_array(remoteProviderRouteIds);
82
+ }
83
+ export {
84
+ fetchRemoteFullRoutesMap,
85
+ getSSRByRouteIds
86
+ };
@@ -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 } from "../constant";
10
10
  function setEnv() {
11
11
  process.env["MF_DISABLE_EMIT_STATS"] = "true";
12
12
  process.env["MF_SSR_PRJ"] = "true";
@@ -79,7 +79,9 @@ var replaceRemoteUrl = function(mfConfig, remoteIpStrategy) {
79
79
  if (typeof remoteObject === "string") {
80
80
  return;
81
81
  }
82
- handleRemoteObject(remoteObject);
82
+ if ("entry" in remoteObject && typeof remoteObject.entry === "string") {
83
+ remoteObject.entry = remoteObject.entry.replace(LOCALHOST, ipv4);
84
+ }
83
85
  });
84
86
  } else if (typeof mfConfig.remotes !== "string") {
85
87
  handleRemoteObject(mfConfig.remotes);
@@ -132,7 +134,7 @@ var patchMFConfig = function(mfConfig, isServer, remoteIpStrategy) {
132
134
  injectRuntimePlugins(path.resolve(__dirname, "./mfRuntimePlugins/resolve-entry-ipv4.js"), runtimePlugins);
133
135
  }
134
136
  if (isServer) {
135
- injectRuntimePlugins(require.resolve("@module-federation/node/runtimePlugin"), runtimePlugins);
137
+ injectRuntimePlugins(require.resolve("./mfRuntimePlugins/node.js"), runtimePlugins);
136
138
  if (isDev) {
137
139
  injectRuntimePlugins(require.resolve("@module-federation/node/record-dynamic-remote-entry-hash-plugin"), runtimePlugins);
138
140
  }
@@ -339,5 +341,6 @@ export {
339
341
  getMFConfig,
340
342
  patchBundlerConfig,
341
343
  patchIgnoreWarning,
342
- patchMFConfig
344
+ patchMFConfig,
345
+ replaceRemoteUrl
343
346
  };
@@ -43,7 +43,7 @@ describe("patchMFConfig", /* @__PURE__ */ _async_to_generator(function() {
43
43
  remoteType: "script",
44
44
  runtimePlugins: [
45
45
  path.resolve(__dirname, "./mfRuntimePlugins/shared-strategy.js"),
46
- require.resolve("@module-federation/node/runtimePlugin"),
46
+ require.resolve("./mfRuntimePlugins/node.js"),
47
47
  path.resolve(__dirname, "./mfRuntimePlugins/inject-node-fetch.js")
48
48
  ],
49
49
  shared: {
@@ -1,8 +1,28 @@
1
1
  var MODERN_JS_SERVER_DIR = "bundles";
2
2
  var LOCALHOST = "localhost";
3
3
  var PLUGIN_IDENTIFIER = "[ Modern.js Module Federation ]";
4
+ var isDev = process.env.NODE_ENV === "development";
5
+ var MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME = "routes";
6
+ var MODERN_JS_ROUTE_SERVER_LOADER = "route-server-loaders";
7
+ var META_NAME = "modern-js";
8
+ var FOLD_IDENTIFIER = "@mf";
9
+ var SEPARATOR = "_";
10
+ var MF_ROUTES = "mf-routes";
11
+ var DEFAULT_ENTRY = "main";
12
+ var DEFAULT_LAYOUT = "layout";
13
+ var MF_JSON = "mf.json";
4
14
  export {
15
+ DEFAULT_ENTRY,
16
+ DEFAULT_LAYOUT,
17
+ FOLD_IDENTIFIER,
5
18
  LOCALHOST,
19
+ META_NAME,
20
+ MF_JSON,
21
+ MF_ROUTES,
22
+ MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME,
23
+ MODERN_JS_ROUTE_SERVER_LOADER,
6
24
  MODERN_JS_SERVER_DIR,
7
- PLUGIN_IDENTIFIER
25
+ PLUGIN_IDENTIFIER,
26
+ SEPARATOR,
27
+ isDev
8
28
  };
File without changes
@@ -0,0 +1,92 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
+ import { getInstance } from "@module-federation/enhanced/runtime";
4
+ import { MF_ROUTES, DEFAULT_ENTRY } from "../../constant";
5
+ import { injectRemoteRoutes } from "./utils";
6
+ var ssrDataLoaderPlugin = function() {
7
+ return {
8
+ name: "@modern-js/plugin-mf-data-loader",
9
+ post: [
10
+ "@modern-js/plugin-router"
11
+ ],
12
+ setup: function() {
13
+ var remoteRoutesMap = {};
14
+ return {
15
+ beforeRender: function beforeRender() {
16
+ return _async_to_generator(function() {
17
+ var instance;
18
+ return _ts_generator(this, function(_state) {
19
+ switch (_state.label) {
20
+ case 0:
21
+ console.log("init");
22
+ if (Object.keys(remoteRoutesMap).length) {
23
+ return [
24
+ 2
25
+ ];
26
+ }
27
+ instance = getInstance();
28
+ if (!instance) {
29
+ console.log("no instance!");
30
+ return [
31
+ 2
32
+ ];
33
+ }
34
+ if (!instance.options.remotes.length) {
35
+ return [
36
+ 2
37
+ ];
38
+ }
39
+ return [
40
+ 4,
41
+ Promise.all(instance.options.remotes.map(function() {
42
+ var _ref = _async_to_generator(function(remote) {
43
+ var remoteId, routes;
44
+ return _ts_generator(this, function(_state2) {
45
+ switch (_state2.label) {
46
+ case 0:
47
+ remoteId = "".concat(remote.name, "/").concat(DEFAULT_ENTRY, "/").concat(MF_ROUTES);
48
+ return [
49
+ 4,
50
+ instance.loadRemote(remoteId)
51
+ ];
52
+ case 1:
53
+ routes = _state2.sent().routes;
54
+ remoteRoutesMap[remote.name] = {
55
+ routes
56
+ };
57
+ return [
58
+ 2
59
+ ];
60
+ }
61
+ });
62
+ });
63
+ return function(remote) {
64
+ return _ref.apply(this, arguments);
65
+ };
66
+ }()))
67
+ ];
68
+ case 1:
69
+ _state.sent();
70
+ return [
71
+ 2
72
+ ];
73
+ }
74
+ });
75
+ })();
76
+ },
77
+ // runtime plugin not save route , so it needs to inject again when trigger
78
+ modifyRoutes: function(routes) {
79
+ injectRemoteRoutes({
80
+ entry: {
81
+ routes
82
+ }
83
+ }, remoteRoutesMap);
84
+ return routes;
85
+ }
86
+ };
87
+ }
88
+ };
89
+ };
90
+ export {
91
+ ssrDataLoaderPlugin
92
+ };
@@ -0,0 +1,63 @@
1
+ import { DEFAULT_LAYOUT } from "../../constant";
2
+ import { transformName2Prefix, transformPrefix2Name } from "../utils";
3
+ function getRemoteRoutesInfos(route, remoteRoutesMap) {
4
+ if (!route.id) {
5
+ return;
6
+ }
7
+ var remoteName = transformPrefix2Name(route.id);
8
+ var remoteRouteObj = remoteRoutesMap[remoteName];
9
+ if (!remoteRouteObj) {
10
+ return;
11
+ }
12
+ var remoteRoutes = remoteRouteObj.routes[0];
13
+ return {
14
+ routes: remoteRoutes,
15
+ name: remoteName,
16
+ pathName: route.path
17
+ };
18
+ }
19
+ function getRemoteLayoutId(remoteName) {
20
+ var prefix = transformName2Prefix(remoteName);
21
+ return "".concat(prefix).concat(DEFAULT_LAYOUT);
22
+ }
23
+ function injectRemoteRoutes(initialRoutes, remoteRoutesMap) {
24
+ var traverse = function(route) {
25
+ var _route_children;
26
+ var remoteRoutesInfos = getRemoteRoutesInfos(route, remoteRoutesMap);
27
+ if (remoteRoutesInfos) {
28
+ var routes = remoteRoutesInfos.routes, pathName = remoteRoutesInfos.pathName;
29
+ route.id = routes.id;
30
+ route.path = pathName;
31
+ if (routes.loader) {
32
+ route.loader = routes.loader;
33
+ }
34
+ if (routes.children) {
35
+ route.children = routes.children;
36
+ }
37
+ route.element = routes.element;
38
+ route.Component = routes.Component;
39
+ return;
40
+ }
41
+ (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.forEach(function(r) {
42
+ traverse(r);
43
+ });
44
+ };
45
+ Object.keys(initialRoutes).forEach(function(entryName) {
46
+ var routes = initialRoutes[entryName];
47
+ if (Array.isArray(routes)) {
48
+ routes.forEach(function(route) {
49
+ traverse(route);
50
+ });
51
+ } else {
52
+ var _routes_routes;
53
+ routes === null || routes === void 0 ? void 0 : (_routes_routes = routes.routes) === null || _routes_routes === void 0 ? void 0 : _routes_routes.forEach(function(r) {
54
+ traverse(r);
55
+ });
56
+ }
57
+ });
58
+ }
59
+ export {
60
+ getRemoteLayoutId,
61
+ getRemoteRoutesInfos,
62
+ injectRemoteRoutes
63
+ };
@@ -1,5 +1,7 @@
1
1
  export * from "@module-federation/enhanced/runtime";
2
2
  import { createRemoteSSRComponent } from "./createRemoteSSRComponent";
3
+ import { withMFRouteId } from "./withMFRouteId";
3
4
  export {
4
- createRemoteSSRComponent
5
+ createRemoteSSRComponent,
6
+ withMFRouteId
5
7
  };
@@ -0,0 +1,13 @@
1
+ import { encodeName, decodeName } from "@module-federation/sdk";
2
+ var SPLIT_SYMBOL = "@";
3
+ function transformName2Prefix(name) {
4
+ return "".concat(encodeName(name, "", false)).concat(SPLIT_SYMBOL);
5
+ }
6
+ function transformPrefix2Name(prefix) {
7
+ var realPrefix = prefix.split(SPLIT_SYMBOL)[0];
8
+ return decodeName(realPrefix, "", false);
9
+ }
10
+ export {
11
+ transformName2Prefix,
12
+ transformPrefix2Name
13
+ };
@@ -0,0 +1,7 @@
1
+ function withMFRouteId(id) {
2
+ var prefix = typeof MODERN_ROUTER_ID_PREFIX === "string" ? MODERN_ROUTER_ID_PREFIX : "";
3
+ return prefix + id;
4
+ }
5
+ export {
6
+ withMFRouteId
7
+ };
@@ -4,13 +4,16 @@ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
4
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
5
5
  import hoistNonReactStatics from "hoist-non-react-statics";
6
6
  import { SSRLiveReload } from "./SSRLiveReload";
7
+ console.log("mfSSRPlugin trigger");
7
8
  var mfSSRPlugin = function() {
8
9
  return {
9
10
  name: "@module-federation/modern-js",
11
+ pre: [
12
+ "@modern-js/plugin-router"
13
+ ],
10
14
  setup: function() {
11
15
  return {
12
- init: function init(param, next) {
13
- var context = param.context;
16
+ beforeRender: function beforeRender() {
14
17
  return _async_to_generator(function() {
15
18
  var nodeUtils, shouldUpdate;
16
19
  return _ts_generator(this, function(_state) {
@@ -18,10 +21,7 @@ var mfSSRPlugin = function() {
18
21
  case 0:
19
22
  if (typeof window !== "undefined") {
20
23
  return [
21
- 2,
22
- next({
23
- context
24
- })
24
+ 2
25
25
  ];
26
26
  }
27
27
  globalThis.shouldUpdate = false;
@@ -53,10 +53,7 @@ var mfSSRPlugin = function() {
53
53
  _state.label = 4;
54
54
  case 4:
55
55
  return [
56
- 2,
57
- next({
58
- context
59
- })
56
+ 2
60
57
  ];
61
58
  }
62
59
  });
@@ -0,0 +1,18 @@
1
+ const IS_ROOT = "isRoot";
2
+ const ID = "id";
3
+ const COMPONENT = "component";
4
+ const LAZY_COMPONENT = "lazyImport";
5
+ const SHOULD_REVALIDATE = "shouldRevalidate";
6
+ const PRIVATE_COMPONENT = "_component";
7
+ const ELEMENT = "element";
8
+ const LOADER = "loader";
9
+ export {
10
+ COMPONENT,
11
+ ELEMENT,
12
+ ID,
13
+ IS_ROOT,
14
+ LAZY_COMPONENT,
15
+ LOADER,
16
+ PRIVATE_COMPONENT,
17
+ SHOULD_REVALIDATE
18
+ };
@@ -0,0 +1,108 @@
1
+ import fs from "fs";
2
+ import traverse from "@babel/traverse";
3
+ import * as babelParser from "@babel/parser";
4
+ import generate from "@babel/generator";
5
+ import * as t from "@babel/types";
6
+ function findTargetKeyNode(nodeProperties, key) {
7
+ return nodeProperties.find((p) => t.isObjectProperty(p) && t.isStringLiteral(p.key) && p.key.value === key);
8
+ }
9
+ function generateRoutes({ sourceCode, filePath, prefix, baseName }) {
10
+ const ast = babelParser.parse(sourceCode, {
11
+ sourceType: "module"
12
+ });
13
+ const lazyComponentDeclarations = [];
14
+ const componentDeclarations = [];
15
+ let componentId = 0;
16
+ traverse(ast, {
17
+ // ImportDeclaration(path) {
18
+ // const source = path.node.source.value;
19
+ // const routeIdMatch = source.match(/routeId=([^&]+)/);
20
+ // if (routeIdMatch) {
21
+ // const originalRouteId = routeIdMatch[1];
22
+ // const newRouteId = `${prefix}${originalRouteId}`;
23
+ // const newSource = source.replace(
24
+ // /routeId=[^&]+/,
25
+ // `routeId=${newRouteId}`,
26
+ // );
27
+ // path.node.source = t.stringLiteral(newSource);
28
+ // }
29
+ // },
30
+ ObjectExpression(path) {
31
+ let componentName = "";
32
+ let lazyComponentName = "";
33
+ if (!Array.isArray(path.node.properties)) {
34
+ return;
35
+ }
36
+ const idNode = findTargetKeyNode(path.node.properties, "id");
37
+ if (idNode && t.isObjectProperty(idNode) && t.isStringLiteral(idNode.value)) {
38
+ }
39
+ const isRootNode = findTargetKeyNode(path.node.properties, "isRoot");
40
+ if (isRootNode && t.isObjectProperty(isRootNode) && t.isBooleanLiteral(isRootNode.value)) {
41
+ isRootNode.value.value = false;
42
+ }
43
+ if (!isRootNode) {
44
+ const lazyComponentNode = findTargetKeyNode(path.node.properties, "lazyImport");
45
+ if (lazyComponentNode && t.isObjectProperty(lazyComponentNode) && t.isArrowFunctionExpression(lazyComponentNode.value)) {
46
+ lazyComponentName = `LazyComponent_${componentId}`;
47
+ const lazyDeclaration = t.variableDeclaration("const", [
48
+ t.variableDeclarator(t.identifier(lazyComponentName), lazyComponentNode.value)
49
+ ]);
50
+ lazyComponentNode.value = t.identifier(lazyComponentName);
51
+ const componentNode2 = findTargetKeyNode(path.node.properties, "component");
52
+ if (componentNode2 && t.isObjectProperty(componentNode2) && t.isCallExpression(componentNode2.value) && t.isIdentifier(componentNode2.value.callee)) {
53
+ componentNode2.value = t.callExpression(t.identifier("lazy"), [
54
+ t.identifier(lazyComponentName)
55
+ ]);
56
+ }
57
+ lazyComponentDeclarations.push(lazyDeclaration);
58
+ }
59
+ }
60
+ const componentNode = findTargetKeyNode(path.node.properties, "component");
61
+ if (componentNode && t.isObjectProperty(componentNode) && t.isCallExpression(componentNode.value) && t.isIdentifier(componentNode.value.callee)) {
62
+ componentName = `Component_${componentId}`;
63
+ const componentDeclaration = t.variableDeclaration("const", [
64
+ t.variableDeclarator(t.identifier(componentName), lazyComponentName ? t.callExpression(t.identifier("lazy"), [
65
+ t.identifier(lazyComponentName)
66
+ ]) : componentNode.value)
67
+ ]);
68
+ componentDeclarations.push(componentDeclaration);
69
+ componentNode.value = t.identifier(componentName);
70
+ }
71
+ if (lazyComponentName || componentName) {
72
+ componentId++;
73
+ if (componentName) {
74
+ const upperFirstName = componentName.slice(0, 1).toUpperCase() + componentName.slice(1);
75
+ const jsxElement = t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier(upperFirstName), [], true), null, [], true);
76
+ path.node.properties.push(t.objectProperty(t.identifier("element"), jsxElement));
77
+ }
78
+ }
79
+ }
80
+ });
81
+ traverse(ast, {
82
+ Program(path) {
83
+ const lastImportIndex = path.get("body").reduce((lastIndex, p, index) => {
84
+ if (t.isImportDeclaration(p.node)) {
85
+ lastIndex = index;
86
+ }
87
+ return lastIndex;
88
+ }, -1);
89
+ if (lastImportIndex >= 0) {
90
+ const lastImport = path.get(`body.${lastImportIndex}`);
91
+ [
92
+ ...componentDeclarations,
93
+ ...lazyComponentDeclarations
94
+ ].forEach((declaration) => {
95
+ if ("insertAfter" in lastImport) {
96
+ lastImport.insertAfter(declaration);
97
+ }
98
+ });
99
+ }
100
+ }
101
+ });
102
+ const { code: newCode } = generate(ast);
103
+ const finalCode = `${newCode}export const baseName = '${baseName}';`;
104
+ fs.writeFileSync(filePath, finalCode);
105
+ }
106
+ export {
107
+ generateRoutes
108
+ };
@@ -0,0 +1,14 @@
1
+ import fs from "fs";
2
+ import * as t from "@babel/types";
3
+ function findTargetKeyNode(nodeProperties, key) {
4
+ return nodeProperties.find((p) => t.isObjectProperty(p) && t.isStringLiteral(p.key) && p.key.value === key);
5
+ }
6
+ function generateSelfRoutes({ sourceCode, filePath, prefix, baseName }) {
7
+ const lazyComponentDeclarations = [];
8
+ const componentDeclarations = [];
9
+ let componentId = 0;
10
+ fs.writeFileSync(filePath, sourceCode);
11
+ }
12
+ export {
13
+ generateSelfRoutes
14
+ };