@module-federation/modern-js 0.0.0-next-20240812114132 → 0.0.0-next-20240814073142

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 (63) hide show
  1. package/dist/cjs/cli/ast/constant.js +46 -0
  2. package/dist/cjs/cli/ast/generateRoutes.js +140 -0
  3. package/dist/cjs/cli/ast/generateSlimRoutes.js +106 -0
  4. package/dist/cjs/cli/ast/index.js +31 -0
  5. package/dist/cjs/cli/configPlugin.js +1 -0
  6. package/dist/cjs/cli/constant.js +3 -0
  7. package/dist/cjs/cli/dataLoaderPlugin.js +205 -0
  8. package/dist/cjs/cli/index.js +6 -1
  9. package/dist/cjs/cli/server/dataLoaderPlugin.js +87 -0
  10. package/dist/cjs/interfaces/route.js +16 -0
  11. package/dist/cjs/runtime/constant.js +34 -0
  12. package/dist/cjs/runtime/dataLoader.js +87 -0
  13. package/dist/cjs/runtime/index.js +4 -1
  14. package/dist/cjs/runtime/utils.js +43 -0
  15. package/dist/cjs/runtime/withMFRouteId.js +31 -0
  16. package/dist/cjs/ssr-runtime/plugin.js +5 -7
  17. package/dist/esm/cli/ast/constant.js +16 -0
  18. package/dist/esm/cli/ast/generateRoutes.js +107 -0
  19. package/dist/esm/cli/ast/generateSlimRoutes.js +75 -0
  20. package/dist/esm/cli/ast/index.js +6 -0
  21. package/dist/esm/cli/configPlugin.js +1 -0
  22. package/dist/esm/cli/constant.js +2 -0
  23. package/dist/esm/cli/dataLoaderPlugin.js +191 -0
  24. package/dist/esm/cli/index.js +6 -1
  25. package/dist/esm/cli/server/dataLoaderPlugin.js +132 -0
  26. package/dist/esm/interfaces/route.js +0 -0
  27. package/dist/esm/runtime/constant.js +8 -0
  28. package/dist/esm/runtime/dataLoader.js +125 -0
  29. package/dist/esm/runtime/index.js +3 -1
  30. package/dist/esm/runtime/utils.js +16 -0
  31. package/dist/esm/runtime/withMFRouteId.js +7 -0
  32. package/dist/esm/ssr-runtime/plugin.js +5 -10
  33. package/dist/esm-node/cli/ast/constant.js +16 -0
  34. package/dist/esm-node/cli/ast/generateRoutes.js +106 -0
  35. package/dist/esm-node/cli/ast/generateSlimRoutes.js +72 -0
  36. package/dist/esm-node/cli/ast/index.js +6 -0
  37. package/dist/esm-node/cli/configPlugin.js +1 -0
  38. package/dist/esm-node/cli/constant.js +2 -0
  39. package/dist/esm-node/cli/dataLoaderPlugin.js +169 -0
  40. package/dist/esm-node/cli/index.js +6 -1
  41. package/dist/esm-node/cli/server/dataLoaderPlugin.js +67 -0
  42. package/dist/esm-node/interfaces/route.js +0 -0
  43. package/dist/esm-node/runtime/constant.js +8 -0
  44. package/dist/esm-node/runtime/dataLoader.js +63 -0
  45. package/dist/esm-node/runtime/index.js +3 -1
  46. package/dist/esm-node/runtime/utils.js +19 -0
  47. package/dist/esm-node/runtime/withMFRouteId.js +7 -0
  48. package/dist/esm-node/ssr-runtime/plugin.js +5 -7
  49. package/dist/types/cli/ast/constant.d.ts +7 -0
  50. package/dist/types/cli/ast/generateRoutes.d.ts +7 -0
  51. package/dist/types/cli/ast/generateSlimRoutes.d.ts +7 -0
  52. package/dist/types/cli/ast/index.d.ts +2 -0
  53. package/dist/types/cli/constant.d.ts +1 -0
  54. package/dist/types/cli/dataLoaderPlugin.d.ts +6 -0
  55. package/dist/types/cli/server/dataLoaderPlugin.d.ts +4 -0
  56. package/dist/types/interfaces/route.d.ts +13 -0
  57. package/dist/types/runtime/constant.d.ts +3 -0
  58. package/dist/types/runtime/dataLoader.d.ts +7 -0
  59. package/dist/types/runtime/index.d.ts +1 -0
  60. package/dist/types/runtime/utils.d.ts +1 -0
  61. package/dist/types/runtime/withMFRouteId.d.ts +1 -0
  62. package/dist/types/types/index.d.ts +15 -0
  63. package/package.json +36 -10
@@ -0,0 +1,46 @@
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 constant_exports = {};
20
+ __export(constant_exports, {
21
+ COMPONENT: () => COMPONENT,
22
+ ELEMENT: () => ELEMENT,
23
+ ID: () => ID,
24
+ IS_ROOT: () => IS_ROOT,
25
+ LAZY_COMPONENT: () => LAZY_COMPONENT,
26
+ PRIVATE_COMPONENT: () => PRIVATE_COMPONENT,
27
+ SHOULD_REVALIDATE: () => SHOULD_REVALIDATE
28
+ });
29
+ module.exports = __toCommonJS(constant_exports);
30
+ const IS_ROOT = "isRoot";
31
+ const ID = "id";
32
+ const COMPONENT = "component";
33
+ const LAZY_COMPONENT = "lazyImport";
34
+ const SHOULD_REVALIDATE = "shouldRevalidate";
35
+ const PRIVATE_COMPONENT = "_component";
36
+ const ELEMENT = "element";
37
+ // Annotate the CommonJS export names for ESM import in node:
38
+ 0 && (module.exports = {
39
+ COMPONENT,
40
+ ELEMENT,
41
+ ID,
42
+ IS_ROOT,
43
+ LAZY_COMPONENT,
44
+ PRIVATE_COMPONENT,
45
+ SHOULD_REVALIDATE
46
+ });
@@ -0,0 +1,140 @@
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, prefix, baseName }) {
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(/routeId=[^&]+/, `routeId=${newRouteId}`);
57
+ path.node.source = t.stringLiteral(newSource);
58
+ }
59
+ },
60
+ ObjectExpression(path) {
61
+ let componentName = "";
62
+ let lazyComponentName = "";
63
+ if (!Array.isArray(path.node.properties)) {
64
+ return;
65
+ }
66
+ const idNode = findTargetKeyNode(path.node.properties, "id");
67
+ if (idNode && t.isObjectProperty(idNode) && t.isStringLiteral(idNode.value)) {
68
+ idNode.value = t.stringLiteral(`${prefix}${idNode.value.value}`);
69
+ }
70
+ const isRootNode = findTargetKeyNode(path.node.properties, "isRoot");
71
+ if (isRootNode && t.isObjectProperty(isRootNode) && t.isBooleanLiteral(isRootNode.value)) {
72
+ isRootNode.value.value = false;
73
+ }
74
+ if (!isRootNode) {
75
+ const lazyComponentNode = findTargetKeyNode(path.node.properties, "lazyImport");
76
+ if (lazyComponentNode && t.isObjectProperty(lazyComponentNode) && t.isArrowFunctionExpression(lazyComponentNode.value)) {
77
+ lazyComponentName = `LazyComponent_${componentId}`;
78
+ const lazyDeclaration = t.variableDeclaration("const", [
79
+ t.variableDeclarator(t.identifier(lazyComponentName), lazyComponentNode.value)
80
+ ]);
81
+ lazyComponentNode.value = t.identifier(lazyComponentName);
82
+ const componentNode2 = findTargetKeyNode(path.node.properties, "component");
83
+ if (componentNode2 && t.isObjectProperty(componentNode2) && t.isCallExpression(componentNode2.value) && t.isIdentifier(componentNode2.value.callee)) {
84
+ componentNode2.value = t.callExpression(t.identifier("lazy"), [
85
+ t.identifier(lazyComponentName)
86
+ ]);
87
+ }
88
+ lazyComponentDeclarations.push(lazyDeclaration);
89
+ }
90
+ }
91
+ const componentNode = findTargetKeyNode(path.node.properties, "component");
92
+ if (componentNode && t.isObjectProperty(componentNode) && t.isCallExpression(componentNode.value) && t.isIdentifier(componentNode.value.callee)) {
93
+ componentName = `Component_${componentId}`;
94
+ const componentDeclaration = t.variableDeclaration("const", [
95
+ t.variableDeclarator(t.identifier(componentName), lazyComponentName ? t.callExpression(t.identifier("lazy"), [
96
+ t.identifier(lazyComponentName)
97
+ ]) : componentNode.value)
98
+ ]);
99
+ componentDeclarations.push(componentDeclaration);
100
+ componentNode.value = t.identifier(componentName);
101
+ }
102
+ if (lazyComponentName || componentName) {
103
+ componentId++;
104
+ if (componentName) {
105
+ const upperFirstName = componentName.slice(0, 1).toUpperCase() + componentName.slice(1);
106
+ const jsxElement = t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier(upperFirstName), [], true), null, [], true);
107
+ path.node.properties.push(t.objectProperty(t.identifier("element"), jsxElement));
108
+ }
109
+ }
110
+ }
111
+ });
112
+ (0, import_traverse.default)(ast, {
113
+ Program(path) {
114
+ const lastImportIndex = path.get("body").reduce((lastIndex, p, index) => {
115
+ if (t.isImportDeclaration(p.node)) {
116
+ lastIndex = index;
117
+ }
118
+ return lastIndex;
119
+ }, -1);
120
+ if (lastImportIndex >= 0) {
121
+ const lastImport = path.get(`body.${lastImportIndex}`);
122
+ [
123
+ ...componentDeclarations,
124
+ ...lazyComponentDeclarations
125
+ ].forEach((declaration) => {
126
+ if ("insertAfter" in lastImport) {
127
+ lastImport.insertAfter(declaration);
128
+ }
129
+ });
130
+ }
131
+ }
132
+ });
133
+ const { code: newCode } = (0, import_generator.default)(ast);
134
+ const finalCode = `${newCode}export const baseName = '${baseName}';`;
135
+ import_fs.default.writeFileSync(filePath, finalCode);
136
+ }
137
+ // Annotate the CommonJS export names for ESM import in node:
138
+ 0 && (module.exports = {
139
+ generateRoutes
140
+ });
@@ -0,0 +1,106 @@
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 generateSlimRoutes_exports = {};
30
+ __export(generateSlimRoutes_exports, {
31
+ generateSlimRoutes: () => generateSlimRoutes
32
+ });
33
+ module.exports = __toCommonJS(generateSlimRoutes_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
+ var import_constant = require("./constant");
40
+ function generateSlimRoutes({ sourceCode, filePath, prefix, baseName }) {
41
+ const ast = babelParser.parse(sourceCode, {
42
+ sourceType: "module"
43
+ });
44
+ const removedKeys = [
45
+ import_constant.COMPONENT,
46
+ import_constant.SHOULD_REVALIDATE,
47
+ import_constant.LAZY_COMPONENT,
48
+ import_constant.PRIVATE_COMPONENT
49
+ ];
50
+ (0, import_traverse.default)(ast, {
51
+ ImportDeclaration(path) {
52
+ const source = path.node.source.value;
53
+ const routeIdMatch = source.match(/routeId=([^&]+)/);
54
+ if (routeIdMatch) {
55
+ const originalRouteId = routeIdMatch[1];
56
+ const newRouteId = `${prefix}${originalRouteId}`;
57
+ const newSource = source.replace(/routeId=[^&]+/, `routeId=${newRouteId}`);
58
+ path.node.source = t.stringLiteral(newSource);
59
+ }
60
+ },
61
+ ObjectExpression(path) {
62
+ if (!Array.isArray(path.node.properties)) {
63
+ return;
64
+ }
65
+ path.node.properties.forEach((prop) => {
66
+ if (t.isObjectProperty(prop) && t.isStringLiteral(prop.key) && t.isStringLiteral(prop.value) && prop.key.value === import_constant.ID) {
67
+ prop.value = t.stringLiteral(`${prefix}${prop.value.value}`);
68
+ }
69
+ });
70
+ path.node.properties = path.node.properties.filter((p) => {
71
+ if (t.isObjectProperty(p) && t.isStringLiteral(p.key)) {
72
+ return !removedKeys.includes(p.key.value);
73
+ } else {
74
+ return true;
75
+ }
76
+ });
77
+ }
78
+ });
79
+ const tempCode = (0, import_generator.default)(ast).code;
80
+ const tempAst = babelParser.parse(tempCode, {
81
+ sourceType: "module"
82
+ });
83
+ const usedIdentifiers = /* @__PURE__ */ new Set();
84
+ (0, import_traverse.default)(tempAst, {
85
+ Identifier(path) {
86
+ if (t.isProperty(path.parent)) {
87
+ usedIdentifiers.add(path.node.name);
88
+ }
89
+ }
90
+ });
91
+ (0, import_traverse.default)(tempAst, {
92
+ ImportDeclaration(path) {
93
+ path.node.specifiers = path.node.specifiers.filter((specifier) => usedIdentifiers.has(specifier.local.name));
94
+ if (!path.node.specifiers.length) {
95
+ path.remove();
96
+ }
97
+ }
98
+ });
99
+ const { code: newCode } = (0, import_generator.default)(tempAst);
100
+ const finalCode = `${newCode}export const baseName = '${baseName}';`;
101
+ import_fs.default.writeFileSync(filePath, finalCode);
102
+ }
103
+ // Annotate the CommonJS export names for ESM import in node:
104
+ 0 && (module.exports = {
105
+ generateSlimRoutes
106
+ });
@@ -0,0 +1,31 @@
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 ast_exports = {};
20
+ __export(ast_exports, {
21
+ generateRoutes: () => import_generateRoutes.generateRoutes,
22
+ generateSlimRoutes: () => import_generateSlimRoutes.generateSlimRoutes
23
+ });
24
+ module.exports = __toCommonJS(ast_exports);
25
+ var import_generateRoutes = require("./generateRoutes");
26
+ var import_generateSlimRoutes = require("./generateSlimRoutes");
27
+ // Annotate the CommonJS export names for ESM import in node:
28
+ 0 && (module.exports = {
29
+ generateRoutes,
30
+ generateSlimRoutes
31
+ });
@@ -68,6 +68,7 @@ const moduleFederationConfigPlugin = (userConfig) => ({
68
68
  return {
69
69
  config: async () => {
70
70
  var _modernjsConfig_server, _modernjsConfig_dev;
71
+ console.log("config plugin config");
71
72
  const bundlerType = useAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
72
73
  const ipv4 = (0, import_utils.getIPV4)();
73
74
  const enableSSR = Boolean(modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
@@ -18,11 +18,14 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var constant_exports = {};
20
20
  __export(constant_exports, {
21
+ MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME: () => MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME,
21
22
  isDev: () => isDev
22
23
  });
23
24
  module.exports = __toCommonJS(constant_exports);
24
25
  const isDev = process.env.NODE_ENV === "development";
26
+ const MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME = "routes.js";
25
27
  // Annotate the CommonJS export names for ESM import in node:
26
28
  0 && (module.exports = {
29
+ MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME,
27
30
  isDev
28
31
  });
@@ -0,0 +1,205 @@
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 dataLoaderPlugin_exports = {};
30
+ __export(dataLoaderPlugin_exports, {
31
+ default: () => dataLoaderPlugin_default,
32
+ generateRoutes: () => import_ast.generateRoutes,
33
+ generateSlimRoutes: () => import_ast.generateSlimRoutes,
34
+ moduleFederationDataLoaderPlugin: () => moduleFederationDataLoaderPlugin
35
+ });
36
+ module.exports = __toCommonJS(dataLoaderPlugin_exports);
37
+ var import_path = __toESM(require("path"));
38
+ var import_utils = require("@modern-js/utils");
39
+ var import_utils2 = require("../runtime/utils");
40
+ var import_constant = require("../constant");
41
+ var import_constant2 = require("../runtime/constant");
42
+ var import_ast = require("./ast");
43
+ var import_constant3 = require("./constant");
44
+ function generateExtraExposeFiles(options) {
45
+ const { routesFilePath, mfConfig, isServer, baseName } = options;
46
+ const outputDir = import_path.default.resolve(process.cwd(), "node_modules/.federation");
47
+ import_utils.fs.ensureDirSync(outputDir);
48
+ const addSuffix = (fileName) => {
49
+ if (!isServer) {
50
+ return `${fileName}.jsx`;
51
+ }
52
+ return `${fileName}.server.jsx`;
53
+ };
54
+ const routesFileContent = import_utils.fs.readFileSync(routesFilePath, "utf-8");
55
+ const outputSlimRoutesPath = import_path.default.resolve(outputDir, addSuffix(import_constant2.MF_SLIM_ROUTES));
56
+ const outputFullRoutesPath = import_path.default.resolve(outputDir, addSuffix(import_constant2.MF_FULL_ROUTES));
57
+ const outputRoutesMetaPath = import_path.default.resolve(outputDir, `${import_constant2.MF_ROUTES_META}.js`);
58
+ (0, import_ast.generateSlimRoutes)({
59
+ sourceCode: routesFileContent,
60
+ filePath: outputSlimRoutesPath,
61
+ prefix: (0, import_utils2.transformName2Prefix)(mfConfig.name),
62
+ baseName
63
+ });
64
+ (0, import_ast.generateRoutes)({
65
+ sourceCode: routesFileContent,
66
+ filePath: outputFullRoutesPath,
67
+ prefix: (0, import_utils2.transformName2Prefix)(mfConfig.name),
68
+ baseName
69
+ });
70
+ import_utils.fs.writeFileSync(outputRoutesMetaPath, `export const baseName = '${baseName}';`);
71
+ return {
72
+ outputSlimRoutesPath,
73
+ outputFullRoutesPath,
74
+ outputRoutesMetaPath
75
+ };
76
+ }
77
+ function addExpose(options) {
78
+ const { mfConfig } = options;
79
+ const { outputSlimRoutesPath, outputFullRoutesPath, outputRoutesMetaPath } = generateExtraExposeFiles(options);
80
+ const fullRoutesKey = `./${import_constant2.MF_FULL_ROUTES}`;
81
+ const slimRoutesKey = `./${import_constant2.MF_SLIM_ROUTES}`;
82
+ const routeMetaKey = `./${import_constant2.MF_ROUTES_META}`;
83
+ if (!mfConfig.exposes) {
84
+ mfConfig.exposes = {
85
+ [fullRoutesKey]: outputFullRoutesPath,
86
+ [slimRoutesKey]: outputSlimRoutesPath,
87
+ [routeMetaKey]: outputRoutesMetaPath
88
+ };
89
+ } else {
90
+ if (!Array.isArray(mfConfig.exposes)) {
91
+ if (!mfConfig.exposes[fullRoutesKey]) {
92
+ mfConfig.exposes[fullRoutesKey] = outputFullRoutesPath;
93
+ }
94
+ if (!mfConfig.exposes[slimRoutesKey]) {
95
+ mfConfig.exposes[slimRoutesKey] = outputSlimRoutesPath;
96
+ }
97
+ if (!mfConfig.exposes[routeMetaKey]) {
98
+ mfConfig.exposes[routeMetaKey] = outputRoutesMetaPath;
99
+ }
100
+ }
101
+ }
102
+ }
103
+ function addShared(options) {
104
+ const { pkgName, mfConfig } = options;
105
+ const alias = `@${pkgName}/runtime/router`;
106
+ if (!mfConfig.shared) {
107
+ mfConfig.shared = {
108
+ [alias]: {
109
+ singleton: true
110
+ }
111
+ };
112
+ } else {
113
+ if (!Array.isArray(mfConfig.shared)) {
114
+ mfConfig.shared[alias] = {
115
+ singleton: true
116
+ };
117
+ } else {
118
+ mfConfig.shared.push(alias);
119
+ }
120
+ }
121
+ }
122
+ function _pathMfConfig(options) {
123
+ addShared(options);
124
+ addExpose(options);
125
+ }
126
+ const moduleFederationDataLoaderPlugin = (enable, internalOptions, userConfig) => ({
127
+ name: "@modern-js/plugin-module-federation-data-loader",
128
+ pre: [
129
+ "@modern-js/plugin-module-federation-config"
130
+ ],
131
+ post: [
132
+ "@modern-js/plugin-router",
133
+ "@modern-js/plugin-module-federation"
134
+ ],
135
+ setup: async ({ useConfigContext, useAppContext }) => {
136
+ var _modernjsConfig_server, _internalOptions_csrConfig;
137
+ if (!enable) {
138
+ return;
139
+ }
140
+ const { baseName, partialSSRRemotes = [], fetchSSRByRouteIds, patchMFConfig, pkgName = "modern-js", serverPlugin = "@module-federation/modern-js/data-loader-server" } = userConfig;
141
+ if (!baseName) {
142
+ throw new Error(`${import_constant.PLUGIN_IDENTIFIER} 'baseName' is required if you enable 'dataLoader'!`);
143
+ }
144
+ const modernjsConfig = useConfigContext();
145
+ const appContext = useAppContext();
146
+ const enableSSR = Boolean(modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
147
+ const name = (_internalOptions_csrConfig = internalOptions.csrConfig) === null || _internalOptions_csrConfig === void 0 ? void 0 : _internalOptions_csrConfig.name;
148
+ const routesFilePath = import_path.default.resolve(appContext.internalDirectory, `./main/${import_constant3.MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME}`);
149
+ return {
150
+ _internalRuntimePlugins: ({ entrypoint, plugins }) => {
151
+ plugins.push({
152
+ name: "ssrDataLoader",
153
+ path: "@module-federation/modern-js/data-loader",
154
+ config: {}
155
+ });
156
+ return {
157
+ entrypoint,
158
+ plugins
159
+ };
160
+ },
161
+ _internalServerPlugins({ plugins }) {
162
+ plugins.push({
163
+ name: serverPlugin,
164
+ options: internalOptions
165
+ });
166
+ return {
167
+ plugins
168
+ };
169
+ },
170
+ config: async () => {
171
+ console.log("dataloader plugin config");
172
+ const patchMFConfigFn = patchMFConfig || _pathMfConfig;
173
+ return {
174
+ // server: {
175
+ // ssrByRouteIds: ssrByRouteIds,
176
+ // },
177
+ tools: {
178
+ rspack(_config, { isServer }) {
179
+ patchMFConfigFn({
180
+ mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
181
+ baseName,
182
+ pkgName,
183
+ isServer,
184
+ routesFilePath
185
+ });
186
+ console.log("dataloader plugin rspack");
187
+ }
188
+ },
189
+ source: {
190
+ define: {
191
+ MODERN_ROUTER_ID_PREFIX: JSON.stringify((0, import_utils2.transformName2Prefix)(name))
192
+ }
193
+ }
194
+ };
195
+ }
196
+ };
197
+ }
198
+ });
199
+ var dataLoaderPlugin_default = moduleFederationDataLoaderPlugin;
200
+ // Annotate the CommonJS export names for ESM import in node:
201
+ 0 && (module.exports = {
202
+ generateRoutes,
203
+ generateSlimRoutes,
204
+ moduleFederationDataLoaderPlugin
205
+ });
@@ -27,6 +27,7 @@ var import_enhanced = require("@module-federation/enhanced");
27
27
  var import_rspack = require("@module-federation/enhanced/rspack");
28
28
  var import_configPlugin = require("./configPlugin");
29
29
  var import_ssrPlugin = require("./ssrPlugin");
30
+ var import_dataLoaderPlugin = require("./dataLoaderPlugin");
30
31
  var import_enhanced2 = require("@module-federation/enhanced");
31
32
  const moduleFederationPlugin = (userConfig = {}) => {
32
33
  const internalModernPluginOptions = {
@@ -79,7 +80,11 @@ const moduleFederationPlugin = (userConfig = {}) => {
79
80
  },
80
81
  usePlugins: [
81
82
  (0, import_configPlugin.moduleFederationConfigPlugin)(internalModernPluginOptions),
82
- (0, import_ssrPlugin.moduleFederationSSRPlugin)(internalModernPluginOptions)
83
+ (0, import_ssrPlugin.moduleFederationSSRPlugin)(internalModernPluginOptions),
84
+ (0, import_dataLoaderPlugin.moduleFederationDataLoaderPlugin)(Boolean(userConfig.dataLoader), internalModernPluginOptions, {
85
+ baseName: "",
86
+ ...userConfig.dataLoader
87
+ })
83
88
  ]
84
89
  };
85
90
  };
@@ -0,0 +1,87 @@
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 dataLoaderPlugin_exports = {};
20
+ __export(dataLoaderPlugin_exports, {
21
+ default: () => dataLoaderPlugin_default
22
+ });
23
+ module.exports = __toCommonJS(dataLoaderPlugin_exports);
24
+ var import_runtime = require("@module-federation/enhanced/runtime");
25
+ var import_constant = require("../../runtime/constant");
26
+ var dataLoaderPlugin_default = (options) => ({
27
+ name: "MFDataLoaderServerPlugin",
28
+ pre: [
29
+ "@modern-js/plugin-inject-resource"
30
+ ],
31
+ setup(api) {
32
+ const mfConfig = options.csrConfig;
33
+ const remotes = mfConfig.remotes;
34
+ if (!remotes || !Object.keys(remotes).length) {
35
+ return {};
36
+ }
37
+ const runtimeRemotes = Object.entries(remotes).map((item) => {
38
+ const [_alias, nameAndEntry] = item;
39
+ const [name, entry] = nameAndEntry.split("@");
40
+ return {
41
+ name,
42
+ entry
43
+ };
44
+ });
45
+ let isHandled = false;
46
+ return {
47
+ prepare() {
48
+ const { middlewares } = api.useAppContext();
49
+ middlewares.push({
50
+ name: "MFDataLoaderServerPlugin",
51
+ handler: async (c, next) => {
52
+ console.log("isHandled : ", isHandled);
53
+ const serverManifest = c.get("serverManifest");
54
+ const { loaderBundles } = serverManifest;
55
+ console.log("loaderBundles.main.routes : ", loaderBundles.main.routes);
56
+ if (isHandled) {
57
+ await next();
58
+ } else {
59
+ const instance = (0, import_runtime.getInstance)() || (0, import_runtime.init)({
60
+ name: mfConfig.name,
61
+ remotes: runtimeRemotes
62
+ });
63
+ const slimRoutes = await Promise.all(runtimeRemotes.map(async (remote) => {
64
+ const { routes, baseName } = await instance.loadRemote(`${remote.name}/${import_constant.MF_SLIM_ROUTES}`);
65
+ return {
66
+ routes,
67
+ baseName
68
+ };
69
+ }));
70
+ slimRoutes.forEach((slimRoute) => {
71
+ const { routes, baseName } = slimRoute;
72
+ routes[0].path = `/${baseName}`;
73
+ loaderBundles.main.routes.push(...routes);
74
+ });
75
+ console.log("loaderBundles.main.routes : ", loaderBundles.main.routes);
76
+ isHandled = true;
77
+ await next();
78
+ }
79
+ },
80
+ before: [
81
+ "render"
82
+ ]
83
+ });
84
+ }
85
+ };
86
+ }
87
+ });
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
+ var route_exports = {};
16
+ module.exports = __toCommonJS(route_exports);