@module-federation/modern-js 0.0.0-next-20240919023345 → 0.0.0-next-20240919075108

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 (99) hide show
  1. package/dist/cjs/cli/{dataLoader/ast/index.js → constant.js} +6 -6
  2. package/dist/cjs/cli/index.js +1 -5
  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 -10
  12. package/dist/esm/cli/constant.js +4 -0
  13. package/dist/esm/cli/index.js +2 -6
  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 +8 -13
  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 -11
  22. package/dist/esm-node/cli/constant.js +4 -0
  23. package/dist/esm-node/cli/index.js +1 -5
  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 -10
  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 +0 -8
  40. package/package.json +11 -48
  41. package/dist/cjs/cli/dataLoader/ast/constant.js +0 -49
  42. package/dist/cjs/cli/dataLoader/ast/generateRoutes.js +0 -141
  43. package/dist/cjs/cli/dataLoader/clearMFCache.js +0 -57
  44. package/dist/cjs/cli/dataLoader/generateRouteFile.js +0 -147
  45. package/dist/cjs/cli/dataLoader/patchMFConfig.js +0 -137
  46. package/dist/cjs/cli/dataLoader/plugin.js +0 -208
  47. package/dist/cjs/cli/mfRuntimePlugins/node.js +0 -41
  48. package/dist/cjs/cli/server/dataLoaderPlugin.js +0 -86
  49. package/dist/cjs/cli/server/fetchRemoteFullRoutesMap.js +0 -72
  50. package/dist/cjs/interfaces/route.js +0 -16
  51. package/dist/cjs/runtime/dataLoader/plugin-inject-assets.js +0 -94
  52. package/dist/cjs/runtime/dataLoader/plugin.js +0 -77
  53. package/dist/cjs/runtime/dataLoader/utils.js +0 -106
  54. package/dist/esm/cli/dataLoader/ast/constant.js +0 -18
  55. package/dist/esm/cli/dataLoader/ast/generateRoutes.js +0 -108
  56. package/dist/esm/cli/dataLoader/ast/index.js +0 -4
  57. package/dist/esm/cli/dataLoader/clearMFCache.js +0 -34
  58. package/dist/esm/cli/dataLoader/generateRouteFile.js +0 -307
  59. package/dist/esm/cli/dataLoader/patchMFConfig.js +0 -103
  60. package/dist/esm/cli/dataLoader/plugin.js +0 -328
  61. package/dist/esm/cli/mfRuntimePlugins/node.js +0 -13
  62. package/dist/esm/cli/server/dataLoaderPlugin.js +0 -109
  63. package/dist/esm/cli/server/fetchRemoteFullRoutesMap.js +0 -93
  64. package/dist/esm/interfaces/route.js +0 -0
  65. package/dist/esm/runtime/dataLoader/plugin-inject-assets.js +0 -73
  66. package/dist/esm/runtime/dataLoader/plugin.js +0 -99
  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/clearMFCache.js +0 -33
  73. package/dist/esm-node/cli/dataLoader/generateRouteFile.js +0 -112
  74. package/dist/esm-node/cli/dataLoader/patchMFConfig.js +0 -103
  75. package/dist/esm-node/cli/dataLoader/plugin.js +0 -173
  76. package/dist/esm-node/cli/mfRuntimePlugins/node.js +0 -11
  77. package/dist/esm-node/cli/server/dataLoaderPlugin.js +0 -66
  78. package/dist/esm-node/cli/server/fetchRemoteFullRoutesMap.js +0 -47
  79. package/dist/esm-node/interfaces/route.js +0 -0
  80. package/dist/esm-node/runtime/dataLoader/plugin-inject-assets.js +0 -70
  81. package/dist/esm-node/runtime/dataLoader/plugin.js +0 -53
  82. package/dist/esm-node/runtime/dataLoader/utils.js +0 -77
  83. package/dist/esm-node/runtime/withMFRouteId.js +0 -7
  84. package/dist/types/cli/dataLoader/ast/constant.d.ts +0 -8
  85. package/dist/types/cli/dataLoader/ast/generateRoutes.d.ts +0 -5
  86. package/dist/types/cli/dataLoader/ast/index.d.ts +0 -1
  87. package/dist/types/cli/dataLoader/clearMFCache.d.ts +0 -1
  88. package/dist/types/cli/dataLoader/generateRouteFile.d.ts +0 -10
  89. package/dist/types/cli/dataLoader/patchMFConfig.d.ts +0 -10
  90. package/dist/types/cli/dataLoader/plugin.d.ts +0 -6
  91. package/dist/types/cli/mfRuntimePlugins/node.d.ts +0 -3
  92. package/dist/types/cli/server/dataLoaderPlugin.d.ts +0 -10
  93. package/dist/types/cli/server/fetchRemoteFullRoutesMap.d.ts +0 -11
  94. package/dist/types/interfaces/route.d.ts +0 -12
  95. package/dist/types/runtime/dataLoader/plugin-inject-assets.d.ts +0 -4
  96. package/dist/types/runtime/dataLoader/plugin.d.ts +0 -5
  97. package/dist/types/runtime/dataLoader/utils.d.ts +0 -22
  98. package/dist/types/runtime/withMFRouteId.d.ts +0 -1
  99. /package/dist/types/cli/mfRuntimePlugins/{node-fetch.d.ts → inject-node-fetch.d.ts} +0 -0
@@ -1,99 +0,0 @@
1
- import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
- import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
- import { getInstance } from "@module-federation/enhanced/runtime";
4
- import { MF_ROUTES, DEFAULT_ENTRY } from "../../constant";
5
- import { injectRemoteRoutes } from "./utils";
6
- var ssrDataLoaderPlugin = function(param) {
7
- var metaName = param.metaName, dataLoaderRemotes = param.dataLoaderRemotes;
8
- return {
9
- name: "@modern-js/plugin-mf-data-loader",
10
- post: [
11
- "@".concat(metaName, "/plugin-router"),
12
- "@module-federation/modern-js"
13
- ],
14
- setup: function() {
15
- var remoteRoutesMap = {};
16
- return {
17
- beforeRender: function beforeRender() {
18
- return _async_to_generator(function() {
19
- var instance;
20
- return _ts_generator(this, function(_state) {
21
- switch (_state.label) {
22
- case 0:
23
- console.log("init ", dataLoaderRemotes);
24
- if (Object.keys(remoteRoutesMap).length) {
25
- return [
26
- 2
27
- ];
28
- }
29
- instance = getInstance();
30
- if (!instance) {
31
- console.log("no instance!");
32
- return [
33
- 2
34
- ];
35
- }
36
- if (!instance.options.remotes.length) {
37
- return [
38
- 2
39
- ];
40
- }
41
- return [
42
- 4,
43
- Promise.all(instance.options.remotes.map(function() {
44
- var _ref = _async_to_generator(function(remote) {
45
- var remoteId, routes;
46
- return _ts_generator(this, function(_state2) {
47
- switch (_state2.label) {
48
- case 0:
49
- if (!dataLoaderRemotes.includes(remote.name)) {
50
- return [
51
- 2
52
- ];
53
- }
54
- remoteId = "".concat(remote.name, "/").concat(DEFAULT_ENTRY, "/").concat(MF_ROUTES);
55
- return [
56
- 4,
57
- instance.loadRemote(remoteId)
58
- ];
59
- case 1:
60
- routes = _state2.sent().routes;
61
- remoteRoutesMap[remote.name] = {
62
- routes
63
- };
64
- return [
65
- 2
66
- ];
67
- }
68
- });
69
- });
70
- return function(remote) {
71
- return _ref.apply(this, arguments);
72
- };
73
- }()))
74
- ];
75
- case 1:
76
- _state.sent();
77
- return [
78
- 2
79
- ];
80
- }
81
- });
82
- })();
83
- },
84
- // runtime plugin not save route , so it needs to inject again when trigger
85
- modifyRoutes: function(routes) {
86
- injectRemoteRoutes({
87
- entry: {
88
- routes
89
- }
90
- }, remoteRoutesMap);
91
- return routes;
92
- }
93
- };
94
- }
95
- };
96
- };
97
- export {
98
- ssrDataLoaderPlugin
99
- };
@@ -1,77 +0,0 @@
1
- import { encodeName, decodeName } from "@module-federation/sdk";
2
- import { DEFAULT_ENTRY, DEFAULT_LAYOUT, MF_ROUTES } from "../../constant";
3
- var SPLIT_SYMBOL = "@";
4
- function transformName2Prefix(name) {
5
- return "".concat(encodeName(name, "", false)).concat(SPLIT_SYMBOL);
6
- }
7
- function decodeId(prefix) {
8
- var realPrefix = prefix.split(SPLIT_SYMBOL)[0];
9
- return decodeName(realPrefix, "", false);
10
- }
11
- function getRemoteRoutesInfos(route, remoteRoutesMap) {
12
- if (!route.id) {
13
- return;
14
- }
15
- var remoteName = decodeId(route.id);
16
- var remoteRouteObj = remoteRoutesMap[remoteName];
17
- if (!remoteRouteObj) {
18
- return;
19
- }
20
- var remoteRoutes = remoteRouteObj.routes[0];
21
- return {
22
- routes: remoteRoutes,
23
- name: remoteName,
24
- pathName: route.path
25
- };
26
- }
27
- function getRemoteLayoutId(remoteName) {
28
- var prefix = transformName2Prefix(remoteName);
29
- return "".concat(prefix).concat(DEFAULT_LAYOUT);
30
- }
31
- function getRemoteId(remoteName) {
32
- return "".concat(remoteName, "/").concat(DEFAULT_ENTRY, "/").concat(MF_ROUTES);
33
- }
34
- function injectRemoteRoutes(initialRoutes, remoteRoutesMap) {
35
- var traverse = function(route) {
36
- var _route_children;
37
- var remoteRoutesInfos = getRemoteRoutesInfos(route, remoteRoutesMap);
38
- if (remoteRoutesInfos) {
39
- var routes = remoteRoutesInfos.routes, pathName = remoteRoutesInfos.pathName;
40
- route.id = routes.id;
41
- route.path = pathName;
42
- if (routes.loader) {
43
- route.loader = routes.loader;
44
- }
45
- if (routes.children) {
46
- route.children = routes.children;
47
- }
48
- route.element = routes.element;
49
- route.Component = routes.Component;
50
- return;
51
- }
52
- (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.forEach(function(r) {
53
- traverse(r);
54
- });
55
- };
56
- Object.keys(initialRoutes).forEach(function(entryName) {
57
- var routes = initialRoutes[entryName];
58
- if (Array.isArray(routes)) {
59
- routes.forEach(function(route) {
60
- traverse(route);
61
- });
62
- } else {
63
- var _routes_routes;
64
- routes === null || routes === void 0 ? void 0 : (_routes_routes = routes.routes) === null || _routes_routes === void 0 ? void 0 : _routes_routes.forEach(function(r) {
65
- traverse(r);
66
- });
67
- }
68
- });
69
- }
70
- export {
71
- decodeId,
72
- getRemoteId,
73
- getRemoteLayoutId,
74
- getRemoteRoutesInfos,
75
- injectRemoteRoutes,
76
- transformName2Prefix
77
- };
@@ -1,7 +0,0 @@
1
- function withMFRouteId(id) {
2
- var prefix = typeof MODERN_ROUTER_ID_PREFIX === "string" ? MODERN_ROUTER_ID_PREFIX : "";
3
- return prefix + id;
4
- }
5
- export {
6
- withMFRouteId
7
- };
@@ -1,18 +0,0 @@
1
- const IS_ROOT = "isRoot";
2
- const ID = "id";
3
- const COMPONENT = "component";
4
- const LAZY_COMPONENT = "lazyImport";
5
- const SHOULD_REVALIDATE = "shouldRevalidate";
6
- const PRIVATE_COMPONENT = "_component";
7
- const ELEMENT = "element";
8
- const LOADER = "loader";
9
- export {
10
- COMPONENT,
11
- ELEMENT,
12
- ID,
13
- IS_ROOT,
14
- LAZY_COMPONENT,
15
- LOADER,
16
- PRIVATE_COMPONENT,
17
- SHOULD_REVALIDATE
18
- };
@@ -1,107 +0,0 @@
1
- import fs from "fs";
2
- import traverse from "@babel/traverse";
3
- import * as babelParser from "@babel/parser";
4
- import generate from "@babel/generator";
5
- import * as t from "@babel/types";
6
- function findTargetKeyNode(nodeProperties, key) {
7
- return nodeProperties.find((p) => t.isObjectProperty(p) && t.isStringLiteral(p.key) && p.key.value === key);
8
- }
9
- function generateRoutes({ sourceCode, filePath }) {
10
- const ast = babelParser.parse(sourceCode, {
11
- sourceType: "module"
12
- });
13
- const lazyComponentDeclarations = [];
14
- const componentDeclarations = [];
15
- let componentId = 0;
16
- traverse(ast, {
17
- // ImportDeclaration(path) {
18
- // const source = path.node.source.value;
19
- // const routeIdMatch = source.match(/routeId=([^&]+)/);
20
- // if (routeIdMatch) {
21
- // const originalRouteId = routeIdMatch[1];
22
- // const newRouteId = `${prefix}${originalRouteId}`;
23
- // const newSource = source.replace(
24
- // /routeId=[^&]+/,
25
- // `routeId=${newRouteId}`,
26
- // );
27
- // path.node.source = t.stringLiteral(newSource);
28
- // }
29
- // },
30
- ObjectExpression(path) {
31
- let componentName = "";
32
- let lazyComponentName = "";
33
- if (!Array.isArray(path.node.properties)) {
34
- return;
35
- }
36
- const idNode = findTargetKeyNode(path.node.properties, "id");
37
- if (idNode && t.isObjectProperty(idNode) && t.isStringLiteral(idNode.value)) {
38
- }
39
- const isRootNode = findTargetKeyNode(path.node.properties, "isRoot");
40
- if (isRootNode && t.isObjectProperty(isRootNode) && t.isBooleanLiteral(isRootNode.value)) {
41
- isRootNode.value.value = false;
42
- }
43
- if (!isRootNode) {
44
- const lazyComponentNode = findTargetKeyNode(path.node.properties, "lazyImport");
45
- if (lazyComponentNode && t.isObjectProperty(lazyComponentNode) && t.isArrowFunctionExpression(lazyComponentNode.value)) {
46
- lazyComponentName = `LazyComponent_${componentId}`;
47
- const lazyDeclaration = t.variableDeclaration("const", [
48
- t.variableDeclarator(t.identifier(lazyComponentName), lazyComponentNode.value)
49
- ]);
50
- lazyComponentNode.value = t.identifier(lazyComponentName);
51
- const componentNode2 = findTargetKeyNode(path.node.properties, "component");
52
- if (componentNode2 && t.isObjectProperty(componentNode2) && t.isCallExpression(componentNode2.value) && t.isIdentifier(componentNode2.value.callee)) {
53
- componentNode2.value = t.callExpression(t.identifier("lazy"), [
54
- t.identifier(lazyComponentName)
55
- ]);
56
- }
57
- lazyComponentDeclarations.push(lazyDeclaration);
58
- }
59
- }
60
- const componentNode = findTargetKeyNode(path.node.properties, "component");
61
- if (componentNode && t.isObjectProperty(componentNode) && t.isCallExpression(componentNode.value) && t.isIdentifier(componentNode.value.callee)) {
62
- componentName = `Component_${componentId}`;
63
- const componentDeclaration = t.variableDeclaration("const", [
64
- t.variableDeclarator(t.identifier(componentName), lazyComponentName ? t.callExpression(t.identifier("lazy"), [
65
- t.identifier(lazyComponentName)
66
- ]) : componentNode.value)
67
- ]);
68
- componentDeclarations.push(componentDeclaration);
69
- componentNode.value = t.identifier(componentName);
70
- }
71
- if (lazyComponentName || componentName) {
72
- componentId++;
73
- if (componentName) {
74
- const upperFirstName = componentName.slice(0, 1).toUpperCase() + componentName.slice(1);
75
- const jsxElement = t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier(upperFirstName), [], true), null, [], true);
76
- path.node.properties.push(t.objectProperty(t.identifier("element"), jsxElement));
77
- }
78
- }
79
- }
80
- });
81
- traverse(ast, {
82
- Program(path) {
83
- const lastImportIndex = path.get("body").reduce((lastIndex, p, index) => {
84
- if (t.isImportDeclaration(p.node)) {
85
- lastIndex = index;
86
- }
87
- return lastIndex;
88
- }, -1);
89
- if (lastImportIndex >= 0) {
90
- const lastImport = path.get(`body.${lastImportIndex}`);
91
- [
92
- ...componentDeclarations,
93
- ...lazyComponentDeclarations
94
- ].forEach((declaration) => {
95
- if ("insertAfter" in lastImport) {
96
- lastImport.insertAfter(declaration);
97
- }
98
- });
99
- }
100
- }
101
- });
102
- const { code } = generate(ast);
103
- fs.writeFileSync(filePath, code);
104
- }
105
- export {
106
- generateRoutes
107
- };
@@ -1,4 +0,0 @@
1
- import { generateRoutes } from "./generateRoutes";
2
- export {
3
- generateRoutes
4
- };
@@ -1,33 +0,0 @@
1
- function clearMFCache() {
2
- if (!globalThis.__FEDERATION__) {
3
- console.log("clearMFCache no globalThis.__FEDERATION__");
4
- return;
5
- }
6
- globalThis.__FEDERATION__.__INSTANCES__.map((i) => {
7
- i.moduleCache.forEach((mc) => {
8
- if (mc.remoteInfo && mc.remoteInfo.entryGlobalName) {
9
- delete globalThis[mc.remoteInfo.entryGlobalName];
10
- }
11
- });
12
- i.moduleCache.clear();
13
- if (globalThis[i.name]) {
14
- delete globalThis[i.name];
15
- }
16
- });
17
- Object.keys(globalThis.__FEDERATION__).forEach((key) => {
18
- if (Array.isArray(globalThis.__FEDERATION__[key])) {
19
- globalThis.__FEDERATION__[key] = [];
20
- } else if (key === "__PRELOADED_MAP__") {
21
- globalThis.__FEDERATION__[key] = /* @__PURE__ */ new Map();
22
- } else if (typeof globalThis.__FEDERATION__[key] === "object") {
23
- globalThis.__FEDERATION__[key] = {};
24
- }
25
- });
26
- if (globalThis.__GLOBAL_LOADING_REMOTE_ENTRY__) {
27
- globalThis.__GLOBAL_LOADING_REMOTE_ENTRY__ = {};
28
- }
29
- globalThis.FORCE_MF_REFRESH = true;
30
- }
31
- export {
32
- clearMFCache
33
- };
@@ -1,112 +0,0 @@
1
- import { fs, normalizeToPosixPath } from "@modern-js/utils";
2
- import path from "path";
3
- import { MF_JSON, ROUTE_ID } from "../../constant";
4
- import { getRemoteLayoutId } from "../../runtime/dataLoader/utils";
5
- const ROUTES = "routes";
6
- function calcPathName(filePath, rootDir, entry) {
7
- const relativePath = path.relative(rootDir, filePath);
8
- const conditionalReg = /^\[(.*)\]$/;
9
- const splitPaths = relativePath.split("/").reduce((sum, cur) => {
10
- if (cur.includes(".")) {
11
- const split = cur.split(".").map((i) => {
12
- const match = i.match(conditionalReg);
13
- if (match) {
14
- return `:${match[1]}`;
15
- }
16
- return i.startsWith("__") ? "" : i;
17
- });
18
- sum.push(...split);
19
- } else {
20
- const match = cur.match(conditionalReg);
21
- if (match) {
22
- sum.push(`:${match[1]}`);
23
- } else {
24
- if (!cur.startsWith("__")) {
25
- sum.push(cur);
26
- }
27
- }
28
- }
29
- return sum;
30
- }, []);
31
- return path.join(entry, splitPaths.filter((p) => !!p).join("/"));
32
- }
33
- async function generateRouteFile(options) {
34
- const { appDirectory } = options;
35
- const entries = [];
36
- const remotePathMap = {};
37
- const srcPath = `${appDirectory}/src`;
38
- const traverse = async (filepath, rootDir, entry) => {
39
- if (!await fs.pathExists(filepath)) {
40
- return null;
41
- }
42
- const isDirectory = (await fs.stat(filepath)).isDirectory();
43
- if (!isDirectory) {
44
- return null;
45
- }
46
- const items = await fs.readdir(filepath);
47
- const mfJson = items.find((i) => i === MF_JSON);
48
- if (mfJson) {
49
- const mfJsonContent = await fs.readJSON(normalizeToPosixPath(path.join(filepath, mfJson)));
50
- const { remoteName } = mfJsonContent;
51
- const remoteLayoutId = getRemoteLayoutId(remoteName);
52
- const writeFile = async (id, type, pathname) => {
53
- const generatedFilePath = normalizeToPosixPath(path.join(filepath, `${type}.tsx`));
54
- const content = [
55
- "// Do not edit this file. It is a auto generated.",
56
- `// It will render ${remoteName}'s ${type}.`,
57
- `// The ${type} pathname is: ${pathname}`,
58
- `// The ${type} route id is: ${ROUTE_ID}`
59
- ];
60
- await fs.writeFile(generatedFilePath, content.join("\n"));
61
- return generatedFilePath;
62
- };
63
- if (remoteLayoutId) {
64
- const pathname = calcPathName(filepath, rootDir, entry);
65
- const filePath = await writeFile(remoteLayoutId, "layout", pathname);
66
- remotePathMap[pathname] = {
67
- name: remoteName,
68
- path: filePath
69
- };
70
- }
71
- return;
72
- }
73
- await Promise.all(items.map(async (item) => {
74
- await traverse(normalizeToPosixPath(path.join(filepath, item)), rootDir, entry);
75
- }));
76
- };
77
- const traverseRoutes = async (dir, level = 1) => {
78
- if (level > 3) {
79
- return;
80
- }
81
- const isDirectory = (await fs.stat(dir)).isDirectory();
82
- if (!isDirectory) {
83
- return;
84
- }
85
- const items = await fs.readdir(dir);
86
- if (items.find((i) => i === ROUTES)) {
87
- const entry = path.relative(srcPath, dir);
88
- entries.push(entry);
89
- const rootDir = `${dir}/${ROUTES}`;
90
- await traverse(rootDir, rootDir, entry);
91
- } else {
92
- await Promise.all(items.map(async (item) => {
93
- await traverseRoutes(path.join(dir, item), level++);
94
- }));
95
- }
96
- };
97
- await traverseRoutes(srcPath);
98
- const normalizedEntries = entries.map((entry) => {
99
- if (entry === "") {
100
- return "main";
101
- }
102
- return entry;
103
- });
104
- return {
105
- entries: normalizedEntries,
106
- remotePathMap
107
- };
108
- }
109
- export {
110
- calcPathName,
111
- generateRouteFile
112
- };
@@ -1,103 +0,0 @@
1
- import path from "path";
2
- import { fs } from "@modern-js/utils";
3
- import { MODERN_JS_ROUTE_SERVER_LOADER } from "../../constant";
4
- import { MF_ROUTES, MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME } from "../../constant";
5
- import { generateRoutes } from "./ast";
6
- function generateExtraExposeFiles(options) {
7
- const { internalDirectory, mfConfig, isServer, entries } = options;
8
- const entryMap = {};
9
- const outputDir = path.resolve(process.cwd(), "node_modules/.federation/data-loader");
10
- fs.ensureDirSync(outputDir);
11
- const addSuffix = (fileName, ext = ".jsx") => {
12
- if (!isServer) {
13
- return `${fileName}${ext}`;
14
- }
15
- return `${fileName}.server${ext}`;
16
- };
17
- const generateEntryRoutes = (entry) => {
18
- const outputEntryDir = path.resolve(outputDir, entry);
19
- fs.ensureDirSync(outputEntryDir);
20
- const sourceDir = path.resolve(internalDirectory, entry);
21
- const routesFilePath = path.resolve(sourceDir, addSuffix(MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME, ".js"));
22
- const routesFileContent = fs.readFileSync(routesFilePath, "utf-8");
23
- const outputFullRoutesPath = path.resolve(outputEntryDir, addSuffix(MF_ROUTES));
24
- generateRoutes({
25
- sourceCode: routesFileContent,
26
- filePath: outputFullRoutesPath
27
- });
28
- const routeServerLoaderPath = path.resolve(sourceDir, `${MODERN_JS_ROUTE_SERVER_LOADER}.js`);
29
- const outputRouteServerLoaderPath = path.resolve(outputEntryDir, `${MODERN_JS_ROUTE_SERVER_LOADER}.js`);
30
- const clientRouteServerLoaderPath = outputRouteServerLoaderPath.replace(MODERN_JS_ROUTE_SERVER_LOADER, `${MODERN_JS_ROUTE_SERVER_LOADER}-client`);
31
- if (isServer) {
32
- const routeServerLoaderContent = fs.readFileSync(routeServerLoaderPath, "utf-8");
33
- generateRoutes({
34
- sourceCode: routeServerLoaderContent,
35
- filePath: outputRouteServerLoaderPath
36
- });
37
- } else {
38
- fs.writeFileSync(clientRouteServerLoaderPath, `export const routes = []`);
39
- }
40
- entryMap[entry] = {
41
- routesPath: outputFullRoutesPath,
42
- routeServerLoaderPath: outputRouteServerLoaderPath,
43
- clientRouteServerLoaderPath
44
- };
45
- };
46
- entries.forEach((entry) => {
47
- generateEntryRoutes(entry);
48
- });
49
- return {
50
- entryMap
51
- };
52
- }
53
- function addExpose(options) {
54
- const { mfConfig, isServer } = options;
55
- const { entryMap } = generateExtraExposeFiles(options);
56
- const addExposeByEntry = (entry, routesPath, routeServerLoaderPath, clientRouteServerLoaderPath) => {
57
- if (!mfConfig.exposes) {
58
- mfConfig.exposes = {};
59
- }
60
- const routesKey = `./${entry}/${MF_ROUTES}`;
61
- if (!mfConfig.exposes[routesKey]) {
62
- mfConfig.exposes[routesKey] = routesPath;
63
- }
64
- const routeServerLoaderKey = `./${entry}/${MODERN_JS_ROUTE_SERVER_LOADER}`;
65
- if (!mfConfig.exposes[routeServerLoaderKey]) {
66
- if (isServer) {
67
- mfConfig.exposes[routeServerLoaderKey] = routeServerLoaderPath;
68
- } else {
69
- mfConfig.exposes[routeServerLoaderKey] = clientRouteServerLoaderPath;
70
- }
71
- }
72
- };
73
- Object.keys(entryMap).forEach((entry) => {
74
- const { routesPath, routeServerLoaderPath, clientRouteServerLoaderPath } = entryMap[entry];
75
- addExposeByEntry(entry, routesPath, routeServerLoaderPath, clientRouteServerLoaderPath);
76
- });
77
- }
78
- function addShared(options) {
79
- const { metaName, mfConfig } = options;
80
- const alias = `@${metaName}/runtime/router`;
81
- if (!mfConfig.shared) {
82
- mfConfig.shared = {
83
- [alias]: {
84
- singleton: true
85
- }
86
- };
87
- } else {
88
- if (!Array.isArray(mfConfig.shared)) {
89
- mfConfig.shared[alias] = {
90
- singleton: true
91
- };
92
- } else {
93
- mfConfig.shared.push(alias);
94
- }
95
- }
96
- }
97
- function patchMFConfig(options) {
98
- addShared(options);
99
- addExpose(options);
100
- }
101
- export {
102
- patchMFConfig
103
- };