@module-federation/modern-js 0.0.0-next-20241014092946 → 0.0.0-next-20241015060033

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 (103) 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 -7
  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 -8
  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 -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 +0 -8
  40. package/package.json +13 -50
  41. package/dist/cjs/cli/dataLoader/ast/constant.js +0 -49
  42. package/dist/cjs/cli/dataLoader/ast/generateRoutes.js +0 -143
  43. package/dist/cjs/cli/dataLoader/clearMFCache.js +0 -57
  44. package/dist/cjs/cli/dataLoader/generateRouteFile.js +0 -148
  45. package/dist/cjs/cli/dataLoader/patchMFConfig.js +0 -160
  46. package/dist/cjs/cli/dataLoader/plugin.js +0 -219
  47. package/dist/cjs/cli/mfRuntimePlugins/node.js +0 -41
  48. package/dist/cjs/cli/server/dataLoaderPlugin.js +0 -81
  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 -92
  52. package/dist/cjs/runtime/dataLoader/plugin.js +0 -75
  53. package/dist/cjs/runtime/dataLoader/utils.js +0 -115
  54. package/dist/cjs/types/modern.js +0 -16
  55. package/dist/esm/cli/dataLoader/ast/constant.js +0 -18
  56. package/dist/esm/cli/dataLoader/ast/generateRoutes.js +0 -110
  57. package/dist/esm/cli/dataLoader/ast/index.js +0 -4
  58. package/dist/esm/cli/dataLoader/clearMFCache.js +0 -34
  59. package/dist/esm/cli/dataLoader/generateRouteFile.js +0 -308
  60. package/dist/esm/cli/dataLoader/patchMFConfig.js +0 -124
  61. package/dist/esm/cli/dataLoader/plugin.js +0 -341
  62. package/dist/esm/cli/mfRuntimePlugins/node.js +0 -13
  63. package/dist/esm/cli/server/dataLoaderPlugin.js +0 -104
  64. package/dist/esm/cli/server/fetchRemoteFullRoutesMap.js +0 -93
  65. package/dist/esm/interfaces/route.js +0 -0
  66. package/dist/esm/runtime/dataLoader/plugin-inject-assets.js +0 -71
  67. package/dist/esm/runtime/dataLoader/plugin.js +0 -97
  68. package/dist/esm/runtime/dataLoader/utils.js +0 -86
  69. package/dist/esm/runtime/withMFRouteId.js +0 -7
  70. package/dist/esm/types/modern.js +0 -0
  71. package/dist/esm-node/cli/dataLoader/ast/constant.js +0 -18
  72. package/dist/esm-node/cli/dataLoader/ast/generateRoutes.js +0 -109
  73. package/dist/esm-node/cli/dataLoader/ast/index.js +0 -4
  74. package/dist/esm-node/cli/dataLoader/clearMFCache.js +0 -33
  75. package/dist/esm-node/cli/dataLoader/generateRouteFile.js +0 -113
  76. package/dist/esm-node/cli/dataLoader/patchMFConfig.js +0 -126
  77. package/dist/esm-node/cli/dataLoader/plugin.js +0 -184
  78. package/dist/esm-node/cli/mfRuntimePlugins/node.js +0 -11
  79. package/dist/esm-node/cli/server/dataLoaderPlugin.js +0 -61
  80. package/dist/esm-node/cli/server/fetchRemoteFullRoutesMap.js +0 -47
  81. package/dist/esm-node/interfaces/route.js +0 -0
  82. package/dist/esm-node/runtime/dataLoader/plugin-inject-assets.js +0 -68
  83. package/dist/esm-node/runtime/dataLoader/plugin.js +0 -51
  84. package/dist/esm-node/runtime/dataLoader/utils.js +0 -86
  85. package/dist/esm-node/runtime/withMFRouteId.js +0 -7
  86. package/dist/esm-node/types/modern.js +0 -0
  87. package/dist/types/cli/dataLoader/ast/constant.d.ts +0 -8
  88. package/dist/types/cli/dataLoader/ast/generateRoutes.d.ts +0 -5
  89. package/dist/types/cli/dataLoader/ast/index.d.ts +0 -1
  90. package/dist/types/cli/dataLoader/clearMFCache.d.ts +0 -1
  91. package/dist/types/cli/dataLoader/generateRouteFile.d.ts +0 -10
  92. package/dist/types/cli/dataLoader/patchMFConfig.d.ts +0 -10
  93. package/dist/types/cli/dataLoader/plugin.d.ts +0 -6
  94. package/dist/types/cli/mfRuntimePlugins/node.d.ts +0 -3
  95. package/dist/types/cli/server/dataLoaderPlugin.d.ts +0 -10
  96. package/dist/types/cli/server/fetchRemoteFullRoutesMap.d.ts +0 -11
  97. package/dist/types/interfaces/route.d.ts +0 -12
  98. package/dist/types/runtime/dataLoader/plugin-inject-assets.d.ts +0 -4
  99. package/dist/types/runtime/dataLoader/plugin.d.ts +0 -5
  100. package/dist/types/runtime/dataLoader/utils.d.ts +0 -22
  101. package/dist/types/runtime/withMFRouteId.d.ts +0 -1
  102. package/dist/types/types/modern.d.ts +0 -12
  103. /package/dist/types/cli/mfRuntimePlugins/{node-fetch.d.ts → inject-node-fetch.d.ts} +0 -0
@@ -1,71 +0,0 @@
1
- import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
2
- import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
3
- import { useContext } from "react";
4
- import { getInstance } from "@module-federation/enhanced/runtime";
5
- import { collectSSRAssets } from "../createRemoteSSRComponent";
6
- import { RuntimeReactContext } from "@meta/runtime";
7
- import { decodeId, getRemoteId } from "./utils";
8
- function traverseRoutes(routes, remoteNames) {
9
- routes.forEach(function(route) {
10
- route.id && remoteNames.add(decodeId(route.id));
11
- route.children && traverseRoutes(route.children, remoteNames);
12
- });
13
- }
14
- var ssrDataLoaderInjectAssetsPlugin = function(param) {
15
- var metaName = param.metaName;
16
- return {
17
- name: "@modern-js/plugin-mf-data-loader-inject-assets",
18
- pre: [
19
- "@modern-js/plugin-mf-data-loader"
20
- ],
21
- post: [
22
- "@module-federation/modern-js",
23
- "@".concat(metaName, "/plugin-router")
24
- ],
25
- setup: function() {
26
- return {
27
- wrapRoot: function wrapRoot(App) {
28
- var AppWrapper = function(props) {
29
- var instance = getInstance();
30
- if (!instance || !instance.options.remotes.length) {
31
- return /* @__PURE__ */ _jsx(_Fragment, {
32
- children: props.children
33
- });
34
- }
35
- var context = useContext(RuntimeReactContext);
36
- var remotes = instance.options.remotes;
37
- var remoteNames = /* @__PURE__ */ new Set();
38
- traverseRoutes(context.routes, remoteNames);
39
- var remoteIds = _to_consumable_array(remoteNames).reduce(function(sum, cur) {
40
- var matchRemote = remotes.find(function(r) {
41
- return r.name === cur;
42
- });
43
- if (matchRemote) {
44
- sum.add(getRemoteId(cur));
45
- }
46
- return sum;
47
- }, /* @__PURE__ */ new Set());
48
- var assets = [];
49
- remoteIds.forEach(function(remoteId) {
50
- var _assets;
51
- (_assets = assets).push.apply(_assets, _to_consumable_array(collectSSRAssets({
52
- id: remoteId,
53
- injectScript: false
54
- })));
55
- });
56
- return /* @__PURE__ */ _jsxs(_Fragment, {
57
- children: [
58
- assets,
59
- props.children
60
- ]
61
- });
62
- };
63
- return AppWrapper;
64
- }
65
- };
66
- }
67
- };
68
- };
69
- export {
70
- ssrDataLoaderInjectAssetsPlugin
71
- };
@@ -1,97 +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
- if (Object.keys(remoteRoutesMap).length) {
24
- return [
25
- 2
26
- ];
27
- }
28
- instance = getInstance();
29
- if (!instance) {
30
- return [
31
- 2
32
- ];
33
- }
34
- if (!instance.options.remotes.length) {
35
- return [
36
- 2
37
- ];
38
- }
39
- return [
40
- 4,
41
- Promise.all(instance.options.remotes.map(function() {
42
- var _ref = _async_to_generator(function(remote) {
43
- var remoteId, routes;
44
- return _ts_generator(this, function(_state2) {
45
- switch (_state2.label) {
46
- case 0:
47
- if (!dataLoaderRemotes.includes(remote.name)) {
48
- return [
49
- 2
50
- ];
51
- }
52
- remoteId = "".concat(remote.name, "/").concat(DEFAULT_ENTRY, "/").concat(MF_ROUTES);
53
- return [
54
- 4,
55
- instance.loadRemote(remoteId)
56
- ];
57
- case 1:
58
- routes = _state2.sent().routes;
59
- remoteRoutesMap[remote.name] = {
60
- routes
61
- };
62
- return [
63
- 2
64
- ];
65
- }
66
- });
67
- });
68
- return function(remote) {
69
- return _ref.apply(this, arguments);
70
- };
71
- }()))
72
- ];
73
- case 1:
74
- _state.sent();
75
- return [
76
- 2
77
- ];
78
- }
79
- });
80
- })();
81
- },
82
- // runtime plugin not save route , so it needs to inject again when trigger
83
- modifyRoutes: function(routes) {
84
- injectRemoteRoutes({
85
- entry: {
86
- routes
87
- }
88
- }, remoteRoutesMap);
89
- return routes;
90
- }
91
- };
92
- }
93
- };
94
- };
95
- export {
96
- ssrDataLoaderPlugin
97
- };
@@ -1,86 +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
- if (routes.Component) {
49
- route.Component = routes.Component;
50
- if (route.element) {
51
- delete route.element;
52
- }
53
- } else if (routes.element) {
54
- route.element = routes.element;
55
- if (route.Component) {
56
- delete route.Component;
57
- }
58
- }
59
- return;
60
- }
61
- (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.forEach(function(r) {
62
- traverse(r);
63
- });
64
- };
65
- Object.keys(initialRoutes).forEach(function(entryName) {
66
- var routes = initialRoutes[entryName];
67
- if (Array.isArray(routes)) {
68
- routes.forEach(function(route) {
69
- traverse(route);
70
- });
71
- } else {
72
- var _routes_routes;
73
- routes === null || routes === void 0 ? void 0 : (_routes_routes = routes.routes) === null || _routes_routes === void 0 ? void 0 : _routes_routes.forEach(function(r) {
74
- traverse(r);
75
- });
76
- }
77
- });
78
- }
79
- export {
80
- decodeId,
81
- getRemoteId,
82
- getRemoteLayoutId,
83
- getRemoteRoutesInfos,
84
- injectRemoteRoutes,
85
- transformName2Prefix
86
- };
@@ -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
- };
File without changes
@@ -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,109 +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
- const lazyComponentNode = findTargetKeyNode(path.node.properties, "lazyImport");
44
- if (lazyComponentNode && t.isObjectProperty(lazyComponentNode) && t.isArrowFunctionExpression(lazyComponentNode.value)) {
45
- lazyComponentName = `LazyComponent_${componentId}`;
46
- const lazyDeclaration = t.variableDeclaration("const", [
47
- t.variableDeclarator(t.identifier(lazyComponentName), lazyComponentNode.value)
48
- ]);
49
- lazyComponentNode.value = t.identifier(lazyComponentName);
50
- const componentNode2 = findTargetKeyNode(path.node.properties, "component");
51
- if (componentNode2 && t.isObjectProperty(componentNode2) && t.isCallExpression(componentNode2.value) && t.isIdentifier(componentNode2.value.callee)) {
52
- componentNode2.value = t.callExpression(t.identifier("lazy"), [
53
- t.identifier(lazyComponentName)
54
- ]);
55
- }
56
- lazyComponentDeclarations.push(lazyDeclaration);
57
- }
58
- const componentNode = findTargetKeyNode(path.node.properties, "component");
59
- if (componentNode && t.isObjectProperty(componentNode) && t.isCallExpression(componentNode.value) && t.isIdentifier(componentNode.value.callee)) {
60
- componentName = `Component_${componentId}`;
61
- const componentDeclaration = t.variableDeclaration("const", [
62
- t.variableDeclarator(t.identifier(componentName), lazyComponentName ? t.callExpression(t.identifier("lazy"), [
63
- t.identifier(lazyComponentName)
64
- ]) : componentNode.value)
65
- ]);
66
- componentDeclarations.push(componentDeclaration);
67
- componentNode.value = t.identifier(componentName);
68
- }
69
- if (lazyComponentName || componentName) {
70
- componentId++;
71
- if (componentName) {
72
- const upperFirstName = componentName.slice(0, 1).toUpperCase() + componentName.slice(1);
73
- const jsxElement = t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier(upperFirstName), [], true), null, [], true);
74
- path.node.properties.push(t.objectProperty(t.identifier("element"), jsxElement));
75
- } else {
76
- path.node.properties.push(t.objectProperty(t.identifier("Component"), t.callExpression(t.identifier("lazy"), [
77
- t.identifier(lazyComponentName)
78
- ])));
79
- }
80
- }
81
- }
82
- });
83
- traverse(ast, {
84
- Program(path) {
85
- const lastImportIndex = path.get("body").reduce((lastIndex, p, index) => {
86
- if (t.isImportDeclaration(p.node)) {
87
- lastIndex = index;
88
- }
89
- return lastIndex;
90
- }, -1);
91
- if (lastImportIndex >= 0) {
92
- const lastImport = path.get(`body.${lastImportIndex}`);
93
- [
94
- ...componentDeclarations,
95
- ...lazyComponentDeclarations
96
- ].forEach((declaration) => {
97
- if ("insertAfter" in lastImport) {
98
- lastImport.insertAfter(declaration);
99
- }
100
- });
101
- }
102
- }
103
- });
104
- const { code } = generate(ast);
105
- fs.writeFileSync(filePath, code);
106
- }
107
- export {
108
- generateRoutes
109
- };
@@ -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,113 +0,0 @@
1
- import { fs, normalizeToPosixPath } from "@modern-js/utils";
2
- import path from "path";
3
- import os from "os";
4
- import { MF_JSON, ROUTE_ID } from "../../constant";
5
- import { getRemoteLayoutId } from "../../runtime/dataLoader/utils";
6
- const ROUTES = "routes";
7
- function calcPathName(filePath, rootDir, entry) {
8
- const relativePath = path.relative(rootDir, filePath);
9
- const conditionalReg = /^\[(.*)\]$/;
10
- const splitPaths = relativePath.split("/").reduce((sum, cur) => {
11
- if (cur.includes(".")) {
12
- const split = cur.split(".").map((i) => {
13
- const match = i.match(conditionalReg);
14
- if (match) {
15
- return `:${match[1]}`;
16
- }
17
- return i.startsWith("__") ? "" : i;
18
- });
19
- sum.push(...split);
20
- } else {
21
- const match = cur.match(conditionalReg);
22
- if (match) {
23
- sum.push(`:${match[1]}`);
24
- } else {
25
- if (!cur.startsWith("__")) {
26
- sum.push(cur);
27
- }
28
- }
29
- }
30
- return sum;
31
- }, []);
32
- return path.join(entry, splitPaths.filter((p) => !!p).join("/"));
33
- }
34
- async function generateRouteFile(options) {
35
- const { appDirectory } = options;
36
- const entries = [];
37
- const remotePathMap = {};
38
- const srcPath = `${appDirectory}/src`;
39
- const traverse = async (filepath, rootDir, entry) => {
40
- if (!await fs.pathExists(filepath)) {
41
- return null;
42
- }
43
- const isDirectory = (await fs.stat(filepath)).isDirectory();
44
- if (!isDirectory) {
45
- return null;
46
- }
47
- const items = await fs.readdir(filepath);
48
- const mfJson = items.find((i) => i === MF_JSON);
49
- if (mfJson) {
50
- const mfJsonContent = await fs.readJSON(normalizeToPosixPath(path.join(filepath, mfJson)));
51
- const { remoteName } = mfJsonContent;
52
- const remoteLayoutId = getRemoteLayoutId(remoteName);
53
- const writeFile = async (id, type, pathname) => {
54
- const generatedFilePath = normalizeToPosixPath(path.join(filepath, `${type}.tsx`));
55
- const content = [
56
- "// Do not edit this file. It is a auto generated.",
57
- `// It will render ${remoteName}'s ${type}.`,
58
- `// The ${type} pathname is: ${pathname}`,
59
- `// The ${type} route id is: ${ROUTE_ID}`
60
- ];
61
- await fs.writeFile(generatedFilePath, content.join(os.EOL));
62
- return generatedFilePath;
63
- };
64
- if (remoteLayoutId) {
65
- const pathname = calcPathName(filepath, rootDir, entry);
66
- const filePath = await writeFile(remoteLayoutId, "layout", pathname);
67
- remotePathMap[pathname] = {
68
- name: remoteName,
69
- path: filePath
70
- };
71
- }
72
- return;
73
- }
74
- await Promise.all(items.map(async (item) => {
75
- await traverse(normalizeToPosixPath(path.join(filepath, item)), rootDir, entry);
76
- }));
77
- };
78
- const traverseRoutes = async (dir, level = 1) => {
79
- if (level > 3) {
80
- return;
81
- }
82
- const isDirectory = (await fs.stat(dir)).isDirectory();
83
- if (!isDirectory) {
84
- return;
85
- }
86
- const items = await fs.readdir(dir);
87
- if (items.find((i) => i === ROUTES)) {
88
- const entry = path.relative(srcPath, dir);
89
- entries.push(entry);
90
- const rootDir = `${dir}/${ROUTES}`;
91
- await traverse(rootDir, rootDir, entry);
92
- } else {
93
- await Promise.all(items.map(async (item) => {
94
- await traverseRoutes(path.join(dir, item), level++);
95
- }));
96
- }
97
- };
98
- await traverseRoutes(srcPath);
99
- const normalizedEntries = entries.map((entry) => {
100
- if (entry === "") {
101
- return "main";
102
- }
103
- return entry;
104
- });
105
- return {
106
- entries: normalizedEntries,
107
- remotePathMap
108
- };
109
- }
110
- export {
111
- calcPathName,
112
- generateRouteFile
113
- };
@@ -1,126 +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 reactRouterDom = `react-router-dom`;
81
- const reactRouterDomServer = `react-router-dom/server`;
82
- if (!mfConfig.shared) {
83
- mfConfig.shared = {
84
- [reactRouterDom]: {
85
- singleton: true
86
- },
87
- [reactRouterDomServer]: {
88
- singleton: true
89
- }
90
- };
91
- } else {
92
- if (!Array.isArray(mfConfig.shared)) {
93
- mfConfig.shared[reactRouterDom] = {
94
- singleton: true
95
- };
96
- mfConfig.shared[reactRouterDomServer] = {
97
- singleton: true
98
- };
99
- } else {
100
- mfConfig.shared = mfConfig.shared.reduce((sum, cur) => {
101
- if (typeof cur === "string") {
102
- sum[cur] = {};
103
- } else {
104
- sum = {
105
- ...sum,
106
- ...cur
107
- };
108
- }
109
- return sum;
110
- }, {});
111
- mfConfig.shared[reactRouterDom] = {
112
- singleton: true
113
- };
114
- mfConfig.shared[reactRouterDomServer] = {
115
- singleton: true
116
- };
117
- }
118
- }
119
- }
120
- function patchMFConfig(options) {
121
- addShared(options);
122
- addExpose(options);
123
- }
124
- export {
125
- patchMFConfig
126
- };