@module-federation/modern-js 0.0.0-next-20240911083839 → 0.0.0-next-20240911090730
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/configPlugin.js +2 -4
- 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 +140 -0
- package/dist/cjs/cli/dataLoader/plugin.js +199 -0
- package/dist/cjs/cli/index.js +5 -1
- package/dist/cjs/cli/mfRuntimePlugins/{inject-node-fetch.js → node-fetch.js} +6 -6
- package/dist/cjs/cli/mfRuntimePlugins/node.js +41 -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 +7 -1
- package/dist/cjs/cli/utils.js +13 -8
- package/dist/cjs/cli/utils.spec.js +5 -6
- package/dist/cjs/constant.js +32 -2
- package/dist/cjs/interfaces/route.js +16 -0
- package/dist/cjs/runtime/dataLoader/plugin-inject-assets.js +84 -0
- package/dist/cjs/runtime/dataLoader/plugin.js +74 -0
- package/dist/cjs/runtime/dataLoader/utils.js +106 -0
- package/dist/cjs/runtime/index.js +4 -1
- package/dist/cjs/{cli/mfRuntimePlugins/shared-strategy.js → runtime/withMFRouteId.js} +11 -23
- package/dist/cjs/ssr-runtime/plugin.js +7 -7
- package/dist/esm/cli/configPlugin.js +2 -3
- 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 +106 -0
- package/dist/esm/cli/dataLoader/plugin.js +214 -0
- package/dist/esm/cli/index.js +4 -1
- package/dist/esm/cli/mfRuntimePlugins/{inject-node-fetch.js → node-fetch.js} +3 -3
- package/dist/esm/cli/mfRuntimePlugins/node.js +13 -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 +6 -1
- package/dist/esm/cli/utils.js +11 -7
- package/dist/esm/cli/utils.spec.js +5 -6
- package/dist/esm/constant.js +21 -1
- package/dist/esm/interfaces/route.js +0 -0
- package/dist/esm/runtime/dataLoader/plugin-inject-assets.js +63 -0
- package/dist/esm/runtime/dataLoader/plugin.js +93 -0
- package/dist/esm/runtime/dataLoader/utils.js +77 -0
- package/dist/esm/runtime/index.js +3 -1
- package/dist/esm/runtime/withMFRouteId.js +7 -0
- package/dist/esm/ssr-runtime/plugin.js +7 -10
- package/dist/esm-node/cli/configPlugin.js +2 -4
- 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 +106 -0
- package/dist/esm-node/cli/dataLoader/plugin.js +164 -0
- package/dist/esm-node/cli/index.js +5 -1
- package/dist/esm-node/cli/mfRuntimePlugins/{inject-node-fetch.js → node-fetch.js} +3 -3
- package/dist/esm-node/cli/mfRuntimePlugins/node.js +11 -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 +7 -1
- package/dist/esm-node/cli/utils.js +11 -7
- package/dist/esm-node/cli/utils.spec.js +5 -6
- package/dist/esm-node/constant.js +21 -1
- package/dist/esm-node/interfaces/route.js +0 -0
- package/dist/esm-node/runtime/dataLoader/plugin-inject-assets.js +60 -0
- package/dist/esm-node/runtime/dataLoader/plugin.js +50 -0
- package/dist/esm-node/runtime/dataLoader/utils.js +77 -0
- package/dist/esm-node/runtime/index.js +3 -1
- 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/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-inject-assets.d.ts +2 -0
- package/dist/types/runtime/dataLoader/plugin.d.ts +2 -0
- package/dist/types/runtime/dataLoader/utils.d.ts +22 -0
- package/dist/types/runtime/index.d.ts +1 -0
- package/dist/types/runtime/withMFRouteId.d.ts +1 -0
- package/dist/types/types/index.d.ts +9 -0
- package/package.json +48 -11
- package/dist/esm/cli/constant.js +0 -4
- package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +0 -25
- package/dist/esm-node/cli/constant.js +0 -4
- package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +0 -23
- package/dist/types/cli/constant.d.ts +0 -1
- package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +0 -3
- /package/dist/types/cli/mfRuntimePlugins/{inject-node-fetch.d.ts → node-fetch.d.ts} +0 -0
|
@@ -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,106 @@
|
|
|
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
|
+
if (mfConfig.remotes && mfConfig.exposes) {
|
|
56
|
+
throw new Error(`dataLoader only support for pure provider/consumer!`);
|
|
57
|
+
}
|
|
58
|
+
const { entryMap } = generateExtraExposeFiles(options);
|
|
59
|
+
const addExposeByEntry = (entry, routesPath, routeServerLoaderPath, clientRouteServerLoaderPath) => {
|
|
60
|
+
if (!mfConfig.exposes) {
|
|
61
|
+
mfConfig.exposes = {};
|
|
62
|
+
}
|
|
63
|
+
const routesKey = `./${entry}/${MF_ROUTES}`;
|
|
64
|
+
if (!mfConfig.exposes[routesKey]) {
|
|
65
|
+
mfConfig.exposes[routesKey] = routesPath;
|
|
66
|
+
}
|
|
67
|
+
const routeServerLoaderKey = `./${entry}/${MODERN_JS_ROUTE_SERVER_LOADER}`;
|
|
68
|
+
if (!mfConfig.exposes[routeServerLoaderKey]) {
|
|
69
|
+
if (isServer) {
|
|
70
|
+
mfConfig.exposes[routeServerLoaderKey] = routeServerLoaderPath;
|
|
71
|
+
} else {
|
|
72
|
+
mfConfig.exposes[routeServerLoaderKey] = clientRouteServerLoaderPath;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
Object.keys(entryMap).forEach((entry) => {
|
|
77
|
+
const { routesPath, routeServerLoaderPath, clientRouteServerLoaderPath } = entryMap[entry];
|
|
78
|
+
addExposeByEntry(entry, routesPath, routeServerLoaderPath, clientRouteServerLoaderPath);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
function addShared(options) {
|
|
82
|
+
const { metaName, mfConfig } = options;
|
|
83
|
+
const alias = `@${metaName}/runtime/router`;
|
|
84
|
+
if (!mfConfig.shared) {
|
|
85
|
+
mfConfig.shared = {
|
|
86
|
+
[alias]: {
|
|
87
|
+
singleton: true
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
} else {
|
|
91
|
+
if (!Array.isArray(mfConfig.shared)) {
|
|
92
|
+
mfConfig.shared[alias] = {
|
|
93
|
+
singleton: true
|
|
94
|
+
};
|
|
95
|
+
} else {
|
|
96
|
+
mfConfig.shared.push(alias);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function patchMFConfig(options) {
|
|
101
|
+
addShared(options);
|
|
102
|
+
addExpose(options);
|
|
103
|
+
}
|
|
104
|
+
export {
|
|
105
|
+
patchMFConfig
|
|
106
|
+
};
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { transformName2Prefix } from "../../runtime/dataLoader/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
|
+
plugins.push({
|
|
74
|
+
name: "ssrDataLoaderInjectAssets",
|
|
75
|
+
path: "@module-federation/modern-js/data-loader-inject-assets",
|
|
76
|
+
config: {}
|
|
77
|
+
});
|
|
78
|
+
return {
|
|
79
|
+
entrypoint,
|
|
80
|
+
plugins
|
|
81
|
+
};
|
|
82
|
+
},
|
|
83
|
+
_internalServerPlugins({ plugins }) {
|
|
84
|
+
plugins.push({
|
|
85
|
+
name: serverPlugin,
|
|
86
|
+
options: serverPluginOptions
|
|
87
|
+
});
|
|
88
|
+
return {
|
|
89
|
+
plugins
|
|
90
|
+
};
|
|
91
|
+
},
|
|
92
|
+
async modifyFileSystemRoutes(options) {
|
|
93
|
+
const { routes, entrypoint } = options;
|
|
94
|
+
const { entryName } = entrypoint;
|
|
95
|
+
const prefix = transformName2Prefix(internalOptions.csrConfig.name);
|
|
96
|
+
const modifyRouteIds = (route, pathName) => {
|
|
97
|
+
const currentPathName = route.path ? path.join(pathName, route.path) : pathName;
|
|
98
|
+
if (route.id) {
|
|
99
|
+
var _route_children;
|
|
100
|
+
const originalId = route.id;
|
|
101
|
+
route.originalId = originalId;
|
|
102
|
+
const remoteName = remotePathMap[currentPathName];
|
|
103
|
+
if (remoteName) {
|
|
104
|
+
const remoteIdPrefix = transformName2Prefix(remoteName);
|
|
105
|
+
const isLayout = Boolean(route.children);
|
|
106
|
+
const newId = `${remoteIdPrefix}${isLayout ? "layout" : "page"}`;
|
|
107
|
+
ssrByRouteIdsMap[originalId] = newId;
|
|
108
|
+
route.id = newId;
|
|
109
|
+
if (!partialSSRRemotes.includes(remoteName)) {
|
|
110
|
+
partialSSRRemotes.push(remoteName);
|
|
111
|
+
}
|
|
112
|
+
} else {
|
|
113
|
+
route.id = `${prefix}${route.id}`;
|
|
114
|
+
}
|
|
115
|
+
(_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.forEach((r) => {
|
|
116
|
+
modifyRouteIds(r, currentPathName);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
routes.forEach((route) => {
|
|
121
|
+
modifyRouteIds(route, entryName);
|
|
122
|
+
});
|
|
123
|
+
return options;
|
|
124
|
+
},
|
|
125
|
+
config: async () => {
|
|
126
|
+
return {
|
|
127
|
+
tools: {
|
|
128
|
+
// bundlerChain can not keep target order
|
|
129
|
+
rspack(_config, { isServer }) {
|
|
130
|
+
patchMFConfig({
|
|
131
|
+
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
132
|
+
metaName,
|
|
133
|
+
isServer,
|
|
134
|
+
internalDirectory,
|
|
135
|
+
entries
|
|
136
|
+
});
|
|
137
|
+
},
|
|
138
|
+
// bundlerChain can not keep target order
|
|
139
|
+
webpack(_config, { isServer }) {
|
|
140
|
+
patchMFConfig({
|
|
141
|
+
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
142
|
+
metaName,
|
|
143
|
+
isServer,
|
|
144
|
+
internalDirectory,
|
|
145
|
+
entries
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
source: {
|
|
150
|
+
define: {
|
|
151
|
+
MODERN_ROUTER_ID_PREFIX: JSON.stringify(transformName2Prefix(name))
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
var plugin_default = moduleFederationDataLoaderPlugin;
|
|
160
|
+
export {
|
|
161
|
+
plugin_default as default,
|
|
162
|
+
generateRoutes,
|
|
163
|
+
moduleFederationDataLoaderPlugin
|
|
164
|
+
};
|
|
@@ -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,6 +1,6 @@
|
|
|
1
1
|
import nodeFetch from "node-fetch";
|
|
2
2
|
const injectNodeFetchPlugin = () => ({
|
|
3
|
-
name: "
|
|
3
|
+
name: "node-fetch-plugin",
|
|
4
4
|
beforeInit(args) {
|
|
5
5
|
if (!globalThis.fetch) {
|
|
6
6
|
globalThis.fetch = nodeFetch;
|
|
@@ -8,7 +8,7 @@ const injectNodeFetchPlugin = () => ({
|
|
|
8
8
|
return args;
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
|
-
var
|
|
11
|
+
var node_fetch_default = injectNodeFetchPlugin;
|
|
12
12
|
export {
|
|
13
|
-
|
|
13
|
+
node_fetch_default as default
|
|
14
14
|
};
|
|
@@ -0,0 +1,11 @@
|
|
|
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
|
|
7
|
+
});
|
|
8
|
+
var node_default = nodePlugin;
|
|
9
|
+
export {
|
|
10
|
+
node_default as default
|
|
11
|
+
};
|
|
@@ -16,46 +16,48 @@ function replaceObjectLocalhost(key, obj) {
|
|
|
16
16
|
function replaceLocalhost(url) {
|
|
17
17
|
return url.replace(LOCALHOST, ipv4);
|
|
18
18
|
}
|
|
19
|
-
const resolveEntryIpv4Plugin = () =>
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
19
|
+
const resolveEntryIpv4Plugin = () => {
|
|
20
|
+
return {
|
|
21
|
+
name: "resolve-entry-ipv4",
|
|
22
|
+
beforeRegisterRemote(args) {
|
|
23
|
+
const { remote } = args;
|
|
24
|
+
replaceObjectLocalhost("entry", remote);
|
|
25
|
+
return args;
|
|
26
|
+
},
|
|
27
|
+
async afterResolve(args) {
|
|
28
|
+
const { remoteInfo } = args;
|
|
29
|
+
replaceObjectLocalhost("entry", remoteInfo);
|
|
30
|
+
return args;
|
|
31
|
+
},
|
|
32
|
+
beforeLoadRemoteSnapshot(args) {
|
|
33
|
+
const { moduleInfo } = args;
|
|
34
|
+
if ("entry" in moduleInfo) {
|
|
35
|
+
replaceObjectLocalhost("entry", moduleInfo);
|
|
36
|
+
return args;
|
|
37
|
+
}
|
|
38
|
+
if ("version" in moduleInfo) {
|
|
39
|
+
replaceObjectLocalhost("version", moduleInfo);
|
|
40
|
+
}
|
|
41
|
+
return args;
|
|
42
|
+
},
|
|
43
|
+
loadRemoteSnapshot(args) {
|
|
44
|
+
const { remoteSnapshot } = args;
|
|
45
|
+
if ("publicPath" in remoteSnapshot) {
|
|
46
|
+
replaceObjectLocalhost("publicPath", remoteSnapshot);
|
|
47
|
+
}
|
|
48
|
+
if ("getPublicPath" in remoteSnapshot) {
|
|
49
|
+
replaceObjectLocalhost("getPublicPath", remoteSnapshot);
|
|
50
|
+
}
|
|
51
|
+
if (remoteSnapshot.remotesInfo) {
|
|
52
|
+
Object.keys(remoteSnapshot.remotesInfo).forEach((key) => {
|
|
53
|
+
const remoteInfo = remoteSnapshot.remotesInfo[key];
|
|
54
|
+
replaceObjectLocalhost("matchedVersion", remoteInfo);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
35
57
|
return args;
|
|
36
58
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
return args;
|
|
41
|
-
},
|
|
42
|
-
loadRemoteSnapshot(args) {
|
|
43
|
-
const { remoteSnapshot } = args;
|
|
44
|
-
if ("publicPath" in remoteSnapshot) {
|
|
45
|
-
replaceObjectLocalhost("publicPath", remoteSnapshot);
|
|
46
|
-
}
|
|
47
|
-
if ("getPublicPath" in remoteSnapshot) {
|
|
48
|
-
replaceObjectLocalhost("getPublicPath", remoteSnapshot);
|
|
49
|
-
}
|
|
50
|
-
if (remoteSnapshot.remotesInfo) {
|
|
51
|
-
Object.keys(remoteSnapshot.remotesInfo).forEach((key) => {
|
|
52
|
-
const remoteInfo = remoteSnapshot.remotesInfo[key];
|
|
53
|
-
replaceObjectLocalhost("matchedVersion", remoteInfo);
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
return args;
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
+
};
|
|
60
|
+
};
|
|
59
61
|
var resolve_entry_ipv4_default = resolveEntryIpv4Plugin;
|
|
60
62
|
export {
|
|
61
63
|
resolve_entry_ipv4_default as default
|