@module-federation/modern-js 0.0.0-next-20240814085621 → 0.0.0-next-20240815093707
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/cli/configPlugin.js +0 -1
- package/dist/cjs/cli/constant.js +0 -6
- package/dist/cjs/cli/index.js +1 -6
- package/dist/cjs/cli/ssrPlugin.js +4 -4
- package/dist/cjs/runtime/index.js +1 -4
- package/dist/cjs/ssr-runtime/plugin.js +7 -5
- package/dist/esm/cli/configPlugin.js +0 -1
- package/dist/esm/cli/constant.js +0 -4
- package/dist/esm/cli/index.js +1 -6
- package/dist/esm/cli/ssrPlugin.js +2 -2
- package/dist/esm/runtime/index.js +1 -3
- package/dist/esm/ssr-runtime/plugin.js +10 -5
- package/dist/esm-node/cli/configPlugin.js +0 -1
- package/dist/esm-node/cli/constant.js +0 -4
- package/dist/esm-node/cli/index.js +1 -6
- package/dist/esm-node/cli/ssrPlugin.js +2 -2
- package/dist/esm-node/runtime/index.js +1 -3
- package/dist/esm-node/ssr-runtime/plugin.js +7 -5
- package/dist/types/cli/constant.d.ts +0 -2
- package/dist/types/runtime/index.d.ts +0 -1
- package/dist/types/types/index.d.ts +0 -17
- package/package.json +11 -37
- package/dist/cjs/cli/ast/constant.js +0 -46
- package/dist/cjs/cli/ast/generateRoutes.js +0 -140
- package/dist/cjs/cli/ast/generateSlimRoutes.js +0 -106
- package/dist/cjs/cli/ast/index.js +0 -31
- package/dist/cjs/cli/dataLoaderPlugin.js +0 -221
- package/dist/cjs/cli/server/dataLoaderPlugin.js +0 -78
- package/dist/cjs/interfaces/route.js +0 -16
- package/dist/cjs/runtime/constant.js +0 -34
- package/dist/cjs/runtime/dataLoader.js +0 -87
- package/dist/cjs/runtime/utils.js +0 -43
- package/dist/cjs/runtime/withMFRouteId.js +0 -31
- package/dist/esm/cli/ast/constant.js +0 -16
- package/dist/esm/cli/ast/generateRoutes.js +0 -107
- package/dist/esm/cli/ast/generateSlimRoutes.js +0 -75
- package/dist/esm/cli/ast/index.js +0 -6
- package/dist/esm/cli/dataLoaderPlugin.js +0 -208
- package/dist/esm/cli/server/dataLoaderPlugin.js +0 -122
- package/dist/esm/interfaces/route.js +0 -0
- package/dist/esm/runtime/constant.js +0 -8
- package/dist/esm/runtime/dataLoader.js +0 -125
- package/dist/esm/runtime/utils.js +0 -16
- package/dist/esm/runtime/withMFRouteId.js +0 -7
- package/dist/esm-node/cli/ast/constant.js +0 -16
- package/dist/esm-node/cli/ast/generateRoutes.js +0 -106
- package/dist/esm-node/cli/ast/generateSlimRoutes.js +0 -72
- package/dist/esm-node/cli/ast/index.js +0 -6
- package/dist/esm-node/cli/dataLoaderPlugin.js +0 -185
- package/dist/esm-node/cli/server/dataLoaderPlugin.js +0 -58
- package/dist/esm-node/interfaces/route.js +0 -0
- package/dist/esm-node/runtime/constant.js +0 -8
- package/dist/esm-node/runtime/dataLoader.js +0 -63
- package/dist/esm-node/runtime/utils.js +0 -19
- package/dist/esm-node/runtime/withMFRouteId.js +0 -7
- package/dist/types/cli/ast/constant.d.ts +0 -7
- package/dist/types/cli/ast/generateRoutes.d.ts +0 -7
- package/dist/types/cli/ast/generateSlimRoutes.d.ts +0 -7
- package/dist/types/cli/ast/index.d.ts +0 -2
- package/dist/types/cli/dataLoaderPlugin.d.ts +0 -6
- package/dist/types/cli/server/dataLoaderPlugin.d.ts +0 -5
- package/dist/types/interfaces/route.d.ts +0 -13
- package/dist/types/runtime/constant.d.ts +0 -3
- package/dist/types/runtime/dataLoader.d.ts +0 -7
- package/dist/types/runtime/utils.d.ts +0 -1
- package/dist/types/runtime/withMFRouteId.d.ts +0 -1
|
@@ -1,16 +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
|
-
export {
|
|
9
|
-
COMPONENT,
|
|
10
|
-
ELEMENT,
|
|
11
|
-
ID,
|
|
12
|
-
IS_ROOT,
|
|
13
|
-
LAZY_COMPONENT,
|
|
14
|
-
PRIVATE_COMPONENT,
|
|
15
|
-
SHOULD_REVALIDATE
|
|
16
|
-
};
|
|
@@ -1,106 +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, 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
|
-
};
|
|
@@ -1,72 +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
|
-
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
|
-
};
|
|
@@ -1,185 +0,0 @@
|
|
|
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", transformRuntimeOptions } = 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
|
-
const transformRuntimeFn = transformRuntimeOptions || _transformRuntimeOptions;
|
|
129
|
-
return {
|
|
130
|
-
_internalRuntimePlugins: ({ entrypoint, plugins }) => {
|
|
131
|
-
plugins.push({
|
|
132
|
-
name: "ssrDataLoader",
|
|
133
|
-
path: "@module-federation/modern-js/data-loader",
|
|
134
|
-
config: {}
|
|
135
|
-
});
|
|
136
|
-
return {
|
|
137
|
-
entrypoint,
|
|
138
|
-
plugins
|
|
139
|
-
};
|
|
140
|
-
},
|
|
141
|
-
_internalServerPlugins({ plugins }) {
|
|
142
|
-
plugins.push({
|
|
143
|
-
name: serverPlugin,
|
|
144
|
-
options: transformRuntimeFn(internalOptions.csrConfig)
|
|
145
|
-
});
|
|
146
|
-
return {
|
|
147
|
-
plugins
|
|
148
|
-
};
|
|
149
|
-
},
|
|
150
|
-
config: async () => {
|
|
151
|
-
console.log("dataloader plugin config");
|
|
152
|
-
const patchMFConfigFn = patchMFConfig || _pathMfConfig;
|
|
153
|
-
return {
|
|
154
|
-
// server: {
|
|
155
|
-
// ssrByRouteIds: ssrByRouteIds,
|
|
156
|
-
// },
|
|
157
|
-
tools: {
|
|
158
|
-
rspack(_config, { isServer }) {
|
|
159
|
-
patchMFConfigFn({
|
|
160
|
-
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
161
|
-
baseName,
|
|
162
|
-
metaName,
|
|
163
|
-
isServer,
|
|
164
|
-
routesFilePath
|
|
165
|
-
});
|
|
166
|
-
console.log("dataloader plugin rspack");
|
|
167
|
-
}
|
|
168
|
-
},
|
|
169
|
-
source: {
|
|
170
|
-
define: {
|
|
171
|
-
MODERN_ROUTER_ID_PREFIX: JSON.stringify(transformName2Prefix(name))
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
var dataLoaderPlugin_default = moduleFederationDataLoaderPlugin;
|
|
180
|
-
export {
|
|
181
|
-
dataLoaderPlugin_default as default,
|
|
182
|
-
generateRoutes,
|
|
183
|
-
generateSlimRoutes,
|
|
184
|
-
moduleFederationDataLoaderPlugin
|
|
185
|
-
};
|
|
@@ -1,58 +0,0 @@
|
|
|
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
|
-
};
|
|
File without changes
|
|
@@ -1,63 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,19 +0,0 @@
|
|
|
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,7 +0,0 @@
|
|
|
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";
|
|
@@ -1,6 +0,0 @@
|
|
|
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 };
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { ServerPlugin } from '@modern-js/server-core';
|
|
2
|
-
import { init } from '@module-federation/enhanced/runtime';
|
|
3
|
-
type MFRuntimeOptions = Parameters<typeof init>[0];
|
|
4
|
-
declare const _default: (mfRuntimeOptions: MFRuntimeOptions) => ServerPlugin;
|
|
5
|
-
export default _default;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export type Route = {
|
|
2
|
-
id: string;
|
|
3
|
-
type: string;
|
|
4
|
-
loader?: boolean;
|
|
5
|
-
index?: boolean;
|
|
6
|
-
isRoot?: boolean;
|
|
7
|
-
children?: Route[];
|
|
8
|
-
};
|
|
9
|
-
export type MFModernRouteJson = {
|
|
10
|
-
baseName: string;
|
|
11
|
-
routes: Record<string, Route[]>;
|
|
12
|
-
prefix: string;
|
|
13
|
-
};
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { Plugin } from '@modern-js/runtime';
|
|
2
|
-
import { type RouteObject } from '@modern-js/runtime/router';
|
|
3
|
-
declare global {
|
|
4
|
-
var mfRemoteRoutes: RouteObject[];
|
|
5
|
-
var mfHasLoadedRemoteRoutes: boolean;
|
|
6
|
-
}
|
|
7
|
-
export declare const ssrDataLoaderPlugin: () => Plugin;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function transformName2Prefix(name: string): string;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function withMFRouteId(id: string): string;
|