@module-federation/modern-js 0.0.0-next-20240819040425 → 0.0.0-next-20240820034939
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 +49 -0
- package/dist/cjs/cli/ast/generateRoutes.js +140 -0
- package/dist/cjs/cli/ast/generateSerializableRoutes.js +91 -0
- package/dist/cjs/cli/ast/generateSlimRoutes.js +106 -0
- package/dist/cjs/cli/ast/index.js +34 -0
- package/dist/cjs/cli/bundler-plugins/SerializableRoutesPlugin.js +65 -0
- package/dist/cjs/cli/constant.js +6 -0
- package/dist/cjs/cli/dataLoaderPlugin.js +288 -0
- package/dist/cjs/cli/index.js +6 -1
- package/dist/cjs/cli/server/dataLoaderPlugin.js +83 -0
- package/dist/cjs/cli/ssrPlugin.js +4 -4
- package/dist/cjs/cli/utils.js +7 -3
- 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 +8 -7
- package/dist/esm/cli/ast/constant.js +18 -0
- package/dist/esm/cli/ast/generateRoutes.js +107 -0
- package/dist/esm/cli/ast/generateSerializableRoutes.js +58 -0
- package/dist/esm/cli/ast/generateSlimRoutes.js +75 -0
- package/dist/esm/cli/ast/index.js +8 -0
- package/dist/esm/cli/bundler-plugins/SerializableRoutesPlugin.js +45 -0
- package/dist/esm/cli/constant.js +4 -0
- package/dist/esm/cli/dataLoaderPlugin.js +325 -0
- package/dist/esm/cli/index.js +6 -1
- package/dist/esm/cli/server/dataLoaderPlugin.js +146 -0
- package/dist/esm/cli/ssrPlugin.js +2 -2
- package/dist/esm/cli/utils.js +5 -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 +8 -10
- package/dist/esm-node/cli/ast/constant.js +18 -0
- package/dist/esm-node/cli/ast/generateRoutes.js +106 -0
- package/dist/esm-node/cli/ast/generateSerializableRoutes.js +57 -0
- package/dist/esm-node/cli/ast/generateSlimRoutes.js +72 -0
- package/dist/esm-node/cli/ast/index.js +8 -0
- package/dist/esm-node/cli/bundler-plugins/SerializableRoutesPlugin.js +30 -0
- package/dist/esm-node/cli/constant.js +4 -0
- package/dist/esm-node/cli/dataLoaderPlugin.js +252 -0
- package/dist/esm-node/cli/index.js +6 -1
- package/dist/esm-node/cli/server/dataLoaderPlugin.js +63 -0
- package/dist/esm-node/cli/ssrPlugin.js +2 -2
- package/dist/esm-node/cli/utils.js +5 -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 +8 -7
- package/dist/types/cli/ast/constant.d.ts +8 -0
- package/dist/types/cli/ast/generateRoutes.d.ts +7 -0
- package/dist/types/cli/ast/generateSerializableRoutes.d.ts +5 -0
- package/dist/types/cli/ast/generateSlimRoutes.d.ts +7 -0
- package/dist/types/cli/ast/index.d.ts +3 -0
- package/dist/types/cli/bundler-plugins/SerializableRoutesPlugin.d.ts +12 -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/cli/utils.d.ts +2 -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 +11 -0
- package/package.json +40 -11
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
2
|
+
function transformName2Prefix(name) {
|
|
3
|
+
var NameTransformSymbol = {
|
|
4
|
+
AT: "@",
|
|
5
|
+
HYPHEN: "-",
|
|
6
|
+
SLASH: "/",
|
|
7
|
+
UNDERLINE: "_"
|
|
8
|
+
};
|
|
9
|
+
var _obj;
|
|
10
|
+
var NameTransformMap = (_obj = {}, _define_property(_obj, NameTransformSymbol.AT, "SCOPE"), _define_property(_obj, NameTransformSymbol.HYPHEN, "HYPHEN"), _define_property(_obj, NameTransformSymbol.SLASH, "SLASH"), _define_property(_obj, NameTransformSymbol.UNDERLINE, "UNDERLINE"), _obj);
|
|
11
|
+
var SPLIT_SYMBOL = "@";
|
|
12
|
+
return "".concat(name.replace(new RegExp("".concat(NameTransformSymbol.AT), "g"), NameTransformMap[NameTransformSymbol.AT]).replace(new RegExp("".concat(NameTransformSymbol.HYPHEN), "g"), NameTransformMap[NameTransformSymbol.HYPHEN]).replace(new RegExp("".concat(NameTransformSymbol.SLASH), "g"), NameTransformMap[NameTransformSymbol.SLASH]).replace(new RegExp("".concat(NameTransformSymbol.UNDERLINE), "g"), NameTransformMap[NameTransformSymbol.UNDERLINE])).concat(SPLIT_SYMBOL);
|
|
13
|
+
}
|
|
14
|
+
export {
|
|
15
|
+
transformName2Prefix
|
|
16
|
+
};
|
|
@@ -4,24 +4,25 @@ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
|
4
4
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
5
5
|
import hoistNonReactStatics from "hoist-non-react-statics";
|
|
6
6
|
import { SSRLiveReload } from "./SSRLiveReload";
|
|
7
|
+
console.log("mfSSRPlugin trigger");
|
|
7
8
|
var mfSSRPlugin = function() {
|
|
8
9
|
return {
|
|
9
10
|
name: "@module-federation/modern-js",
|
|
11
|
+
pre: [
|
|
12
|
+
"@modern-js/plugin-router"
|
|
13
|
+
],
|
|
10
14
|
setup: function() {
|
|
11
15
|
return {
|
|
12
|
-
|
|
13
|
-
var context = param.context;
|
|
16
|
+
beforeRender: function beforeRender() {
|
|
14
17
|
return _async_to_generator(function() {
|
|
15
18
|
var nodeUtils, shouldUpdate;
|
|
16
19
|
return _ts_generator(this, function(_state) {
|
|
17
20
|
switch (_state.label) {
|
|
18
21
|
case 0:
|
|
22
|
+
console.log(111, "beforeRender");
|
|
19
23
|
if (typeof window !== "undefined") {
|
|
20
24
|
return [
|
|
21
|
-
2
|
|
22
|
-
next({
|
|
23
|
-
context
|
|
24
|
-
})
|
|
25
|
+
2
|
|
25
26
|
];
|
|
26
27
|
}
|
|
27
28
|
globalThis.shouldUpdate = false;
|
|
@@ -53,10 +54,7 @@ var mfSSRPlugin = function() {
|
|
|
53
54
|
_state.label = 4;
|
|
54
55
|
case 4:
|
|
55
56
|
return [
|
|
56
|
-
2
|
|
57
|
-
next({
|
|
58
|
-
context
|
|
59
|
-
})
|
|
57
|
+
2
|
|
60
58
|
];
|
|
61
59
|
}
|
|
62
60
|
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const IS_ROOT = "isRoot";
|
|
2
|
+
const ID = "id";
|
|
3
|
+
const COMPONENT = "component";
|
|
4
|
+
const LAZY_COMPONENT = "lazyImport";
|
|
5
|
+
const SHOULD_REVALIDATE = "shouldRevalidate";
|
|
6
|
+
const PRIVATE_COMPONENT = "_component";
|
|
7
|
+
const ELEMENT = "element";
|
|
8
|
+
const LOADER = "loader";
|
|
9
|
+
export {
|
|
10
|
+
COMPONENT,
|
|
11
|
+
ELEMENT,
|
|
12
|
+
ID,
|
|
13
|
+
IS_ROOT,
|
|
14
|
+
LAZY_COMPONENT,
|
|
15
|
+
LOADER,
|
|
16
|
+
PRIVATE_COMPONENT,
|
|
17
|
+
SHOULD_REVALIDATE
|
|
18
|
+
};
|
|
@@ -0,0 +1,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,57 @@
|
|
|
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
|
+
prop1.value = t.stringLiteral(`${prefix}${prop1.value.value}`);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
path1.node.properties = path1.node.properties.filter((p1) => {
|
|
28
|
+
if (t.isObjectProperty(p1) && t.isStringLiteral(p1.key)) {
|
|
29
|
+
return !removedKeys.includes(p1.key.value);
|
|
30
|
+
} else {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
let routesValue = "";
|
|
37
|
+
traverse(ast, {
|
|
38
|
+
VariableDeclarator(path) {
|
|
39
|
+
if (t.isVariableDeclarator(path.node) && t.isIdentifier(path.node.id) && path.node.id.name === "routes") {
|
|
40
|
+
const routesAst = path.node.init;
|
|
41
|
+
if (!routesAst) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const { code } = generate(routesAst, {
|
|
45
|
+
compact: true,
|
|
46
|
+
retainLines: false,
|
|
47
|
+
concise: true
|
|
48
|
+
});
|
|
49
|
+
routesValue = eval("(" + code + ")");
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
return routesValue;
|
|
54
|
+
}
|
|
55
|
+
export {
|
|
56
|
+
generateSerializableRoutes
|
|
57
|
+
};
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { generateRoutes } from "./generateRoutes";
|
|
2
|
+
import { generateSlimRoutes } from "./generateSlimRoutes";
|
|
3
|
+
import { generateSerializableRoutes } from "./generateSerializableRoutes";
|
|
4
|
+
export {
|
|
5
|
+
generateRoutes,
|
|
6
|
+
generateSerializableRoutes,
|
|
7
|
+
generateSlimRoutes
|
|
8
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { generateSerializableRoutes } from "../ast";
|
|
3
|
+
const SERIALIZABLE_ROUTES = "serializable-routes.json";
|
|
4
|
+
class SerializableRoutesPlugin {
|
|
5
|
+
apply(compiler) {
|
|
6
|
+
compiler.hooks.thisCompilation.tap("generateStats", (compilation) => {
|
|
7
|
+
compilation.hooks.processAssets.tapPromise({
|
|
8
|
+
name: "generateSerializableRoutes",
|
|
9
|
+
// @ts-ignore use runtime variable in case peer dep not installed
|
|
10
|
+
stage: compilation.constructor.PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER
|
|
11
|
+
}, async () => {
|
|
12
|
+
const sourceCode = fs.readFileSync(this.routesFilePath, "utf-8");
|
|
13
|
+
const routesCode = generateSerializableRoutes({
|
|
14
|
+
sourceCode,
|
|
15
|
+
prefix: this.prefix
|
|
16
|
+
});
|
|
17
|
+
compilation.emitAsset(SERIALIZABLE_ROUTES, new compiler.webpack.sources.RawSource(JSON.stringify(routesCode, null, 2)));
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
constructor(options) {
|
|
22
|
+
const { routesFilePath, prefix } = options;
|
|
23
|
+
this.routesFilePath = routesFilePath;
|
|
24
|
+
this.prefix = prefix;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export {
|
|
28
|
+
SERIALIZABLE_ROUTES,
|
|
29
|
+
SerializableRoutesPlugin
|
|
30
|
+
};
|
|
@@ -0,0 +1,252 @@
|
|
|
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
|
+
import { getIPV4, replaceRemoteUrl } from "./utils";
|
|
9
|
+
import { SerializableRoutesPlugin, SERIALIZABLE_ROUTES } from "./bundler-plugins/SerializableRoutesPlugin";
|
|
10
|
+
function generateExtraExposeFiles(options) {
|
|
11
|
+
const { routesFilePath, mfConfig, isServer, baseName } = options;
|
|
12
|
+
const outputDir = path.resolve(process.cwd(), "node_modules/.federation");
|
|
13
|
+
fs.ensureDirSync(outputDir);
|
|
14
|
+
const addSuffix = (fileName) => {
|
|
15
|
+
if (!isServer) {
|
|
16
|
+
return `${fileName}.jsx`;
|
|
17
|
+
}
|
|
18
|
+
return `${fileName}.server.jsx`;
|
|
19
|
+
};
|
|
20
|
+
const routesFileContent = fs.readFileSync(routesFilePath, "utf-8");
|
|
21
|
+
const outputSlimRoutesPath = path.resolve(outputDir, addSuffix(MF_SLIM_ROUTES));
|
|
22
|
+
const outputFullRoutesPath = path.resolve(outputDir, addSuffix(MF_FULL_ROUTES));
|
|
23
|
+
const outputRoutesMetaPath = path.resolve(outputDir, `${MF_ROUTES_META}.js`);
|
|
24
|
+
generateSlimRoutes({
|
|
25
|
+
sourceCode: routesFileContent,
|
|
26
|
+
filePath: outputSlimRoutesPath,
|
|
27
|
+
prefix: transformName2Prefix(mfConfig.name),
|
|
28
|
+
baseName
|
|
29
|
+
});
|
|
30
|
+
generateRoutes({
|
|
31
|
+
sourceCode: routesFileContent,
|
|
32
|
+
filePath: outputFullRoutesPath,
|
|
33
|
+
prefix: transformName2Prefix(mfConfig.name),
|
|
34
|
+
baseName
|
|
35
|
+
});
|
|
36
|
+
fs.writeFileSync(outputRoutesMetaPath, `export const baseName = '${baseName}';`);
|
|
37
|
+
return {
|
|
38
|
+
outputSlimRoutesPath,
|
|
39
|
+
outputFullRoutesPath,
|
|
40
|
+
outputRoutesMetaPath
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function addExpose(options) {
|
|
44
|
+
const { mfConfig } = options;
|
|
45
|
+
const { outputSlimRoutesPath, outputFullRoutesPath, outputRoutesMetaPath } = generateExtraExposeFiles(options);
|
|
46
|
+
const fullRoutesKey = `./${MF_FULL_ROUTES}`;
|
|
47
|
+
const slimRoutesKey = `./${MF_SLIM_ROUTES}`;
|
|
48
|
+
const routeMetaKey = `./${MF_ROUTES_META}`;
|
|
49
|
+
if (!mfConfig.exposes) {
|
|
50
|
+
mfConfig.exposes = {
|
|
51
|
+
[fullRoutesKey]: outputFullRoutesPath,
|
|
52
|
+
[slimRoutesKey]: outputSlimRoutesPath,
|
|
53
|
+
[routeMetaKey]: outputRoutesMetaPath
|
|
54
|
+
};
|
|
55
|
+
} else {
|
|
56
|
+
if (!Array.isArray(mfConfig.exposes)) {
|
|
57
|
+
if (!mfConfig.exposes[fullRoutesKey]) {
|
|
58
|
+
mfConfig.exposes[fullRoutesKey] = outputFullRoutesPath;
|
|
59
|
+
}
|
|
60
|
+
if (!mfConfig.exposes[slimRoutesKey]) {
|
|
61
|
+
mfConfig.exposes[slimRoutesKey] = outputSlimRoutesPath;
|
|
62
|
+
}
|
|
63
|
+
if (!mfConfig.exposes[routeMetaKey]) {
|
|
64
|
+
mfConfig.exposes[routeMetaKey] = outputRoutesMetaPath;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function addShared(options) {
|
|
70
|
+
const { metaName, mfConfig } = options;
|
|
71
|
+
const alias = `@${metaName}/runtime/router`;
|
|
72
|
+
if (!mfConfig.shared) {
|
|
73
|
+
mfConfig.shared = {
|
|
74
|
+
[alias]: {
|
|
75
|
+
singleton: true
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
} else {
|
|
79
|
+
if (!Array.isArray(mfConfig.shared)) {
|
|
80
|
+
mfConfig.shared[alias] = {
|
|
81
|
+
singleton: true
|
|
82
|
+
};
|
|
83
|
+
} else {
|
|
84
|
+
mfConfig.shared.push(alias);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function patchMfConfig(options) {
|
|
89
|
+
addShared(options);
|
|
90
|
+
addExpose(options);
|
|
91
|
+
}
|
|
92
|
+
async function _fetchSSRByRouteIds(partialSSRRemotes, mfConfig, transformRuntimeFn) {
|
|
93
|
+
const partialMfConfig = {
|
|
94
|
+
name: mfConfig.name,
|
|
95
|
+
remotes: {
|
|
96
|
+
...mfConfig.remotes
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
const runtimeInitOptions = transformRuntimeFn(partialMfConfig);
|
|
100
|
+
replaceRemoteUrl(runtimeInitOptions, "ipv4");
|
|
101
|
+
if (!runtimeInitOptions.remotes.length || !partialSSRRemotes.length) {
|
|
102
|
+
return void 0;
|
|
103
|
+
}
|
|
104
|
+
const remoteProviderRouteIds = /* @__PURE__ */ new Set();
|
|
105
|
+
const collectIds = (route) => {
|
|
106
|
+
remoteProviderRouteIds.add(route.id);
|
|
107
|
+
if (route.children) {
|
|
108
|
+
route.children.forEach((r) => {
|
|
109
|
+
collectIds(r);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
await Promise.all(runtimeInitOptions.remotes.map(async (remote) => {
|
|
114
|
+
const entry = "entry" in remote ? remote.entry : "";
|
|
115
|
+
if (!entry) {
|
|
116
|
+
return void 0;
|
|
117
|
+
}
|
|
118
|
+
const ipv4 = getIPV4();
|
|
119
|
+
const url = new URL(entry);
|
|
120
|
+
const serializableRoutesUrl = url.href.replace(url.pathname, `/${SERIALIZABLE_ROUTES}`).replace("localhost", ipv4);
|
|
121
|
+
const serializableRoutesRet = await fetch(serializableRoutesUrl);
|
|
122
|
+
const serializableRoutes = await serializableRoutesRet.json();
|
|
123
|
+
serializableRoutes.forEach((serializableRoute) => {
|
|
124
|
+
collectIds(serializableRoute);
|
|
125
|
+
});
|
|
126
|
+
}));
|
|
127
|
+
return [
|
|
128
|
+
...remoteProviderRouteIds
|
|
129
|
+
];
|
|
130
|
+
}
|
|
131
|
+
function _transformRuntimeOptions(buildOptions) {
|
|
132
|
+
const remotes = buildOptions.remotes || {};
|
|
133
|
+
const runtimeRemotes = Object.entries(remotes).map((remote) => {
|
|
134
|
+
const [_alias, nameAndEntry] = remote;
|
|
135
|
+
const [name, entry] = nameAndEntry.split("@");
|
|
136
|
+
return {
|
|
137
|
+
name,
|
|
138
|
+
entry
|
|
139
|
+
};
|
|
140
|
+
});
|
|
141
|
+
return {
|
|
142
|
+
name: buildOptions.name,
|
|
143
|
+
remotes: runtimeRemotes
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
const moduleFederationDataLoaderPlugin = (enable, internalOptions, userConfig) => ({
|
|
147
|
+
name: "@modern-js/plugin-module-federation-data-loader",
|
|
148
|
+
pre: [
|
|
149
|
+
"@modern-js/plugin-module-federation-config"
|
|
150
|
+
],
|
|
151
|
+
post: [
|
|
152
|
+
"@modern-js/plugin-router",
|
|
153
|
+
"@modern-js/plugin-module-federation"
|
|
154
|
+
],
|
|
155
|
+
setup: async ({ useConfigContext, useAppContext }) => {
|
|
156
|
+
var _modernjsConfig_server, _internalOptions_csrConfig;
|
|
157
|
+
if (!enable) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const {
|
|
161
|
+
baseName,
|
|
162
|
+
partialSSRRemotes = [],
|
|
163
|
+
fetchSSRByRouteIds,
|
|
164
|
+
// patchMFConfig,
|
|
165
|
+
metaName = META_NAME,
|
|
166
|
+
serverPlugin = "@module-federation/modern-js/data-loader-server",
|
|
167
|
+
transformRuntimeOptions
|
|
168
|
+
} = userConfig;
|
|
169
|
+
if (!baseName) {
|
|
170
|
+
throw new Error(`${PLUGIN_IDENTIFIER} 'baseName' is required if you enable 'dataLoader'!`);
|
|
171
|
+
}
|
|
172
|
+
const modernjsConfig = useConfigContext();
|
|
173
|
+
const appContext = useAppContext();
|
|
174
|
+
const enableSSR = Boolean(modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
|
|
175
|
+
const name = (_internalOptions_csrConfig = internalOptions.csrConfig) === null || _internalOptions_csrConfig === void 0 ? void 0 : _internalOptions_csrConfig.name;
|
|
176
|
+
const routesFilePath = path.resolve(appContext.internalDirectory.replace(META_NAME, metaName || META_NAME), `./main/${MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME}`);
|
|
177
|
+
const transformRuntimeFn = transformRuntimeOptions || _transformRuntimeOptions;
|
|
178
|
+
return {
|
|
179
|
+
_internalRuntimePlugins: ({ entrypoint, plugins }) => {
|
|
180
|
+
plugins.push({
|
|
181
|
+
name: "ssrDataLoader",
|
|
182
|
+
path: "@module-federation/modern-js/data-loader",
|
|
183
|
+
config: {}
|
|
184
|
+
});
|
|
185
|
+
return {
|
|
186
|
+
entrypoint,
|
|
187
|
+
plugins
|
|
188
|
+
};
|
|
189
|
+
},
|
|
190
|
+
_internalServerPlugins({ plugins }) {
|
|
191
|
+
plugins.push({
|
|
192
|
+
name: serverPlugin,
|
|
193
|
+
options: transformRuntimeFn(internalOptions.csrConfig)
|
|
194
|
+
});
|
|
195
|
+
return {
|
|
196
|
+
plugins
|
|
197
|
+
};
|
|
198
|
+
},
|
|
199
|
+
config: async () => {
|
|
200
|
+
const fetchFn = fetchSSRByRouteIds || _fetchSSRByRouteIds;
|
|
201
|
+
const ssrByRouteIds = await fetchFn(partialSSRRemotes, internalOptions.csrConfig, transformRuntimeFn);
|
|
202
|
+
return {
|
|
203
|
+
server: {
|
|
204
|
+
ssrByRouteIds
|
|
205
|
+
},
|
|
206
|
+
tools: {
|
|
207
|
+
rspack(_config, { isServer }) {
|
|
208
|
+
patchMfConfig({
|
|
209
|
+
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
210
|
+
baseName,
|
|
211
|
+
metaName,
|
|
212
|
+
isServer,
|
|
213
|
+
routesFilePath
|
|
214
|
+
});
|
|
215
|
+
},
|
|
216
|
+
webpack(_config, { isServer }) {
|
|
217
|
+
patchMfConfig({
|
|
218
|
+
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
219
|
+
baseName,
|
|
220
|
+
metaName,
|
|
221
|
+
isServer,
|
|
222
|
+
routesFilePath
|
|
223
|
+
});
|
|
224
|
+
},
|
|
225
|
+
bundlerChain(chain, { isServer }) {
|
|
226
|
+
if (!isServer && internalOptions.csrConfig && Object.keys(internalOptions.csrConfig.exposes).length) {
|
|
227
|
+
chain.plugin("SerializableRoutesPlugin").use(SerializableRoutesPlugin, [
|
|
228
|
+
{
|
|
229
|
+
routesFilePath,
|
|
230
|
+
prefix: transformName2Prefix(internalOptions.csrConfig.name)
|
|
231
|
+
}
|
|
232
|
+
]);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
source: {
|
|
237
|
+
define: {
|
|
238
|
+
MODERN_ROUTER_ID_PREFIX: JSON.stringify(transformName2Prefix(name))
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
var dataLoaderPlugin_default = moduleFederationDataLoaderPlugin;
|
|
247
|
+
export {
|
|
248
|
+
dataLoaderPlugin_default as default,
|
|
249
|
+
generateRoutes,
|
|
250
|
+
generateSlimRoutes,
|
|
251
|
+
moduleFederationDataLoaderPlugin
|
|
252
|
+
};
|
|
@@ -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
|
};
|