@module-federation/modern-js 0.0.0-next-20240909073915 → 0.0.0-next-20240909092952
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.
- package/dist/cjs/cli/dataLoader/ast/constant.js +49 -0
- package/dist/cjs/cli/dataLoader/ast/generateRoutes.js +141 -0
- package/dist/cjs/cli/{constant.js → dataLoader/ast/index.js} +6 -6
- package/dist/cjs/cli/dataLoader/generateRouteFile.js +143 -0
- package/dist/cjs/cli/dataLoader/patchMFConfig.js +132 -0
- package/dist/cjs/cli/dataLoader/plugin.js +194 -0
- package/dist/cjs/cli/index.js +5 -1
- package/dist/cjs/cli/mfRuntimePlugins/inject-node-fetch.js +7 -10
- package/dist/cjs/cli/mfRuntimePlugins/node.js +44 -0
- package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +40 -38
- package/dist/cjs/cli/server/dataLoaderPlugin.js +82 -0
- package/dist/cjs/cli/server/fetchRemoteFullRoutesMap.js +69 -0
- package/dist/cjs/cli/ssrPlugin.js +1 -1
- package/dist/cjs/cli/utils.js +8 -4
- package/dist/cjs/cli/utils.spec.js +1 -1
- package/dist/cjs/constant.js +32 -2
- package/dist/cjs/interfaces/route.js +16 -0
- package/dist/cjs/runtime/dataLoader/plugin.js +73 -0
- package/dist/cjs/runtime/dataLoader/utils.js +89 -0
- package/dist/cjs/runtime/index.js +4 -1
- package/dist/cjs/runtime/utils.js +38 -0
- package/dist/cjs/runtime/withMFRouteId.js +31 -0
- package/dist/cjs/ssr-runtime/plugin.js +7 -7
- package/dist/esm/cli/dataLoader/ast/constant.js +18 -0
- package/dist/esm/cli/dataLoader/ast/generateRoutes.js +108 -0
- package/dist/esm/cli/dataLoader/ast/index.js +4 -0
- package/dist/esm/cli/dataLoader/generateRouteFile.js +293 -0
- package/dist/esm/cli/dataLoader/patchMFConfig.js +98 -0
- package/dist/esm/cli/dataLoader/plugin.js +209 -0
- package/dist/esm/cli/index.js +4 -1
- package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js +7 -10
- package/dist/esm/cli/mfRuntimePlugins/node.js +16 -0
- package/dist/esm/cli/server/dataLoaderPlugin.js +105 -0
- package/dist/esm/cli/server/fetchRemoteFullRoutesMap.js +88 -0
- package/dist/esm/cli/ssrPlugin.js +1 -1
- package/dist/esm/cli/utils.js +6 -3
- package/dist/esm/cli/utils.spec.js +1 -1
- package/dist/esm/constant.js +21 -1
- package/dist/esm/interfaces/route.js +0 -0
- package/dist/esm/runtime/dataLoader/plugin.js +92 -0
- package/dist/esm/runtime/dataLoader/utils.js +63 -0
- package/dist/esm/runtime/index.js +3 -1
- package/dist/esm/runtime/utils.js +13 -0
- package/dist/esm/runtime/withMFRouteId.js +7 -0
- package/dist/esm/ssr-runtime/plugin.js +7 -10
- package/dist/esm-node/cli/dataLoader/ast/constant.js +18 -0
- package/dist/esm-node/cli/dataLoader/ast/generateRoutes.js +107 -0
- package/dist/esm-node/cli/dataLoader/ast/index.js +4 -0
- package/dist/esm-node/cli/dataLoader/generateRouteFile.js +108 -0
- package/dist/esm-node/cli/dataLoader/patchMFConfig.js +98 -0
- package/dist/esm-node/cli/dataLoader/plugin.js +159 -0
- package/dist/esm-node/cli/index.js +5 -1
- package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.js +7 -10
- package/dist/esm-node/cli/mfRuntimePlugins/node.js +14 -0
- package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +40 -38
- package/dist/esm-node/cli/server/dataLoaderPlugin.js +62 -0
- package/dist/esm-node/cli/server/fetchRemoteFullRoutesMap.js +44 -0
- package/dist/esm-node/cli/ssrPlugin.js +1 -1
- package/dist/esm-node/cli/utils.js +6 -3
- package/dist/esm-node/cli/utils.spec.js +1 -1
- package/dist/esm-node/constant.js +21 -1
- package/dist/esm-node/interfaces/route.js +0 -0
- package/dist/esm-node/runtime/dataLoader/plugin.js +49 -0
- package/dist/esm-node/runtime/dataLoader/utils.js +63 -0
- package/dist/esm-node/runtime/index.js +3 -1
- package/dist/esm-node/runtime/utils.js +13 -0
- package/dist/esm-node/runtime/withMFRouteId.js +7 -0
- package/dist/esm-node/ssr-runtime/plugin.js +7 -7
- package/dist/types/cli/dataLoader/ast/constant.d.ts +8 -0
- package/dist/types/cli/dataLoader/ast/generateRoutes.d.ts +5 -0
- package/dist/types/cli/dataLoader/ast/index.d.ts +1 -0
- package/dist/types/cli/dataLoader/generateRouteFile.d.ts +7 -0
- package/dist/types/cli/dataLoader/patchMFConfig.d.ts +10 -0
- package/dist/types/cli/dataLoader/plugin.d.ts +6 -0
- package/dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts +2 -2
- package/dist/types/cli/mfRuntimePlugins/node.d.ts +3 -0
- package/dist/types/cli/server/dataLoaderPlugin.d.ts +10 -0
- package/dist/types/cli/server/fetchRemoteFullRoutesMap.d.ts +10 -0
- package/dist/types/cli/utils.d.ts +2 -0
- package/dist/types/constant.d.ts +10 -0
- package/dist/types/interfaces/route.d.ts +12 -0
- package/dist/types/runtime/dataLoader/plugin.d.ts +2 -0
- package/dist/types/runtime/dataLoader/utils.d.ts +19 -0
- package/dist/types/runtime/index.d.ts +1 -0
- package/dist/types/runtime/utils.d.ts +2 -0
- package/dist/types/runtime/withMFRouteId.d.ts +1 -0
- package/dist/types/types/index.d.ts +9 -0
- package/package.json +40 -11
- package/dist/esm/cli/constant.js +0 -4
- package/dist/esm-node/cli/constant.js +0 -4
- package/dist/types/cli/constant.d.ts +0 -1
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { DEFAULT_LAYOUT } from "../../constant";
|
|
2
|
+
import { transformName2Prefix, transformPrefix2Name } from "../utils";
|
|
3
|
+
function getRemoteRoutesInfos(route, remoteRoutesMap) {
|
|
4
|
+
if (!route.id) {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
var remoteName = transformPrefix2Name(route.id);
|
|
8
|
+
var remoteRouteObj = remoteRoutesMap[remoteName];
|
|
9
|
+
if (!remoteRouteObj) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
var remoteRoutes = remoteRouteObj.routes[0];
|
|
13
|
+
return {
|
|
14
|
+
routes: remoteRoutes,
|
|
15
|
+
name: remoteName,
|
|
16
|
+
pathName: route.path
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function getRemoteLayoutId(remoteName) {
|
|
20
|
+
var prefix = transformName2Prefix(remoteName);
|
|
21
|
+
return "".concat(prefix).concat(DEFAULT_LAYOUT);
|
|
22
|
+
}
|
|
23
|
+
function injectRemoteRoutes(initialRoutes, remoteRoutesMap) {
|
|
24
|
+
var traverse = function(route) {
|
|
25
|
+
var _route_children;
|
|
26
|
+
var remoteRoutesInfos = getRemoteRoutesInfos(route, remoteRoutesMap);
|
|
27
|
+
if (remoteRoutesInfos) {
|
|
28
|
+
var routes = remoteRoutesInfos.routes, pathName = remoteRoutesInfos.pathName;
|
|
29
|
+
route.id = routes.id;
|
|
30
|
+
route.path = pathName;
|
|
31
|
+
if (routes.loader) {
|
|
32
|
+
route.loader = routes.loader;
|
|
33
|
+
}
|
|
34
|
+
if (routes.children) {
|
|
35
|
+
route.children = routes.children;
|
|
36
|
+
}
|
|
37
|
+
route.element = routes.element;
|
|
38
|
+
route.Component = routes.Component;
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
(_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.forEach(function(r) {
|
|
42
|
+
traverse(r);
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
Object.keys(initialRoutes).forEach(function(entryName) {
|
|
46
|
+
var routes = initialRoutes[entryName];
|
|
47
|
+
if (Array.isArray(routes)) {
|
|
48
|
+
routes.forEach(function(route) {
|
|
49
|
+
traverse(route);
|
|
50
|
+
});
|
|
51
|
+
} else {
|
|
52
|
+
var _routes_routes;
|
|
53
|
+
routes === null || routes === void 0 ? void 0 : (_routes_routes = routes.routes) === null || _routes_routes === void 0 ? void 0 : _routes_routes.forEach(function(r) {
|
|
54
|
+
traverse(r);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
export {
|
|
60
|
+
getRemoteLayoutId,
|
|
61
|
+
getRemoteRoutesInfos,
|
|
62
|
+
injectRemoteRoutes
|
|
63
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { encodeName, decodeName } from "@module-federation/sdk";
|
|
2
|
+
var SPLIT_SYMBOL = "@";
|
|
3
|
+
function transformName2Prefix(name) {
|
|
4
|
+
return "".concat(encodeName(name, "", false)).concat(SPLIT_SYMBOL);
|
|
5
|
+
}
|
|
6
|
+
function transformPrefix2Name(prefix) {
|
|
7
|
+
var realPrefix = prefix.split(SPLIT_SYMBOL)[0];
|
|
8
|
+
return decodeName(realPrefix, "", false);
|
|
9
|
+
}
|
|
10
|
+
export {
|
|
11
|
+
transformName2Prefix,
|
|
12
|
+
transformPrefix2Name
|
|
13
|
+
};
|
|
@@ -4,13 +4,16 @@ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
|
4
4
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
5
5
|
import hoistNonReactStatics from "hoist-non-react-statics";
|
|
6
6
|
import { SSRLiveReload } from "./SSRLiveReload";
|
|
7
|
+
console.log("mfSSRPlugin trigger");
|
|
7
8
|
var mfSSRPlugin = function() {
|
|
8
9
|
return {
|
|
9
10
|
name: "@module-federation/modern-js",
|
|
11
|
+
pre: [
|
|
12
|
+
"@modern-js/plugin-router"
|
|
13
|
+
],
|
|
10
14
|
setup: function() {
|
|
11
15
|
return {
|
|
12
|
-
|
|
13
|
-
var context = param.context;
|
|
16
|
+
beforeRender: function beforeRender() {
|
|
14
17
|
return _async_to_generator(function() {
|
|
15
18
|
var nodeUtils, shouldUpdate;
|
|
16
19
|
return _ts_generator(this, function(_state) {
|
|
@@ -18,10 +21,7 @@ var mfSSRPlugin = function() {
|
|
|
18
21
|
case 0:
|
|
19
22
|
if (typeof window !== "undefined") {
|
|
20
23
|
return [
|
|
21
|
-
2
|
|
22
|
-
next({
|
|
23
|
-
context
|
|
24
|
-
})
|
|
24
|
+
2
|
|
25
25
|
];
|
|
26
26
|
}
|
|
27
27
|
globalThis.shouldUpdate = false;
|
|
@@ -53,10 +53,7 @@ var mfSSRPlugin = function() {
|
|
|
53
53
|
_state.label = 4;
|
|
54
54
|
case 4:
|
|
55
55
|
return [
|
|
56
|
-
2
|
|
57
|
-
next({
|
|
58
|
-
context
|
|
59
|
-
})
|
|
56
|
+
2
|
|
60
57
|
];
|
|
61
58
|
}
|
|
62
59
|
});
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,107 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { fs, normalizeToPosixPath } from "@modern-js/utils";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { MF_JSON } 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: ${id}`
|
|
59
|
+
];
|
|
60
|
+
await fs.writeFile(generatedFilePath, content.join("\n"));
|
|
61
|
+
};
|
|
62
|
+
if (remoteLayoutId) {
|
|
63
|
+
const pathname = calcPathName(filepath, rootDir, entry);
|
|
64
|
+
writeFile(remoteLayoutId, "layout", pathname);
|
|
65
|
+
remotePathMap[pathname] = remoteName;
|
|
66
|
+
}
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
await Promise.all(items.map(async (item) => {
|
|
70
|
+
await traverse(normalizeToPosixPath(path.join(filepath, item)), rootDir, entry);
|
|
71
|
+
}));
|
|
72
|
+
};
|
|
73
|
+
const traverseRoutes = async (dir, level = 1) => {
|
|
74
|
+
if (level > 3) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const isDirectory = (await fs.stat(dir)).isDirectory();
|
|
78
|
+
if (!isDirectory) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const items = await fs.readdir(dir);
|
|
82
|
+
if (items.find((i) => i === ROUTES)) {
|
|
83
|
+
const entry = path.relative(srcPath, dir);
|
|
84
|
+
entries.push(entry);
|
|
85
|
+
const rootDir = `${dir}/${ROUTES}`;
|
|
86
|
+
await traverse(rootDir, rootDir, entry);
|
|
87
|
+
} else {
|
|
88
|
+
await Promise.all(items.map(async (item) => {
|
|
89
|
+
await traverseRoutes(path.join(dir, item), level++);
|
|
90
|
+
}));
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
await traverseRoutes(srcPath);
|
|
94
|
+
const normalizedEntries = entries.map((entry) => {
|
|
95
|
+
if (entry === "") {
|
|
96
|
+
return "main";
|
|
97
|
+
}
|
|
98
|
+
return entry;
|
|
99
|
+
});
|
|
100
|
+
return {
|
|
101
|
+
entries: normalizedEntries,
|
|
102
|
+
remotePathMap
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
export {
|
|
106
|
+
calcPathName,
|
|
107
|
+
generateRouteFile
|
|
108
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
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
|
+
if (isServer) {
|
|
31
|
+
const routeServerLoaderContent = fs.readFileSync(routeServerLoaderPath, "utf-8");
|
|
32
|
+
generateRoutes({
|
|
33
|
+
sourceCode: routeServerLoaderContent,
|
|
34
|
+
filePath: outputRouteServerLoaderPath
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
entryMap[entry] = {
|
|
38
|
+
routesPath: outputFullRoutesPath,
|
|
39
|
+
routeServerLoaderPath: outputRouteServerLoaderPath
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
entries.forEach((entry) => {
|
|
43
|
+
generateEntryRoutes(entry);
|
|
44
|
+
});
|
|
45
|
+
return {
|
|
46
|
+
entryMap
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function addExpose(options) {
|
|
50
|
+
const { mfConfig, isServer } = options;
|
|
51
|
+
if (mfConfig.remotes && mfConfig.exposes) {
|
|
52
|
+
throw new Error(`dataLoader only support for pure provider/consumer !`);
|
|
53
|
+
}
|
|
54
|
+
const { entryMap } = generateExtraExposeFiles(options);
|
|
55
|
+
const addExposeByEntry = (entry, routesPath, routeServerLoaderPath) => {
|
|
56
|
+
if (!mfConfig.exposes) {
|
|
57
|
+
mfConfig.exposes = {};
|
|
58
|
+
}
|
|
59
|
+
const routesKey = `./${entry}/${MF_ROUTES}`;
|
|
60
|
+
if (!mfConfig.exposes[routesKey]) {
|
|
61
|
+
mfConfig.exposes[routesKey] = routesPath;
|
|
62
|
+
}
|
|
63
|
+
const routeServerLoaderKey = `./${entry}/${MODERN_JS_ROUTE_SERVER_LOADER}`;
|
|
64
|
+
if (isServer && !mfConfig.exposes[routeServerLoaderKey]) {
|
|
65
|
+
mfConfig.exposes[routeServerLoaderKey] = routeServerLoaderPath;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
Object.keys(entryMap).forEach((entry) => {
|
|
69
|
+
const { routesPath, routeServerLoaderPath } = entryMap[entry];
|
|
70
|
+
addExposeByEntry(entry, routesPath, routeServerLoaderPath);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
function addShared(options) {
|
|
74
|
+
const { metaName, mfConfig } = options;
|
|
75
|
+
const alias = `@${metaName}/runtime/router`;
|
|
76
|
+
if (!mfConfig.shared) {
|
|
77
|
+
mfConfig.shared = {
|
|
78
|
+
[alias]: {
|
|
79
|
+
singleton: true
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
} else {
|
|
83
|
+
if (!Array.isArray(mfConfig.shared)) {
|
|
84
|
+
mfConfig.shared[alias] = {
|
|
85
|
+
singleton: true
|
|
86
|
+
};
|
|
87
|
+
} else {
|
|
88
|
+
mfConfig.shared.push(alias);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function patchMFConfig(options) {
|
|
93
|
+
addShared(options);
|
|
94
|
+
addExpose(options);
|
|
95
|
+
}
|
|
96
|
+
export {
|
|
97
|
+
patchMFConfig
|
|
98
|
+
};
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { transformName2Prefix } from "../../runtime/utils";
|
|
2
|
+
import { DEFAULT_ENTRY } from "../../constant";
|
|
3
|
+
import { META_NAME } from "../../constant";
|
|
4
|
+
import { generateRoutes } from "./ast";
|
|
5
|
+
import { generateRouteFile } from "./generateRouteFile";
|
|
6
|
+
import { patchMFConfig } from "./patchMFConfig";
|
|
7
|
+
import path from "path";
|
|
8
|
+
function _transformRuntimeOptions(buildOptions) {
|
|
9
|
+
const remotes = buildOptions.remotes || {};
|
|
10
|
+
const runtimeRemotes = Object.entries(remotes).map((remote) => {
|
|
11
|
+
const [alias, nameAndEntry] = remote;
|
|
12
|
+
const [name, entry] = nameAndEntry.split("@");
|
|
13
|
+
return {
|
|
14
|
+
name,
|
|
15
|
+
entry,
|
|
16
|
+
alias
|
|
17
|
+
};
|
|
18
|
+
});
|
|
19
|
+
return {
|
|
20
|
+
name: buildOptions.name,
|
|
21
|
+
remotes: runtimeRemotes
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
const moduleFederationDataLoaderPlugin = (enable, internalOptions, userConfig) => ({
|
|
25
|
+
name: "@modern-js/plugin-module-federation-data-loader",
|
|
26
|
+
pre: [
|
|
27
|
+
"@modern-js/plugin-module-federation-config"
|
|
28
|
+
],
|
|
29
|
+
post: [
|
|
30
|
+
"@modern-js/plugin-router",
|
|
31
|
+
"@modern-js/plugin-module-federation"
|
|
32
|
+
],
|
|
33
|
+
setup: async ({ useAppContext }) => {
|
|
34
|
+
if (!enable) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const {
|
|
38
|
+
// patchMFConfig,
|
|
39
|
+
metaName = META_NAME,
|
|
40
|
+
serverPlugin = "@module-federation/modern-js/data-loader-server",
|
|
41
|
+
transformRuntimeOptions
|
|
42
|
+
} = userConfig;
|
|
43
|
+
const appContext = useAppContext();
|
|
44
|
+
const name = internalOptions.csrConfig.name;
|
|
45
|
+
const internalDirectory = appContext.internalDirectory.replace(META_NAME, metaName || META_NAME);
|
|
46
|
+
const transformRuntimeFn = transformRuntimeOptions || _transformRuntimeOptions;
|
|
47
|
+
const partialSSRRemotes = [];
|
|
48
|
+
const entries = /* @__PURE__ */ new Set();
|
|
49
|
+
let remotePathMap = {};
|
|
50
|
+
const ssrByRouteIdsMap = {};
|
|
51
|
+
const serverPluginOptions = {
|
|
52
|
+
runtimeOptions: transformRuntimeFn(internalOptions.csrConfig),
|
|
53
|
+
partialSSRRemotes,
|
|
54
|
+
ssrByRouteIdsMap
|
|
55
|
+
};
|
|
56
|
+
const csrConfig = internalOptions.csrConfig;
|
|
57
|
+
if (typeof csrConfig.remotes === "object" && Object.keys(csrConfig.remotes).length) {
|
|
58
|
+
const { entries: scanEntries, remotePathMap: scanRemotePathMap } = await generateRouteFile({
|
|
59
|
+
appDirectory: appContext.appDirectory
|
|
60
|
+
});
|
|
61
|
+
scanEntries.forEach((e) => entries.add(e));
|
|
62
|
+
remotePathMap = scanRemotePathMap;
|
|
63
|
+
} else {
|
|
64
|
+
entries.add(DEFAULT_ENTRY);
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
_internalRuntimePlugins: ({ entrypoint, plugins }) => {
|
|
68
|
+
plugins.push({
|
|
69
|
+
name: "ssrDataLoader",
|
|
70
|
+
path: "@module-federation/modern-js/data-loader",
|
|
71
|
+
config: {}
|
|
72
|
+
});
|
|
73
|
+
return {
|
|
74
|
+
entrypoint,
|
|
75
|
+
plugins
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
_internalServerPlugins({ plugins }) {
|
|
79
|
+
plugins.push({
|
|
80
|
+
name: serverPlugin,
|
|
81
|
+
options: serverPluginOptions
|
|
82
|
+
});
|
|
83
|
+
return {
|
|
84
|
+
plugins
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
async modifyFileSystemRoutes(options) {
|
|
88
|
+
const { routes, entrypoint } = options;
|
|
89
|
+
const { entryName } = entrypoint;
|
|
90
|
+
const prefix = transformName2Prefix(internalOptions.csrConfig.name);
|
|
91
|
+
const modifyRouteIds = (route, pathName) => {
|
|
92
|
+
const currentPathName = route.path ? path.join(pathName, route.path) : pathName;
|
|
93
|
+
if (route.id) {
|
|
94
|
+
var _route_children;
|
|
95
|
+
const originalId = route.id;
|
|
96
|
+
route.originalId = originalId;
|
|
97
|
+
const remoteName = remotePathMap[currentPathName];
|
|
98
|
+
if (remoteName) {
|
|
99
|
+
const remoteIdPrefix = transformName2Prefix(remoteName);
|
|
100
|
+
const isLayout = Boolean(route.children);
|
|
101
|
+
const newId = `${remoteIdPrefix}${isLayout ? "layout" : "page"}`;
|
|
102
|
+
ssrByRouteIdsMap[originalId] = newId;
|
|
103
|
+
route.id = newId;
|
|
104
|
+
if (!partialSSRRemotes.includes(remoteName)) {
|
|
105
|
+
partialSSRRemotes.push(remoteName);
|
|
106
|
+
}
|
|
107
|
+
} else {
|
|
108
|
+
route.id = `${prefix}${route.id}`;
|
|
109
|
+
}
|
|
110
|
+
(_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.forEach((r) => {
|
|
111
|
+
modifyRouteIds(r, currentPathName);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
routes.forEach((route) => {
|
|
116
|
+
modifyRouteIds(route, entryName);
|
|
117
|
+
});
|
|
118
|
+
return options;
|
|
119
|
+
},
|
|
120
|
+
config: async () => {
|
|
121
|
+
return {
|
|
122
|
+
tools: {
|
|
123
|
+
// bundlerChain can not keep target order
|
|
124
|
+
rspack(_config, { isServer }) {
|
|
125
|
+
patchMFConfig({
|
|
126
|
+
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
127
|
+
metaName,
|
|
128
|
+
isServer,
|
|
129
|
+
internalDirectory,
|
|
130
|
+
entries
|
|
131
|
+
});
|
|
132
|
+
},
|
|
133
|
+
// bundlerChain can not keep target order
|
|
134
|
+
webpack(_config, { isServer }) {
|
|
135
|
+
patchMFConfig({
|
|
136
|
+
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
137
|
+
metaName,
|
|
138
|
+
isServer,
|
|
139
|
+
internalDirectory,
|
|
140
|
+
entries
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
source: {
|
|
145
|
+
define: {
|
|
146
|
+
MODERN_ROUTER_ID_PREFIX: JSON.stringify(transformName2Prefix(name))
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
var plugin_default = moduleFederationDataLoaderPlugin;
|
|
155
|
+
export {
|
|
156
|
+
plugin_default as default,
|
|
157
|
+
generateRoutes,
|
|
158
|
+
moduleFederationDataLoaderPlugin
|
|
159
|
+
};
|
|
@@ -2,6 +2,7 @@ import { ModuleFederationPlugin as WebpackModuleFederationPlugin, AsyncBoundaryP
|
|
|
2
2
|
import { ModuleFederationPlugin as RspackModuleFederationPlugin } from "@module-federation/enhanced/rspack";
|
|
3
3
|
import { moduleFederationConfigPlugin } from "./configPlugin";
|
|
4
4
|
import { moduleFederationSSRPlugin } from "./ssrPlugin";
|
|
5
|
+
import { moduleFederationDataLoaderPlugin } from "./dataLoader/plugin";
|
|
5
6
|
const moduleFederationPlugin = (userConfig = {}) => {
|
|
6
7
|
const internalModernPluginOptions = {
|
|
7
8
|
csrConfig: void 0,
|
|
@@ -53,7 +54,10 @@ const moduleFederationPlugin = (userConfig = {}) => {
|
|
|
53
54
|
},
|
|
54
55
|
usePlugins: [
|
|
55
56
|
moduleFederationConfigPlugin(internalModernPluginOptions),
|
|
56
|
-
moduleFederationSSRPlugin(internalModernPluginOptions)
|
|
57
|
+
moduleFederationSSRPlugin(internalModernPluginOptions),
|
|
58
|
+
moduleFederationDataLoaderPlugin(Boolean(userConfig.dataLoader), internalModernPluginOptions, {
|
|
59
|
+
...typeof userConfig.dataLoader === "boolean" ? {} : userConfig.dataLoader
|
|
60
|
+
})
|
|
57
61
|
]
|
|
58
62
|
};
|
|
59
63
|
};
|
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
return args;
|
|
9
|
-
}
|
|
1
|
+
import nodeRuntimePlugin from "@module-federation/node/runtimePlugin";
|
|
2
|
+
const isContainer = typeof __filename === "string" ? __filename.includes("remote") ? true : false : false;
|
|
3
|
+
const pluginName = `node-plugin-${isContainer}`;
|
|
4
|
+
const nodePlugin = () => ({
|
|
5
|
+
name: pluginName,
|
|
6
|
+
beforeInit: nodeRuntimePlugin().beforeInit
|
|
10
7
|
});
|
|
11
|
-
var inject_node_fetch_default =
|
|
8
|
+
var inject_node_fetch_default = nodePlugin;
|
|
12
9
|
export {
|
|
13
10
|
inject_node_fetch_default as default
|
|
14
11
|
};
|