@module-federation/modern-js 0.0.0-next-20240909062831 → 0.0.0-next-20240909083238
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 +142 -0
- package/dist/cjs/cli/dataLoader/ast/generateSelfRoutes.js +48 -0
- package/dist/cjs/cli/dataLoader/ast/generateSerializableRoutes.js +90 -0
- package/dist/cjs/cli/dataLoader/ast/generateSlimRoutes.js +104 -0
- package/dist/cjs/cli/dataLoader/ast/index.js +37 -0
- package/dist/cjs/cli/dataLoader/generateRouteFile.js +143 -0
- package/dist/cjs/cli/dataLoader/patchMFConfig.js +137 -0
- package/dist/cjs/cli/dataLoader/plugin.js +205 -0
- package/dist/cjs/cli/index.js +6 -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 +68 -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/{cli/constant.js → runtime/withMFRouteId.js} +9 -6
- 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 +109 -0
- package/dist/esm/cli/dataLoader/ast/generateSelfRoutes.js +17 -0
- package/dist/esm/cli/dataLoader/ast/generateSerializableRoutes.js +57 -0
- package/dist/esm/cli/dataLoader/ast/generateSlimRoutes.js +73 -0
- package/dist/esm/cli/dataLoader/ast/index.js +10 -0
- package/dist/esm/cli/dataLoader/generateRouteFile.js +293 -0
- package/dist/esm/cli/dataLoader/patchMFConfig.js +103 -0
- package/dist/esm/cli/dataLoader/plugin.js +216 -0
- package/dist/esm/cli/index.js +6 -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 +86 -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 +108 -0
- package/dist/esm-node/cli/dataLoader/ast/generateSelfRoutes.js +14 -0
- package/dist/esm-node/cli/dataLoader/ast/generateSerializableRoutes.js +56 -0
- package/dist/esm-node/cli/dataLoader/ast/generateSlimRoutes.js +70 -0
- package/dist/esm-node/cli/dataLoader/ast/index.js +10 -0
- package/dist/esm-node/cli/dataLoader/generateRouteFile.js +108 -0
- package/dist/esm-node/cli/dataLoader/patchMFConfig.js +103 -0
- package/dist/esm-node/cli/dataLoader/plugin.js +169 -0
- package/dist/esm-node/cli/index.js +6 -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 +43 -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 +7 -0
- package/dist/types/cli/dataLoader/ast/generateSelfRoutes.d.ts +7 -0
- package/dist/types/cli/dataLoader/ast/generateSerializableRoutes.d.ts +5 -0
- package/dist/types/cli/dataLoader/ast/generateSlimRoutes.d.ts +7 -0
- package/dist/types/cli/dataLoader/ast/index.d.ts +4 -0
- package/dist/types/cli/dataLoader/generateRouteFile.d.ts +7 -0
- package/dist/types/cli/dataLoader/patchMFConfig.d.ts +11 -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 +13 -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 +10 -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,56 @@
|
|
|
1
|
+
import traverse from "@babel/traverse";
|
|
2
|
+
import * as babelParser from "@babel/parser";
|
|
3
|
+
import generate from "@babel/generator";
|
|
4
|
+
import * as t from "@babel/types";
|
|
5
|
+
import { COMPONENT, ID, SHOULD_REVALIDATE, LAZY_COMPONENT, PRIVATE_COMPONENT, LOADER } from "./constant";
|
|
6
|
+
function generateSerializableRoutes({ sourceCode, prefix }) {
|
|
7
|
+
const ast = babelParser.parse(sourceCode, {
|
|
8
|
+
sourceType: "module"
|
|
9
|
+
});
|
|
10
|
+
const removedKeys = [
|
|
11
|
+
COMPONENT,
|
|
12
|
+
SHOULD_REVALIDATE,
|
|
13
|
+
LAZY_COMPONENT,
|
|
14
|
+
PRIVATE_COMPONENT,
|
|
15
|
+
LOADER
|
|
16
|
+
];
|
|
17
|
+
traverse(ast, {
|
|
18
|
+
ObjectExpression(path1) {
|
|
19
|
+
if (!Array.isArray(path1.node.properties)) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
path1.node.properties.forEach((prop1) => {
|
|
23
|
+
if (t.isObjectProperty(prop1) && t.isStringLiteral(prop1.key) && t.isStringLiteral(prop1.value) && prop1.key.value === ID) {
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
path1.node.properties = path1.node.properties.filter((p1) => {
|
|
27
|
+
if (t.isObjectProperty(p1) && t.isStringLiteral(p1.key)) {
|
|
28
|
+
return !removedKeys.includes(p1.key.value);
|
|
29
|
+
} else {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
let routesValue = "";
|
|
36
|
+
traverse(ast, {
|
|
37
|
+
VariableDeclarator(path) {
|
|
38
|
+
if (t.isVariableDeclarator(path.node) && t.isIdentifier(path.node.id) && path.node.id.name === "routes") {
|
|
39
|
+
const routesAst = path.node.init;
|
|
40
|
+
if (!routesAst) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const { code } = generate(routesAst, {
|
|
44
|
+
compact: true,
|
|
45
|
+
retainLines: false,
|
|
46
|
+
concise: true
|
|
47
|
+
});
|
|
48
|
+
routesValue = eval("(" + code + ")");
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
return routesValue;
|
|
53
|
+
}
|
|
54
|
+
export {
|
|
55
|
+
generateSerializableRoutes
|
|
56
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
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
|
+
import { COMPONENT, SHOULD_REVALIDATE, LAZY_COMPONENT, PRIVATE_COMPONENT } from "./constant";
|
|
7
|
+
function generateSlimRoutes({ sourceCode, filePath, prefix, baseName }) {
|
|
8
|
+
const ast = babelParser.parse(sourceCode, {
|
|
9
|
+
sourceType: "module"
|
|
10
|
+
});
|
|
11
|
+
const removedKeys = [
|
|
12
|
+
COMPONENT,
|
|
13
|
+
SHOULD_REVALIDATE,
|
|
14
|
+
LAZY_COMPONENT,
|
|
15
|
+
PRIVATE_COMPONENT
|
|
16
|
+
];
|
|
17
|
+
traverse(ast, {
|
|
18
|
+
// ImportDeclaration(path) {
|
|
19
|
+
// const source = path.node.source.value;
|
|
20
|
+
// const routeIdMatch = source.match(/routeId=([^&]+)/);
|
|
21
|
+
// if (routeIdMatch) {
|
|
22
|
+
// const originalRouteId = routeIdMatch[1];
|
|
23
|
+
// const newRouteId = `${prefix}${originalRouteId}`;
|
|
24
|
+
// const newSource = source.replace(
|
|
25
|
+
// /routeId=[^&]+/,
|
|
26
|
+
// `routeId=${newRouteId}`,
|
|
27
|
+
// );
|
|
28
|
+
// path.node.source = t.stringLiteral(newSource);
|
|
29
|
+
// }
|
|
30
|
+
// },
|
|
31
|
+
ObjectExpression(path) {
|
|
32
|
+
if (!Array.isArray(path.node.properties)) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
path.node.properties = path.node.properties.filter((p) => {
|
|
36
|
+
if (t.isObjectProperty(p) && t.isStringLiteral(p.key)) {
|
|
37
|
+
return !removedKeys.includes(p.key.value);
|
|
38
|
+
} else {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
const tempCode = generate(ast).code;
|
|
45
|
+
const tempAst = babelParser.parse(tempCode, {
|
|
46
|
+
sourceType: "module"
|
|
47
|
+
});
|
|
48
|
+
const usedIdentifiers = /* @__PURE__ */ new Set();
|
|
49
|
+
traverse(tempAst, {
|
|
50
|
+
Identifier(path) {
|
|
51
|
+
if (t.isProperty(path.parent)) {
|
|
52
|
+
usedIdentifiers.add(path.node.name);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
traverse(tempAst, {
|
|
57
|
+
ImportDeclaration(path) {
|
|
58
|
+
path.node.specifiers = path.node.specifiers.filter((specifier) => usedIdentifiers.has(specifier.local.name));
|
|
59
|
+
if (!path.node.specifiers.length) {
|
|
60
|
+
path.remove();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
const { code: newCode } = generate(tempAst);
|
|
65
|
+
const finalCode = `${newCode}export const baseName = '${baseName}';`;
|
|
66
|
+
fs.writeFileSync(filePath, finalCode);
|
|
67
|
+
}
|
|
68
|
+
export {
|
|
69
|
+
generateSlimRoutes
|
|
70
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { generateRoutes } from "./generateRoutes";
|
|
2
|
+
import { generateSlimRoutes } from "./generateSlimRoutes";
|
|
3
|
+
import { generateSerializableRoutes } from "./generateSerializableRoutes";
|
|
4
|
+
import { generateSelfRoutes } from "./generateSelfRoutes";
|
|
5
|
+
export {
|
|
6
|
+
generateRoutes,
|
|
7
|
+
generateSelfRoutes,
|
|
8
|
+
generateSerializableRoutes,
|
|
9
|
+
generateSlimRoutes
|
|
10
|
+
};
|
|
@@ -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,103 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { fs } from "@modern-js/utils";
|
|
3
|
+
import { transformName2Prefix } from "../../runtime/utils";
|
|
4
|
+
import { MODERN_JS_ROUTE_SERVER_LOADER } from "../../constant";
|
|
5
|
+
import { MF_ROUTES, MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME } from "../../constant";
|
|
6
|
+
import { generateRoutes } from "./ast";
|
|
7
|
+
function generateExtraExposeFiles(options) {
|
|
8
|
+
const { internalDirectory, mfConfig, isServer, baseName, entries } = options;
|
|
9
|
+
const entryMap = {};
|
|
10
|
+
const outputDir = path.resolve(process.cwd(), "node_modules/.federation/data-loader");
|
|
11
|
+
fs.ensureDirSync(outputDir);
|
|
12
|
+
const addSuffix = (fileName, ext = ".jsx") => {
|
|
13
|
+
if (!isServer) {
|
|
14
|
+
return `${fileName}${ext}`;
|
|
15
|
+
}
|
|
16
|
+
return `${fileName}.server${ext}`;
|
|
17
|
+
};
|
|
18
|
+
const generateEntryRoutes = (entry) => {
|
|
19
|
+
const outputEntryDir = path.resolve(outputDir, entry);
|
|
20
|
+
fs.ensureDirSync(outputEntryDir);
|
|
21
|
+
const sourceDir = path.resolve(internalDirectory, entry);
|
|
22
|
+
const routesFilePath = path.resolve(sourceDir, addSuffix(MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME, ".js"));
|
|
23
|
+
const routesFileContent = fs.readFileSync(routesFilePath, "utf-8");
|
|
24
|
+
const outputFullRoutesPath = path.resolve(outputEntryDir, addSuffix(MF_ROUTES));
|
|
25
|
+
generateRoutes({
|
|
26
|
+
sourceCode: routesFileContent,
|
|
27
|
+
filePath: outputFullRoutesPath,
|
|
28
|
+
prefix: transformName2Prefix(mfConfig.name),
|
|
29
|
+
baseName
|
|
30
|
+
});
|
|
31
|
+
const routeServerLoaderPath = path.resolve(sourceDir, `${MODERN_JS_ROUTE_SERVER_LOADER}.js`);
|
|
32
|
+
const outputRouteServerLoaderPath = path.resolve(outputEntryDir, `${MODERN_JS_ROUTE_SERVER_LOADER}.js`);
|
|
33
|
+
if (isServer) {
|
|
34
|
+
const routeServerLoaderContent = fs.readFileSync(routeServerLoaderPath, "utf-8");
|
|
35
|
+
generateRoutes({
|
|
36
|
+
sourceCode: routeServerLoaderContent,
|
|
37
|
+
filePath: outputRouteServerLoaderPath,
|
|
38
|
+
prefix: transformName2Prefix(mfConfig.name),
|
|
39
|
+
baseName
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
entryMap[entry] = {
|
|
43
|
+
routesPath: outputFullRoutesPath,
|
|
44
|
+
routeServerLoaderPath: outputRouteServerLoaderPath
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
entries.forEach((entry) => {
|
|
48
|
+
generateEntryRoutes(entry);
|
|
49
|
+
});
|
|
50
|
+
return {
|
|
51
|
+
entryMap
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function addExpose(options) {
|
|
55
|
+
const { mfConfig, isServer } = options;
|
|
56
|
+
if (mfConfig.remotes && mfConfig.exposes) {
|
|
57
|
+
throw new Error(`dataLoader only support for pure provider/consumer !`);
|
|
58
|
+
}
|
|
59
|
+
const { entryMap } = generateExtraExposeFiles(options);
|
|
60
|
+
const addExposeByEntry = (entry, routesPath, routeServerLoaderPath) => {
|
|
61
|
+
if (!mfConfig.exposes) {
|
|
62
|
+
mfConfig.exposes = {};
|
|
63
|
+
}
|
|
64
|
+
const routesKey = `./${entry}/${MF_ROUTES}`;
|
|
65
|
+
if (!mfConfig.exposes[routesKey]) {
|
|
66
|
+
mfConfig.exposes[routesKey] = routesPath;
|
|
67
|
+
}
|
|
68
|
+
const routeServerLoaderKey = `./${entry}/${MODERN_JS_ROUTE_SERVER_LOADER}`;
|
|
69
|
+
if (isServer && !mfConfig.exposes[routeServerLoaderKey]) {
|
|
70
|
+
mfConfig.exposes[routeServerLoaderKey] = routeServerLoaderPath;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
Object.keys(entryMap).forEach((entry) => {
|
|
74
|
+
const { routesPath, routeServerLoaderPath } = entryMap[entry];
|
|
75
|
+
addExposeByEntry(entry, routesPath, routeServerLoaderPath);
|
|
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
|
+
};
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { transformName2Prefix } from "../../runtime/utils";
|
|
2
|
+
import { DEFAULT_ENTRY, PLUGIN_IDENTIFIER } from "../../constant";
|
|
3
|
+
import { META_NAME } from "../../constant";
|
|
4
|
+
import { generateRoutes, generateSlimRoutes } 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 ({ useConfigContext, useAppContext }) => {
|
|
34
|
+
var _modernjsConfig_server, _modernjsConfig_server1, _modernjsConfig_server2, _modernjsConfig_server3, _modernjsConfig_server4;
|
|
35
|
+
if (!enable) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const {
|
|
39
|
+
baseName,
|
|
40
|
+
partialSSRRemotes = [],
|
|
41
|
+
// patchMFConfig,
|
|
42
|
+
metaName = META_NAME,
|
|
43
|
+
serverPlugin = "@module-federation/modern-js/data-loader-server",
|
|
44
|
+
transformRuntimeOptions
|
|
45
|
+
} = userConfig;
|
|
46
|
+
if (!baseName) {
|
|
47
|
+
throw new Error(`${PLUGIN_IDENTIFIER} 'baseName' is required if you enable 'dataLoader'!`);
|
|
48
|
+
}
|
|
49
|
+
const modernjsConfig = useConfigContext();
|
|
50
|
+
const appContext = useAppContext();
|
|
51
|
+
const enableSSR = Boolean(modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
|
|
52
|
+
const name = internalOptions.csrConfig.name;
|
|
53
|
+
const internalDirectory = appContext.internalDirectory.replace(META_NAME, metaName || META_NAME);
|
|
54
|
+
const transformRuntimeFn = transformRuntimeOptions || _transformRuntimeOptions;
|
|
55
|
+
const baseUrls = ((_modernjsConfig_server1 = modernjsConfig.server) === null || _modernjsConfig_server1 === void 0 ? void 0 : _modernjsConfig_server1.baseUrl) ? Array.isArray((_modernjsConfig_server2 = modernjsConfig.server) === null || _modernjsConfig_server2 === void 0 ? void 0 : _modernjsConfig_server2.baseUrl) ? (_modernjsConfig_server3 = modernjsConfig.server) === null || _modernjsConfig_server3 === void 0 ? void 0 : _modernjsConfig_server3.baseUrl : [
|
|
56
|
+
(_modernjsConfig_server4 = modernjsConfig.server) === null || _modernjsConfig_server4 === void 0 ? void 0 : _modernjsConfig_server4.baseUrl
|
|
57
|
+
] : [];
|
|
58
|
+
const entries = /* @__PURE__ */ new Set();
|
|
59
|
+
let remotePathMap = {};
|
|
60
|
+
const ssrByRouteIdsMap = {};
|
|
61
|
+
const serverPluginOptions = {
|
|
62
|
+
runtimeOptions: transformRuntimeFn(internalOptions.csrConfig),
|
|
63
|
+
partialSSRRemotes,
|
|
64
|
+
ssrByRouteIdsMap
|
|
65
|
+
};
|
|
66
|
+
const csrConfig = internalOptions.csrConfig;
|
|
67
|
+
if (typeof csrConfig.remotes === "object" && Object.keys(csrConfig.remotes).length) {
|
|
68
|
+
const { entries: scanEntries, remotePathMap: scanRemotePathMap } = await generateRouteFile({
|
|
69
|
+
appDirectory: appContext.appDirectory
|
|
70
|
+
});
|
|
71
|
+
scanEntries.forEach((e) => entries.add(e));
|
|
72
|
+
remotePathMap = scanRemotePathMap;
|
|
73
|
+
} else {
|
|
74
|
+
entries.add(DEFAULT_ENTRY);
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
_internalRuntimePlugins: ({ entrypoint, plugins }) => {
|
|
78
|
+
plugins.push({
|
|
79
|
+
name: "ssrDataLoader",
|
|
80
|
+
path: "@module-federation/modern-js/data-loader",
|
|
81
|
+
config: {}
|
|
82
|
+
});
|
|
83
|
+
return {
|
|
84
|
+
entrypoint,
|
|
85
|
+
plugins
|
|
86
|
+
};
|
|
87
|
+
},
|
|
88
|
+
_internalServerPlugins({ plugins }) {
|
|
89
|
+
plugins.push({
|
|
90
|
+
name: serverPlugin,
|
|
91
|
+
options: serverPluginOptions
|
|
92
|
+
});
|
|
93
|
+
return {
|
|
94
|
+
plugins
|
|
95
|
+
};
|
|
96
|
+
},
|
|
97
|
+
async modifyFileSystemRoutes(options) {
|
|
98
|
+
const { routes, entrypoint } = options;
|
|
99
|
+
const { entryName } = entrypoint;
|
|
100
|
+
const prefix = transformName2Prefix(internalOptions.csrConfig.name);
|
|
101
|
+
const modifyRouteIds = (route, pathName) => {
|
|
102
|
+
const currentPathName = route.path ? path.join(pathName, route.path) : pathName;
|
|
103
|
+
if (route.id) {
|
|
104
|
+
var _route_children;
|
|
105
|
+
const originalId = route.id;
|
|
106
|
+
route.originalId = originalId;
|
|
107
|
+
const remoteName = remotePathMap[currentPathName];
|
|
108
|
+
if (remoteName) {
|
|
109
|
+
const remoteIdPrefix = transformName2Prefix(remoteName);
|
|
110
|
+
const isLayout = Boolean(route.children);
|
|
111
|
+
const newId = `${remoteIdPrefix}${isLayout ? "layout" : "page"}`;
|
|
112
|
+
ssrByRouteIdsMap[originalId] = newId;
|
|
113
|
+
route.id = newId;
|
|
114
|
+
} else {
|
|
115
|
+
route.id = `${prefix}${route.id}`;
|
|
116
|
+
}
|
|
117
|
+
(_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.forEach((r) => {
|
|
118
|
+
modifyRouteIds(r, currentPathName);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
routes.forEach((route) => {
|
|
123
|
+
modifyRouteIds(route, entryName);
|
|
124
|
+
});
|
|
125
|
+
return options;
|
|
126
|
+
},
|
|
127
|
+
config: async () => {
|
|
128
|
+
return {
|
|
129
|
+
tools: {
|
|
130
|
+
// bundlerChain can not keep target order
|
|
131
|
+
rspack(config, { isServer }) {
|
|
132
|
+
patchMFConfig({
|
|
133
|
+
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
134
|
+
baseName,
|
|
135
|
+
metaName,
|
|
136
|
+
isServer,
|
|
137
|
+
internalDirectory,
|
|
138
|
+
entries
|
|
139
|
+
});
|
|
140
|
+
},
|
|
141
|
+
// bundlerChain can not keep target order
|
|
142
|
+
webpack(config, { isServer }) {
|
|
143
|
+
patchMFConfig({
|
|
144
|
+
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
145
|
+
baseName,
|
|
146
|
+
metaName,
|
|
147
|
+
isServer,
|
|
148
|
+
internalDirectory,
|
|
149
|
+
entries
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
source: {
|
|
154
|
+
define: {
|
|
155
|
+
MODERN_ROUTER_ID_PREFIX: JSON.stringify(transformName2Prefix(name))
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
var plugin_default = moduleFederationDataLoaderPlugin;
|
|
164
|
+
export {
|
|
165
|
+
plugin_default as default,
|
|
166
|
+
generateRoutes,
|
|
167
|
+
generateSlimRoutes,
|
|
168
|
+
moduleFederationDataLoaderPlugin
|
|
169
|
+
};
|
|
@@ -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,11 @@ 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
|
+
baseName: "",
|
|
60
|
+
...userConfig.dataLoader
|
|
61
|
+
})
|
|
57
62
|
]
|
|
58
63
|
};
|
|
59
64
|
};
|
|
@@ -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
|
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import nodeFetch from "node-fetch";
|
|
2
|
+
const injectNodeFetchPlugin = () => ({
|
|
3
|
+
name: "inject-node-fetch-plugin",
|
|
4
|
+
beforeInit(args) {
|
|
5
|
+
if (!globalThis.fetch) {
|
|
6
|
+
globalThis.fetch = nodeFetch;
|
|
7
|
+
}
|
|
8
|
+
return args;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
var node_default = injectNodeFetchPlugin;
|
|
12
|
+
export {
|
|
13
|
+
node_default as default
|
|
14
|
+
};
|
|
@@ -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
|