@module-federation/modern-js 0.0.0-next-20241106033151 → 0.0.0-next-20241106063644

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 (115) hide show
  1. package/dist/cjs/cli/index.js +7 -2
  2. package/dist/cjs/cli/mfRuntimePlugins/{inject-node-fetch.js → node-fetch.js} +6 -6
  3. package/dist/cjs/cli/mfRuntimePlugins/node.js +41 -0
  4. package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +40 -38
  5. package/dist/cjs/cli/routes/ast/constant.js +49 -0
  6. package/dist/cjs/cli/routes/ast/generateRoutes.js +143 -0
  7. package/dist/cjs/cli/{constant.js → routes/ast/index.js} +6 -6
  8. package/dist/cjs/cli/routes/clearMFCache.js +57 -0
  9. package/dist/cjs/cli/routes/exportRoutePlugin.js +77 -0
  10. package/dist/cjs/cli/routes/importRoutesPlugin.js +120 -0
  11. package/dist/cjs/cli/routes/plugin.js +157 -0
  12. package/dist/cjs/cli/routes/traverseRouteFiles.js +148 -0
  13. package/dist/cjs/cli/routes/utils.js +168 -0
  14. package/dist/cjs/cli/server/fetchRemoteFullRoutesMap.js +72 -0
  15. package/dist/cjs/cli/server/fetchRoutePlugin.js +82 -0
  16. package/dist/cjs/cli/ssrPlugin.js +5 -2
  17. package/dist/cjs/cli/utils.js +11 -6
  18. package/dist/cjs/cli/utils.spec.js +5 -6
  19. package/dist/cjs/constant.js +35 -2
  20. package/dist/cjs/interfaces/route.js +16 -0
  21. package/dist/cjs/runtime/index.js +4 -1
  22. package/dist/cjs/runtime/routes/plugin-inject-assets.js +92 -0
  23. package/dist/cjs/runtime/routes/plugin.js +75 -0
  24. package/dist/cjs/runtime/routes/utils.js +115 -0
  25. package/dist/cjs/{cli/mfRuntimePlugins/shared-strategy.js → runtime/withMFRouteId.js} +11 -23
  26. package/dist/cjs/ssr-runtime/plugin.js +7 -8
  27. package/dist/cjs/types/modern.js +16 -0
  28. package/dist/cjs/types/routes.js +16 -0
  29. package/dist/esm/cli/index.js +7 -2
  30. package/dist/esm/cli/mfRuntimePlugins/{inject-node-fetch.js → node-fetch.js} +3 -3
  31. package/dist/esm/cli/mfRuntimePlugins/node.js +13 -0
  32. package/dist/esm/cli/routes/ast/constant.js +18 -0
  33. package/dist/esm/cli/routes/ast/generateRoutes.js +110 -0
  34. package/dist/esm/cli/routes/ast/index.js +4 -0
  35. package/dist/esm/cli/routes/clearMFCache.js +34 -0
  36. package/dist/esm/cli/routes/exportRoutePlugin.js +79 -0
  37. package/dist/esm/cli/routes/importRoutesPlugin.js +122 -0
  38. package/dist/esm/cli/routes/plugin.js +266 -0
  39. package/dist/esm/cli/routes/traverseRouteFiles.js +313 -0
  40. package/dist/esm/cli/routes/utils.js +130 -0
  41. package/dist/esm/cli/server/fetchRemoteFullRoutesMap.js +93 -0
  42. package/dist/esm/cli/server/fetchRoutePlugin.js +105 -0
  43. package/dist/esm/cli/ssrPlugin.js +6 -3
  44. package/dist/esm/cli/utils.js +9 -5
  45. package/dist/esm/cli/utils.spec.js +5 -6
  46. package/dist/esm/constant.js +23 -1
  47. package/dist/esm/interfaces/route.js +0 -0
  48. package/dist/esm/runtime/index.js +3 -1
  49. package/dist/esm/runtime/routes/plugin-inject-assets.js +71 -0
  50. package/dist/esm/runtime/routes/plugin.js +97 -0
  51. package/dist/esm/runtime/routes/utils.js +86 -0
  52. package/dist/esm/runtime/withMFRouteId.js +7 -0
  53. package/dist/esm/ssr-runtime/plugin.js +8 -11
  54. package/dist/esm/types/modern.js +0 -0
  55. package/dist/esm/types/routes.js +0 -0
  56. package/dist/esm-node/cli/index.js +7 -2
  57. package/dist/esm-node/cli/mfRuntimePlugins/{inject-node-fetch.js → node-fetch.js} +3 -3
  58. package/dist/esm-node/cli/mfRuntimePlugins/node.js +11 -0
  59. package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +40 -38
  60. package/dist/esm-node/cli/routes/ast/constant.js +18 -0
  61. package/dist/esm-node/cli/routes/ast/generateRoutes.js +109 -0
  62. package/dist/esm-node/cli/routes/ast/index.js +4 -0
  63. package/dist/esm-node/cli/routes/clearMFCache.js +33 -0
  64. package/dist/esm-node/cli/routes/exportRoutePlugin.js +53 -0
  65. package/dist/esm-node/cli/routes/importRoutesPlugin.js +96 -0
  66. package/dist/esm-node/cli/routes/plugin.js +123 -0
  67. package/dist/esm-node/cli/routes/traverseRouteFiles.js +113 -0
  68. package/dist/esm-node/cli/routes/utils.js +133 -0
  69. package/dist/esm-node/cli/server/fetchRemoteFullRoutesMap.js +47 -0
  70. package/dist/esm-node/cli/server/fetchRoutePlugin.js +62 -0
  71. package/dist/esm-node/cli/ssrPlugin.js +5 -2
  72. package/dist/esm-node/cli/utils.js +9 -5
  73. package/dist/esm-node/cli/utils.spec.js +5 -6
  74. package/dist/esm-node/constant.js +23 -1
  75. package/dist/esm-node/interfaces/route.js +0 -0
  76. package/dist/esm-node/runtime/index.js +3 -1
  77. package/dist/esm-node/runtime/routes/plugin-inject-assets.js +68 -0
  78. package/dist/esm-node/runtime/routes/plugin.js +51 -0
  79. package/dist/esm-node/runtime/routes/utils.js +86 -0
  80. package/dist/esm-node/runtime/withMFRouteId.js +7 -0
  81. package/dist/esm-node/ssr-runtime/plugin.js +7 -8
  82. package/dist/esm-node/types/modern.js +0 -0
  83. package/dist/esm-node/types/routes.js +0 -0
  84. package/dist/types/cli/mfRuntimePlugins/node.d.ts +3 -0
  85. package/dist/types/cli/routes/ast/constant.d.ts +8 -0
  86. package/dist/types/cli/routes/ast/generateRoutes.d.ts +5 -0
  87. package/dist/types/cli/routes/ast/index.d.ts +1 -0
  88. package/dist/types/cli/routes/clearMFCache.d.ts +1 -0
  89. package/dist/types/cli/routes/exportRoutePlugin.d.ts +4 -0
  90. package/dist/types/cli/routes/importRoutesPlugin.d.ts +4 -0
  91. package/dist/types/cli/routes/plugin.d.ts +4 -0
  92. package/dist/types/cli/routes/traverseRouteFiles.d.ts +11 -0
  93. package/dist/types/cli/routes/utils.d.ts +11 -0
  94. package/dist/types/cli/server/fetchRemoteFullRoutesMap.d.ts +11 -0
  95. package/dist/types/cli/server/fetchRoutePlugin.d.ts +10 -0
  96. package/dist/types/cli/utils.d.ts +2 -0
  97. package/dist/types/constant.d.ts +11 -0
  98. package/dist/types/interfaces/route.d.ts +12 -0
  99. package/dist/types/runtime/index.d.ts +1 -0
  100. package/dist/types/runtime/routes/plugin-inject-assets.d.ts +4 -0
  101. package/dist/types/runtime/routes/plugin.d.ts +5 -0
  102. package/dist/types/runtime/routes/utils.d.ts +22 -0
  103. package/dist/types/runtime/withMFRouteId.d.ts +1 -0
  104. package/dist/types/ssr-runtime/plugin.d.ts +3 -1
  105. package/dist/types/types/index.d.ts +3 -1
  106. package/dist/types/types/modern.d.ts +12 -0
  107. package/dist/types/types/routes.d.ts +19 -0
  108. package/package.json +42 -5
  109. package/dist/esm/cli/constant.js +0 -4
  110. package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +0 -25
  111. package/dist/esm-node/cli/constant.js +0 -4
  112. package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +0 -23
  113. package/dist/types/cli/constant.d.ts +0 -1
  114. package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +0 -3
  115. /package/dist/types/cli/mfRuntimePlugins/{inject-node-fetch.d.ts → node-fetch.d.ts} +0 -0
@@ -0,0 +1,157 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var plugin_exports = {};
30
+ __export(plugin_exports, {
31
+ default: () => plugin_default,
32
+ moduleFederationRoutesPlugin: () => moduleFederationRoutesPlugin
33
+ });
34
+ module.exports = __toCommonJS(plugin_exports);
35
+ var import_path = __toESM(require("path"));
36
+ var import_utils = require("@modern-js/utils");
37
+ var import_utils2 = require("../../runtime/routes/utils");
38
+ var import_constant = require("../../constant");
39
+ var import_traverseRouteFiles = require("./traverseRouteFiles");
40
+ var import_exportRoutePlugin = require("./exportRoutePlugin");
41
+ var import_importRoutesPlugin = require("./importRoutesPlugin");
42
+ var import_utils3 = require("./utils");
43
+ var import_constant2 = require("../../constant");
44
+ const moduleFederationRoutesPlugin = ({ userConfig, internalOptions, serverPlugin, transformRuntimeOptions }) => {
45
+ const entries = /* @__PURE__ */ new Set();
46
+ const remotePathMap = {};
47
+ const ssrByRouteIdsMap = {};
48
+ const internalRoutesPluginOptions = {
49
+ userConfig,
50
+ internalOptions,
51
+ entries,
52
+ remotePathMap,
53
+ ssrByRouteIdsMap,
54
+ serverPlugin,
55
+ transformRuntimeOptions
56
+ };
57
+ return {
58
+ name: "@modern-js/plugin-module-federation-route",
59
+ pre: [
60
+ "@modern-js/plugin-module-federation-config"
61
+ ],
62
+ setup: async ({ useAppContext }) => {
63
+ if (!userConfig.exportRoutes && !userConfig.importRoutes) {
64
+ return;
65
+ }
66
+ const appContext = useAppContext();
67
+ const name = internalOptions.csrConfig.name;
68
+ const { metaName = import_constant.META_NAME } = internalOptions;
69
+ const internalDirectory = appContext.internalDirectory.replace(import_constant.META_NAME, metaName || import_constant.META_NAME);
70
+ const csrConfig = internalOptions.csrConfig;
71
+ if (typeof csrConfig.remotes === "object" && Object.keys(csrConfig.remotes).length) {
72
+ const { entries: scanEntries } = await (0, import_traverseRouteFiles.traverseRouteFiles)({
73
+ appDirectory: appContext.appDirectory,
74
+ generateRouteFile: userConfig.importRoutes,
75
+ remotePathMap
76
+ });
77
+ scanEntries.forEach((e) => entries.add(e));
78
+ } else {
79
+ entries.add(import_constant2.DEFAULT_ENTRY);
80
+ }
81
+ return {
82
+ config: async () => {
83
+ return {
84
+ tools: {
85
+ // bundlerChain can not keep target order
86
+ rspack(_config, { isServer }) {
87
+ (0, import_utils3.addShared)({
88
+ mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
89
+ metaName,
90
+ isServer,
91
+ internalDirectory,
92
+ entries
93
+ });
94
+ },
95
+ // bundlerChain can not keep target order
96
+ webpack(_config, { isServer }) {
97
+ (0, import_utils3.addShared)({
98
+ mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
99
+ metaName,
100
+ isServer,
101
+ internalDirectory,
102
+ entries
103
+ });
104
+ }
105
+ },
106
+ source: {
107
+ define: {
108
+ MODERN_ROUTER_ID_PREFIX: JSON.stringify((0, import_utils2.transformName2Prefix)(name))
109
+ }
110
+ }
111
+ };
112
+ },
113
+ async modifyFileSystemRoutes(options) {
114
+ const { routes, entrypoint } = options;
115
+ const { entryName } = entrypoint;
116
+ const prefix = (0, import_utils2.transformName2Prefix)(internalOptions.csrConfig.name);
117
+ const modifyRouteIds = async (route, pathName) => {
118
+ const currentPathName = route.path ? import_path.default.join("/", pathName, route.path) : pathName;
119
+ if (route.id) {
120
+ const originalId = route.id;
121
+ route.originalId = originalId;
122
+ const remoteInfo = remotePathMap[currentPathName];
123
+ if (remoteInfo) {
124
+ const { path: path2, name: name2 } = remoteInfo;
125
+ const remoteIdPrefix = (0, import_utils2.transformName2Prefix)(name2);
126
+ const isLayout = Boolean(route.children);
127
+ const newId = `${remoteIdPrefix}${isLayout ? "layout" : "page"}`;
128
+ ssrByRouteIdsMap[originalId] = newId;
129
+ const content = import_utils.fs.readFileSync(path2, "utf-8");
130
+ await import_utils.fs.writeFile(path2, content.replace(import_constant2.ROUTE_ID, originalId));
131
+ route.id = newId;
132
+ } else {
133
+ route.id = `${prefix}${route.id}`;
134
+ }
135
+ route.children && await Promise.all(route.children.map(async (r) => {
136
+ return await modifyRouteIds(r, currentPathName);
137
+ }));
138
+ }
139
+ };
140
+ await Promise.all(routes.map(async (route) => {
141
+ await modifyRouteIds(route, entryName === import_constant2.DEFAULT_ENTRY ? "" : entryName);
142
+ }));
143
+ return options;
144
+ }
145
+ };
146
+ },
147
+ usePlugins: [
148
+ (0, import_exportRoutePlugin.moduleFederationExportRoutePlugin)(internalRoutesPluginOptions),
149
+ (0, import_importRoutesPlugin.moduleFederationImportRoutePlugin)(internalRoutesPluginOptions)
150
+ ]
151
+ };
152
+ };
153
+ var plugin_default = moduleFederationRoutesPlugin;
154
+ // Annotate the CommonJS export names for ESM import in node:
155
+ 0 && (module.exports = {
156
+ moduleFederationRoutesPlugin
157
+ });
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var traverseRouteFiles_exports = {};
30
+ __export(traverseRouteFiles_exports, {
31
+ calcPathName: () => calcPathName,
32
+ traverseRouteFiles: () => traverseRouteFiles
33
+ });
34
+ module.exports = __toCommonJS(traverseRouteFiles_exports);
35
+ var import_utils = require("@modern-js/utils");
36
+ var import_path = __toESM(require("path"));
37
+ var import_os = __toESM(require("os"));
38
+ var import_constant = require("../../constant");
39
+ var import_utils2 = require("../../runtime/routes/utils");
40
+ const ROUTES = "routes";
41
+ function calcPathName(filePath, rootDir, entry) {
42
+ const relativePath = import_path.default.relative(rootDir, filePath);
43
+ const conditionalReg = /^\[(.*)\]$/;
44
+ const splitPaths = relativePath.split("/").reduce((sum, cur) => {
45
+ if (cur.includes(".")) {
46
+ const split = cur.split(".").map((i) => {
47
+ const match = i.match(conditionalReg);
48
+ if (match) {
49
+ return `:${match[1]}`;
50
+ }
51
+ return i.startsWith("__") ? "" : i;
52
+ });
53
+ sum.push(...split);
54
+ } else {
55
+ const match = cur.match(conditionalReg);
56
+ if (match) {
57
+ sum.push(`:${match[1]}`);
58
+ } else {
59
+ if (!cur.startsWith("__")) {
60
+ sum.push(cur);
61
+ }
62
+ }
63
+ }
64
+ return sum;
65
+ }, []);
66
+ return import_path.default.join("/", entry, splitPaths.filter((p) => !!p).join("/"));
67
+ }
68
+ async function traverseRouteFiles(options) {
69
+ const { appDirectory, generateRouteFile, remotePathMap } = options;
70
+ const entries = [];
71
+ const srcPath = `${appDirectory}/src`;
72
+ const traverse = async (filepath, rootDir, entry) => {
73
+ if (!await import_utils.fs.pathExists(filepath)) {
74
+ return null;
75
+ }
76
+ const isDirectory = (await import_utils.fs.stat(filepath)).isDirectory();
77
+ if (!isDirectory) {
78
+ return null;
79
+ }
80
+ const items = await import_utils.fs.readdir(filepath);
81
+ const mfJson = items.find((i) => i === import_constant.MF_JSON);
82
+ if (mfJson) {
83
+ const mfJsonContent = await import_utils.fs.readJSON((0, import_utils.normalizeToPosixPath)(import_path.default.join(filepath, mfJson)));
84
+ const { remoteName } = mfJsonContent;
85
+ const remoteLayoutId = (0, import_utils2.getRemoteLayoutId)(remoteName);
86
+ const writeFile = async (id, type, pathname) => {
87
+ const generatedFilePath = (0, import_utils.normalizeToPosixPath)(import_path.default.join(filepath, `${type}.tsx`));
88
+ const content = [
89
+ "// Do not edit this file. It is a auto generated.",
90
+ `// It will render ${remoteName}'s ${type}.`,
91
+ `// The ${type} pathname is: ${pathname}`,
92
+ `// The ${type} route id is: ${import_constant.ROUTE_ID}`
93
+ ];
94
+ await import_utils.fs.writeFile(generatedFilePath, content.join(import_os.default.EOL));
95
+ return generatedFilePath;
96
+ };
97
+ if (remoteLayoutId) {
98
+ const pathname = calcPathName(filepath, rootDir, entry);
99
+ const filePath = await writeFile(remoteLayoutId, "layout", pathname);
100
+ remotePathMap[pathname] = {
101
+ name: remoteName,
102
+ path: filePath
103
+ };
104
+ }
105
+ return;
106
+ }
107
+ await Promise.all(items.map(async (item) => {
108
+ await traverse((0, import_utils.normalizeToPosixPath)(import_path.default.join(filepath, item)), rootDir, entry);
109
+ }));
110
+ };
111
+ const traverseRoutes = async (dir, level = 1) => {
112
+ if (level > 3) {
113
+ return;
114
+ }
115
+ const isDirectory = (await import_utils.fs.stat(dir)).isDirectory();
116
+ if (!isDirectory) {
117
+ return;
118
+ }
119
+ const items = await import_utils.fs.readdir(dir);
120
+ if (items.find((i) => i === ROUTES)) {
121
+ const entry = import_path.default.relative(srcPath, dir);
122
+ entries.push(entry);
123
+ const rootDir = `${dir}/${ROUTES}`;
124
+ if (generateRouteFile) {
125
+ await traverse(rootDir, rootDir, entry);
126
+ }
127
+ } else {
128
+ await Promise.all(items.map(async (item) => {
129
+ await traverseRoutes(import_path.default.join(dir, item), level++);
130
+ }));
131
+ }
132
+ };
133
+ await traverseRoutes(srcPath);
134
+ const normalizedEntries = entries.map((entry) => {
135
+ if (entry === "") {
136
+ return import_constant.DEFAULT_ENTRY;
137
+ }
138
+ return entry;
139
+ });
140
+ return {
141
+ entries: normalizedEntries
142
+ };
143
+ }
144
+ // Annotate the CommonJS export names for ESM import in node:
145
+ 0 && (module.exports = {
146
+ calcPathName,
147
+ traverseRouteFiles
148
+ });
@@ -0,0 +1,168 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var utils_exports = {};
30
+ __export(utils_exports, {
31
+ addExpose: () => addExpose,
32
+ addShared: () => addShared
33
+ });
34
+ module.exports = __toCommonJS(utils_exports);
35
+ var import_path = __toESM(require("path"));
36
+ var import_utils = require("@modern-js/utils");
37
+ var import_constant = require("../../constant");
38
+ var import_constant2 = require("../../constant");
39
+ var import_ast = require("./ast");
40
+ function generateExtraExposeFiles(options) {
41
+ const { internalDirectory, mfConfig, isServer, entries } = options;
42
+ const entryMap = {};
43
+ const outputDir = import_path.default.resolve(process.cwd(), "node_modules/.federation/data-loader");
44
+ import_utils.fs.ensureDirSync(outputDir);
45
+ const addSuffix = (fileName, ext = ".jsx") => {
46
+ if (!isServer) {
47
+ return `${fileName}${ext}`;
48
+ }
49
+ return `${fileName}.server${ext}`;
50
+ };
51
+ const generateEntryRoutes = (entry) => {
52
+ const outputEntryDir = import_path.default.resolve(outputDir, entry);
53
+ import_utils.fs.ensureDirSync(outputEntryDir);
54
+ const sourceDir = import_path.default.resolve(internalDirectory, entry);
55
+ const routesFilePath = import_path.default.resolve(sourceDir, addSuffix(import_constant2.MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME, ".js"));
56
+ const routesFileContent = import_utils.fs.readFileSync(routesFilePath, "utf-8");
57
+ const outputFullRoutesPath = import_path.default.resolve(outputEntryDir, addSuffix(import_constant2.MF_ROUTES));
58
+ (0, import_ast.generateRoutes)({
59
+ sourceCode: routesFileContent,
60
+ filePath: outputFullRoutesPath
61
+ });
62
+ const routeServerLoaderPath = import_path.default.resolve(sourceDir, `${import_constant.MODERN_JS_ROUTE_SERVER_LOADER}.js`);
63
+ const outputRouteServerLoaderPath = import_path.default.resolve(outputEntryDir, `${import_constant.MODERN_JS_ROUTE_SERVER_LOADER}.js`);
64
+ const clientRouteServerLoaderPath = outputRouteServerLoaderPath.replace(import_constant.MODERN_JS_ROUTE_SERVER_LOADER, `${import_constant.MODERN_JS_ROUTE_SERVER_LOADER}-client`);
65
+ if (isServer) {
66
+ const routeServerLoaderContent = import_utils.fs.readFileSync(routeServerLoaderPath, "utf-8");
67
+ (0, import_ast.generateRoutes)({
68
+ sourceCode: routeServerLoaderContent,
69
+ filePath: outputRouteServerLoaderPath
70
+ });
71
+ } else {
72
+ import_utils.fs.writeFileSync(clientRouteServerLoaderPath, `export const routes = []`);
73
+ }
74
+ entryMap[entry] = {
75
+ routesPath: outputFullRoutesPath,
76
+ routeServerLoaderPath: outputRouteServerLoaderPath,
77
+ clientRouteServerLoaderPath
78
+ };
79
+ };
80
+ entries.forEach((entry) => {
81
+ generateEntryRoutes(entry);
82
+ });
83
+ return {
84
+ entryMap
85
+ };
86
+ }
87
+ function addExpose(options) {
88
+ const { mfConfig, isServer } = options;
89
+ const { entryMap } = generateExtraExposeFiles(options);
90
+ const addExposeByEntry = (entry, routesPath, routeServerLoaderPath, clientRouteServerLoaderPath) => {
91
+ if (!mfConfig.exposes) {
92
+ mfConfig.exposes = {};
93
+ }
94
+ const routesKey = `./${entry}/${import_constant2.MF_ROUTES}`;
95
+ if (!mfConfig.exposes[routesKey]) {
96
+ mfConfig.exposes[routesKey] = routesPath;
97
+ }
98
+ const routeServerLoaderKey = `./${entry}/${import_constant.MODERN_JS_ROUTE_SERVER_LOADER}`;
99
+ if (!mfConfig.exposes[routeServerLoaderKey]) {
100
+ if (isServer) {
101
+ mfConfig.exposes[routeServerLoaderKey] = routeServerLoaderPath;
102
+ } else {
103
+ mfConfig.exposes[routeServerLoaderKey] = clientRouteServerLoaderPath;
104
+ }
105
+ }
106
+ };
107
+ Object.keys(entryMap).forEach((entry) => {
108
+ const { routesPath, routeServerLoaderPath, clientRouteServerLoaderPath } = entryMap[entry];
109
+ addExposeByEntry(entry, routesPath, routeServerLoaderPath, clientRouteServerLoaderPath);
110
+ });
111
+ }
112
+ function addShared(options) {
113
+ const { mfConfig } = options;
114
+ const styledComponents = `styled-components`;
115
+ const reactRouterDom = `react-router-dom`;
116
+ const reactRouterDomServer = `react-router-dom/server`;
117
+ if (!mfConfig.shared) {
118
+ mfConfig.shared = {
119
+ [reactRouterDom]: {
120
+ singleton: true
121
+ },
122
+ [reactRouterDomServer]: {
123
+ singleton: true
124
+ },
125
+ [styledComponents]: {
126
+ singleton: true
127
+ }
128
+ };
129
+ } else {
130
+ if (!Array.isArray(mfConfig.shared)) {
131
+ mfConfig.shared[reactRouterDom] = {
132
+ singleton: true
133
+ };
134
+ mfConfig.shared[reactRouterDomServer] = {
135
+ singleton: true
136
+ };
137
+ mfConfig.shared[styledComponents] = {
138
+ singleton: true
139
+ };
140
+ } else {
141
+ mfConfig.shared = mfConfig.shared.reduce((sum, cur) => {
142
+ if (typeof cur === "string") {
143
+ sum[cur] = {};
144
+ } else {
145
+ sum = {
146
+ ...sum,
147
+ ...cur
148
+ };
149
+ }
150
+ return sum;
151
+ }, {});
152
+ mfConfig.shared[reactRouterDom] = {
153
+ singleton: true
154
+ };
155
+ mfConfig.shared[reactRouterDomServer] = {
156
+ singleton: true
157
+ };
158
+ mfConfig.shared[styledComponents] = {
159
+ singleton: true
160
+ };
161
+ }
162
+ }
163
+ }
164
+ // Annotate the CommonJS export names for ESM import in node:
165
+ 0 && (module.exports = {
166
+ addExpose,
167
+ addShared
168
+ });
@@ -0,0 +1,72 @@
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 fetchRemoteFullRoutesMap_exports = {};
20
+ __export(fetchRemoteFullRoutesMap_exports, {
21
+ fetchRemoteFullRoutesMap: () => fetchRemoteFullRoutesMap,
22
+ getSSRByRouteIds: () => getSSRByRouteIds
23
+ });
24
+ module.exports = __toCommonJS(fetchRemoteFullRoutesMap_exports);
25
+ async function fetchRemoteFullRoutesMap(options) {
26
+ const { instance, remotePath, dataLoaderRemotes } = options;
27
+ const { remotes } = instance.options;
28
+ const remoteRoutesMap = {};
29
+ if (!remotes.length) {
30
+ return remoteRoutesMap;
31
+ }
32
+ await Promise.all(remotes.map(async (remote) => {
33
+ if (!dataLoaderRemotes.includes(remote.name)) {
34
+ return;
35
+ }
36
+ const remoteId = `${remote.name}/${remotePath}`;
37
+ const { routes } = await instance.loadRemote(remoteId);
38
+ remoteRoutesMap[remote.name] = {
39
+ routes
40
+ };
41
+ }));
42
+ return remoteRoutesMap;
43
+ }
44
+ function getSSRByRouteIds(dataLoaderRemotes, remoteRoutesMap) {
45
+ const remoteProviderRouteIds = /* @__PURE__ */ new Set();
46
+ const collectIds = (route) => {
47
+ remoteProviderRouteIds.add(route.id);
48
+ if (route.children) {
49
+ route.children.forEach((r) => {
50
+ collectIds(r);
51
+ });
52
+ }
53
+ };
54
+ Object.entries(remoteRoutesMap).forEach((item) => {
55
+ const [remoteName, routesObj] = item;
56
+ if (!dataLoaderRemotes.includes(remoteName)) {
57
+ return;
58
+ }
59
+ const { routes } = routesObj;
60
+ routes.forEach((route) => {
61
+ collectIds(route);
62
+ });
63
+ });
64
+ return [
65
+ ...remoteProviderRouteIds
66
+ ];
67
+ }
68
+ // Annotate the CommonJS export names for ESM import in node:
69
+ 0 && (module.exports = {
70
+ fetchRemoteFullRoutesMap,
71
+ getSSRByRouteIds
72
+ });
@@ -0,0 +1,82 @@
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 fetchRoutePlugin_exports = {};
20
+ __export(fetchRoutePlugin_exports, {
21
+ default: () => fetchRoutePlugin_default
22
+ });
23
+ module.exports = __toCommonJS(fetchRoutePlugin_exports);
24
+ var import_runtime = require("@module-federation/enhanced/runtime");
25
+ var import_sdk = require("@module-federation/sdk");
26
+ var import_fetchRemoteFullRoutesMap = require("./fetchRemoteFullRoutesMap");
27
+ var import_utils = require("../../runtime/routes/utils");
28
+ var import_constant = require("../../constant");
29
+ var fetchRoutePlugin_default = ({ runtimeOptions, dataLoaderRemotes, ssrByRouteIdsMap }) => ({
30
+ name: "@module-federation/modern-js-fetch-route",
31
+ pre: [
32
+ "@modern-js/plugin-inject-resource"
33
+ ],
34
+ setup(api) {
35
+ const { remotes, name } = runtimeOptions;
36
+ let isHandled = false;
37
+ return {
38
+ prepare() {
39
+ const { middlewares } = api.useAppContext();
40
+ middlewares.push({
41
+ name: "MFPatchRouteMiddleWare",
42
+ handler: async (c, next) => {
43
+ console.log("MFPatchRouteMiddleWare apply");
44
+ const serverManifest = c.get("serverManifest");
45
+ const { loaderBundles, nestedRoutesJson } = serverManifest;
46
+ if (isHandled && !globalThis.FORCE_MF_REFRESH) {
47
+ await next();
48
+ } else {
49
+ var _api_useConfigContext_server;
50
+ const instance = (0, import_runtime.init)({
51
+ name,
52
+ remotes
53
+ });
54
+ const remoteFullRoutesMap = await (0, import_fetchRemoteFullRoutesMap.fetchRemoteFullRoutesMap)({
55
+ instance,
56
+ remotePath: (0, import_sdk.isBrowserEnv)() ? `${import_constant.DEFAULT_ENTRY}/${import_constant.MF_ROUTES}` : `${import_constant.DEFAULT_ENTRY}/${import_constant.MODERN_JS_ROUTE_SERVER_LOADER}`,
57
+ dataLoaderRemotes
58
+ });
59
+ const originalSSRByRouteIds = (_api_useConfigContext_server = api.useConfigContext().server) === null || _api_useConfigContext_server === void 0 ? void 0 : _api_useConfigContext_server.ssrByRouteIds;
60
+ if (originalSSRByRouteIds && dataLoaderRemotes.length) {
61
+ const remoteSSRByRouteIds = (0, import_fetchRemoteFullRoutesMap.getSSRByRouteIds)(dataLoaderRemotes, remoteFullRoutesMap) || [];
62
+ const ssrByRouteIds = /* @__PURE__ */ new Set([
63
+ ...originalSSRByRouteIds,
64
+ ...remoteSSRByRouteIds.map((id) => ssrByRouteIdsMap[id] || id)
65
+ ]);
66
+ api.useConfigContext().server.ssrByRouteIds = Array.from(ssrByRouteIds);
67
+ }
68
+ (0, import_utils.injectRemoteRoutes)(loaderBundles, remoteFullRoutesMap);
69
+ (0, import_utils.injectRemoteRoutes)(nestedRoutesJson, remoteFullRoutesMap);
70
+ isHandled = true;
71
+ globalThis.FORCE_MF_REFRESH = false;
72
+ await next();
73
+ }
74
+ },
75
+ before: [
76
+ "render"
77
+ ]
78
+ });
79
+ }
80
+ };
81
+ }
82
+ });
@@ -39,8 +39,8 @@ var import_webpack = require("@module-federation/enhanced/webpack");
39
39
  var import_rspack = require("@module-federation/enhanced/rspack");
40
40
  var import_universe_entry_chunk_tracker_plugin = __toESM(require("@module-federation/node/universe-entry-chunk-tracker-plugin"));
41
41
  var import_manifest = require("./manifest");
42
- var import_constant = require("./constant");
43
42
  var import_logger = __toESM(require("./logger"));
43
+ var import_constant = require("../constant");
44
44
  function setEnv() {
45
45
  process.env["MF_DISABLE_EMIT_STATS"] = "true";
46
46
  process.env["MF_SSR_PRJ"] = "true";
@@ -54,6 +54,7 @@ const moduleFederationSSRPlugin = (userConfig) => ({
54
54
  setup: async ({ useConfigContext, useAppContext }) => {
55
55
  var _modernjsConfig_server;
56
56
  const modernjsConfig = useConfigContext();
57
+ const { metaName = import_constant.META_NAME } = userConfig;
57
58
  const enableSSR = Boolean(modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
58
59
  if (!enableSSR) {
59
60
  return {};
@@ -70,7 +71,9 @@ const moduleFederationSSRPlugin = (userConfig) => ({
70
71
  plugins.push({
71
72
  name: "mfSSR",
72
73
  path: "@module-federation/modern-js/ssr-runtime",
73
- config: {}
74
+ config: {
75
+ metaName
76
+ }
74
77
  });
75
78
  return {
76
79
  entrypoint,