@module-federation/modern-js 0.0.0-next-20241017102355 → 0.0.0-next-20241018034800
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/{routes/ast/index.js → constant.js} +6 -6
- package/dist/cjs/cli/index.js +2 -7
- 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 -35
- package/dist/cjs/runtime/index.js +1 -4
- package/dist/cjs/ssr-runtime/plugin.js +8 -7
- package/dist/esm/cli/constant.js +4 -0
- package/dist/esm/cli/index.js +2 -7
- 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 +5 -9
- package/dist/esm/cli/utils.spec.js +6 -5
- package/dist/esm/constant.js +1 -23
- package/dist/esm/runtime/index.js +1 -3
- package/dist/esm/ssr-runtime/plugin.js +11 -8
- package/dist/esm-node/cli/constant.js +4 -0
- package/dist/esm-node/cli/index.js +2 -7
- 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 -23
- package/dist/esm-node/runtime/index.js +1 -3
- package/dist/esm-node/ssr-runtime/plugin.js +8 -7
- 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 -11
- 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 +1 -3
- package/package.json +12 -49
- package/dist/cjs/cli/mfRuntimePlugins/node.js +0 -41
- package/dist/cjs/cli/routes/ast/constant.js +0 -49
- package/dist/cjs/cli/routes/ast/generateRoutes.js +0 -143
- package/dist/cjs/cli/routes/clearMFCache.js +0 -57
- package/dist/cjs/cli/routes/exportRoutePlugin.js +0 -77
- package/dist/cjs/cli/routes/importRoutesPlugin.js +0 -120
- package/dist/cjs/cli/routes/plugin.js +0 -157
- package/dist/cjs/cli/routes/traverseRouteFiles.js +0 -148
- package/dist/cjs/cli/routes/utils.js +0 -168
- package/dist/cjs/cli/server/fetchRemoteFullRoutesMap.js +0 -72
- package/dist/cjs/cli/server/fetchRoutePlugin.js +0 -81
- package/dist/cjs/interfaces/route.js +0 -16
- package/dist/cjs/runtime/routes/plugin-inject-assets.js +0 -92
- package/dist/cjs/runtime/routes/plugin.js +0 -75
- package/dist/cjs/runtime/routes/utils.js +0 -115
- package/dist/cjs/types/modern.js +0 -16
- package/dist/cjs/types/routes.js +0 -16
- package/dist/esm/cli/mfRuntimePlugins/node.js +0 -13
- package/dist/esm/cli/routes/ast/constant.js +0 -18
- package/dist/esm/cli/routes/ast/generateRoutes.js +0 -110
- package/dist/esm/cli/routes/ast/index.js +0 -4
- package/dist/esm/cli/routes/clearMFCache.js +0 -34
- package/dist/esm/cli/routes/exportRoutePlugin.js +0 -79
- package/dist/esm/cli/routes/importRoutesPlugin.js +0 -122
- package/dist/esm/cli/routes/plugin.js +0 -266
- package/dist/esm/cli/routes/traverseRouteFiles.js +0 -313
- package/dist/esm/cli/routes/utils.js +0 -130
- package/dist/esm/cli/server/fetchRemoteFullRoutesMap.js +0 -93
- package/dist/esm/cli/server/fetchRoutePlugin.js +0 -104
- package/dist/esm/interfaces/route.js +0 -0
- package/dist/esm/runtime/routes/plugin-inject-assets.js +0 -71
- package/dist/esm/runtime/routes/plugin.js +0 -97
- package/dist/esm/runtime/routes/utils.js +0 -86
- package/dist/esm/runtime/withMFRouteId.js +0 -7
- package/dist/esm/types/modern.js +0 -0
- package/dist/esm/types/routes.js +0 -0
- package/dist/esm-node/cli/mfRuntimePlugins/node.js +0 -11
- package/dist/esm-node/cli/routes/ast/constant.js +0 -18
- package/dist/esm-node/cli/routes/ast/generateRoutes.js +0 -109
- package/dist/esm-node/cli/routes/ast/index.js +0 -4
- package/dist/esm-node/cli/routes/clearMFCache.js +0 -33
- package/dist/esm-node/cli/routes/exportRoutePlugin.js +0 -53
- package/dist/esm-node/cli/routes/importRoutesPlugin.js +0 -96
- package/dist/esm-node/cli/routes/plugin.js +0 -123
- package/dist/esm-node/cli/routes/traverseRouteFiles.js +0 -113
- package/dist/esm-node/cli/routes/utils.js +0 -133
- package/dist/esm-node/cli/server/fetchRemoteFullRoutesMap.js +0 -47
- package/dist/esm-node/cli/server/fetchRoutePlugin.js +0 -61
- package/dist/esm-node/interfaces/route.js +0 -0
- package/dist/esm-node/runtime/routes/plugin-inject-assets.js +0 -68
- package/dist/esm-node/runtime/routes/plugin.js +0 -51
- package/dist/esm-node/runtime/routes/utils.js +0 -86
- package/dist/esm-node/runtime/withMFRouteId.js +0 -7
- package/dist/esm-node/types/modern.js +0 -0
- package/dist/esm-node/types/routes.js +0 -0
- package/dist/types/cli/mfRuntimePlugins/node.d.ts +0 -3
- package/dist/types/cli/routes/ast/constant.d.ts +0 -8
- package/dist/types/cli/routes/ast/generateRoutes.d.ts +0 -5
- package/dist/types/cli/routes/ast/index.d.ts +0 -1
- package/dist/types/cli/routes/clearMFCache.d.ts +0 -1
- package/dist/types/cli/routes/exportRoutePlugin.d.ts +0 -4
- package/dist/types/cli/routes/importRoutesPlugin.d.ts +0 -4
- package/dist/types/cli/routes/plugin.d.ts +0 -4
- package/dist/types/cli/routes/traverseRouteFiles.d.ts +0 -11
- package/dist/types/cli/routes/utils.d.ts +0 -11
- package/dist/types/cli/server/fetchRemoteFullRoutesMap.d.ts +0 -11
- package/dist/types/cli/server/fetchRoutePlugin.d.ts +0 -10
- package/dist/types/interfaces/route.d.ts +0 -12
- package/dist/types/runtime/routes/plugin-inject-assets.d.ts +0 -4
- package/dist/types/runtime/routes/plugin.d.ts +0 -5
- package/dist/types/runtime/routes/utils.d.ts +0 -22
- package/dist/types/runtime/withMFRouteId.d.ts +0 -1
- package/dist/types/types/modern.d.ts +0 -12
- package/dist/types/types/routes.d.ts +0 -19
- /package/dist/types/cli/mfRuntimePlugins/{node-fetch.d.ts → inject-node-fetch.d.ts} +0 -0
|
@@ -1,109 +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
|
-
const lazyComponentNode = findTargetKeyNode(path.node.properties, "lazyImport");
|
|
44
|
-
if (lazyComponentNode && t.isObjectProperty(lazyComponentNode) && t.isArrowFunctionExpression(lazyComponentNode.value)) {
|
|
45
|
-
lazyComponentName = `LazyComponent_${componentId}`;
|
|
46
|
-
const lazyDeclaration = t.variableDeclaration("const", [
|
|
47
|
-
t.variableDeclarator(t.identifier(lazyComponentName), lazyComponentNode.value)
|
|
48
|
-
]);
|
|
49
|
-
lazyComponentNode.value = t.identifier(lazyComponentName);
|
|
50
|
-
const componentNode2 = findTargetKeyNode(path.node.properties, "component");
|
|
51
|
-
if (componentNode2 && t.isObjectProperty(componentNode2) && t.isCallExpression(componentNode2.value) && t.isIdentifier(componentNode2.value.callee)) {
|
|
52
|
-
componentNode2.value = t.callExpression(t.identifier("lazy"), [
|
|
53
|
-
t.identifier(lazyComponentName)
|
|
54
|
-
]);
|
|
55
|
-
}
|
|
56
|
-
lazyComponentDeclarations.push(lazyDeclaration);
|
|
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
|
-
} else {
|
|
76
|
-
path.node.properties.push(t.objectProperty(t.identifier("Component"), t.callExpression(t.identifier("lazy"), [
|
|
77
|
-
t.identifier(lazyComponentName)
|
|
78
|
-
])));
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
traverse(ast, {
|
|
84
|
-
Program(path) {
|
|
85
|
-
const lastImportIndex = path.get("body").reduce((lastIndex, p, index) => {
|
|
86
|
-
if (t.isImportDeclaration(p.node)) {
|
|
87
|
-
lastIndex = index;
|
|
88
|
-
}
|
|
89
|
-
return lastIndex;
|
|
90
|
-
}, -1);
|
|
91
|
-
if (lastImportIndex >= 0) {
|
|
92
|
-
const lastImport = path.get(`body.${lastImportIndex}`);
|
|
93
|
-
[
|
|
94
|
-
...componentDeclarations,
|
|
95
|
-
...lazyComponentDeclarations
|
|
96
|
-
].forEach((declaration) => {
|
|
97
|
-
if ("insertAfter" in lastImport) {
|
|
98
|
-
lastImport.insertAfter(declaration);
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
const { code } = generate(ast);
|
|
105
|
-
fs.writeFileSync(filePath, code);
|
|
106
|
-
}
|
|
107
|
-
export {
|
|
108
|
-
generateRoutes
|
|
109
|
-
};
|
|
@@ -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,53 +0,0 @@
|
|
|
1
|
-
import { META_NAME } from "../../constant";
|
|
2
|
-
import { addExpose } from "./utils";
|
|
3
|
-
const moduleFederationExportRoutePlugin = ({ userConfig, internalOptions, entries }) => ({
|
|
4
|
-
name: "@modern-js/plugin-module-federation-export-routes",
|
|
5
|
-
pre: [
|
|
6
|
-
"@modern-js/plugin-module-federation-route"
|
|
7
|
-
],
|
|
8
|
-
post: [
|
|
9
|
-
"@modern-js/plugin-router",
|
|
10
|
-
"@modern-js/plugin-module-federation"
|
|
11
|
-
],
|
|
12
|
-
setup: async ({ useAppContext }) => {
|
|
13
|
-
if (!userConfig.exportRoutes) {
|
|
14
|
-
return;
|
|
15
|
-
}
|
|
16
|
-
const appContext = useAppContext();
|
|
17
|
-
const { metaName = META_NAME } = internalOptions;
|
|
18
|
-
const internalDirectory = appContext.internalDirectory.replace(META_NAME, metaName || META_NAME);
|
|
19
|
-
return {
|
|
20
|
-
config: async () => {
|
|
21
|
-
return {
|
|
22
|
-
tools: {
|
|
23
|
-
// bundlerChain can not keep target order
|
|
24
|
-
rspack(_config, { isServer }) {
|
|
25
|
-
addExpose({
|
|
26
|
-
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
27
|
-
metaName,
|
|
28
|
-
isServer,
|
|
29
|
-
internalDirectory,
|
|
30
|
-
entries
|
|
31
|
-
});
|
|
32
|
-
},
|
|
33
|
-
// bundlerChain can not keep target order
|
|
34
|
-
webpack(_config, { isServer }) {
|
|
35
|
-
addExpose({
|
|
36
|
-
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
37
|
-
metaName,
|
|
38
|
-
isServer,
|
|
39
|
-
internalDirectory,
|
|
40
|
-
entries
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
var exportRoutePlugin_default = moduleFederationExportRoutePlugin;
|
|
50
|
-
export {
|
|
51
|
-
exportRoutePlugin_default as default,
|
|
52
|
-
moduleFederationExportRoutePlugin
|
|
53
|
-
};
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { META_NAME } from "../../constant";
|
|
2
|
-
import { clearMFCache } from "./clearMFCache";
|
|
3
|
-
function _transformRuntimeOptions(buildOptions) {
|
|
4
|
-
const remotes = buildOptions.remotes || {};
|
|
5
|
-
const runtimeRemotes = Object.entries(remotes).map((remote) => {
|
|
6
|
-
const [alias, nameAndEntry] = remote;
|
|
7
|
-
const [name, entry] = nameAndEntry.split("@");
|
|
8
|
-
return {
|
|
9
|
-
name,
|
|
10
|
-
entry,
|
|
11
|
-
alias
|
|
12
|
-
};
|
|
13
|
-
});
|
|
14
|
-
return {
|
|
15
|
-
name: buildOptions.name,
|
|
16
|
-
remotes: runtimeRemotes
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
const moduleFederationImportRoutePlugin = ({ userConfig, internalOptions, remotePathMap, serverPlugin = "@module-federation/modern-js/fetch-router-server-plugin", ssrByRouteIdsMap, transformRuntimeOptions }) => ({
|
|
20
|
-
name: "@modern-js/plugin-module-federation-import-routes",
|
|
21
|
-
pre: [
|
|
22
|
-
"@modern-js/plugin-module-federation-route"
|
|
23
|
-
],
|
|
24
|
-
post: [
|
|
25
|
-
"@modern-js/plugin-router",
|
|
26
|
-
"@modern-js/plugin-module-federation"
|
|
27
|
-
],
|
|
28
|
-
setup: async () => {
|
|
29
|
-
if (!userConfig.importRoutes) {
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
const { metaName = META_NAME } = internalOptions;
|
|
33
|
-
const transformRuntimeFn = transformRuntimeOptions || _transformRuntimeOptions;
|
|
34
|
-
const dataLoaderRemotes = [
|
|
35
|
-
...new Set(Object.values(remotePathMap).map((i) => i.name))
|
|
36
|
-
];
|
|
37
|
-
const serverPluginOptions = {
|
|
38
|
-
runtimeOptions: transformRuntimeFn(internalOptions.csrConfig),
|
|
39
|
-
dataLoaderRemotes,
|
|
40
|
-
ssrByRouteIdsMap
|
|
41
|
-
};
|
|
42
|
-
const hasDataLoaderRemotes = () => Boolean(serverPluginOptions.dataLoaderRemotes.length);
|
|
43
|
-
return {
|
|
44
|
-
_internalRuntimePlugins: ({ entrypoint, plugins }) => {
|
|
45
|
-
if (!hasDataLoaderRemotes()) {
|
|
46
|
-
return {
|
|
47
|
-
entrypoint,
|
|
48
|
-
plugins
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
plugins.push({
|
|
52
|
-
name: "ssrDataLoader",
|
|
53
|
-
path: "@module-federation/modern-js/routes",
|
|
54
|
-
config: {
|
|
55
|
-
metaName,
|
|
56
|
-
dataLoaderRemotes
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
plugins.push({
|
|
60
|
-
name: "ssrDataLoaderInjectAssets",
|
|
61
|
-
path: "@module-federation/modern-js/routes-inject-assets",
|
|
62
|
-
config: {
|
|
63
|
-
metaName,
|
|
64
|
-
dataLoaderRemotes
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
return {
|
|
68
|
-
entrypoint,
|
|
69
|
-
plugins
|
|
70
|
-
};
|
|
71
|
-
},
|
|
72
|
-
_internalServerPlugins({ plugins }) {
|
|
73
|
-
if (!hasDataLoaderRemotes()) {
|
|
74
|
-
return {
|
|
75
|
-
plugins
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
plugins.push({
|
|
79
|
-
name: serverPlugin,
|
|
80
|
-
options: serverPluginOptions
|
|
81
|
-
});
|
|
82
|
-
return {
|
|
83
|
-
plugins
|
|
84
|
-
};
|
|
85
|
-
},
|
|
86
|
-
async afterDev() {
|
|
87
|
-
clearMFCache();
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
var importRoutesPlugin_default = moduleFederationImportRoutePlugin;
|
|
93
|
-
export {
|
|
94
|
-
importRoutesPlugin_default as default,
|
|
95
|
-
moduleFederationImportRoutePlugin
|
|
96
|
-
};
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import { fs } from "@modern-js/utils";
|
|
3
|
-
import { transformName2Prefix } from "../../runtime/routes/utils";
|
|
4
|
-
import { META_NAME } from "../../constant";
|
|
5
|
-
import { traverseRouteFiles } from "./traverseRouteFiles";
|
|
6
|
-
import { moduleFederationExportRoutePlugin } from "./exportRoutePlugin";
|
|
7
|
-
import { moduleFederationImportRoutePlugin } from "./importRoutesPlugin";
|
|
8
|
-
import { addShared } from "./utils";
|
|
9
|
-
import { DEFAULT_ENTRY, ROUTE_ID } from "../../constant";
|
|
10
|
-
const moduleFederationRoutesPlugin = ({ userConfig, internalOptions, serverPlugin, transformRuntimeOptions }) => {
|
|
11
|
-
const entries = /* @__PURE__ */ new Set();
|
|
12
|
-
const remotePathMap = {};
|
|
13
|
-
const ssrByRouteIdsMap = {};
|
|
14
|
-
const internalRoutesPluginOptions = {
|
|
15
|
-
userConfig,
|
|
16
|
-
internalOptions,
|
|
17
|
-
entries,
|
|
18
|
-
remotePathMap,
|
|
19
|
-
ssrByRouteIdsMap,
|
|
20
|
-
serverPlugin,
|
|
21
|
-
transformRuntimeOptions
|
|
22
|
-
};
|
|
23
|
-
return {
|
|
24
|
-
name: "@modern-js/plugin-module-federation-route",
|
|
25
|
-
pre: [
|
|
26
|
-
"@modern-js/plugin-module-federation-config"
|
|
27
|
-
],
|
|
28
|
-
setup: async ({ useAppContext }) => {
|
|
29
|
-
if (!userConfig.exportRoutes && !userConfig.importRoutes) {
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
const appContext = useAppContext();
|
|
33
|
-
const name = internalOptions.csrConfig.name;
|
|
34
|
-
const { metaName = META_NAME } = internalOptions;
|
|
35
|
-
const internalDirectory = appContext.internalDirectory.replace(META_NAME, metaName || META_NAME);
|
|
36
|
-
const csrConfig = internalOptions.csrConfig;
|
|
37
|
-
if (typeof csrConfig.remotes === "object" && Object.keys(csrConfig.remotes).length) {
|
|
38
|
-
const { entries: scanEntries } = await traverseRouteFiles({
|
|
39
|
-
appDirectory: appContext.appDirectory,
|
|
40
|
-
generateRouteFile: userConfig.importRoutes,
|
|
41
|
-
remotePathMap
|
|
42
|
-
});
|
|
43
|
-
scanEntries.forEach((e) => entries.add(e));
|
|
44
|
-
} else {
|
|
45
|
-
entries.add(DEFAULT_ENTRY);
|
|
46
|
-
}
|
|
47
|
-
return {
|
|
48
|
-
config: async () => {
|
|
49
|
-
return {
|
|
50
|
-
tools: {
|
|
51
|
-
// bundlerChain can not keep target order
|
|
52
|
-
rspack(_config, { isServer }) {
|
|
53
|
-
addShared({
|
|
54
|
-
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
55
|
-
metaName,
|
|
56
|
-
isServer,
|
|
57
|
-
internalDirectory,
|
|
58
|
-
entries
|
|
59
|
-
});
|
|
60
|
-
},
|
|
61
|
-
// bundlerChain can not keep target order
|
|
62
|
-
webpack(_config, { isServer }) {
|
|
63
|
-
addShared({
|
|
64
|
-
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
65
|
-
metaName,
|
|
66
|
-
isServer,
|
|
67
|
-
internalDirectory,
|
|
68
|
-
entries
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
source: {
|
|
73
|
-
define: {
|
|
74
|
-
MODERN_ROUTER_ID_PREFIX: JSON.stringify(transformName2Prefix(name))
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
},
|
|
79
|
-
async modifyFileSystemRoutes(options) {
|
|
80
|
-
const { routes, entrypoint } = options;
|
|
81
|
-
const { entryName } = entrypoint;
|
|
82
|
-
const prefix = transformName2Prefix(internalOptions.csrConfig.name);
|
|
83
|
-
const modifyRouteIds = async (route, pathName) => {
|
|
84
|
-
const currentPathName = route.path ? path.join("/", pathName, route.path) : pathName;
|
|
85
|
-
if (route.id) {
|
|
86
|
-
const originalId = route.id;
|
|
87
|
-
route.originalId = originalId;
|
|
88
|
-
const remoteInfo = remotePathMap[currentPathName];
|
|
89
|
-
if (remoteInfo) {
|
|
90
|
-
const { path: path2, name: name2 } = remoteInfo;
|
|
91
|
-
const remoteIdPrefix = transformName2Prefix(name2);
|
|
92
|
-
const isLayout = Boolean(route.children);
|
|
93
|
-
const newId = `${remoteIdPrefix}${isLayout ? "layout" : "page"}`;
|
|
94
|
-
ssrByRouteIdsMap[originalId] = newId;
|
|
95
|
-
const content = fs.readFileSync(path2, "utf-8");
|
|
96
|
-
await fs.writeFile(path2, content.replace(ROUTE_ID, originalId));
|
|
97
|
-
route.id = newId;
|
|
98
|
-
} else {
|
|
99
|
-
route.id = `${prefix}${route.id}`;
|
|
100
|
-
}
|
|
101
|
-
route.children && await Promise.all(route.children.map(async (r) => {
|
|
102
|
-
return await modifyRouteIds(r, currentPathName);
|
|
103
|
-
}));
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
await Promise.all(routes.map(async (route) => {
|
|
107
|
-
await modifyRouteIds(route, entryName === DEFAULT_ENTRY ? "" : entryName);
|
|
108
|
-
}));
|
|
109
|
-
return options;
|
|
110
|
-
}
|
|
111
|
-
};
|
|
112
|
-
},
|
|
113
|
-
usePlugins: [
|
|
114
|
-
moduleFederationExportRoutePlugin(internalRoutesPluginOptions),
|
|
115
|
-
moduleFederationImportRoutePlugin(internalRoutesPluginOptions)
|
|
116
|
-
]
|
|
117
|
-
};
|
|
118
|
-
};
|
|
119
|
-
var plugin_default = moduleFederationRoutesPlugin;
|
|
120
|
-
export {
|
|
121
|
-
plugin_default as default,
|
|
122
|
-
moduleFederationRoutesPlugin
|
|
123
|
-
};
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { fs, normalizeToPosixPath } from "@modern-js/utils";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import os from "os";
|
|
4
|
-
import { DEFAULT_ENTRY, MF_JSON, ROUTE_ID } from "../../constant";
|
|
5
|
-
import { getRemoteLayoutId } from "../../runtime/routes/utils";
|
|
6
|
-
const ROUTES = "routes";
|
|
7
|
-
function calcPathName(filePath, rootDir, entry) {
|
|
8
|
-
const relativePath = path.relative(rootDir, filePath);
|
|
9
|
-
const conditionalReg = /^\[(.*)\]$/;
|
|
10
|
-
const splitPaths = relativePath.split("/").reduce((sum, cur) => {
|
|
11
|
-
if (cur.includes(".")) {
|
|
12
|
-
const split = cur.split(".").map((i) => {
|
|
13
|
-
const match = i.match(conditionalReg);
|
|
14
|
-
if (match) {
|
|
15
|
-
return `:${match[1]}`;
|
|
16
|
-
}
|
|
17
|
-
return i.startsWith("__") ? "" : i;
|
|
18
|
-
});
|
|
19
|
-
sum.push(...split);
|
|
20
|
-
} else {
|
|
21
|
-
const match = cur.match(conditionalReg);
|
|
22
|
-
if (match) {
|
|
23
|
-
sum.push(`:${match[1]}`);
|
|
24
|
-
} else {
|
|
25
|
-
if (!cur.startsWith("__")) {
|
|
26
|
-
sum.push(cur);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
return sum;
|
|
31
|
-
}, []);
|
|
32
|
-
return path.join("/", entry, splitPaths.filter((p) => !!p).join("/"));
|
|
33
|
-
}
|
|
34
|
-
async function traverseRouteFiles(options) {
|
|
35
|
-
const { appDirectory, generateRouteFile, remotePathMap } = options;
|
|
36
|
-
const entries = [];
|
|
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: ${ROUTE_ID}`
|
|
59
|
-
];
|
|
60
|
-
await fs.writeFile(generatedFilePath, content.join(os.EOL));
|
|
61
|
-
return generatedFilePath;
|
|
62
|
-
};
|
|
63
|
-
if (remoteLayoutId) {
|
|
64
|
-
const pathname = calcPathName(filepath, rootDir, entry);
|
|
65
|
-
const filePath = await writeFile(remoteLayoutId, "layout", pathname);
|
|
66
|
-
remotePathMap[pathname] = {
|
|
67
|
-
name: remoteName,
|
|
68
|
-
path: filePath
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
await Promise.all(items.map(async (item) => {
|
|
74
|
-
await traverse(normalizeToPosixPath(path.join(filepath, item)), rootDir, entry);
|
|
75
|
-
}));
|
|
76
|
-
};
|
|
77
|
-
const traverseRoutes = async (dir, level = 1) => {
|
|
78
|
-
if (level > 3) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
const isDirectory = (await fs.stat(dir)).isDirectory();
|
|
82
|
-
if (!isDirectory) {
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
const items = await fs.readdir(dir);
|
|
86
|
-
if (items.find((i) => i === ROUTES)) {
|
|
87
|
-
const entry = path.relative(srcPath, dir);
|
|
88
|
-
entries.push(entry);
|
|
89
|
-
const rootDir = `${dir}/${ROUTES}`;
|
|
90
|
-
if (generateRouteFile) {
|
|
91
|
-
await traverse(rootDir, rootDir, entry);
|
|
92
|
-
}
|
|
93
|
-
} else {
|
|
94
|
-
await Promise.all(items.map(async (item) => {
|
|
95
|
-
await traverseRoutes(path.join(dir, item), level++);
|
|
96
|
-
}));
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
await traverseRoutes(srcPath);
|
|
100
|
-
const normalizedEntries = entries.map((entry) => {
|
|
101
|
-
if (entry === "") {
|
|
102
|
-
return DEFAULT_ENTRY;
|
|
103
|
-
}
|
|
104
|
-
return entry;
|
|
105
|
-
});
|
|
106
|
-
return {
|
|
107
|
-
entries: normalizedEntries
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
export {
|
|
111
|
-
calcPathName,
|
|
112
|
-
traverseRouteFiles
|
|
113
|
-
};
|
|
@@ -1,133 +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
|
-
const { entryMap } = generateExtraExposeFiles(options);
|
|
56
|
-
const addExposeByEntry = (entry, routesPath, routeServerLoaderPath, clientRouteServerLoaderPath) => {
|
|
57
|
-
if (!mfConfig.exposes) {
|
|
58
|
-
mfConfig.exposes = {};
|
|
59
|
-
}
|
|
60
|
-
const routesKey = `./${entry}/${MF_ROUTES}`;
|
|
61
|
-
if (!mfConfig.exposes[routesKey]) {
|
|
62
|
-
mfConfig.exposes[routesKey] = routesPath;
|
|
63
|
-
}
|
|
64
|
-
const routeServerLoaderKey = `./${entry}/${MODERN_JS_ROUTE_SERVER_LOADER}`;
|
|
65
|
-
if (!mfConfig.exposes[routeServerLoaderKey]) {
|
|
66
|
-
if (isServer) {
|
|
67
|
-
mfConfig.exposes[routeServerLoaderKey] = routeServerLoaderPath;
|
|
68
|
-
} else {
|
|
69
|
-
mfConfig.exposes[routeServerLoaderKey] = clientRouteServerLoaderPath;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
Object.keys(entryMap).forEach((entry) => {
|
|
74
|
-
const { routesPath, routeServerLoaderPath, clientRouteServerLoaderPath } = entryMap[entry];
|
|
75
|
-
addExposeByEntry(entry, routesPath, routeServerLoaderPath, clientRouteServerLoaderPath);
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
function addShared(options) {
|
|
79
|
-
const { mfConfig } = options;
|
|
80
|
-
const styledComponents = `styled-components`;
|
|
81
|
-
const reactRouterDom = `react-router-dom`;
|
|
82
|
-
const reactRouterDomServer = `react-router-dom/server`;
|
|
83
|
-
if (!mfConfig.shared) {
|
|
84
|
-
mfConfig.shared = {
|
|
85
|
-
[reactRouterDom]: {
|
|
86
|
-
singleton: true
|
|
87
|
-
},
|
|
88
|
-
[reactRouterDomServer]: {
|
|
89
|
-
singleton: true
|
|
90
|
-
},
|
|
91
|
-
[styledComponents]: {
|
|
92
|
-
singleton: true
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
} else {
|
|
96
|
-
if (!Array.isArray(mfConfig.shared)) {
|
|
97
|
-
mfConfig.shared[reactRouterDom] = {
|
|
98
|
-
singleton: true
|
|
99
|
-
};
|
|
100
|
-
mfConfig.shared[reactRouterDomServer] = {
|
|
101
|
-
singleton: true
|
|
102
|
-
};
|
|
103
|
-
mfConfig.shared[styledComponents] = {
|
|
104
|
-
singleton: true
|
|
105
|
-
};
|
|
106
|
-
} else {
|
|
107
|
-
mfConfig.shared = mfConfig.shared.reduce((sum, cur) => {
|
|
108
|
-
if (typeof cur === "string") {
|
|
109
|
-
sum[cur] = {};
|
|
110
|
-
} else {
|
|
111
|
-
sum = {
|
|
112
|
-
...sum,
|
|
113
|
-
...cur
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
return sum;
|
|
117
|
-
}, {});
|
|
118
|
-
mfConfig.shared[reactRouterDom] = {
|
|
119
|
-
singleton: true
|
|
120
|
-
};
|
|
121
|
-
mfConfig.shared[reactRouterDomServer] = {
|
|
122
|
-
singleton: true
|
|
123
|
-
};
|
|
124
|
-
mfConfig.shared[styledComponents] = {
|
|
125
|
-
singleton: true
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
export {
|
|
131
|
-
addExpose,
|
|
132
|
-
addShared
|
|
133
|
-
};
|