@module-federation/modern-js 0.0.0-next-20240910102933 → 0.0.0-next-20240911025819

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 (97) hide show
  1. package/dist/cjs/cli/configPlugin.js +4 -2
  2. package/dist/cjs/cli/{dataLoader/ast/index.js → constant.js} +6 -6
  3. package/dist/cjs/cli/index.js +1 -5
  4. package/dist/cjs/cli/mfRuntimePlugins/{node-fetch.js → inject-node-fetch.js} +6 -6
  5. package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +38 -40
  6. package/dist/cjs/{runtime/withMFRouteId.js → cli/mfRuntimePlugins/shared-strategy.js} +23 -11
  7. package/dist/cjs/cli/ssrPlugin.js +1 -7
  8. package/dist/cjs/cli/utils.js +8 -13
  9. package/dist/cjs/cli/utils.spec.js +6 -5
  10. package/dist/cjs/constant.js +2 -32
  11. package/dist/cjs/runtime/index.js +1 -4
  12. package/dist/cjs/ssr-runtime/plugin.js +7 -7
  13. package/dist/esm/cli/configPlugin.js +3 -2
  14. package/dist/esm/cli/constant.js +4 -0
  15. package/dist/esm/cli/index.js +1 -4
  16. package/dist/esm/cli/mfRuntimePlugins/{node-fetch.js → inject-node-fetch.js} +3 -3
  17. package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +25 -0
  18. package/dist/esm/cli/ssrPlugin.js +1 -6
  19. package/dist/esm/cli/utils.js +7 -11
  20. package/dist/esm/cli/utils.spec.js +6 -5
  21. package/dist/esm/constant.js +1 -21
  22. package/dist/esm/runtime/index.js +1 -3
  23. package/dist/esm/ssr-runtime/plugin.js +10 -7
  24. package/dist/esm-node/cli/configPlugin.js +4 -2
  25. package/dist/esm-node/cli/constant.js +4 -0
  26. package/dist/esm-node/cli/index.js +1 -5
  27. package/dist/esm-node/cli/mfRuntimePlugins/{node-fetch.js → inject-node-fetch.js} +3 -3
  28. package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +38 -40
  29. package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +23 -0
  30. package/dist/esm-node/cli/ssrPlugin.js +1 -7
  31. package/dist/esm-node/cli/utils.js +7 -11
  32. package/dist/esm-node/cli/utils.spec.js +6 -5
  33. package/dist/esm-node/constant.js +1 -21
  34. package/dist/esm-node/runtime/index.js +1 -3
  35. package/dist/esm-node/ssr-runtime/plugin.js +7 -7
  36. package/dist/types/cli/constant.d.ts +1 -0
  37. package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +3 -0
  38. package/dist/types/cli/utils.d.ts +0 -2
  39. package/dist/types/constant.d.ts +0 -10
  40. package/dist/types/runtime/index.d.ts +0 -1
  41. package/dist/types/types/index.d.ts +0 -9
  42. package/package.json +11 -48
  43. package/dist/cjs/cli/dataLoader/ast/constant.js +0 -49
  44. package/dist/cjs/cli/dataLoader/ast/generateRoutes.js +0 -141
  45. package/dist/cjs/cli/dataLoader/generateRouteFile.js +0 -143
  46. package/dist/cjs/cli/dataLoader/patchMFConfig.js +0 -140
  47. package/dist/cjs/cli/dataLoader/plugin.js +0 -199
  48. package/dist/cjs/cli/mfRuntimePlugins/node.js +0 -41
  49. package/dist/cjs/cli/server/dataLoaderPlugin.js +0 -82
  50. package/dist/cjs/cli/server/fetchRemoteFullRoutesMap.js +0 -69
  51. package/dist/cjs/interfaces/route.js +0 -16
  52. package/dist/cjs/runtime/dataLoader/plugin-inject-assets.js +0 -84
  53. package/dist/cjs/runtime/dataLoader/plugin.js +0 -74
  54. package/dist/cjs/runtime/dataLoader/utils.js +0 -106
  55. package/dist/esm/cli/dataLoader/ast/constant.js +0 -18
  56. package/dist/esm/cli/dataLoader/ast/generateRoutes.js +0 -108
  57. package/dist/esm/cli/dataLoader/ast/index.js +0 -4
  58. package/dist/esm/cli/dataLoader/generateRouteFile.js +0 -293
  59. package/dist/esm/cli/dataLoader/patchMFConfig.js +0 -106
  60. package/dist/esm/cli/dataLoader/plugin.js +0 -214
  61. package/dist/esm/cli/mfRuntimePlugins/node.js +0 -13
  62. package/dist/esm/cli/server/dataLoaderPlugin.js +0 -105
  63. package/dist/esm/cli/server/fetchRemoteFullRoutesMap.js +0 -88
  64. package/dist/esm/interfaces/route.js +0 -0
  65. package/dist/esm/runtime/dataLoader/plugin-inject-assets.js +0 -63
  66. package/dist/esm/runtime/dataLoader/plugin.js +0 -93
  67. package/dist/esm/runtime/dataLoader/utils.js +0 -77
  68. package/dist/esm/runtime/withMFRouteId.js +0 -7
  69. package/dist/esm-node/cli/dataLoader/ast/constant.js +0 -18
  70. package/dist/esm-node/cli/dataLoader/ast/generateRoutes.js +0 -107
  71. package/dist/esm-node/cli/dataLoader/ast/index.js +0 -4
  72. package/dist/esm-node/cli/dataLoader/generateRouteFile.js +0 -108
  73. package/dist/esm-node/cli/dataLoader/patchMFConfig.js +0 -106
  74. package/dist/esm-node/cli/dataLoader/plugin.js +0 -164
  75. package/dist/esm-node/cli/mfRuntimePlugins/node.js +0 -11
  76. package/dist/esm-node/cli/server/dataLoaderPlugin.js +0 -62
  77. package/dist/esm-node/cli/server/fetchRemoteFullRoutesMap.js +0 -44
  78. package/dist/esm-node/interfaces/route.js +0 -0
  79. package/dist/esm-node/runtime/dataLoader/plugin-inject-assets.js +0 -60
  80. package/dist/esm-node/runtime/dataLoader/plugin.js +0 -50
  81. package/dist/esm-node/runtime/dataLoader/utils.js +0 -77
  82. package/dist/esm-node/runtime/withMFRouteId.js +0 -7
  83. package/dist/types/cli/dataLoader/ast/constant.d.ts +0 -8
  84. package/dist/types/cli/dataLoader/ast/generateRoutes.d.ts +0 -5
  85. package/dist/types/cli/dataLoader/ast/index.d.ts +0 -1
  86. package/dist/types/cli/dataLoader/generateRouteFile.d.ts +0 -7
  87. package/dist/types/cli/dataLoader/patchMFConfig.d.ts +0 -10
  88. package/dist/types/cli/dataLoader/plugin.d.ts +0 -6
  89. package/dist/types/cli/mfRuntimePlugins/node.d.ts +0 -3
  90. package/dist/types/cli/server/dataLoaderPlugin.d.ts +0 -10
  91. package/dist/types/cli/server/fetchRemoteFullRoutesMap.d.ts +0 -10
  92. package/dist/types/interfaces/route.d.ts +0 -12
  93. package/dist/types/runtime/dataLoader/plugin-inject-assets.d.ts +0 -2
  94. package/dist/types/runtime/dataLoader/plugin.d.ts +0 -2
  95. package/dist/types/runtime/dataLoader/utils.d.ts +0 -22
  96. package/dist/types/runtime/withMFRouteId.d.ts +0 -1
  97. /package/dist/types/cli/mfRuntimePlugins/{node-fetch.d.ts → inject-node-fetch.d.ts} +0 -0
@@ -1,141 +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 generateRoutes_exports = {};
30
- __export(generateRoutes_exports, {
31
- generateRoutes: () => generateRoutes
32
- });
33
- module.exports = __toCommonJS(generateRoutes_exports);
34
- var import_fs = __toESM(require("fs"));
35
- var import_traverse = __toESM(require("@babel/traverse"));
36
- var babelParser = __toESM(require("@babel/parser"));
37
- var import_generator = __toESM(require("@babel/generator"));
38
- var t = __toESM(require("@babel/types"));
39
- function findTargetKeyNode(nodeProperties, key) {
40
- return nodeProperties.find((p) => t.isObjectProperty(p) && t.isStringLiteral(p.key) && p.key.value === key);
41
- }
42
- function generateRoutes({ sourceCode, filePath }) {
43
- const ast = babelParser.parse(sourceCode, {
44
- sourceType: "module"
45
- });
46
- const lazyComponentDeclarations = [];
47
- const componentDeclarations = [];
48
- let componentId = 0;
49
- (0, import_traverse.default)(ast, {
50
- // ImportDeclaration(path) {
51
- // const source = path.node.source.value;
52
- // const routeIdMatch = source.match(/routeId=([^&]+)/);
53
- // if (routeIdMatch) {
54
- // const originalRouteId = routeIdMatch[1];
55
- // const newRouteId = `${prefix}${originalRouteId}`;
56
- // const newSource = source.replace(
57
- // /routeId=[^&]+/,
58
- // `routeId=${newRouteId}`,
59
- // );
60
- // path.node.source = t.stringLiteral(newSource);
61
- // }
62
- // },
63
- ObjectExpression(path) {
64
- let componentName = "";
65
- let lazyComponentName = "";
66
- if (!Array.isArray(path.node.properties)) {
67
- return;
68
- }
69
- const idNode = findTargetKeyNode(path.node.properties, "id");
70
- if (idNode && t.isObjectProperty(idNode) && t.isStringLiteral(idNode.value)) {
71
- }
72
- const isRootNode = findTargetKeyNode(path.node.properties, "isRoot");
73
- if (isRootNode && t.isObjectProperty(isRootNode) && t.isBooleanLiteral(isRootNode.value)) {
74
- isRootNode.value.value = false;
75
- }
76
- if (!isRootNode) {
77
- const lazyComponentNode = findTargetKeyNode(path.node.properties, "lazyImport");
78
- if (lazyComponentNode && t.isObjectProperty(lazyComponentNode) && t.isArrowFunctionExpression(lazyComponentNode.value)) {
79
- lazyComponentName = `LazyComponent_${componentId}`;
80
- const lazyDeclaration = t.variableDeclaration("const", [
81
- t.variableDeclarator(t.identifier(lazyComponentName), lazyComponentNode.value)
82
- ]);
83
- lazyComponentNode.value = t.identifier(lazyComponentName);
84
- const componentNode2 = findTargetKeyNode(path.node.properties, "component");
85
- if (componentNode2 && t.isObjectProperty(componentNode2) && t.isCallExpression(componentNode2.value) && t.isIdentifier(componentNode2.value.callee)) {
86
- componentNode2.value = t.callExpression(t.identifier("lazy"), [
87
- t.identifier(lazyComponentName)
88
- ]);
89
- }
90
- lazyComponentDeclarations.push(lazyDeclaration);
91
- }
92
- }
93
- const componentNode = findTargetKeyNode(path.node.properties, "component");
94
- if (componentNode && t.isObjectProperty(componentNode) && t.isCallExpression(componentNode.value) && t.isIdentifier(componentNode.value.callee)) {
95
- componentName = `Component_${componentId}`;
96
- const componentDeclaration = t.variableDeclaration("const", [
97
- t.variableDeclarator(t.identifier(componentName), lazyComponentName ? t.callExpression(t.identifier("lazy"), [
98
- t.identifier(lazyComponentName)
99
- ]) : componentNode.value)
100
- ]);
101
- componentDeclarations.push(componentDeclaration);
102
- componentNode.value = t.identifier(componentName);
103
- }
104
- if (lazyComponentName || componentName) {
105
- componentId++;
106
- if (componentName) {
107
- const upperFirstName = componentName.slice(0, 1).toUpperCase() + componentName.slice(1);
108
- const jsxElement = t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier(upperFirstName), [], true), null, [], true);
109
- path.node.properties.push(t.objectProperty(t.identifier("element"), jsxElement));
110
- }
111
- }
112
- }
113
- });
114
- (0, import_traverse.default)(ast, {
115
- Program(path) {
116
- const lastImportIndex = path.get("body").reduce((lastIndex, p, index) => {
117
- if (t.isImportDeclaration(p.node)) {
118
- lastIndex = index;
119
- }
120
- return lastIndex;
121
- }, -1);
122
- if (lastImportIndex >= 0) {
123
- const lastImport = path.get(`body.${lastImportIndex}`);
124
- [
125
- ...componentDeclarations,
126
- ...lazyComponentDeclarations
127
- ].forEach((declaration) => {
128
- if ("insertAfter" in lastImport) {
129
- lastImport.insertAfter(declaration);
130
- }
131
- });
132
- }
133
- }
134
- });
135
- const { code } = (0, import_generator.default)(ast);
136
- import_fs.default.writeFileSync(filePath, code);
137
- }
138
- // Annotate the CommonJS export names for ESM import in node:
139
- 0 && (module.exports = {
140
- generateRoutes
141
- });
@@ -1,143 +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 generateRouteFile_exports = {};
30
- __export(generateRouteFile_exports, {
31
- calcPathName: () => calcPathName,
32
- generateRouteFile: () => generateRouteFile
33
- });
34
- module.exports = __toCommonJS(generateRouteFile_exports);
35
- var import_utils = require("@modern-js/utils");
36
- var import_path = __toESM(require("path"));
37
- var import_constant = require("../../constant");
38
- var import_utils2 = require("../../runtime/dataLoader/utils");
39
- const ROUTES = "routes";
40
- function calcPathName(filePath, rootDir, entry) {
41
- const relativePath = import_path.default.relative(rootDir, filePath);
42
- const conditionalReg = /^\[(.*)\]$/;
43
- const splitPaths = relativePath.split("/").reduce((sum, cur) => {
44
- if (cur.includes(".")) {
45
- const split = cur.split(".").map((i) => {
46
- const match = i.match(conditionalReg);
47
- if (match) {
48
- return `:${match[1]}`;
49
- }
50
- return i.startsWith("__") ? "" : i;
51
- });
52
- sum.push(...split);
53
- } else {
54
- const match = cur.match(conditionalReg);
55
- if (match) {
56
- sum.push(`:${match[1]}`);
57
- } else {
58
- if (!cur.startsWith("__")) {
59
- sum.push(cur);
60
- }
61
- }
62
- }
63
- return sum;
64
- }, []);
65
- return import_path.default.join(entry, splitPaths.filter((p) => !!p).join("/"));
66
- }
67
- async function generateRouteFile(options) {
68
- const { appDirectory } = options;
69
- const entries = [];
70
- const remotePathMap = {};
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: ${id}`
93
- ];
94
- await import_utils.fs.writeFile(generatedFilePath, content.join("\n"));
95
- };
96
- if (remoteLayoutId) {
97
- const pathname = calcPathName(filepath, rootDir, entry);
98
- writeFile(remoteLayoutId, "layout", pathname);
99
- remotePathMap[pathname] = remoteName;
100
- }
101
- return;
102
- }
103
- await Promise.all(items.map(async (item) => {
104
- await traverse((0, import_utils.normalizeToPosixPath)(import_path.default.join(filepath, item)), rootDir, entry);
105
- }));
106
- };
107
- const traverseRoutes = async (dir, level = 1) => {
108
- if (level > 3) {
109
- return;
110
- }
111
- const isDirectory = (await import_utils.fs.stat(dir)).isDirectory();
112
- if (!isDirectory) {
113
- return;
114
- }
115
- const items = await import_utils.fs.readdir(dir);
116
- if (items.find((i) => i === ROUTES)) {
117
- const entry = import_path.default.relative(srcPath, dir);
118
- entries.push(entry);
119
- const rootDir = `${dir}/${ROUTES}`;
120
- await traverse(rootDir, rootDir, entry);
121
- } else {
122
- await Promise.all(items.map(async (item) => {
123
- await traverseRoutes(import_path.default.join(dir, item), level++);
124
- }));
125
- }
126
- };
127
- await traverseRoutes(srcPath);
128
- const normalizedEntries = entries.map((entry) => {
129
- if (entry === "") {
130
- return "main";
131
- }
132
- return entry;
133
- });
134
- return {
135
- entries: normalizedEntries,
136
- remotePathMap
137
- };
138
- }
139
- // Annotate the CommonJS export names for ESM import in node:
140
- 0 && (module.exports = {
141
- calcPathName,
142
- generateRouteFile
143
- });
@@ -1,140 +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 patchMFConfig_exports = {};
30
- __export(patchMFConfig_exports, {
31
- patchMFConfig: () => patchMFConfig
32
- });
33
- module.exports = __toCommonJS(patchMFConfig_exports);
34
- var import_path = __toESM(require("path"));
35
- var import_utils = require("@modern-js/utils");
36
- var import_constant = require("../../constant");
37
- var import_constant2 = require("../../constant");
38
- var import_ast = require("./ast");
39
- function generateExtraExposeFiles(options) {
40
- const { internalDirectory, mfConfig, isServer, entries } = options;
41
- const entryMap = {};
42
- const outputDir = import_path.default.resolve(process.cwd(), "node_modules/.federation/data-loader");
43
- import_utils.fs.ensureDirSync(outputDir);
44
- const addSuffix = (fileName, ext = ".jsx") => {
45
- if (!isServer) {
46
- return `${fileName}${ext}`;
47
- }
48
- return `${fileName}.server${ext}`;
49
- };
50
- const generateEntryRoutes = (entry) => {
51
- const outputEntryDir = import_path.default.resolve(outputDir, entry);
52
- import_utils.fs.ensureDirSync(outputEntryDir);
53
- const sourceDir = import_path.default.resolve(internalDirectory, entry);
54
- const routesFilePath = import_path.default.resolve(sourceDir, addSuffix(import_constant2.MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME, ".js"));
55
- const routesFileContent = import_utils.fs.readFileSync(routesFilePath, "utf-8");
56
- const outputFullRoutesPath = import_path.default.resolve(outputEntryDir, addSuffix(import_constant2.MF_ROUTES));
57
- (0, import_ast.generateRoutes)({
58
- sourceCode: routesFileContent,
59
- filePath: outputFullRoutesPath
60
- });
61
- const routeServerLoaderPath = import_path.default.resolve(sourceDir, `${import_constant.MODERN_JS_ROUTE_SERVER_LOADER}.js`);
62
- const outputRouteServerLoaderPath = import_path.default.resolve(outputEntryDir, `${import_constant.MODERN_JS_ROUTE_SERVER_LOADER}.js`);
63
- const clientRouteServerLoaderPath = outputRouteServerLoaderPath.replace(import_constant.MODERN_JS_ROUTE_SERVER_LOADER, `${import_constant.MODERN_JS_ROUTE_SERVER_LOADER}-client`);
64
- if (isServer) {
65
- const routeServerLoaderContent = import_utils.fs.readFileSync(routeServerLoaderPath, "utf-8");
66
- (0, import_ast.generateRoutes)({
67
- sourceCode: routeServerLoaderContent,
68
- filePath: outputRouteServerLoaderPath
69
- });
70
- } else {
71
- import_utils.fs.writeFileSync(clientRouteServerLoaderPath, `export const routes = []`);
72
- }
73
- entryMap[entry] = {
74
- routesPath: outputFullRoutesPath,
75
- routeServerLoaderPath: outputRouteServerLoaderPath,
76
- clientRouteServerLoaderPath
77
- };
78
- };
79
- entries.forEach((entry) => {
80
- generateEntryRoutes(entry);
81
- });
82
- return {
83
- entryMap
84
- };
85
- }
86
- function addExpose(options) {
87
- const { mfConfig, isServer } = options;
88
- if (mfConfig.remotes && mfConfig.exposes) {
89
- throw new Error(`dataLoader only support for pure provider/consumer!`);
90
- }
91
- const { entryMap } = generateExtraExposeFiles(options);
92
- const addExposeByEntry = (entry, routesPath, routeServerLoaderPath, clientRouteServerLoaderPath) => {
93
- if (!mfConfig.exposes) {
94
- mfConfig.exposes = {};
95
- }
96
- const routesKey = `./${entry}/${import_constant2.MF_ROUTES}`;
97
- if (!mfConfig.exposes[routesKey]) {
98
- mfConfig.exposes[routesKey] = routesPath;
99
- }
100
- const routeServerLoaderKey = `./${entry}/${import_constant.MODERN_JS_ROUTE_SERVER_LOADER}`;
101
- if (!mfConfig.exposes[routeServerLoaderKey]) {
102
- if (isServer) {
103
- mfConfig.exposes[routeServerLoaderKey] = routeServerLoaderPath;
104
- } else {
105
- mfConfig.exposes[routeServerLoaderKey] = clientRouteServerLoaderPath;
106
- }
107
- }
108
- };
109
- Object.keys(entryMap).forEach((entry) => {
110
- const { routesPath, routeServerLoaderPath, clientRouteServerLoaderPath } = entryMap[entry];
111
- addExposeByEntry(entry, routesPath, routeServerLoaderPath, clientRouteServerLoaderPath);
112
- });
113
- }
114
- function addShared(options) {
115
- const { metaName, mfConfig } = options;
116
- const alias = `@${metaName}/runtime/router`;
117
- if (!mfConfig.shared) {
118
- mfConfig.shared = {
119
- [alias]: {
120
- singleton: true
121
- }
122
- };
123
- } else {
124
- if (!Array.isArray(mfConfig.shared)) {
125
- mfConfig.shared[alias] = {
126
- singleton: true
127
- };
128
- } else {
129
- mfConfig.shared.push(alias);
130
- }
131
- }
132
- }
133
- function patchMFConfig(options) {
134
- addShared(options);
135
- addExpose(options);
136
- }
137
- // Annotate the CommonJS export names for ESM import in node:
138
- 0 && (module.exports = {
139
- patchMFConfig
140
- });
@@ -1,199 +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 plugin_exports = {};
30
- __export(plugin_exports, {
31
- default: () => plugin_default,
32
- generateRoutes: () => import_ast.generateRoutes,
33
- moduleFederationDataLoaderPlugin: () => moduleFederationDataLoaderPlugin
34
- });
35
- module.exports = __toCommonJS(plugin_exports);
36
- var import_utils = require("../../runtime/dataLoader/utils");
37
- var import_constant = require("../../constant");
38
- var import_constant2 = require("../../constant");
39
- var import_ast = require("./ast");
40
- var import_generateRouteFile = require("./generateRouteFile");
41
- var import_patchMFConfig = require("./patchMFConfig");
42
- var import_path = __toESM(require("path"));
43
- function _transformRuntimeOptions(buildOptions) {
44
- const remotes = buildOptions.remotes || {};
45
- const runtimeRemotes = Object.entries(remotes).map((remote) => {
46
- const [alias, nameAndEntry] = remote;
47
- const [name, entry] = nameAndEntry.split("@");
48
- return {
49
- name,
50
- entry,
51
- alias
52
- };
53
- });
54
- return {
55
- name: buildOptions.name,
56
- remotes: runtimeRemotes
57
- };
58
- }
59
- const moduleFederationDataLoaderPlugin = (enable, internalOptions, userConfig) => ({
60
- name: "@modern-js/plugin-module-federation-data-loader",
61
- pre: [
62
- "@modern-js/plugin-module-federation-config"
63
- ],
64
- post: [
65
- "@modern-js/plugin-router",
66
- "@modern-js/plugin-module-federation"
67
- ],
68
- setup: async ({ useAppContext }) => {
69
- if (!enable) {
70
- return;
71
- }
72
- const {
73
- // patchMFConfig,
74
- metaName = import_constant2.META_NAME,
75
- serverPlugin = "@module-federation/modern-js/data-loader-server",
76
- transformRuntimeOptions
77
- } = userConfig;
78
- const appContext = useAppContext();
79
- const name = internalOptions.csrConfig.name;
80
- const internalDirectory = appContext.internalDirectory.replace(import_constant2.META_NAME, metaName || import_constant2.META_NAME);
81
- const transformRuntimeFn = transformRuntimeOptions || _transformRuntimeOptions;
82
- const partialSSRRemotes = [];
83
- const entries = /* @__PURE__ */ new Set();
84
- let remotePathMap = {};
85
- const ssrByRouteIdsMap = {};
86
- const serverPluginOptions = {
87
- runtimeOptions: transformRuntimeFn(internalOptions.csrConfig),
88
- partialSSRRemotes,
89
- ssrByRouteIdsMap
90
- };
91
- const csrConfig = internalOptions.csrConfig;
92
- if (typeof csrConfig.remotes === "object" && Object.keys(csrConfig.remotes).length) {
93
- const { entries: scanEntries, remotePathMap: scanRemotePathMap } = await (0, import_generateRouteFile.generateRouteFile)({
94
- appDirectory: appContext.appDirectory
95
- });
96
- scanEntries.forEach((e) => entries.add(e));
97
- remotePathMap = scanRemotePathMap;
98
- } else {
99
- entries.add(import_constant.DEFAULT_ENTRY);
100
- }
101
- return {
102
- _internalRuntimePlugins: ({ entrypoint, plugins }) => {
103
- plugins.push({
104
- name: "ssrDataLoader",
105
- path: "@module-federation/modern-js/data-loader",
106
- config: {}
107
- });
108
- plugins.push({
109
- name: "ssrDataLoaderInjectAssets",
110
- path: "@module-federation/modern-js/data-loader-inject-assets",
111
- config: {}
112
- });
113
- return {
114
- entrypoint,
115
- plugins
116
- };
117
- },
118
- _internalServerPlugins({ plugins }) {
119
- plugins.push({
120
- name: serverPlugin,
121
- options: serverPluginOptions
122
- });
123
- return {
124
- plugins
125
- };
126
- },
127
- async modifyFileSystemRoutes(options) {
128
- const { routes, entrypoint } = options;
129
- const { entryName } = entrypoint;
130
- const prefix = (0, import_utils.transformName2Prefix)(internalOptions.csrConfig.name);
131
- const modifyRouteIds = (route, pathName) => {
132
- const currentPathName = route.path ? import_path.default.join(pathName, route.path) : pathName;
133
- if (route.id) {
134
- var _route_children;
135
- const originalId = route.id;
136
- route.originalId = originalId;
137
- const remoteName = remotePathMap[currentPathName];
138
- if (remoteName) {
139
- const remoteIdPrefix = (0, import_utils.transformName2Prefix)(remoteName);
140
- const isLayout = Boolean(route.children);
141
- const newId = `${remoteIdPrefix}${isLayout ? "layout" : "page"}`;
142
- ssrByRouteIdsMap[originalId] = newId;
143
- route.id = newId;
144
- if (!partialSSRRemotes.includes(remoteName)) {
145
- partialSSRRemotes.push(remoteName);
146
- }
147
- } else {
148
- route.id = `${prefix}${route.id}`;
149
- }
150
- (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.forEach((r) => {
151
- modifyRouteIds(r, currentPathName);
152
- });
153
- }
154
- };
155
- routes.forEach((route) => {
156
- modifyRouteIds(route, entryName);
157
- });
158
- return options;
159
- },
160
- config: async () => {
161
- return {
162
- tools: {
163
- // bundlerChain can not keep target order
164
- rspack(_config, { isServer }) {
165
- (0, import_patchMFConfig.patchMFConfig)({
166
- mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
167
- metaName,
168
- isServer,
169
- internalDirectory,
170
- entries
171
- });
172
- },
173
- // bundlerChain can not keep target order
174
- webpack(_config, { isServer }) {
175
- (0, import_patchMFConfig.patchMFConfig)({
176
- mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
177
- metaName,
178
- isServer,
179
- internalDirectory,
180
- entries
181
- });
182
- }
183
- },
184
- source: {
185
- define: {
186
- MODERN_ROUTER_ID_PREFIX: JSON.stringify((0, import_utils.transformName2Prefix)(name))
187
- }
188
- }
189
- };
190
- }
191
- };
192
- }
193
- });
194
- var plugin_default = moduleFederationDataLoaderPlugin;
195
- // Annotate the CommonJS export names for ESM import in node:
196
- 0 && (module.exports = {
197
- generateRoutes,
198
- moduleFederationDataLoaderPlugin
199
- });
@@ -1,41 +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 node_exports = {};
30
- __export(node_exports, {
31
- default: () => node_default
32
- });
33
- module.exports = __toCommonJS(node_exports);
34
- var import_runtimePlugin = __toESM(require("@module-federation/node/runtimePlugin"));
35
- const isContainer = typeof __filename === "string" ? __filename.includes("remote") ? true : false : false;
36
- const pluginName = `node-plugin-${isContainer}`;
37
- const nodePlugin = () => ({
38
- name: pluginName,
39
- beforeInit: (0, import_runtimePlugin.default)().beforeInit
40
- });
41
- var node_default = nodePlugin;