@module-federation/modern-js 0.0.0-next-20240913095223 → 0.0.0-next-20240918063302
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/index.js → constant.js} +6 -6
- package/dist/cjs/cli/index.js +1 -5
- package/dist/cjs/cli/mfRuntimePlugins/{node-fetch.js → inject-node-fetch.js} +6 -6
- package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +38 -40
- package/dist/cjs/{runtime/withMFRouteId.js → cli/mfRuntimePlugins/shared-strategy.js} +23 -11
- package/dist/cjs/cli/ssrPlugin.js +2 -5
- package/dist/cjs/cli/utils.js +6 -11
- package/dist/cjs/cli/utils.spec.js +6 -5
- package/dist/cjs/constant.js +2 -32
- package/dist/cjs/runtime/index.js +1 -4
- package/dist/cjs/ssr-runtime/plugin.js +22 -9
- package/dist/esm/cli/constant.js +4 -0
- package/dist/esm/cli/index.js +2 -6
- package/dist/esm/cli/mfRuntimePlugins/{node-fetch.js → inject-node-fetch.js} +3 -3
- package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +25 -0
- package/dist/esm/cli/ssrPlugin.js +3 -6
- package/dist/esm/cli/utils.js +8 -13
- package/dist/esm/cli/utils.spec.js +6 -5
- package/dist/esm/constant.js +1 -21
- package/dist/esm/runtime/index.js +1 -3
- package/dist/esm/ssr-runtime/plugin.js +26 -10
- package/dist/esm-node/cli/constant.js +4 -0
- package/dist/esm-node/cli/index.js +1 -5
- package/dist/esm-node/cli/mfRuntimePlugins/{node-fetch.js → inject-node-fetch.js} +3 -3
- package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +38 -40
- package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +23 -0
- package/dist/esm-node/cli/ssrPlugin.js +2 -5
- package/dist/esm-node/cli/utils.js +5 -9
- package/dist/esm-node/cli/utils.spec.js +6 -5
- package/dist/esm-node/constant.js +1 -21
- package/dist/esm-node/runtime/index.js +1 -3
- package/dist/esm-node/ssr-runtime/plugin.js +22 -9
- package/dist/types/cli/constant.d.ts +1 -0
- package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +3 -0
- package/dist/types/cli/utils.d.ts +0 -2
- package/dist/types/constant.d.ts +0 -10
- package/dist/types/runtime/index.d.ts +0 -1
- package/dist/types/ssr-runtime/plugin.d.ts +1 -3
- package/dist/types/types/index.d.ts +0 -9
- package/package.json +11 -48
- package/dist/cjs/cli/dataLoader/ast/constant.js +0 -49
- package/dist/cjs/cli/dataLoader/ast/generateRoutes.js +0 -141
- package/dist/cjs/cli/dataLoader/clearMFCache.js +0 -57
- package/dist/cjs/cli/dataLoader/generateRouteFile.js +0 -143
- package/dist/cjs/cli/dataLoader/patchMFConfig.js +0 -140
- package/dist/cjs/cli/dataLoader/plugin.js +0 -204
- package/dist/cjs/cli/mfRuntimePlugins/node.js +0 -41
- package/dist/cjs/cli/server/dataLoaderPlugin.js +0 -84
- package/dist/cjs/cli/server/fetchRemoteFullRoutesMap.js +0 -69
- package/dist/cjs/interfaces/route.js +0 -16
- package/dist/cjs/runtime/dataLoader/plugin-inject-assets.js +0 -94
- package/dist/cjs/runtime/dataLoader/plugin.js +0 -74
- package/dist/cjs/runtime/dataLoader/utils.js +0 -106
- package/dist/esm/cli/dataLoader/ast/constant.js +0 -18
- package/dist/esm/cli/dataLoader/ast/generateRoutes.js +0 -108
- package/dist/esm/cli/dataLoader/ast/index.js +0 -4
- package/dist/esm/cli/dataLoader/clearMFCache.js +0 -34
- package/dist/esm/cli/dataLoader/generateRouteFile.js +0 -293
- package/dist/esm/cli/dataLoader/patchMFConfig.js +0 -106
- package/dist/esm/cli/dataLoader/plugin.js +0 -232
- package/dist/esm/cli/mfRuntimePlugins/node.js +0 -13
- package/dist/esm/cli/server/dataLoaderPlugin.js +0 -107
- package/dist/esm/cli/server/fetchRemoteFullRoutesMap.js +0 -88
- package/dist/esm/interfaces/route.js +0 -0
- package/dist/esm/runtime/dataLoader/plugin-inject-assets.js +0 -73
- package/dist/esm/runtime/dataLoader/plugin.js +0 -94
- package/dist/esm/runtime/dataLoader/utils.js +0 -77
- package/dist/esm/runtime/withMFRouteId.js +0 -7
- package/dist/esm-node/cli/dataLoader/ast/constant.js +0 -18
- package/dist/esm-node/cli/dataLoader/ast/generateRoutes.js +0 -107
- package/dist/esm-node/cli/dataLoader/ast/index.js +0 -4
- package/dist/esm-node/cli/dataLoader/clearMFCache.js +0 -33
- package/dist/esm-node/cli/dataLoader/generateRouteFile.js +0 -108
- package/dist/esm-node/cli/dataLoader/patchMFConfig.js +0 -106
- package/dist/esm-node/cli/dataLoader/plugin.js +0 -169
- package/dist/esm-node/cli/mfRuntimePlugins/node.js +0 -11
- package/dist/esm-node/cli/server/dataLoaderPlugin.js +0 -64
- package/dist/esm-node/cli/server/fetchRemoteFullRoutesMap.js +0 -44
- package/dist/esm-node/interfaces/route.js +0 -0
- package/dist/esm-node/runtime/dataLoader/plugin-inject-assets.js +0 -70
- package/dist/esm-node/runtime/dataLoader/plugin.js +0 -50
- package/dist/esm-node/runtime/dataLoader/utils.js +0 -77
- package/dist/esm-node/runtime/withMFRouteId.js +0 -7
- package/dist/types/cli/dataLoader/ast/constant.d.ts +0 -8
- package/dist/types/cli/dataLoader/ast/generateRoutes.d.ts +0 -5
- package/dist/types/cli/dataLoader/ast/index.d.ts +0 -1
- package/dist/types/cli/dataLoader/clearMFCache.d.ts +0 -1
- package/dist/types/cli/dataLoader/generateRouteFile.d.ts +0 -7
- package/dist/types/cli/dataLoader/patchMFConfig.d.ts +0 -10
- package/dist/types/cli/dataLoader/plugin.d.ts +0 -6
- package/dist/types/cli/mfRuntimePlugins/node.d.ts +0 -3
- package/dist/types/cli/server/dataLoaderPlugin.d.ts +0 -10
- package/dist/types/cli/server/fetchRemoteFullRoutesMap.d.ts +0 -10
- package/dist/types/interfaces/route.d.ts +0 -12
- package/dist/types/runtime/dataLoader/plugin-inject-assets.d.ts +0 -4
- package/dist/types/runtime/dataLoader/plugin.d.ts +0 -4
- package/dist/types/runtime/dataLoader/utils.d.ts +0 -22
- package/dist/types/runtime/withMFRouteId.d.ts +0 -1
- /package/dist/types/cli/mfRuntimePlugins/{node-fetch.d.ts → inject-node-fetch.d.ts} +0 -0
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
const IS_ROOT = "isRoot";
|
|
2
|
-
const ID = "id";
|
|
3
|
-
const COMPONENT = "component";
|
|
4
|
-
const LAZY_COMPONENT = "lazyImport";
|
|
5
|
-
const SHOULD_REVALIDATE = "shouldRevalidate";
|
|
6
|
-
const PRIVATE_COMPONENT = "_component";
|
|
7
|
-
const ELEMENT = "element";
|
|
8
|
-
const LOADER = "loader";
|
|
9
|
-
export {
|
|
10
|
-
COMPONENT,
|
|
11
|
-
ELEMENT,
|
|
12
|
-
ID,
|
|
13
|
-
IS_ROOT,
|
|
14
|
-
LAZY_COMPONENT,
|
|
15
|
-
LOADER,
|
|
16
|
-
PRIVATE_COMPONENT,
|
|
17
|
-
SHOULD_REVALIDATE
|
|
18
|
-
};
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import traverse from "@babel/traverse";
|
|
3
|
-
import * as babelParser from "@babel/parser";
|
|
4
|
-
import generate from "@babel/generator";
|
|
5
|
-
import * as t from "@babel/types";
|
|
6
|
-
function findTargetKeyNode(nodeProperties, key) {
|
|
7
|
-
return nodeProperties.find((p) => t.isObjectProperty(p) && t.isStringLiteral(p.key) && p.key.value === key);
|
|
8
|
-
}
|
|
9
|
-
function generateRoutes({ sourceCode, filePath }) {
|
|
10
|
-
const ast = babelParser.parse(sourceCode, {
|
|
11
|
-
sourceType: "module"
|
|
12
|
-
});
|
|
13
|
-
const lazyComponentDeclarations = [];
|
|
14
|
-
const componentDeclarations = [];
|
|
15
|
-
let componentId = 0;
|
|
16
|
-
traverse(ast, {
|
|
17
|
-
// ImportDeclaration(path) {
|
|
18
|
-
// const source = path.node.source.value;
|
|
19
|
-
// const routeIdMatch = source.match(/routeId=([^&]+)/);
|
|
20
|
-
// if (routeIdMatch) {
|
|
21
|
-
// const originalRouteId = routeIdMatch[1];
|
|
22
|
-
// const newRouteId = `${prefix}${originalRouteId}`;
|
|
23
|
-
// const newSource = source.replace(
|
|
24
|
-
// /routeId=[^&]+/,
|
|
25
|
-
// `routeId=${newRouteId}`,
|
|
26
|
-
// );
|
|
27
|
-
// path.node.source = t.stringLiteral(newSource);
|
|
28
|
-
// }
|
|
29
|
-
// },
|
|
30
|
-
ObjectExpression(path) {
|
|
31
|
-
let componentName = "";
|
|
32
|
-
let lazyComponentName = "";
|
|
33
|
-
if (!Array.isArray(path.node.properties)) {
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
const idNode = findTargetKeyNode(path.node.properties, "id");
|
|
37
|
-
if (idNode && t.isObjectProperty(idNode) && t.isStringLiteral(idNode.value)) {
|
|
38
|
-
}
|
|
39
|
-
const isRootNode = findTargetKeyNode(path.node.properties, "isRoot");
|
|
40
|
-
if (isRootNode && t.isObjectProperty(isRootNode) && t.isBooleanLiteral(isRootNode.value)) {
|
|
41
|
-
isRootNode.value.value = false;
|
|
42
|
-
}
|
|
43
|
-
if (!isRootNode) {
|
|
44
|
-
const lazyComponentNode = findTargetKeyNode(path.node.properties, "lazyImport");
|
|
45
|
-
if (lazyComponentNode && t.isObjectProperty(lazyComponentNode) && t.isArrowFunctionExpression(lazyComponentNode.value)) {
|
|
46
|
-
lazyComponentName = `LazyComponent_${componentId}`;
|
|
47
|
-
const lazyDeclaration = t.variableDeclaration("const", [
|
|
48
|
-
t.variableDeclarator(t.identifier(lazyComponentName), lazyComponentNode.value)
|
|
49
|
-
]);
|
|
50
|
-
lazyComponentNode.value = t.identifier(lazyComponentName);
|
|
51
|
-
const componentNode2 = findTargetKeyNode(path.node.properties, "component");
|
|
52
|
-
if (componentNode2 && t.isObjectProperty(componentNode2) && t.isCallExpression(componentNode2.value) && t.isIdentifier(componentNode2.value.callee)) {
|
|
53
|
-
componentNode2.value = t.callExpression(t.identifier("lazy"), [
|
|
54
|
-
t.identifier(lazyComponentName)
|
|
55
|
-
]);
|
|
56
|
-
}
|
|
57
|
-
lazyComponentDeclarations.push(lazyDeclaration);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
const componentNode = findTargetKeyNode(path.node.properties, "component");
|
|
61
|
-
if (componentNode && t.isObjectProperty(componentNode) && t.isCallExpression(componentNode.value) && t.isIdentifier(componentNode.value.callee)) {
|
|
62
|
-
componentName = `Component_${componentId}`;
|
|
63
|
-
const componentDeclaration = t.variableDeclaration("const", [
|
|
64
|
-
t.variableDeclarator(t.identifier(componentName), lazyComponentName ? t.callExpression(t.identifier("lazy"), [
|
|
65
|
-
t.identifier(lazyComponentName)
|
|
66
|
-
]) : componentNode.value)
|
|
67
|
-
]);
|
|
68
|
-
componentDeclarations.push(componentDeclaration);
|
|
69
|
-
componentNode.value = t.identifier(componentName);
|
|
70
|
-
}
|
|
71
|
-
if (lazyComponentName || componentName) {
|
|
72
|
-
componentId++;
|
|
73
|
-
if (componentName) {
|
|
74
|
-
const upperFirstName = componentName.slice(0, 1).toUpperCase() + componentName.slice(1);
|
|
75
|
-
const jsxElement = t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier(upperFirstName), [], true), null, [], true);
|
|
76
|
-
path.node.properties.push(t.objectProperty(t.identifier("element"), jsxElement));
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
traverse(ast, {
|
|
82
|
-
Program(path) {
|
|
83
|
-
const lastImportIndex = path.get("body").reduce((lastIndex, p, index) => {
|
|
84
|
-
if (t.isImportDeclaration(p.node)) {
|
|
85
|
-
lastIndex = index;
|
|
86
|
-
}
|
|
87
|
-
return lastIndex;
|
|
88
|
-
}, -1);
|
|
89
|
-
if (lastImportIndex >= 0) {
|
|
90
|
-
const lastImport = path.get(`body.${lastImportIndex}`);
|
|
91
|
-
[
|
|
92
|
-
...componentDeclarations,
|
|
93
|
-
...lazyComponentDeclarations
|
|
94
|
-
].forEach((declaration) => {
|
|
95
|
-
if ("insertAfter" in lastImport) {
|
|
96
|
-
lastImport.insertAfter(declaration);
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
const { code } = generate(ast);
|
|
103
|
-
fs.writeFileSync(filePath, code);
|
|
104
|
-
}
|
|
105
|
-
export {
|
|
106
|
-
generateRoutes
|
|
107
|
-
};
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
function clearMFCache() {
|
|
2
|
-
if (!globalThis.__FEDERATION__) {
|
|
3
|
-
console.log("clearMFCache no globalThis.__FEDERATION__");
|
|
4
|
-
return;
|
|
5
|
-
}
|
|
6
|
-
globalThis.__FEDERATION__.__INSTANCES__.map((i) => {
|
|
7
|
-
i.moduleCache.forEach((mc) => {
|
|
8
|
-
if (mc.remoteInfo && mc.remoteInfo.entryGlobalName) {
|
|
9
|
-
delete globalThis[mc.remoteInfo.entryGlobalName];
|
|
10
|
-
}
|
|
11
|
-
});
|
|
12
|
-
i.moduleCache.clear();
|
|
13
|
-
if (globalThis[i.name]) {
|
|
14
|
-
delete globalThis[i.name];
|
|
15
|
-
}
|
|
16
|
-
});
|
|
17
|
-
Object.keys(globalThis.__FEDERATION__).forEach((key) => {
|
|
18
|
-
if (Array.isArray(globalThis.__FEDERATION__[key])) {
|
|
19
|
-
globalThis.__FEDERATION__[key] = [];
|
|
20
|
-
} else if (key === "__PRELOADED_MAP__") {
|
|
21
|
-
globalThis.__FEDERATION__[key] = /* @__PURE__ */ new Map();
|
|
22
|
-
} else if (typeof globalThis.__FEDERATION__[key] === "object") {
|
|
23
|
-
globalThis.__FEDERATION__[key] = {};
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
if (globalThis.__GLOBAL_LOADING_REMOTE_ENTRY__) {
|
|
27
|
-
globalThis.__GLOBAL_LOADING_REMOTE_ENTRY__ = {};
|
|
28
|
-
}
|
|
29
|
-
globalThis.FORCE_MF_REFRESH = true;
|
|
30
|
-
}
|
|
31
|
-
export {
|
|
32
|
-
clearMFCache
|
|
33
|
-
};
|
|
@@ -1,108 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import { fs } from "@modern-js/utils";
|
|
3
|
-
import { MODERN_JS_ROUTE_SERVER_LOADER } from "../../constant";
|
|
4
|
-
import { MF_ROUTES, MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME } from "../../constant";
|
|
5
|
-
import { generateRoutes } from "./ast";
|
|
6
|
-
function generateExtraExposeFiles(options) {
|
|
7
|
-
const { internalDirectory, mfConfig, isServer, entries } = options;
|
|
8
|
-
const entryMap = {};
|
|
9
|
-
const outputDir = path.resolve(process.cwd(), "node_modules/.federation/data-loader");
|
|
10
|
-
fs.ensureDirSync(outputDir);
|
|
11
|
-
const addSuffix = (fileName, ext = ".jsx") => {
|
|
12
|
-
if (!isServer) {
|
|
13
|
-
return `${fileName}${ext}`;
|
|
14
|
-
}
|
|
15
|
-
return `${fileName}.server${ext}`;
|
|
16
|
-
};
|
|
17
|
-
const generateEntryRoutes = (entry) => {
|
|
18
|
-
const outputEntryDir = path.resolve(outputDir, entry);
|
|
19
|
-
fs.ensureDirSync(outputEntryDir);
|
|
20
|
-
const sourceDir = path.resolve(internalDirectory, entry);
|
|
21
|
-
const routesFilePath = path.resolve(sourceDir, addSuffix(MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME, ".js"));
|
|
22
|
-
const routesFileContent = fs.readFileSync(routesFilePath, "utf-8");
|
|
23
|
-
const outputFullRoutesPath = path.resolve(outputEntryDir, addSuffix(MF_ROUTES));
|
|
24
|
-
generateRoutes({
|
|
25
|
-
sourceCode: routesFileContent,
|
|
26
|
-
filePath: outputFullRoutesPath
|
|
27
|
-
});
|
|
28
|
-
const routeServerLoaderPath = path.resolve(sourceDir, `${MODERN_JS_ROUTE_SERVER_LOADER}.js`);
|
|
29
|
-
const outputRouteServerLoaderPath = path.resolve(outputEntryDir, `${MODERN_JS_ROUTE_SERVER_LOADER}.js`);
|
|
30
|
-
const clientRouteServerLoaderPath = outputRouteServerLoaderPath.replace(MODERN_JS_ROUTE_SERVER_LOADER, `${MODERN_JS_ROUTE_SERVER_LOADER}-client`);
|
|
31
|
-
if (isServer) {
|
|
32
|
-
const routeServerLoaderContent = fs.readFileSync(routeServerLoaderPath, "utf-8");
|
|
33
|
-
generateRoutes({
|
|
34
|
-
sourceCode: routeServerLoaderContent,
|
|
35
|
-
filePath: outputRouteServerLoaderPath
|
|
36
|
-
});
|
|
37
|
-
} else {
|
|
38
|
-
fs.writeFileSync(clientRouteServerLoaderPath, `export const routes = []`);
|
|
39
|
-
}
|
|
40
|
-
entryMap[entry] = {
|
|
41
|
-
routesPath: outputFullRoutesPath,
|
|
42
|
-
routeServerLoaderPath: outputRouteServerLoaderPath,
|
|
43
|
-
clientRouteServerLoaderPath
|
|
44
|
-
};
|
|
45
|
-
};
|
|
46
|
-
entries.forEach((entry) => {
|
|
47
|
-
generateEntryRoutes(entry);
|
|
48
|
-
});
|
|
49
|
-
return {
|
|
50
|
-
entryMap
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
function addExpose(options) {
|
|
54
|
-
const { mfConfig, isServer } = options;
|
|
55
|
-
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
|
-
};
|
|
@@ -1,169 +0,0 @@
|
|
|
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 { clearMFCache } from "./clearMFCache";
|
|
8
|
-
import path from "path";
|
|
9
|
-
function _transformRuntimeOptions(buildOptions) {
|
|
10
|
-
const remotes = buildOptions.remotes || {};
|
|
11
|
-
const runtimeRemotes = Object.entries(remotes).map((remote) => {
|
|
12
|
-
const [alias, nameAndEntry] = remote;
|
|
13
|
-
const [name, entry] = nameAndEntry.split("@");
|
|
14
|
-
return {
|
|
15
|
-
name,
|
|
16
|
-
entry,
|
|
17
|
-
alias
|
|
18
|
-
};
|
|
19
|
-
});
|
|
20
|
-
return {
|
|
21
|
-
name: buildOptions.name,
|
|
22
|
-
remotes: runtimeRemotes
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
const moduleFederationDataLoaderPlugin = (enable, internalOptions, userConfig) => ({
|
|
26
|
-
name: "@modern-js/plugin-module-federation-data-loader",
|
|
27
|
-
pre: [
|
|
28
|
-
"@modern-js/plugin-module-federation-config"
|
|
29
|
-
],
|
|
30
|
-
post: [
|
|
31
|
-
"@modern-js/plugin-router",
|
|
32
|
-
"@modern-js/plugin-module-federation"
|
|
33
|
-
],
|
|
34
|
-
setup: async ({ useAppContext }) => {
|
|
35
|
-
if (!enable) {
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
const { serverPlugin = "@module-federation/modern-js/data-loader-server", transformRuntimeOptions } = userConfig;
|
|
39
|
-
const appContext = useAppContext();
|
|
40
|
-
const name = internalOptions.csrConfig.name;
|
|
41
|
-
const { metaName = META_NAME } = internalOptions;
|
|
42
|
-
const internalDirectory = appContext.internalDirectory.replace(META_NAME, metaName || META_NAME);
|
|
43
|
-
const transformRuntimeFn = transformRuntimeOptions || _transformRuntimeOptions;
|
|
44
|
-
const partialSSRRemotes = [];
|
|
45
|
-
const entries = /* @__PURE__ */ new Set();
|
|
46
|
-
let remotePathMap = {};
|
|
47
|
-
const ssrByRouteIdsMap = {};
|
|
48
|
-
const serverPluginOptions = {
|
|
49
|
-
runtimeOptions: transformRuntimeFn(internalOptions.csrConfig),
|
|
50
|
-
partialSSRRemotes,
|
|
51
|
-
ssrByRouteIdsMap
|
|
52
|
-
};
|
|
53
|
-
const csrConfig = internalOptions.csrConfig;
|
|
54
|
-
if (typeof csrConfig.remotes === "object" && Object.keys(csrConfig.remotes).length) {
|
|
55
|
-
const { entries: scanEntries, remotePathMap: scanRemotePathMap } = await generateRouteFile({
|
|
56
|
-
appDirectory: appContext.appDirectory
|
|
57
|
-
});
|
|
58
|
-
scanEntries.forEach((e) => entries.add(e));
|
|
59
|
-
remotePathMap = scanRemotePathMap;
|
|
60
|
-
} else {
|
|
61
|
-
entries.add(DEFAULT_ENTRY);
|
|
62
|
-
}
|
|
63
|
-
return {
|
|
64
|
-
_internalRuntimePlugins: ({ entrypoint, plugins }) => {
|
|
65
|
-
plugins.push({
|
|
66
|
-
name: "ssrDataLoader",
|
|
67
|
-
path: "@module-federation/modern-js/data-loader",
|
|
68
|
-
config: {
|
|
69
|
-
metaName
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
plugins.push({
|
|
73
|
-
name: "ssrDataLoaderInjectAssets",
|
|
74
|
-
path: "@module-federation/modern-js/data-loader-inject-assets",
|
|
75
|
-
config: {
|
|
76
|
-
metaName
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
return {
|
|
80
|
-
entrypoint,
|
|
81
|
-
plugins
|
|
82
|
-
};
|
|
83
|
-
},
|
|
84
|
-
_internalServerPlugins({ plugins }) {
|
|
85
|
-
plugins.push({
|
|
86
|
-
name: serverPlugin,
|
|
87
|
-
options: serverPluginOptions
|
|
88
|
-
});
|
|
89
|
-
return {
|
|
90
|
-
plugins
|
|
91
|
-
};
|
|
92
|
-
},
|
|
93
|
-
async modifyFileSystemRoutes(options) {
|
|
94
|
-
const { routes, entrypoint } = options;
|
|
95
|
-
const { entryName } = entrypoint;
|
|
96
|
-
const prefix = transformName2Prefix(internalOptions.csrConfig.name);
|
|
97
|
-
const modifyRouteIds = (route, pathName) => {
|
|
98
|
-
const currentPathName = route.path ? path.join(pathName, route.path) : pathName;
|
|
99
|
-
if (route.id) {
|
|
100
|
-
var _route_children;
|
|
101
|
-
const originalId = route.id;
|
|
102
|
-
route.originalId = originalId;
|
|
103
|
-
const remoteName = remotePathMap[currentPathName];
|
|
104
|
-
if (remoteName) {
|
|
105
|
-
const remoteIdPrefix = transformName2Prefix(remoteName);
|
|
106
|
-
const isLayout = Boolean(route.children);
|
|
107
|
-
const newId = `${remoteIdPrefix}${isLayout ? "layout" : "page"}`;
|
|
108
|
-
ssrByRouteIdsMap[originalId] = newId;
|
|
109
|
-
route.id = newId;
|
|
110
|
-
if (!partialSSRRemotes.includes(remoteName)) {
|
|
111
|
-
partialSSRRemotes.push(remoteName);
|
|
112
|
-
}
|
|
113
|
-
} else {
|
|
114
|
-
route.id = `${prefix}${route.id}`;
|
|
115
|
-
}
|
|
116
|
-
(_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.forEach((r) => {
|
|
117
|
-
modifyRouteIds(r, currentPathName);
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
routes.forEach((route) => {
|
|
122
|
-
modifyRouteIds(route, entryName);
|
|
123
|
-
});
|
|
124
|
-
return options;
|
|
125
|
-
},
|
|
126
|
-
config: async () => {
|
|
127
|
-
return {
|
|
128
|
-
tools: {
|
|
129
|
-
// bundlerChain can not keep target order
|
|
130
|
-
rspack(_config, { isServer }) {
|
|
131
|
-
patchMFConfig({
|
|
132
|
-
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
133
|
-
metaName,
|
|
134
|
-
isServer,
|
|
135
|
-
internalDirectory,
|
|
136
|
-
entries
|
|
137
|
-
});
|
|
138
|
-
},
|
|
139
|
-
// bundlerChain can not keep target order
|
|
140
|
-
webpack(_config, { isServer }) {
|
|
141
|
-
patchMFConfig({
|
|
142
|
-
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
143
|
-
metaName,
|
|
144
|
-
isServer,
|
|
145
|
-
internalDirectory,
|
|
146
|
-
entries
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
},
|
|
150
|
-
source: {
|
|
151
|
-
define: {
|
|
152
|
-
MODERN_ROUTER_ID_PREFIX: JSON.stringify(transformName2Prefix(name))
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
},
|
|
157
|
-
async afterDev() {
|
|
158
|
-
clearMFCache();
|
|
159
|
-
console.log("afterDev", globalThis.__VMOK__);
|
|
160
|
-
}
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
var plugin_default = moduleFederationDataLoaderPlugin;
|
|
165
|
-
export {
|
|
166
|
-
plugin_default as default,
|
|
167
|
-
generateRoutes,
|
|
168
|
-
moduleFederationDataLoaderPlugin
|
|
169
|
-
};
|
|
@@ -1,11 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { init } from "@module-federation/enhanced/runtime";
|
|
2
|
-
import { isBrowserEnv } from "@module-federation/sdk";
|
|
3
|
-
import { fetchRemoteFullRoutesMap, getSSRByRouteIds } from "./fetchRemoteFullRoutesMap";
|
|
4
|
-
import { injectRemoteRoutes } from "../../runtime/dataLoader/utils";
|
|
5
|
-
import { MF_ROUTES, DEFAULT_ENTRY, MODERN_JS_ROUTE_SERVER_LOADER } from "../../constant";
|
|
6
|
-
var dataLoaderPlugin_default = ({ runtimeOptions, partialSSRRemotes, ssrByRouteIdsMap }) => ({
|
|
7
|
-
name: "MFDataLoaderServerPlugin",
|
|
8
|
-
pre: [
|
|
9
|
-
"@modern-js/plugin-inject-resource"
|
|
10
|
-
],
|
|
11
|
-
setup(api) {
|
|
12
|
-
const { remotes, name } = runtimeOptions;
|
|
13
|
-
if (!remotes.length) {
|
|
14
|
-
return {};
|
|
15
|
-
}
|
|
16
|
-
let isHandled = false;
|
|
17
|
-
return {
|
|
18
|
-
prepare() {
|
|
19
|
-
const { middlewares } = api.useAppContext();
|
|
20
|
-
middlewares.push({
|
|
21
|
-
name: "MFDataLoaderServerPlugin",
|
|
22
|
-
handler: async (c, next) => {
|
|
23
|
-
const serverManifest = c.get("serverManifest");
|
|
24
|
-
const { loaderBundles, nestedRoutesJson } = serverManifest;
|
|
25
|
-
console.log("server plugin ", isHandled, loaderBundles);
|
|
26
|
-
if (isHandled && !globalThis.FORCE_MF_REFRESH) {
|
|
27
|
-
await next();
|
|
28
|
-
} else {
|
|
29
|
-
var _api_useConfigContext_server;
|
|
30
|
-
const instance = init({
|
|
31
|
-
name,
|
|
32
|
-
remotes
|
|
33
|
-
});
|
|
34
|
-
const remoteFullRoutesMap = await fetchRemoteFullRoutesMap({
|
|
35
|
-
instance,
|
|
36
|
-
remotePath: isBrowserEnv() ? `${DEFAULT_ENTRY}/${MF_ROUTES}` : `${DEFAULT_ENTRY}/${MODERN_JS_ROUTE_SERVER_LOADER}`
|
|
37
|
-
});
|
|
38
|
-
const originalSSRByRouteIds = (_api_useConfigContext_server = api.useConfigContext().server) === null || _api_useConfigContext_server === void 0 ? void 0 : _api_useConfigContext_server.ssrByRouteIds;
|
|
39
|
-
if (originalSSRByRouteIds && partialSSRRemotes.length) {
|
|
40
|
-
const remoteSSRByRouteIds = getSSRByRouteIds(partialSSRRemotes, remoteFullRoutesMap) || [];
|
|
41
|
-
const ssrByRouteIds = /* @__PURE__ */ new Set([
|
|
42
|
-
...originalSSRByRouteIds,
|
|
43
|
-
...remoteSSRByRouteIds.map((id) => ssrByRouteIdsMap[id] || id)
|
|
44
|
-
]);
|
|
45
|
-
api.useConfigContext().server.ssrByRouteIds = Array.from(ssrByRouteIds);
|
|
46
|
-
}
|
|
47
|
-
injectRemoteRoutes(loaderBundles, remoteFullRoutesMap);
|
|
48
|
-
injectRemoteRoutes(nestedRoutesJson, remoteFullRoutesMap);
|
|
49
|
-
isHandled = true;
|
|
50
|
-
globalThis.FORCE_MF_REFRESH = false;
|
|
51
|
-
await next();
|
|
52
|
-
}
|
|
53
|
-
},
|
|
54
|
-
before: [
|
|
55
|
-
"render"
|
|
56
|
-
]
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
export {
|
|
63
|
-
dataLoaderPlugin_default as default
|
|
64
|
-
};
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
async function fetchRemoteFullRoutesMap(options) {
|
|
2
|
-
const { instance, remotePath } = options;
|
|
3
|
-
const { remotes } = instance.options;
|
|
4
|
-
const remoteRoutesMap = {};
|
|
5
|
-
if (!remotes.length) {
|
|
6
|
-
return remoteRoutesMap;
|
|
7
|
-
}
|
|
8
|
-
await Promise.all(remotes.map(async (remote) => {
|
|
9
|
-
const remoteId = `${remote.name}/${remotePath}`;
|
|
10
|
-
const { routes } = await instance.loadRemote(remoteId);
|
|
11
|
-
remoteRoutesMap[remote.name] = {
|
|
12
|
-
routes
|
|
13
|
-
};
|
|
14
|
-
}));
|
|
15
|
-
return remoteRoutesMap;
|
|
16
|
-
}
|
|
17
|
-
function getSSRByRouteIds(partialSSRRemotes, remoteRoutesMap) {
|
|
18
|
-
const remoteProviderRouteIds = /* @__PURE__ */ new Set();
|
|
19
|
-
const collectIds = (route) => {
|
|
20
|
-
remoteProviderRouteIds.add(route.id);
|
|
21
|
-
if (route.children) {
|
|
22
|
-
route.children.forEach((r) => {
|
|
23
|
-
collectIds(r);
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
Object.entries(remoteRoutesMap).forEach((item) => {
|
|
28
|
-
const [remoteName, routesObj] = item;
|
|
29
|
-
if (!partialSSRRemotes.includes(remoteName)) {
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
const { routes } = routesObj;
|
|
33
|
-
routes.forEach((route) => {
|
|
34
|
-
collectIds(route);
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
return [
|
|
38
|
-
...remoteProviderRouteIds
|
|
39
|
-
];
|
|
40
|
-
}
|
|
41
|
-
export {
|
|
42
|
-
fetchRemoteFullRoutesMap,
|
|
43
|
-
getSSRByRouteIds
|
|
44
|
-
};
|
|
File without changes
|