@module-federation/modern-js 0.0.0-next-20241017102355 → 0.0.0-next-20241018034800

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/{routes/ast/index.js → constant.js} +6 -6
  2. package/dist/cjs/cli/index.js +2 -7
  3. package/dist/cjs/cli/mfRuntimePlugins/{node-fetch.js → inject-node-fetch.js} +6 -6
  4. package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +38 -40
  5. package/dist/cjs/{runtime/withMFRouteId.js → cli/mfRuntimePlugins/shared-strategy.js} +23 -11
  6. package/dist/cjs/cli/ssrPlugin.js +2 -5
  7. package/dist/cjs/cli/utils.js +6 -11
  8. package/dist/cjs/cli/utils.spec.js +6 -5
  9. package/dist/cjs/constant.js +2 -35
  10. package/dist/cjs/runtime/index.js +1 -4
  11. package/dist/cjs/ssr-runtime/plugin.js +8 -7
  12. package/dist/esm/cli/constant.js +4 -0
  13. package/dist/esm/cli/index.js +2 -7
  14. package/dist/esm/cli/mfRuntimePlugins/{node-fetch.js → inject-node-fetch.js} +3 -3
  15. package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +25 -0
  16. package/dist/esm/cli/ssrPlugin.js +3 -6
  17. package/dist/esm/cli/utils.js +5 -9
  18. package/dist/esm/cli/utils.spec.js +6 -5
  19. package/dist/esm/constant.js +1 -23
  20. package/dist/esm/runtime/index.js +1 -3
  21. package/dist/esm/ssr-runtime/plugin.js +11 -8
  22. package/dist/esm-node/cli/constant.js +4 -0
  23. package/dist/esm-node/cli/index.js +2 -7
  24. package/dist/esm-node/cli/mfRuntimePlugins/{node-fetch.js → inject-node-fetch.js} +3 -3
  25. package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +38 -40
  26. package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +23 -0
  27. package/dist/esm-node/cli/ssrPlugin.js +2 -5
  28. package/dist/esm-node/cli/utils.js +5 -9
  29. package/dist/esm-node/cli/utils.spec.js +6 -5
  30. package/dist/esm-node/constant.js +1 -23
  31. package/dist/esm-node/runtime/index.js +1 -3
  32. package/dist/esm-node/ssr-runtime/plugin.js +8 -7
  33. package/dist/types/cli/constant.d.ts +1 -0
  34. package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +3 -0
  35. package/dist/types/cli/utils.d.ts +0 -2
  36. package/dist/types/constant.d.ts +0 -11
  37. package/dist/types/runtime/index.d.ts +0 -1
  38. package/dist/types/ssr-runtime/plugin.d.ts +1 -3
  39. package/dist/types/types/index.d.ts +1 -3
  40. package/package.json +12 -49
  41. package/dist/cjs/cli/mfRuntimePlugins/node.js +0 -41
  42. package/dist/cjs/cli/routes/ast/constant.js +0 -49
  43. package/dist/cjs/cli/routes/ast/generateRoutes.js +0 -143
  44. package/dist/cjs/cli/routes/clearMFCache.js +0 -57
  45. package/dist/cjs/cli/routes/exportRoutePlugin.js +0 -77
  46. package/dist/cjs/cli/routes/importRoutesPlugin.js +0 -120
  47. package/dist/cjs/cli/routes/plugin.js +0 -157
  48. package/dist/cjs/cli/routes/traverseRouteFiles.js +0 -148
  49. package/dist/cjs/cli/routes/utils.js +0 -168
  50. package/dist/cjs/cli/server/fetchRemoteFullRoutesMap.js +0 -72
  51. package/dist/cjs/cli/server/fetchRoutePlugin.js +0 -81
  52. package/dist/cjs/interfaces/route.js +0 -16
  53. package/dist/cjs/runtime/routes/plugin-inject-assets.js +0 -92
  54. package/dist/cjs/runtime/routes/plugin.js +0 -75
  55. package/dist/cjs/runtime/routes/utils.js +0 -115
  56. package/dist/cjs/types/modern.js +0 -16
  57. package/dist/cjs/types/routes.js +0 -16
  58. package/dist/esm/cli/mfRuntimePlugins/node.js +0 -13
  59. package/dist/esm/cli/routes/ast/constant.js +0 -18
  60. package/dist/esm/cli/routes/ast/generateRoutes.js +0 -110
  61. package/dist/esm/cli/routes/ast/index.js +0 -4
  62. package/dist/esm/cli/routes/clearMFCache.js +0 -34
  63. package/dist/esm/cli/routes/exportRoutePlugin.js +0 -79
  64. package/dist/esm/cli/routes/importRoutesPlugin.js +0 -122
  65. package/dist/esm/cli/routes/plugin.js +0 -266
  66. package/dist/esm/cli/routes/traverseRouteFiles.js +0 -313
  67. package/dist/esm/cli/routes/utils.js +0 -130
  68. package/dist/esm/cli/server/fetchRemoteFullRoutesMap.js +0 -93
  69. package/dist/esm/cli/server/fetchRoutePlugin.js +0 -104
  70. package/dist/esm/interfaces/route.js +0 -0
  71. package/dist/esm/runtime/routes/plugin-inject-assets.js +0 -71
  72. package/dist/esm/runtime/routes/plugin.js +0 -97
  73. package/dist/esm/runtime/routes/utils.js +0 -86
  74. package/dist/esm/runtime/withMFRouteId.js +0 -7
  75. package/dist/esm/types/modern.js +0 -0
  76. package/dist/esm/types/routes.js +0 -0
  77. package/dist/esm-node/cli/mfRuntimePlugins/node.js +0 -11
  78. package/dist/esm-node/cli/routes/ast/constant.js +0 -18
  79. package/dist/esm-node/cli/routes/ast/generateRoutes.js +0 -109
  80. package/dist/esm-node/cli/routes/ast/index.js +0 -4
  81. package/dist/esm-node/cli/routes/clearMFCache.js +0 -33
  82. package/dist/esm-node/cli/routes/exportRoutePlugin.js +0 -53
  83. package/dist/esm-node/cli/routes/importRoutesPlugin.js +0 -96
  84. package/dist/esm-node/cli/routes/plugin.js +0 -123
  85. package/dist/esm-node/cli/routes/traverseRouteFiles.js +0 -113
  86. package/dist/esm-node/cli/routes/utils.js +0 -133
  87. package/dist/esm-node/cli/server/fetchRemoteFullRoutesMap.js +0 -47
  88. package/dist/esm-node/cli/server/fetchRoutePlugin.js +0 -61
  89. package/dist/esm-node/interfaces/route.js +0 -0
  90. package/dist/esm-node/runtime/routes/plugin-inject-assets.js +0 -68
  91. package/dist/esm-node/runtime/routes/plugin.js +0 -51
  92. package/dist/esm-node/runtime/routes/utils.js +0 -86
  93. package/dist/esm-node/runtime/withMFRouteId.js +0 -7
  94. package/dist/esm-node/types/modern.js +0 -0
  95. package/dist/esm-node/types/routes.js +0 -0
  96. package/dist/types/cli/mfRuntimePlugins/node.d.ts +0 -3
  97. package/dist/types/cli/routes/ast/constant.d.ts +0 -8
  98. package/dist/types/cli/routes/ast/generateRoutes.d.ts +0 -5
  99. package/dist/types/cli/routes/ast/index.d.ts +0 -1
  100. package/dist/types/cli/routes/clearMFCache.d.ts +0 -1
  101. package/dist/types/cli/routes/exportRoutePlugin.d.ts +0 -4
  102. package/dist/types/cli/routes/importRoutesPlugin.d.ts +0 -4
  103. package/dist/types/cli/routes/plugin.d.ts +0 -4
  104. package/dist/types/cli/routes/traverseRouteFiles.d.ts +0 -11
  105. package/dist/types/cli/routes/utils.d.ts +0 -11
  106. package/dist/types/cli/server/fetchRemoteFullRoutesMap.d.ts +0 -11
  107. package/dist/types/cli/server/fetchRoutePlugin.d.ts +0 -10
  108. package/dist/types/interfaces/route.d.ts +0 -12
  109. package/dist/types/runtime/routes/plugin-inject-assets.d.ts +0 -4
  110. package/dist/types/runtime/routes/plugin.d.ts +0 -5
  111. package/dist/types/runtime/routes/utils.d.ts +0 -22
  112. package/dist/types/runtime/withMFRouteId.d.ts +0 -1
  113. package/dist/types/types/modern.d.ts +0 -12
  114. package/dist/types/types/routes.d.ts +0 -19
  115. /package/dist/types/cli/mfRuntimePlugins/{node-fetch.d.ts → inject-node-fetch.d.ts} +0 -0
@@ -1,148 +0,0 @@
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
- });
@@ -1,168 +0,0 @@
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
- });
@@ -1,72 +0,0 @@
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
- });
@@ -1,81 +0,0 @@
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
- const serverManifest = c.get("serverManifest");
44
- const { loaderBundles, nestedRoutesJson } = serverManifest;
45
- if (isHandled && !globalThis.FORCE_MF_REFRESH) {
46
- await next();
47
- } else {
48
- var _api_useConfigContext_server;
49
- const instance = (0, import_runtime.init)({
50
- name,
51
- remotes
52
- });
53
- const remoteFullRoutesMap = await (0, import_fetchRemoteFullRoutesMap.fetchRemoteFullRoutesMap)({
54
- instance,
55
- remotePath: (0, import_sdk.isBrowserEnv)() ? `${import_constant.DEFAULT_ENTRY}/${import_constant.MF_ROUTES}` : `${import_constant.DEFAULT_ENTRY}/${import_constant.MODERN_JS_ROUTE_SERVER_LOADER}`,
56
- dataLoaderRemotes
57
- });
58
- const originalSSRByRouteIds = (_api_useConfigContext_server = api.useConfigContext().server) === null || _api_useConfigContext_server === void 0 ? void 0 : _api_useConfigContext_server.ssrByRouteIds;
59
- if (originalSSRByRouteIds && dataLoaderRemotes.length) {
60
- const remoteSSRByRouteIds = (0, import_fetchRemoteFullRoutesMap.getSSRByRouteIds)(dataLoaderRemotes, remoteFullRoutesMap) || [];
61
- const ssrByRouteIds = /* @__PURE__ */ new Set([
62
- ...originalSSRByRouteIds,
63
- ...remoteSSRByRouteIds.map((id) => ssrByRouteIdsMap[id] || id)
64
- ]);
65
- api.useConfigContext().server.ssrByRouteIds = Array.from(ssrByRouteIds);
66
- }
67
- (0, import_utils.injectRemoteRoutes)(loaderBundles, remoteFullRoutesMap);
68
- (0, import_utils.injectRemoteRoutes)(nestedRoutesJson, remoteFullRoutesMap);
69
- isHandled = true;
70
- globalThis.FORCE_MF_REFRESH = false;
71
- await next();
72
- }
73
- },
74
- before: [
75
- "render"
76
- ]
77
- });
78
- }
79
- };
80
- }
81
- });
@@ -1,16 +0,0 @@
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);
@@ -1,92 +0,0 @@
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_inject_assets_exports = {};
20
- __export(plugin_inject_assets_exports, {
21
- ssrDataLoaderInjectAssetsPlugin: () => ssrDataLoaderInjectAssetsPlugin
22
- });
23
- module.exports = __toCommonJS(plugin_inject_assets_exports);
24
- var import_jsx_runtime = require("react/jsx-runtime");
25
- var import_react = require("react");
26
- var import_runtime = require("@module-federation/enhanced/runtime");
27
- var import_createRemoteSSRComponent = require("../createRemoteSSRComponent");
28
- var import_runtime2 = require("@meta/runtime");
29
- var import_utils = require("./utils");
30
- function traverseRoutes(routes, remoteNames) {
31
- routes.forEach((route) => {
32
- route.id && remoteNames.add((0, import_utils.decodeId)(route.id));
33
- route.children && traverseRoutes(route.children, remoteNames);
34
- });
35
- }
36
- const ssrDataLoaderInjectAssetsPlugin = ({ metaName }) => {
37
- return {
38
- name: "@modern-js/plugin-mf-routes-inject-assets",
39
- pre: [
40
- "@modern-js/plugin-mf-routes"
41
- ],
42
- post: [
43
- "@module-federation/modern-js",
44
- `@${metaName}/plugin-router`
45
- ],
46
- setup: () => {
47
- return {
48
- wrapRoot() {
49
- const AppWrapper = (props) => {
50
- const instance = (0, import_runtime.getInstance)();
51
- if (!instance || !instance.options.remotes.length) {
52
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {
53
- children: props.children
54
- });
55
- }
56
- const context = (0, import_react.useContext)(import_runtime2.RuntimeReactContext);
57
- const { remotes } = instance.options;
58
- const remoteNames = /* @__PURE__ */ new Set();
59
- traverseRoutes(context.routes, remoteNames);
60
- const remoteIds = [
61
- ...remoteNames
62
- ].reduce((sum, cur) => {
63
- const matchRemote = remotes.find((r) => r.name === cur);
64
- if (matchRemote) {
65
- sum.add((0, import_utils.getRemoteId)(cur));
66
- }
67
- return sum;
68
- }, /* @__PURE__ */ new Set());
69
- const assets = [];
70
- remoteIds.forEach((remoteId) => {
71
- assets.push(...(0, import_createRemoteSSRComponent.collectSSRAssets)({
72
- id: remoteId,
73
- injectScript: false
74
- }));
75
- });
76
- return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, {
77
- children: [
78
- assets,
79
- props.children
80
- ]
81
- });
82
- };
83
- return AppWrapper;
84
- }
85
- };
86
- }
87
- };
88
- };
89
- // Annotate the CommonJS export names for ESM import in node:
90
- 0 && (module.exports = {
91
- ssrDataLoaderInjectAssetsPlugin
92
- });
@@ -1,75 +0,0 @@
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 = ({ metaName, dataLoaderRemotes }) => {
28
- return {
29
- name: "@modern-js/plugin-mf-routes",
30
- post: [
31
- `@${metaName}/plugin-router`,
32
- "@module-federation/modern-js"
33
- ],
34
- setup: () => {
35
- const remoteRoutesMap = {};
36
- return {
37
- async beforeRender() {
38
- if (Object.keys(remoteRoutesMap).length) {
39
- return;
40
- }
41
- const instance = (0, import_runtime.getInstance)();
42
- if (!instance) {
43
- return;
44
- }
45
- if (!instance.options.remotes.length) {
46
- return;
47
- }
48
- await Promise.all(instance.options.remotes.map(async (remote) => {
49
- if (!dataLoaderRemotes.includes(remote.name)) {
50
- return;
51
- }
52
- const remoteId = `${remote.name}/${import_constant.DEFAULT_ENTRY}/${import_constant.MF_ROUTES}`;
53
- const { routes } = await instance.loadRemote(remoteId);
54
- remoteRoutesMap[remote.name] = {
55
- routes
56
- };
57
- }));
58
- },
59
- // runtime plugin not save route , so it needs to inject again when trigger
60
- modifyRoutes: (routes) => {
61
- (0, import_utils.injectRemoteRoutes)({
62
- entry: {
63
- routes
64
- }
65
- }, remoteRoutesMap);
66
- return routes;
67
- }
68
- };
69
- }
70
- };
71
- };
72
- // Annotate the CommonJS export names for ESM import in node:
73
- 0 && (module.exports = {
74
- ssrDataLoaderPlugin
75
- });