@module-federation/modern-js 0.0.0-next-20240813065037 → 0.0.0-next-20240814082511
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/ast/constant.js +46 -0
- package/dist/cjs/cli/ast/generateRoutes.js +140 -0
- package/dist/cjs/cli/ast/generateSlimRoutes.js +106 -0
- package/dist/cjs/cli/ast/index.js +31 -0
- package/dist/cjs/cli/configPlugin.js +1 -0
- package/dist/cjs/cli/constant.js +6 -0
- package/dist/cjs/cli/dataLoaderPlugin.js +220 -0
- package/dist/cjs/cli/index.js +6 -1
- package/dist/cjs/cli/server/dataLoaderPlugin.js +78 -0
- package/dist/cjs/cli/ssrPlugin.js +4 -4
- package/dist/cjs/interfaces/route.js +16 -0
- package/dist/cjs/runtime/constant.js +34 -0
- package/dist/cjs/runtime/dataLoader.js +87 -0
- package/dist/cjs/runtime/index.js +4 -1
- package/dist/cjs/runtime/utils.js +43 -0
- package/dist/cjs/runtime/withMFRouteId.js +31 -0
- package/dist/cjs/ssr-runtime/plugin.js +5 -7
- package/dist/esm/cli/ast/constant.js +16 -0
- package/dist/esm/cli/ast/generateRoutes.js +107 -0
- package/dist/esm/cli/ast/generateSlimRoutes.js +75 -0
- package/dist/esm/cli/ast/index.js +6 -0
- package/dist/esm/cli/configPlugin.js +1 -0
- package/dist/esm/cli/constant.js +4 -0
- package/dist/esm/cli/dataLoaderPlugin.js +207 -0
- package/dist/esm/cli/index.js +6 -1
- package/dist/esm/cli/server/dataLoaderPlugin.js +122 -0
- package/dist/esm/cli/ssrPlugin.js +2 -2
- package/dist/esm/interfaces/route.js +0 -0
- package/dist/esm/runtime/constant.js +8 -0
- package/dist/esm/runtime/dataLoader.js +125 -0
- package/dist/esm/runtime/index.js +3 -1
- package/dist/esm/runtime/utils.js +16 -0
- package/dist/esm/runtime/withMFRouteId.js +7 -0
- package/dist/esm/ssr-runtime/plugin.js +5 -10
- package/dist/esm-node/cli/ast/constant.js +16 -0
- package/dist/esm-node/cli/ast/generateRoutes.js +106 -0
- package/dist/esm-node/cli/ast/generateSlimRoutes.js +72 -0
- package/dist/esm-node/cli/ast/index.js +6 -0
- package/dist/esm-node/cli/configPlugin.js +1 -0
- package/dist/esm-node/cli/constant.js +4 -0
- package/dist/esm-node/cli/dataLoaderPlugin.js +184 -0
- package/dist/esm-node/cli/index.js +6 -1
- package/dist/esm-node/cli/server/dataLoaderPlugin.js +58 -0
- package/dist/esm-node/cli/ssrPlugin.js +2 -2
- package/dist/esm-node/interfaces/route.js +0 -0
- package/dist/esm-node/runtime/constant.js +8 -0
- package/dist/esm-node/runtime/dataLoader.js +63 -0
- package/dist/esm-node/runtime/index.js +3 -1
- package/dist/esm-node/runtime/utils.js +19 -0
- package/dist/esm-node/runtime/withMFRouteId.js +7 -0
- package/dist/esm-node/ssr-runtime/plugin.js +5 -7
- package/dist/types/cli/ast/constant.d.ts +7 -0
- package/dist/types/cli/ast/generateRoutes.d.ts +7 -0
- package/dist/types/cli/ast/generateSlimRoutes.d.ts +7 -0
- package/dist/types/cli/ast/index.d.ts +2 -0
- package/dist/types/cli/constant.d.ts +2 -0
- package/dist/types/cli/dataLoaderPlugin.d.ts +6 -0
- package/dist/types/cli/server/dataLoaderPlugin.d.ts +5 -0
- package/dist/types/interfaces/route.d.ts +13 -0
- package/dist/types/runtime/constant.d.ts +3 -0
- package/dist/types/runtime/dataLoader.d.ts +7 -0
- package/dist/types/runtime/index.d.ts +1 -0
- package/dist/types/runtime/utils.d.ts +1 -0
- package/dist/types/runtime/withMFRouteId.d.ts +1 -0
- package/dist/types/types/index.d.ts +17 -0
- package/package.json +37 -11
|
@@ -0,0 +1,106 @@
|
|
|
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, prefix, baseName }) {
|
|
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(/routeId=[^&]+/, `routeId=${newRouteId}`);
|
|
24
|
+
path.node.source = t.stringLiteral(newSource);
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
ObjectExpression(path) {
|
|
28
|
+
let componentName = "";
|
|
29
|
+
let lazyComponentName = "";
|
|
30
|
+
if (!Array.isArray(path.node.properties)) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const idNode = findTargetKeyNode(path.node.properties, "id");
|
|
34
|
+
if (idNode && t.isObjectProperty(idNode) && t.isStringLiteral(idNode.value)) {
|
|
35
|
+
idNode.value = t.stringLiteral(`${prefix}${idNode.value.value}`);
|
|
36
|
+
}
|
|
37
|
+
const isRootNode = findTargetKeyNode(path.node.properties, "isRoot");
|
|
38
|
+
if (isRootNode && t.isObjectProperty(isRootNode) && t.isBooleanLiteral(isRootNode.value)) {
|
|
39
|
+
isRootNode.value.value = false;
|
|
40
|
+
}
|
|
41
|
+
if (!isRootNode) {
|
|
42
|
+
const lazyComponentNode = findTargetKeyNode(path.node.properties, "lazyImport");
|
|
43
|
+
if (lazyComponentNode && t.isObjectProperty(lazyComponentNode) && t.isArrowFunctionExpression(lazyComponentNode.value)) {
|
|
44
|
+
lazyComponentName = `LazyComponent_${componentId}`;
|
|
45
|
+
const lazyDeclaration = t.variableDeclaration("const", [
|
|
46
|
+
t.variableDeclarator(t.identifier(lazyComponentName), lazyComponentNode.value)
|
|
47
|
+
]);
|
|
48
|
+
lazyComponentNode.value = t.identifier(lazyComponentName);
|
|
49
|
+
const componentNode2 = findTargetKeyNode(path.node.properties, "component");
|
|
50
|
+
if (componentNode2 && t.isObjectProperty(componentNode2) && t.isCallExpression(componentNode2.value) && t.isIdentifier(componentNode2.value.callee)) {
|
|
51
|
+
componentNode2.value = t.callExpression(t.identifier("lazy"), [
|
|
52
|
+
t.identifier(lazyComponentName)
|
|
53
|
+
]);
|
|
54
|
+
}
|
|
55
|
+
lazyComponentDeclarations.push(lazyDeclaration);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const componentNode = findTargetKeyNode(path.node.properties, "component");
|
|
59
|
+
if (componentNode && t.isObjectProperty(componentNode) && t.isCallExpression(componentNode.value) && t.isIdentifier(componentNode.value.callee)) {
|
|
60
|
+
componentName = `Component_${componentId}`;
|
|
61
|
+
const componentDeclaration = t.variableDeclaration("const", [
|
|
62
|
+
t.variableDeclarator(t.identifier(componentName), lazyComponentName ? t.callExpression(t.identifier("lazy"), [
|
|
63
|
+
t.identifier(lazyComponentName)
|
|
64
|
+
]) : componentNode.value)
|
|
65
|
+
]);
|
|
66
|
+
componentDeclarations.push(componentDeclaration);
|
|
67
|
+
componentNode.value = t.identifier(componentName);
|
|
68
|
+
}
|
|
69
|
+
if (lazyComponentName || componentName) {
|
|
70
|
+
componentId++;
|
|
71
|
+
if (componentName) {
|
|
72
|
+
const upperFirstName = componentName.slice(0, 1).toUpperCase() + componentName.slice(1);
|
|
73
|
+
const jsxElement = t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier(upperFirstName), [], true), null, [], true);
|
|
74
|
+
path.node.properties.push(t.objectProperty(t.identifier("element"), jsxElement));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
traverse(ast, {
|
|
80
|
+
Program(path) {
|
|
81
|
+
const lastImportIndex = path.get("body").reduce((lastIndex, p, index) => {
|
|
82
|
+
if (t.isImportDeclaration(p.node)) {
|
|
83
|
+
lastIndex = index;
|
|
84
|
+
}
|
|
85
|
+
return lastIndex;
|
|
86
|
+
}, -1);
|
|
87
|
+
if (lastImportIndex >= 0) {
|
|
88
|
+
const lastImport = path.get(`body.${lastImportIndex}`);
|
|
89
|
+
[
|
|
90
|
+
...componentDeclarations,
|
|
91
|
+
...lazyComponentDeclarations
|
|
92
|
+
].forEach((declaration) => {
|
|
93
|
+
if ("insertAfter" in lastImport) {
|
|
94
|
+
lastImport.insertAfter(declaration);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
const { code: newCode } = generate(ast);
|
|
101
|
+
const finalCode = `${newCode}export const baseName = '${baseName}';`;
|
|
102
|
+
fs.writeFileSync(filePath, finalCode);
|
|
103
|
+
}
|
|
104
|
+
export {
|
|
105
|
+
generateRoutes
|
|
106
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
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, ID, 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(/routeId=[^&]+/, `routeId=${newRouteId}`);
|
|
25
|
+
path.node.source = t.stringLiteral(newSource);
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
ObjectExpression(path) {
|
|
29
|
+
if (!Array.isArray(path.node.properties)) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
path.node.properties.forEach((prop) => {
|
|
33
|
+
if (t.isObjectProperty(prop) && t.isStringLiteral(prop.key) && t.isStringLiteral(prop.value) && prop.key.value === ID) {
|
|
34
|
+
prop.value = t.stringLiteral(`${prefix}${prop.value.value}`);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
path.node.properties = path.node.properties.filter((p) => {
|
|
38
|
+
if (t.isObjectProperty(p) && t.isStringLiteral(p.key)) {
|
|
39
|
+
return !removedKeys.includes(p.key.value);
|
|
40
|
+
} else {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
const tempCode = generate(ast).code;
|
|
47
|
+
const tempAst = babelParser.parse(tempCode, {
|
|
48
|
+
sourceType: "module"
|
|
49
|
+
});
|
|
50
|
+
const usedIdentifiers = /* @__PURE__ */ new Set();
|
|
51
|
+
traverse(tempAst, {
|
|
52
|
+
Identifier(path) {
|
|
53
|
+
if (t.isProperty(path.parent)) {
|
|
54
|
+
usedIdentifiers.add(path.node.name);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
traverse(tempAst, {
|
|
59
|
+
ImportDeclaration(path) {
|
|
60
|
+
path.node.specifiers = path.node.specifiers.filter((specifier) => usedIdentifiers.has(specifier.local.name));
|
|
61
|
+
if (!path.node.specifiers.length) {
|
|
62
|
+
path.remove();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
const { code: newCode } = generate(tempAst);
|
|
67
|
+
const finalCode = `${newCode}export const baseName = '${baseName}';`;
|
|
68
|
+
fs.writeFileSync(filePath, finalCode);
|
|
69
|
+
}
|
|
70
|
+
export {
|
|
71
|
+
generateSlimRoutes
|
|
72
|
+
};
|
|
@@ -32,6 +32,7 @@ const moduleFederationConfigPlugin = (userConfig) => ({
|
|
|
32
32
|
return {
|
|
33
33
|
config: async () => {
|
|
34
34
|
var _modernjsConfig_server, _modernjsConfig_dev;
|
|
35
|
+
console.log("config plugin config");
|
|
35
36
|
const bundlerType = useAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
|
|
36
37
|
const ipv4 = getIPV4();
|
|
37
38
|
const enableSSR = Boolean(modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { fs } from "@modern-js/utils";
|
|
3
|
+
import { transformName2Prefix } from "../runtime/utils";
|
|
4
|
+
import { PLUGIN_IDENTIFIER } from "../constant";
|
|
5
|
+
import { MF_FULL_ROUTES, MF_SLIM_ROUTES, MF_ROUTES_META } from "../runtime/constant";
|
|
6
|
+
import { generateRoutes, generateSlimRoutes } from "./ast";
|
|
7
|
+
import { MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME, META_NAME } from "./constant";
|
|
8
|
+
function generateExtraExposeFiles(options) {
|
|
9
|
+
const { routesFilePath, mfConfig, isServer, baseName } = options;
|
|
10
|
+
const outputDir = path.resolve(process.cwd(), "node_modules/.federation");
|
|
11
|
+
fs.ensureDirSync(outputDir);
|
|
12
|
+
const addSuffix = (fileName) => {
|
|
13
|
+
if (!isServer) {
|
|
14
|
+
return `${fileName}.jsx`;
|
|
15
|
+
}
|
|
16
|
+
return `${fileName}.server.jsx`;
|
|
17
|
+
};
|
|
18
|
+
const routesFileContent = fs.readFileSync(routesFilePath, "utf-8");
|
|
19
|
+
const outputSlimRoutesPath = path.resolve(outputDir, addSuffix(MF_SLIM_ROUTES));
|
|
20
|
+
const outputFullRoutesPath = path.resolve(outputDir, addSuffix(MF_FULL_ROUTES));
|
|
21
|
+
const outputRoutesMetaPath = path.resolve(outputDir, `${MF_ROUTES_META}.js`);
|
|
22
|
+
generateSlimRoutes({
|
|
23
|
+
sourceCode: routesFileContent,
|
|
24
|
+
filePath: outputSlimRoutesPath,
|
|
25
|
+
prefix: transformName2Prefix(mfConfig.name),
|
|
26
|
+
baseName
|
|
27
|
+
});
|
|
28
|
+
generateRoutes({
|
|
29
|
+
sourceCode: routesFileContent,
|
|
30
|
+
filePath: outputFullRoutesPath,
|
|
31
|
+
prefix: transformName2Prefix(mfConfig.name),
|
|
32
|
+
baseName
|
|
33
|
+
});
|
|
34
|
+
fs.writeFileSync(outputRoutesMetaPath, `export const baseName = '${baseName}';`);
|
|
35
|
+
return {
|
|
36
|
+
outputSlimRoutesPath,
|
|
37
|
+
outputFullRoutesPath,
|
|
38
|
+
outputRoutesMetaPath
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function addExpose(options) {
|
|
42
|
+
const { mfConfig } = options;
|
|
43
|
+
const { outputSlimRoutesPath, outputFullRoutesPath, outputRoutesMetaPath } = generateExtraExposeFiles(options);
|
|
44
|
+
const fullRoutesKey = `./${MF_FULL_ROUTES}`;
|
|
45
|
+
const slimRoutesKey = `./${MF_SLIM_ROUTES}`;
|
|
46
|
+
const routeMetaKey = `./${MF_ROUTES_META}`;
|
|
47
|
+
if (!mfConfig.exposes) {
|
|
48
|
+
mfConfig.exposes = {
|
|
49
|
+
[fullRoutesKey]: outputFullRoutesPath,
|
|
50
|
+
[slimRoutesKey]: outputSlimRoutesPath,
|
|
51
|
+
[routeMetaKey]: outputRoutesMetaPath
|
|
52
|
+
};
|
|
53
|
+
} else {
|
|
54
|
+
if (!Array.isArray(mfConfig.exposes)) {
|
|
55
|
+
if (!mfConfig.exposes[fullRoutesKey]) {
|
|
56
|
+
mfConfig.exposes[fullRoutesKey] = outputFullRoutesPath;
|
|
57
|
+
}
|
|
58
|
+
if (!mfConfig.exposes[slimRoutesKey]) {
|
|
59
|
+
mfConfig.exposes[slimRoutesKey] = outputSlimRoutesPath;
|
|
60
|
+
}
|
|
61
|
+
if (!mfConfig.exposes[routeMetaKey]) {
|
|
62
|
+
mfConfig.exposes[routeMetaKey] = outputRoutesMetaPath;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function addShared(options) {
|
|
68
|
+
const { metaName, mfConfig } = options;
|
|
69
|
+
const alias = `@${metaName}/runtime/router`;
|
|
70
|
+
if (!mfConfig.shared) {
|
|
71
|
+
mfConfig.shared = {
|
|
72
|
+
[alias]: {
|
|
73
|
+
singleton: true
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
} else {
|
|
77
|
+
if (!Array.isArray(mfConfig.shared)) {
|
|
78
|
+
mfConfig.shared[alias] = {
|
|
79
|
+
singleton: true
|
|
80
|
+
};
|
|
81
|
+
} else {
|
|
82
|
+
mfConfig.shared.push(alias);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function _pathMfConfig(options) {
|
|
87
|
+
addShared(options);
|
|
88
|
+
addExpose(options);
|
|
89
|
+
}
|
|
90
|
+
function transformRuntimeOptions(buildOptions) {
|
|
91
|
+
const remotes = buildOptions.remotes || {};
|
|
92
|
+
const runtimeRemotes = Object.entries(remotes).map((remote) => {
|
|
93
|
+
const [_alias, nameAndEntry] = remote;
|
|
94
|
+
const [name, entry] = nameAndEntry.split("@");
|
|
95
|
+
return {
|
|
96
|
+
name,
|
|
97
|
+
entry
|
|
98
|
+
};
|
|
99
|
+
});
|
|
100
|
+
return {
|
|
101
|
+
name: buildOptions.name,
|
|
102
|
+
remotes: runtimeRemotes
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
const moduleFederationDataLoaderPlugin = (enable, internalOptions, userConfig) => ({
|
|
106
|
+
name: "@modern-js/plugin-module-federation-data-loader",
|
|
107
|
+
pre: [
|
|
108
|
+
"@modern-js/plugin-module-federation-config"
|
|
109
|
+
],
|
|
110
|
+
post: [
|
|
111
|
+
"@modern-js/plugin-router",
|
|
112
|
+
"@modern-js/plugin-module-federation"
|
|
113
|
+
],
|
|
114
|
+
setup: async ({ useConfigContext, useAppContext }) => {
|
|
115
|
+
var _modernjsConfig_server, _internalOptions_csrConfig;
|
|
116
|
+
if (!enable) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const { baseName, partialSSRRemotes = [], fetchSSRByRouteIds, patchMFConfig, metaName = META_NAME, serverPlugin = "@module-federation/modern-js/data-loader-server", runtimeOptions } = userConfig;
|
|
120
|
+
if (!baseName) {
|
|
121
|
+
throw new Error(`${PLUGIN_IDENTIFIER} 'baseName' is required if you enable 'dataLoader'!`);
|
|
122
|
+
}
|
|
123
|
+
const modernjsConfig = useConfigContext();
|
|
124
|
+
const appContext = useAppContext();
|
|
125
|
+
const enableSSR = Boolean(modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
|
|
126
|
+
const name = (_internalOptions_csrConfig = internalOptions.csrConfig) === null || _internalOptions_csrConfig === void 0 ? void 0 : _internalOptions_csrConfig.name;
|
|
127
|
+
const routesFilePath = path.resolve(appContext.internalDirectory.replace(META_NAME, metaName || META_NAME), `./main/${MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME}`);
|
|
128
|
+
return {
|
|
129
|
+
_internalRuntimePlugins: ({ entrypoint, plugins }) => {
|
|
130
|
+
plugins.push({
|
|
131
|
+
name: "ssrDataLoader",
|
|
132
|
+
path: "@module-federation/modern-js/data-loader",
|
|
133
|
+
config: {}
|
|
134
|
+
});
|
|
135
|
+
return {
|
|
136
|
+
entrypoint,
|
|
137
|
+
plugins
|
|
138
|
+
};
|
|
139
|
+
},
|
|
140
|
+
_internalServerPlugins({ plugins }) {
|
|
141
|
+
plugins.push({
|
|
142
|
+
name: serverPlugin,
|
|
143
|
+
options: runtimeOptions || transformRuntimeOptions(internalOptions.csrConfig)
|
|
144
|
+
});
|
|
145
|
+
return {
|
|
146
|
+
plugins
|
|
147
|
+
};
|
|
148
|
+
},
|
|
149
|
+
config: async () => {
|
|
150
|
+
console.log("dataloader plugin config");
|
|
151
|
+
const patchMFConfigFn = patchMFConfig || _pathMfConfig;
|
|
152
|
+
return {
|
|
153
|
+
// server: {
|
|
154
|
+
// ssrByRouteIds: ssrByRouteIds,
|
|
155
|
+
// },
|
|
156
|
+
tools: {
|
|
157
|
+
rspack(_config, { isServer }) {
|
|
158
|
+
patchMFConfigFn({
|
|
159
|
+
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
160
|
+
baseName,
|
|
161
|
+
metaName,
|
|
162
|
+
isServer,
|
|
163
|
+
routesFilePath
|
|
164
|
+
});
|
|
165
|
+
console.log("dataloader plugin rspack");
|
|
166
|
+
}
|
|
167
|
+
},
|
|
168
|
+
source: {
|
|
169
|
+
define: {
|
|
170
|
+
MODERN_ROUTER_ID_PREFIX: JSON.stringify(transformName2Prefix(name))
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
var dataLoaderPlugin_default = moduleFederationDataLoaderPlugin;
|
|
179
|
+
export {
|
|
180
|
+
dataLoaderPlugin_default as default,
|
|
181
|
+
generateRoutes,
|
|
182
|
+
generateSlimRoutes,
|
|
183
|
+
moduleFederationDataLoaderPlugin
|
|
184
|
+
};
|
|
@@ -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 "./dataLoaderPlugin";
|
|
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
|
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { getInstance, init } from "@module-federation/enhanced/runtime";
|
|
2
|
+
import { MF_SLIM_ROUTES } from "../../runtime/constant";
|
|
3
|
+
var dataLoaderPlugin_default = (mfRuntimeOptions) => ({
|
|
4
|
+
name: "MFDataLoaderServerPlugin",
|
|
5
|
+
pre: [
|
|
6
|
+
"@modern-js/plugin-inject-resource"
|
|
7
|
+
],
|
|
8
|
+
setup(api) {
|
|
9
|
+
const { remotes, name } = mfRuntimeOptions;
|
|
10
|
+
if (!remotes.length) {
|
|
11
|
+
return {};
|
|
12
|
+
}
|
|
13
|
+
let isHandled = false;
|
|
14
|
+
return {
|
|
15
|
+
prepare() {
|
|
16
|
+
const { middlewares } = api.useAppContext();
|
|
17
|
+
middlewares.push({
|
|
18
|
+
name: "MFDataLoaderServerPlugin",
|
|
19
|
+
handler: async (c, next) => {
|
|
20
|
+
console.log("isHandled : ", isHandled);
|
|
21
|
+
const serverManifest = c.get("serverManifest");
|
|
22
|
+
const { loaderBundles } = serverManifest;
|
|
23
|
+
console.log("loaderBundles.main.routes : ", loaderBundles.main.routes);
|
|
24
|
+
if (isHandled) {
|
|
25
|
+
await next();
|
|
26
|
+
} else {
|
|
27
|
+
const instance = getInstance() || init({
|
|
28
|
+
name,
|
|
29
|
+
remotes
|
|
30
|
+
});
|
|
31
|
+
const slimRoutes = await Promise.all(remotes.map(async (remote) => {
|
|
32
|
+
const { routes, baseName } = await instance.loadRemote(`${remote.name}/${MF_SLIM_ROUTES}`);
|
|
33
|
+
return {
|
|
34
|
+
routes,
|
|
35
|
+
baseName
|
|
36
|
+
};
|
|
37
|
+
}));
|
|
38
|
+
slimRoutes.forEach((slimRoute) => {
|
|
39
|
+
const { routes, baseName } = slimRoute;
|
|
40
|
+
routes[0].path = `/${baseName}`;
|
|
41
|
+
loaderBundles.main.routes.push(...routes);
|
|
42
|
+
});
|
|
43
|
+
console.log("loaderBundles.main.routes : ", loaderBundles.main.routes);
|
|
44
|
+
isHandled = true;
|
|
45
|
+
await next();
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
before: [
|
|
49
|
+
"render"
|
|
50
|
+
]
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
export {
|
|
57
|
+
dataLoaderPlugin_default as default
|
|
58
|
+
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import path from "path";
|
|
2
2
|
import { fs } from "@modern-js/utils";
|
|
3
|
-
import { ModuleFederationPlugin } from "@module-federation/enhanced
|
|
3
|
+
import { ModuleFederationPlugin } from "@module-federation/enhanced";
|
|
4
4
|
import { ModuleFederationPlugin as RspackModuleFederationPlugin } from "@module-federation/enhanced/rspack";
|
|
5
|
-
import UniverseEntryChunkTrackerPlugin from "@module-federation/node
|
|
5
|
+
import { UniverseEntryChunkTrackerPlugin } from "@module-federation/node";
|
|
6
6
|
import { updateStatsAndManifest } from "./manifest";
|
|
7
7
|
import { isDev } from "./constant";
|
|
8
8
|
function setEnv() {
|
|
File without changes
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { getInstance, loadRemote } from "@module-federation/enhanced/runtime";
|
|
3
|
+
import { MF_FULL_ROUTES } from "./constant";
|
|
4
|
+
globalThis.mfRemoteRoutes = globalThis.mfRemoteRoutes || [];
|
|
5
|
+
var _globalThis_mfHasLoadedRemoteRoutes;
|
|
6
|
+
globalThis.mfHasLoadedRemoteRoutes = (_globalThis_mfHasLoadedRemoteRoutes = globalThis.mfHasLoadedRemoteRoutes) !== null && _globalThis_mfHasLoadedRemoteRoutes !== void 0 ? _globalThis_mfHasLoadedRemoteRoutes : false;
|
|
7
|
+
async function loadRoutes() {
|
|
8
|
+
var _getInstance;
|
|
9
|
+
if (globalThis.mfHasLoadedRemoteRoutes) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const remotes = (_getInstance = getInstance()) === null || _getInstance === void 0 ? void 0 : _getInstance.options.remotes;
|
|
13
|
+
if (!remotes) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const remoteModuleIds = remotes.map((remote) => {
|
|
17
|
+
return `${remote.name}/${MF_FULL_ROUTES}`;
|
|
18
|
+
});
|
|
19
|
+
const traverse = (cRoutes, prefix) => {
|
|
20
|
+
cRoutes.forEach((i) => {
|
|
21
|
+
i.id = prefix + i.id;
|
|
22
|
+
const Comp = i.component;
|
|
23
|
+
i.element = /* @__PURE__ */ _jsx(Comp, {});
|
|
24
|
+
if (i.children) {
|
|
25
|
+
traverse(i.children, prefix);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
await Promise.all(remoteModuleIds.map(async (remoteModuleId) => {
|
|
30
|
+
const remoteName = remoteModuleId.split(`/${MF_FULL_ROUTES}`)[0];
|
|
31
|
+
const { routes, baseName } = await loadRemote(remoteModuleId);
|
|
32
|
+
routes[0].path = `/${baseName}`;
|
|
33
|
+
routes[0].isRoot = false;
|
|
34
|
+
globalThis.mfRemoteRoutes.push(...routes);
|
|
35
|
+
}));
|
|
36
|
+
globalThis.mfHasLoadedRemoteRoutes = true;
|
|
37
|
+
return globalThis.mfRemoteRoutes;
|
|
38
|
+
}
|
|
39
|
+
const ssrDataLoaderPlugin = () => {
|
|
40
|
+
return {
|
|
41
|
+
name: "@modern-js/plugin-mf-data-loader",
|
|
42
|
+
post: [
|
|
43
|
+
"@modern-js/plugin-router"
|
|
44
|
+
],
|
|
45
|
+
setup: () => {
|
|
46
|
+
return {
|
|
47
|
+
async beforeRender({ context }) {
|
|
48
|
+
console.log("init");
|
|
49
|
+
await loadRoutes();
|
|
50
|
+
},
|
|
51
|
+
modifyRoutes: (routes) => {
|
|
52
|
+
console.log("modifyRoutes");
|
|
53
|
+
routes.push(...globalThis.mfRemoteRoutes);
|
|
54
|
+
console.log("routes: ", routes);
|
|
55
|
+
return routes;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
export {
|
|
62
|
+
ssrDataLoaderPlugin
|
|
63
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
function transformName2Prefix(name) {
|
|
2
|
+
const NameTransformSymbol = {
|
|
3
|
+
AT: "@",
|
|
4
|
+
HYPHEN: "-",
|
|
5
|
+
SLASH: "/",
|
|
6
|
+
UNDERLINE: "_"
|
|
7
|
+
};
|
|
8
|
+
const NameTransformMap = {
|
|
9
|
+
[NameTransformSymbol.AT]: "SCOPE",
|
|
10
|
+
[NameTransformSymbol.HYPHEN]: "HYPHEN",
|
|
11
|
+
[NameTransformSymbol.SLASH]: "SLASH",
|
|
12
|
+
[NameTransformSymbol.UNDERLINE]: "UNDERLINE"
|
|
13
|
+
};
|
|
14
|
+
const SPLIT_SYMBOL = "@";
|
|
15
|
+
return `${name.replace(new RegExp(`${NameTransformSymbol.AT}`, "g"), NameTransformMap[NameTransformSymbol.AT]).replace(new RegExp(`${NameTransformSymbol.HYPHEN}`, "g"), NameTransformMap[NameTransformSymbol.HYPHEN]).replace(new RegExp(`${NameTransformSymbol.SLASH}`, "g"), NameTransformMap[NameTransformSymbol.SLASH]).replace(new RegExp(`${NameTransformSymbol.UNDERLINE}`, "g"), NameTransformMap[NameTransformSymbol.UNDERLINE])}${SPLIT_SYMBOL}`;
|
|
16
|
+
}
|
|
17
|
+
export {
|
|
18
|
+
transformName2Prefix
|
|
19
|
+
};
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
2
|
import hoistNonReactStatics from "hoist-non-react-statics";
|
|
3
3
|
import { SSRLiveReload } from "./SSRLiveReload";
|
|
4
|
+
console.log("mfSSRPlugin trigger");
|
|
4
5
|
const mfSSRPlugin = () => ({
|
|
5
6
|
name: "@module-federation/modern-js",
|
|
6
7
|
setup: () => {
|
|
7
8
|
return {
|
|
8
|
-
async
|
|
9
|
+
async beforeRender() {
|
|
10
|
+
console.log(111, "beforeRender");
|
|
9
11
|
if (typeof window !== "undefined") {
|
|
10
|
-
return
|
|
11
|
-
context
|
|
12
|
-
});
|
|
12
|
+
return;
|
|
13
13
|
}
|
|
14
14
|
globalThis.shouldUpdate = false;
|
|
15
15
|
const nodeUtils = await import("@module-federation/node/utils");
|
|
@@ -19,9 +19,7 @@ const mfSSRPlugin = () => ({
|
|
|
19
19
|
await nodeUtils.flushChunks();
|
|
20
20
|
globalThis.shouldUpdate = true;
|
|
21
21
|
}
|
|
22
|
-
return
|
|
23
|
-
context
|
|
24
|
-
});
|
|
22
|
+
return;
|
|
25
23
|
},
|
|
26
24
|
wrapRoot(App) {
|
|
27
25
|
const AppWrapper = (props) => /* @__PURE__ */ _jsxs(_Fragment, {
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const IS_ROOT = "isRoot";
|
|
2
|
+
export declare const ID = "id";
|
|
3
|
+
export declare const COMPONENT = "component";
|
|
4
|
+
export declare const LAZY_COMPONENT = "lazyImport";
|
|
5
|
+
export declare const SHOULD_REVALIDATE = "shouldRevalidate";
|
|
6
|
+
export declare const PRIVATE_COMPONENT = "_component";
|
|
7
|
+
export declare const ELEMENT = "element";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { CliPlugin, AppTools } from '@modern-js/app-tools';
|
|
2
|
+
import type { DataLoaderOptions, InternalModernPluginOptions } from '../types';
|
|
3
|
+
import { generateRoutes, generateSlimRoutes } from './ast';
|
|
4
|
+
export declare const moduleFederationDataLoaderPlugin: (enable: boolean, internalOptions: InternalModernPluginOptions, userConfig: DataLoaderOptions) => CliPlugin<AppTools>;
|
|
5
|
+
export default moduleFederationDataLoaderPlugin;
|
|
6
|
+
export { generateRoutes, generateSlimRoutes };
|