@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
@@ -32,7 +32,8 @@ __export(utils_exports, {
32
32
  getMFConfig: () => getMFConfig,
33
33
  patchBundlerConfig: () => patchBundlerConfig,
34
34
  patchIgnoreWarning: () => patchIgnoreWarning,
35
- patchMFConfig: () => patchMFConfig
35
+ patchMFConfig: () => patchMFConfig,
36
+ replaceRemoteUrl: () => replaceRemoteUrl
36
37
  });
37
38
  module.exports = __toCommonJS(utils_exports);
38
39
  var import_sdk = require("@module-federation/sdk");
@@ -84,7 +85,9 @@ const replaceRemoteUrl = (mfConfig, remoteIpStrategy) => {
84
85
  if (typeof remoteObject === "string") {
85
86
  return;
86
87
  }
87
- handleRemoteObject(remoteObject);
88
+ if ("entry" in remoteObject && typeof remoteObject.entry === "string") {
89
+ remoteObject.entry = remoteObject.entry.replace(import_constant.LOCALHOST, ipv4);
90
+ }
88
91
  });
89
92
  } else if (typeof mfConfig.remotes !== "string") {
90
93
  handleRemoteObject(mfConfig.remotes);
@@ -139,7 +142,7 @@ const patchMFConfig = (mfConfig, isServer, remoteIpStrategy) => {
139
142
  injectRuntimePlugins(import_path.default.resolve(__dirname, "./mfRuntimePlugins/resolve-entry-ipv4.js"), runtimePlugins);
140
143
  }
141
144
  if (isServer) {
142
- injectRuntimePlugins(require.resolve("@module-federation/node/runtimePlugin"), runtimePlugins);
145
+ injectRuntimePlugins(require.resolve("./mfRuntimePlugins/node.js"), runtimePlugins);
143
146
  if (isDev) {
144
147
  injectRuntimePlugins(require.resolve("@module-federation/node/record-dynamic-remote-entry-hash-plugin"), runtimePlugins);
145
148
  }
@@ -329,5 +332,6 @@ function autoDeleteSplitChunkCacheGroups(mfConfig, bundlerConfig) {
329
332
  getMFConfig,
330
333
  patchBundlerConfig,
331
334
  patchIgnoreWarning,
332
- patchMFConfig
335
+ patchMFConfig,
336
+ replaceRemoteUrl
333
337
  });
@@ -61,7 +61,7 @@ const mfConfig = {
61
61
  remoteType: "script",
62
62
  runtimePlugins: [
63
63
  import_path.default.resolve(__dirname, "./mfRuntimePlugins/shared-strategy.js"),
64
- require.resolve("@module-federation/node/runtimePlugin"),
64
+ require.resolve("./mfRuntimePlugins/node.js"),
65
65
  import_path.default.resolve(__dirname, "./mfRuntimePlugins/inject-node-fetch.js")
66
66
  ],
67
67
  shared: {
@@ -18,17 +18,47 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var constant_exports = {};
20
20
  __export(constant_exports, {
21
+ DEFAULT_ENTRY: () => DEFAULT_ENTRY,
22
+ DEFAULT_LAYOUT: () => DEFAULT_LAYOUT,
23
+ FOLD_IDENTIFIER: () => FOLD_IDENTIFIER,
21
24
  LOCALHOST: () => LOCALHOST,
25
+ META_NAME: () => META_NAME,
26
+ MF_JSON: () => MF_JSON,
27
+ MF_ROUTES: () => MF_ROUTES,
28
+ MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME: () => MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME,
29
+ MODERN_JS_ROUTE_SERVER_LOADER: () => MODERN_JS_ROUTE_SERVER_LOADER,
22
30
  MODERN_JS_SERVER_DIR: () => MODERN_JS_SERVER_DIR,
23
- PLUGIN_IDENTIFIER: () => PLUGIN_IDENTIFIER
31
+ PLUGIN_IDENTIFIER: () => PLUGIN_IDENTIFIER,
32
+ SEPARATOR: () => SEPARATOR,
33
+ isDev: () => isDev
24
34
  });
25
35
  module.exports = __toCommonJS(constant_exports);
26
36
  const MODERN_JS_SERVER_DIR = "bundles";
27
37
  const LOCALHOST = "localhost";
28
38
  const PLUGIN_IDENTIFIER = "[ Modern.js Module Federation ]";
39
+ const isDev = process.env.NODE_ENV === "development";
40
+ const MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME = "routes";
41
+ const MODERN_JS_ROUTE_SERVER_LOADER = "route-server-loaders";
42
+ const META_NAME = "modern-js";
43
+ const FOLD_IDENTIFIER = "@mf";
44
+ const SEPARATOR = "_";
45
+ const MF_ROUTES = "mf-routes";
46
+ const DEFAULT_ENTRY = "main";
47
+ const DEFAULT_LAYOUT = "layout";
48
+ const MF_JSON = "mf.json";
29
49
  // Annotate the CommonJS export names for ESM import in node:
30
50
  0 && (module.exports = {
51
+ DEFAULT_ENTRY,
52
+ DEFAULT_LAYOUT,
53
+ FOLD_IDENTIFIER,
31
54
  LOCALHOST,
55
+ META_NAME,
56
+ MF_JSON,
57
+ MF_ROUTES,
58
+ MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME,
59
+ MODERN_JS_ROUTE_SERVER_LOADER,
32
60
  MODERN_JS_SERVER_DIR,
33
- PLUGIN_IDENTIFIER
61
+ PLUGIN_IDENTIFIER,
62
+ SEPARATOR,
63
+ isDev
34
64
  });
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
+ var route_exports = {};
16
+ module.exports = __toCommonJS(route_exports);
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var plugin_exports = {};
20
+ __export(plugin_exports, {
21
+ ssrDataLoaderPlugin: () => ssrDataLoaderPlugin
22
+ });
23
+ module.exports = __toCommonJS(plugin_exports);
24
+ var import_runtime = require("@module-federation/enhanced/runtime");
25
+ var import_constant = require("../../constant");
26
+ var import_utils = require("./utils");
27
+ const ssrDataLoaderPlugin = () => {
28
+ return {
29
+ name: "@modern-js/plugin-mf-data-loader",
30
+ post: [
31
+ "@modern-js/plugin-router"
32
+ ],
33
+ setup: () => {
34
+ const remoteRoutesMap = {};
35
+ return {
36
+ async beforeRender() {
37
+ console.log("init");
38
+ if (Object.keys(remoteRoutesMap).length) {
39
+ return;
40
+ }
41
+ const instance = (0, import_runtime.getInstance)();
42
+ if (!instance) {
43
+ console.log("no instance!");
44
+ return;
45
+ }
46
+ if (!instance.options.remotes.length) {
47
+ return;
48
+ }
49
+ await Promise.all(instance.options.remotes.map(async (remote) => {
50
+ const remoteId = `${remote.name}/${import_constant.DEFAULT_ENTRY}/${import_constant.MF_ROUTES}`;
51
+ const { routes } = await instance.loadRemote(remoteId);
52
+ remoteRoutesMap[remote.name] = {
53
+ routes
54
+ };
55
+ }));
56
+ },
57
+ // runtime plugin not save route , so it needs to inject again when trigger
58
+ modifyRoutes: (routes) => {
59
+ (0, import_utils.injectRemoteRoutes)({
60
+ entry: {
61
+ routes
62
+ }
63
+ }, remoteRoutesMap);
64
+ return routes;
65
+ }
66
+ };
67
+ }
68
+ };
69
+ };
70
+ // Annotate the CommonJS export names for ESM import in node:
71
+ 0 && (module.exports = {
72
+ ssrDataLoaderPlugin
73
+ });
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var utils_exports = {};
20
+ __export(utils_exports, {
21
+ getRemoteLayoutId: () => getRemoteLayoutId,
22
+ getRemoteRoutesInfos: () => getRemoteRoutesInfos,
23
+ injectRemoteRoutes: () => injectRemoteRoutes
24
+ });
25
+ module.exports = __toCommonJS(utils_exports);
26
+ var import_constant = require("../../constant");
27
+ var import_utils = require("../utils");
28
+ function getRemoteRoutesInfos(route, remoteRoutesMap) {
29
+ if (!route.id) {
30
+ return;
31
+ }
32
+ const remoteName = (0, import_utils.transformPrefix2Name)(route.id);
33
+ const remoteRouteObj = remoteRoutesMap[remoteName];
34
+ if (!remoteRouteObj) {
35
+ return;
36
+ }
37
+ const remoteRoutes = remoteRouteObj.routes[0];
38
+ return {
39
+ routes: remoteRoutes,
40
+ name: remoteName,
41
+ pathName: route.path
42
+ };
43
+ }
44
+ function getRemoteLayoutId(remoteName) {
45
+ const prefix = (0, import_utils.transformName2Prefix)(remoteName);
46
+ return `${prefix}${import_constant.DEFAULT_LAYOUT}`;
47
+ }
48
+ function injectRemoteRoutes(initialRoutes, remoteRoutesMap) {
49
+ const traverse = (route) => {
50
+ var _route_children;
51
+ const remoteRoutesInfos = getRemoteRoutesInfos(route, remoteRoutesMap);
52
+ if (remoteRoutesInfos) {
53
+ const { routes, pathName } = remoteRoutesInfos;
54
+ route.id = routes.id;
55
+ route.path = pathName;
56
+ if (routes.loader) {
57
+ route.loader = routes.loader;
58
+ }
59
+ if (routes.children) {
60
+ route.children = routes.children;
61
+ }
62
+ route.element = routes.element;
63
+ route.Component = routes.Component;
64
+ return;
65
+ }
66
+ (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.forEach((r) => {
67
+ traverse(r);
68
+ });
69
+ };
70
+ Object.keys(initialRoutes).forEach((entryName) => {
71
+ const routes = initialRoutes[entryName];
72
+ if (Array.isArray(routes)) {
73
+ routes.forEach((route) => {
74
+ traverse(route);
75
+ });
76
+ } else {
77
+ var _routes_routes;
78
+ routes === null || routes === void 0 ? void 0 : (_routes_routes = routes.routes) === null || _routes_routes === void 0 ? void 0 : _routes_routes.forEach((r) => {
79
+ traverse(r);
80
+ });
81
+ }
82
+ });
83
+ }
84
+ // Annotate the CommonJS export names for ESM import in node:
85
+ 0 && (module.exports = {
86
+ getRemoteLayoutId,
87
+ getRemoteRoutesInfos,
88
+ injectRemoteRoutes
89
+ });
@@ -19,13 +19,16 @@ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "defau
19
19
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
20
  var runtime_exports = {};
21
21
  __export(runtime_exports, {
22
- createRemoteSSRComponent: () => import_createRemoteSSRComponent.createRemoteSSRComponent
22
+ createRemoteSSRComponent: () => import_createRemoteSSRComponent.createRemoteSSRComponent,
23
+ withMFRouteId: () => import_withMFRouteId.withMFRouteId
23
24
  });
24
25
  module.exports = __toCommonJS(runtime_exports);
25
26
  __reExport(runtime_exports, require("@module-federation/enhanced/runtime"), module.exports);
26
27
  var import_createRemoteSSRComponent = require("./createRemoteSSRComponent");
28
+ var import_withMFRouteId = require("./withMFRouteId");
27
29
  // Annotate the CommonJS export names for ESM import in node:
28
30
  0 && (module.exports = {
29
31
  createRemoteSSRComponent,
32
+ withMFRouteId,
30
33
  ...require("@module-federation/enhanced/runtime")
31
34
  });
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var utils_exports = {};
20
+ __export(utils_exports, {
21
+ transformName2Prefix: () => transformName2Prefix,
22
+ transformPrefix2Name: () => transformPrefix2Name
23
+ });
24
+ module.exports = __toCommonJS(utils_exports);
25
+ var import_sdk = require("@module-federation/sdk");
26
+ const SPLIT_SYMBOL = "@";
27
+ function transformName2Prefix(name) {
28
+ return `${(0, import_sdk.encodeName)(name, "", false)}${SPLIT_SYMBOL}`;
29
+ }
30
+ function transformPrefix2Name(prefix) {
31
+ const realPrefix = prefix.split(SPLIT_SYMBOL)[0];
32
+ return (0, import_sdk.decodeName)(realPrefix, "", false);
33
+ }
34
+ // Annotate the CommonJS export names for ESM import in node:
35
+ 0 && (module.exports = {
36
+ transformName2Prefix,
37
+ transformPrefix2Name
38
+ });
@@ -16,13 +16,16 @@ var __copyProps = (to, from, except, desc) => {
16
16
  return to;
17
17
  };
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var constant_exports = {};
20
- __export(constant_exports, {
21
- isDev: () => isDev
19
+ var withMFRouteId_exports = {};
20
+ __export(withMFRouteId_exports, {
21
+ withMFRouteId: () => withMFRouteId
22
22
  });
23
- module.exports = __toCommonJS(constant_exports);
24
- const isDev = process.env.NODE_ENV === "development";
23
+ module.exports = __toCommonJS(withMFRouteId_exports);
24
+ function withMFRouteId(id) {
25
+ const prefix = typeof MODERN_ROUTER_ID_PREFIX === "string" ? MODERN_ROUTER_ID_PREFIX : "";
26
+ return prefix + id;
27
+ }
25
28
  // Annotate the CommonJS export names for ESM import in node:
26
29
  0 && (module.exports = {
27
- isDev
30
+ withMFRouteId
28
31
  });
@@ -34,15 +34,17 @@ module.exports = __toCommonJS(plugin_exports);
34
34
  var import_jsx_runtime = require("react/jsx-runtime");
35
35
  var import_hoist_non_react_statics = __toESM(require("hoist-non-react-statics"));
36
36
  var import_SSRLiveReload = require("./SSRLiveReload");
37
+ console.log("mfSSRPlugin trigger");
37
38
  const mfSSRPlugin = () => ({
38
39
  name: "@module-federation/modern-js",
40
+ pre: [
41
+ "@modern-js/plugin-router"
42
+ ],
39
43
  setup: () => {
40
44
  return {
41
- async init({ context }, next) {
45
+ async beforeRender() {
42
46
  if (typeof window !== "undefined") {
43
- return next({
44
- context
45
- });
47
+ return;
46
48
  }
47
49
  globalThis.shouldUpdate = false;
48
50
  const nodeUtils = await Promise.resolve().then(() => __toESM(require("@module-federation/node/utils")));
@@ -52,9 +54,7 @@ const mfSSRPlugin = () => ({
52
54
  await nodeUtils.flushChunks();
53
55
  globalThis.shouldUpdate = true;
54
56
  }
55
- return next({
56
- context
57
- });
57
+ return;
58
58
  },
59
59
  wrapRoot(App) {
60
60
  const AppWrapper = (props) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, {
@@ -0,0 +1,18 @@
1
+ var IS_ROOT = "isRoot";
2
+ var ID = "id";
3
+ var COMPONENT = "component";
4
+ var LAZY_COMPONENT = "lazyImport";
5
+ var SHOULD_REVALIDATE = "shouldRevalidate";
6
+ var PRIVATE_COMPONENT = "_component";
7
+ var ELEMENT = "element";
8
+ var 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,109 @@
1
+ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
2
+ import fs from "fs";
3
+ import traverse from "@babel/traverse";
4
+ import * as babelParser from "@babel/parser";
5
+ import generate from "@babel/generator";
6
+ import * as t from "@babel/types";
7
+ function findTargetKeyNode(nodeProperties, key) {
8
+ return nodeProperties.find(function(p) {
9
+ return t.isObjectProperty(p) && t.isStringLiteral(p.key) && p.key.value === key;
10
+ });
11
+ }
12
+ function generateRoutes(param) {
13
+ var sourceCode = param.sourceCode, filePath = param.filePath, prefix = param.prefix, baseName = param.baseName;
14
+ var ast = babelParser.parse(sourceCode, {
15
+ sourceType: "module"
16
+ });
17
+ var lazyComponentDeclarations = [];
18
+ var componentDeclarations = [];
19
+ var componentId = 0;
20
+ traverse(ast, {
21
+ // ImportDeclaration(path) {
22
+ // const source = path.node.source.value;
23
+ // const routeIdMatch = source.match(/routeId=([^&]+)/);
24
+ // if (routeIdMatch) {
25
+ // const originalRouteId = routeIdMatch[1];
26
+ // const newRouteId = `${prefix}${originalRouteId}`;
27
+ // const newSource = source.replace(
28
+ // /routeId=[^&]+/,
29
+ // `routeId=${newRouteId}`,
30
+ // );
31
+ // path.node.source = t.stringLiteral(newSource);
32
+ // }
33
+ // },
34
+ ObjectExpression: function ObjectExpression(path) {
35
+ var componentName = "";
36
+ var lazyComponentName = "";
37
+ if (!Array.isArray(path.node.properties)) {
38
+ return;
39
+ }
40
+ var idNode = findTargetKeyNode(path.node.properties, "id");
41
+ if (idNode && t.isObjectProperty(idNode) && t.isStringLiteral(idNode.value)) {
42
+ }
43
+ var isRootNode = findTargetKeyNode(path.node.properties, "isRoot");
44
+ if (isRootNode && t.isObjectProperty(isRootNode) && t.isBooleanLiteral(isRootNode.value)) {
45
+ isRootNode.value.value = false;
46
+ }
47
+ if (!isRootNode) {
48
+ var lazyComponentNode = findTargetKeyNode(path.node.properties, "lazyImport");
49
+ if (lazyComponentNode && t.isObjectProperty(lazyComponentNode) && t.isArrowFunctionExpression(lazyComponentNode.value)) {
50
+ lazyComponentName = "LazyComponent_".concat(componentId);
51
+ var lazyDeclaration = t.variableDeclaration("const", [
52
+ t.variableDeclarator(t.identifier(lazyComponentName), lazyComponentNode.value)
53
+ ]);
54
+ lazyComponentNode.value = t.identifier(lazyComponentName);
55
+ var componentNode = findTargetKeyNode(path.node.properties, "component");
56
+ if (componentNode && t.isObjectProperty(componentNode) && t.isCallExpression(componentNode.value) && t.isIdentifier(componentNode.value.callee)) {
57
+ componentNode.value = t.callExpression(t.identifier("lazy"), [
58
+ t.identifier(lazyComponentName)
59
+ ]);
60
+ }
61
+ lazyComponentDeclarations.push(lazyDeclaration);
62
+ }
63
+ }
64
+ var componentNode1 = findTargetKeyNode(path.node.properties, "component");
65
+ if (componentNode1 && t.isObjectProperty(componentNode1) && t.isCallExpression(componentNode1.value) && t.isIdentifier(componentNode1.value.callee)) {
66
+ componentName = "Component_".concat(componentId);
67
+ var componentDeclaration = t.variableDeclaration("const", [
68
+ t.variableDeclarator(t.identifier(componentName), lazyComponentName ? t.callExpression(t.identifier("lazy"), [
69
+ t.identifier(lazyComponentName)
70
+ ]) : componentNode1.value)
71
+ ]);
72
+ componentDeclarations.push(componentDeclaration);
73
+ componentNode1.value = t.identifier(componentName);
74
+ }
75
+ if (lazyComponentName || componentName) {
76
+ componentId++;
77
+ if (componentName) {
78
+ var upperFirstName = componentName.slice(0, 1).toUpperCase() + componentName.slice(1);
79
+ var jsxElement = t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier(upperFirstName), [], true), null, [], true);
80
+ path.node.properties.push(t.objectProperty(t.identifier("element"), jsxElement));
81
+ }
82
+ }
83
+ }
84
+ });
85
+ traverse(ast, {
86
+ Program: function Program(path) {
87
+ var lastImportIndex = path.get("body").reduce(function(lastIndex, p, index) {
88
+ if (t.isImportDeclaration(p.node)) {
89
+ lastIndex = index;
90
+ }
91
+ return lastIndex;
92
+ }, -1);
93
+ if (lastImportIndex >= 0) {
94
+ var lastImport = path.get("body.".concat(lastImportIndex));
95
+ _to_consumable_array(componentDeclarations).concat(_to_consumable_array(lazyComponentDeclarations)).forEach(function(declaration) {
96
+ if ("insertAfter" in lastImport) {
97
+ lastImport.insertAfter(declaration);
98
+ }
99
+ });
100
+ }
101
+ }
102
+ });
103
+ var _generate = generate(ast), newCode = _generate.code;
104
+ var finalCode = "".concat(newCode, "export const baseName = '").concat(baseName, "';");
105
+ fs.writeFileSync(filePath, finalCode);
106
+ }
107
+ export {
108
+ generateRoutes
109
+ };
@@ -0,0 +1,17 @@
1
+ import fs from "fs";
2
+ import * as t from "@babel/types";
3
+ function findTargetKeyNode(nodeProperties, key) {
4
+ return nodeProperties.find(function(p) {
5
+ return t.isObjectProperty(p) && t.isStringLiteral(p.key) && p.key.value === key;
6
+ });
7
+ }
8
+ function generateSelfRoutes(param) {
9
+ var sourceCode = param.sourceCode, filePath = param.filePath, prefix = param.prefix, baseName = param.baseName;
10
+ var lazyComponentDeclarations = [];
11
+ var componentDeclarations = [];
12
+ var componentId = 0;
13
+ fs.writeFileSync(filePath, sourceCode);
14
+ }
15
+ export {
16
+ generateSelfRoutes
17
+ };
@@ -0,0 +1,57 @@
1
+ import traverse from "@babel/traverse";
2
+ import * as babelParser from "@babel/parser";
3
+ import generate from "@babel/generator";
4
+ import * as t from "@babel/types";
5
+ import { COMPONENT, ID, SHOULD_REVALIDATE, LAZY_COMPONENT, PRIVATE_COMPONENT, LOADER } from "./constant";
6
+ function generateSerializableRoutes(param) {
7
+ var sourceCode = param.sourceCode, prefix = param.prefix;
8
+ var ast = babelParser.parse(sourceCode, {
9
+ sourceType: "module"
10
+ });
11
+ var removedKeys = [
12
+ COMPONENT,
13
+ SHOULD_REVALIDATE,
14
+ LAZY_COMPONENT,
15
+ PRIVATE_COMPONENT,
16
+ LOADER
17
+ ];
18
+ traverse(ast, {
19
+ ObjectExpression: function ObjectExpression(path1) {
20
+ if (!Array.isArray(path1.node.properties)) {
21
+ return;
22
+ }
23
+ path1.node.properties.forEach(function(prop1) {
24
+ if (t.isObjectProperty(prop1) && t.isStringLiteral(prop1.key) && t.isStringLiteral(prop1.value) && prop1.key.value === ID) {
25
+ }
26
+ });
27
+ path1.node.properties = path1.node.properties.filter(function(p1) {
28
+ if (t.isObjectProperty(p1) && t.isStringLiteral(p1.key)) {
29
+ return !removedKeys.includes(p1.key.value);
30
+ } else {
31
+ return true;
32
+ }
33
+ });
34
+ }
35
+ });
36
+ var routesValue = "";
37
+ traverse(ast, {
38
+ VariableDeclarator: function VariableDeclarator(path) {
39
+ if (t.isVariableDeclarator(path.node) && t.isIdentifier(path.node.id) && path.node.id.name === "routes") {
40
+ var routesAst = path.node.init;
41
+ if (!routesAst) {
42
+ return;
43
+ }
44
+ var code = generate(routesAst, {
45
+ compact: true,
46
+ retainLines: false,
47
+ concise: true
48
+ }).code;
49
+ routesValue = eval("(" + code + ")");
50
+ }
51
+ }
52
+ });
53
+ return routesValue;
54
+ }
55
+ export {
56
+ generateSerializableRoutes
57
+ };
@@ -0,0 +1,73 @@
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
+ import { COMPONENT, SHOULD_REVALIDATE, LAZY_COMPONENT, PRIVATE_COMPONENT } from "./constant";
7
+ function generateSlimRoutes(param) {
8
+ var sourceCode = param.sourceCode, filePath = param.filePath, prefix = param.prefix, baseName = param.baseName;
9
+ var ast = babelParser.parse(sourceCode, {
10
+ sourceType: "module"
11
+ });
12
+ var removedKeys = [
13
+ COMPONENT,
14
+ SHOULD_REVALIDATE,
15
+ LAZY_COMPONENT,
16
+ PRIVATE_COMPONENT
17
+ ];
18
+ traverse(ast, {
19
+ // ImportDeclaration(path) {
20
+ // const source = path.node.source.value;
21
+ // const routeIdMatch = source.match(/routeId=([^&]+)/);
22
+ // if (routeIdMatch) {
23
+ // const originalRouteId = routeIdMatch[1];
24
+ // const newRouteId = `${prefix}${originalRouteId}`;
25
+ // const newSource = source.replace(
26
+ // /routeId=[^&]+/,
27
+ // `routeId=${newRouteId}`,
28
+ // );
29
+ // path.node.source = t.stringLiteral(newSource);
30
+ // }
31
+ // },
32
+ ObjectExpression: function ObjectExpression(path) {
33
+ if (!Array.isArray(path.node.properties)) {
34
+ return;
35
+ }
36
+ path.node.properties = path.node.properties.filter(function(p) {
37
+ if (t.isObjectProperty(p) && t.isStringLiteral(p.key)) {
38
+ return !removedKeys.includes(p.key.value);
39
+ } else {
40
+ return true;
41
+ }
42
+ });
43
+ }
44
+ });
45
+ var tempCode = generate(ast).code;
46
+ var tempAst = babelParser.parse(tempCode, {
47
+ sourceType: "module"
48
+ });
49
+ var usedIdentifiers = /* @__PURE__ */ new Set();
50
+ traverse(tempAst, {
51
+ Identifier: function Identifier(path) {
52
+ if (t.isProperty(path.parent)) {
53
+ usedIdentifiers.add(path.node.name);
54
+ }
55
+ }
56
+ });
57
+ traverse(tempAst, {
58
+ ImportDeclaration: function ImportDeclaration(path) {
59
+ path.node.specifiers = path.node.specifiers.filter(function(specifier) {
60
+ return usedIdentifiers.has(specifier.local.name);
61
+ });
62
+ if (!path.node.specifiers.length) {
63
+ path.remove();
64
+ }
65
+ }
66
+ });
67
+ var _generate = generate(tempAst), newCode = _generate.code;
68
+ var finalCode = "".concat(newCode, "export const baseName = '").concat(baseName, "';");
69
+ fs.writeFileSync(filePath, finalCode);
70
+ }
71
+ export {
72
+ generateSlimRoutes
73
+ };