@module-federation/modern-js 0.0.0-next-20240909092952 → 0.0.0-next-20240909101028
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/cli/{dataLoader/ast/index.js → constant.js} +6 -6
- package/dist/cjs/cli/index.js +1 -5
- package/dist/cjs/cli/mfRuntimePlugins/inject-node-fetch.js +10 -7
- package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +38 -40
- package/dist/cjs/cli/ssrPlugin.js +1 -1
- package/dist/cjs/cli/utils.js +4 -8
- package/dist/cjs/cli/utils.spec.js +1 -1
- package/dist/cjs/constant.js +2 -32
- package/dist/cjs/runtime/index.js +1 -4
- package/dist/cjs/ssr-runtime/plugin.js +7 -7
- package/dist/esm/cli/constant.js +4 -0
- package/dist/esm/cli/index.js +1 -4
- package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js +10 -7
- package/dist/esm/cli/ssrPlugin.js +1 -1
- package/dist/esm/cli/utils.js +3 -6
- package/dist/esm/cli/utils.spec.js +1 -1
- package/dist/esm/constant.js +1 -21
- package/dist/esm/runtime/index.js +1 -3
- package/dist/esm/ssr-runtime/plugin.js +10 -7
- package/dist/esm-node/cli/constant.js +4 -0
- package/dist/esm-node/cli/index.js +1 -5
- package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.js +10 -7
- package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +38 -40
- package/dist/esm-node/cli/ssrPlugin.js +1 -1
- package/dist/esm-node/cli/utils.js +3 -6
- package/dist/esm-node/cli/utils.spec.js +1 -1
- package/dist/esm-node/constant.js +1 -21
- package/dist/esm-node/runtime/index.js +1 -3
- package/dist/esm-node/ssr-runtime/plugin.js +7 -7
- package/dist/types/cli/constant.d.ts +1 -0
- package/dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts +2 -2
- package/dist/types/cli/utils.d.ts +0 -2
- package/dist/types/constant.d.ts +0 -10
- package/dist/types/runtime/index.d.ts +0 -1
- package/dist/types/types/index.d.ts +0 -9
- package/package.json +11 -40
- package/dist/cjs/cli/dataLoader/ast/constant.js +0 -49
- package/dist/cjs/cli/dataLoader/ast/generateRoutes.js +0 -141
- package/dist/cjs/cli/dataLoader/generateRouteFile.js +0 -143
- package/dist/cjs/cli/dataLoader/patchMFConfig.js +0 -132
- package/dist/cjs/cli/dataLoader/plugin.js +0 -194
- package/dist/cjs/cli/mfRuntimePlugins/node.js +0 -44
- package/dist/cjs/cli/server/dataLoaderPlugin.js +0 -82
- package/dist/cjs/cli/server/fetchRemoteFullRoutesMap.js +0 -69
- package/dist/cjs/interfaces/route.js +0 -16
- package/dist/cjs/runtime/dataLoader/plugin.js +0 -73
- package/dist/cjs/runtime/dataLoader/utils.js +0 -89
- package/dist/cjs/runtime/utils.js +0 -38
- package/dist/cjs/runtime/withMFRouteId.js +0 -31
- package/dist/esm/cli/dataLoader/ast/constant.js +0 -18
- package/dist/esm/cli/dataLoader/ast/generateRoutes.js +0 -108
- package/dist/esm/cli/dataLoader/ast/index.js +0 -4
- package/dist/esm/cli/dataLoader/generateRouteFile.js +0 -293
- package/dist/esm/cli/dataLoader/patchMFConfig.js +0 -98
- package/dist/esm/cli/dataLoader/plugin.js +0 -209
- package/dist/esm/cli/mfRuntimePlugins/node.js +0 -16
- package/dist/esm/cli/server/dataLoaderPlugin.js +0 -105
- package/dist/esm/cli/server/fetchRemoteFullRoutesMap.js +0 -88
- package/dist/esm/interfaces/route.js +0 -0
- package/dist/esm/runtime/dataLoader/plugin.js +0 -92
- package/dist/esm/runtime/dataLoader/utils.js +0 -63
- package/dist/esm/runtime/utils.js +0 -13
- package/dist/esm/runtime/withMFRouteId.js +0 -7
- package/dist/esm-node/cli/dataLoader/ast/constant.js +0 -18
- package/dist/esm-node/cli/dataLoader/ast/generateRoutes.js +0 -107
- package/dist/esm-node/cli/dataLoader/ast/index.js +0 -4
- package/dist/esm-node/cli/dataLoader/generateRouteFile.js +0 -108
- package/dist/esm-node/cli/dataLoader/patchMFConfig.js +0 -98
- package/dist/esm-node/cli/dataLoader/plugin.js +0 -159
- package/dist/esm-node/cli/mfRuntimePlugins/node.js +0 -14
- package/dist/esm-node/cli/server/dataLoaderPlugin.js +0 -62
- package/dist/esm-node/cli/server/fetchRemoteFullRoutesMap.js +0 -44
- package/dist/esm-node/interfaces/route.js +0 -0
- package/dist/esm-node/runtime/dataLoader/plugin.js +0 -49
- package/dist/esm-node/runtime/dataLoader/utils.js +0 -63
- package/dist/esm-node/runtime/utils.js +0 -13
- package/dist/esm-node/runtime/withMFRouteId.js +0 -7
- package/dist/types/cli/dataLoader/ast/constant.d.ts +0 -8
- package/dist/types/cli/dataLoader/ast/generateRoutes.d.ts +0 -5
- package/dist/types/cli/dataLoader/ast/index.d.ts +0 -1
- package/dist/types/cli/dataLoader/generateRouteFile.d.ts +0 -7
- package/dist/types/cli/dataLoader/patchMFConfig.d.ts +0 -10
- package/dist/types/cli/dataLoader/plugin.d.ts +0 -6
- package/dist/types/cli/mfRuntimePlugins/node.d.ts +0 -3
- package/dist/types/cli/server/dataLoaderPlugin.d.ts +0 -10
- package/dist/types/cli/server/fetchRemoteFullRoutesMap.d.ts +0 -10
- package/dist/types/interfaces/route.d.ts +0 -12
- package/dist/types/runtime/dataLoader/plugin.d.ts +0 -2
- package/dist/types/runtime/dataLoader/utils.d.ts +0 -19
- package/dist/types/runtime/utils.d.ts +0 -2
- package/dist/types/runtime/withMFRouteId.d.ts +0 -1
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var utils_exports = {};
|
|
20
|
-
__export(utils_exports, {
|
|
21
|
-
getRemoteLayoutId: () => getRemoteLayoutId,
|
|
22
|
-
getRemoteRoutesInfos: () => getRemoteRoutesInfos,
|
|
23
|
-
injectRemoteRoutes: () => injectRemoteRoutes
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(utils_exports);
|
|
26
|
-
var import_constant = require("../../constant");
|
|
27
|
-
var import_utils = require("../utils");
|
|
28
|
-
function getRemoteRoutesInfos(route, remoteRoutesMap) {
|
|
29
|
-
if (!route.id) {
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
const remoteName = (0, import_utils.transformPrefix2Name)(route.id);
|
|
33
|
-
const remoteRouteObj = remoteRoutesMap[remoteName];
|
|
34
|
-
if (!remoteRouteObj) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
const remoteRoutes = remoteRouteObj.routes[0];
|
|
38
|
-
return {
|
|
39
|
-
routes: remoteRoutes,
|
|
40
|
-
name: remoteName,
|
|
41
|
-
pathName: route.path
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
function getRemoteLayoutId(remoteName) {
|
|
45
|
-
const prefix = (0, import_utils.transformName2Prefix)(remoteName);
|
|
46
|
-
return `${prefix}${import_constant.DEFAULT_LAYOUT}`;
|
|
47
|
-
}
|
|
48
|
-
function injectRemoteRoutes(initialRoutes, remoteRoutesMap) {
|
|
49
|
-
const traverse = (route) => {
|
|
50
|
-
var _route_children;
|
|
51
|
-
const remoteRoutesInfos = getRemoteRoutesInfos(route, remoteRoutesMap);
|
|
52
|
-
if (remoteRoutesInfos) {
|
|
53
|
-
const { routes, pathName } = remoteRoutesInfos;
|
|
54
|
-
route.id = routes.id;
|
|
55
|
-
route.path = pathName;
|
|
56
|
-
if (routes.loader) {
|
|
57
|
-
route.loader = routes.loader;
|
|
58
|
-
}
|
|
59
|
-
if (routes.children) {
|
|
60
|
-
route.children = routes.children;
|
|
61
|
-
}
|
|
62
|
-
route.element = routes.element;
|
|
63
|
-
route.Component = routes.Component;
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
(_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.forEach((r) => {
|
|
67
|
-
traverse(r);
|
|
68
|
-
});
|
|
69
|
-
};
|
|
70
|
-
Object.keys(initialRoutes).forEach((entryName) => {
|
|
71
|
-
const routes = initialRoutes[entryName];
|
|
72
|
-
if (Array.isArray(routes)) {
|
|
73
|
-
routes.forEach((route) => {
|
|
74
|
-
traverse(route);
|
|
75
|
-
});
|
|
76
|
-
} else {
|
|
77
|
-
var _routes_routes;
|
|
78
|
-
routes === null || routes === void 0 ? void 0 : (_routes_routes = routes.routes) === null || _routes_routes === void 0 ? void 0 : _routes_routes.forEach((r) => {
|
|
79
|
-
traverse(r);
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
85
|
-
0 && (module.exports = {
|
|
86
|
-
getRemoteLayoutId,
|
|
87
|
-
getRemoteRoutesInfos,
|
|
88
|
-
injectRemoteRoutes
|
|
89
|
-
});
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var utils_exports = {};
|
|
20
|
-
__export(utils_exports, {
|
|
21
|
-
transformName2Prefix: () => transformName2Prefix,
|
|
22
|
-
transformPrefix2Name: () => transformPrefix2Name
|
|
23
|
-
});
|
|
24
|
-
module.exports = __toCommonJS(utils_exports);
|
|
25
|
-
var import_sdk = require("@module-federation/sdk");
|
|
26
|
-
const SPLIT_SYMBOL = "@";
|
|
27
|
-
function transformName2Prefix(name) {
|
|
28
|
-
return `${(0, import_sdk.encodeName)(name, "", false)}${SPLIT_SYMBOL}`;
|
|
29
|
-
}
|
|
30
|
-
function transformPrefix2Name(prefix) {
|
|
31
|
-
const realPrefix = prefix.split(SPLIT_SYMBOL)[0];
|
|
32
|
-
return (0, import_sdk.decodeName)(realPrefix, "", false);
|
|
33
|
-
}
|
|
34
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
35
|
-
0 && (module.exports = {
|
|
36
|
-
transformName2Prefix,
|
|
37
|
-
transformPrefix2Name
|
|
38
|
-
});
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var withMFRouteId_exports = {};
|
|
20
|
-
__export(withMFRouteId_exports, {
|
|
21
|
-
withMFRouteId: () => withMFRouteId
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(withMFRouteId_exports);
|
|
24
|
-
function withMFRouteId(id) {
|
|
25
|
-
const prefix = typeof MODERN_ROUTER_ID_PREFIX === "string" ? MODERN_ROUTER_ID_PREFIX : "";
|
|
26
|
-
return prefix + id;
|
|
27
|
-
}
|
|
28
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
29
|
-
0 && (module.exports = {
|
|
30
|
-
withMFRouteId
|
|
31
|
-
});
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
var IS_ROOT = "isRoot";
|
|
2
|
-
var ID = "id";
|
|
3
|
-
var COMPONENT = "component";
|
|
4
|
-
var LAZY_COMPONENT = "lazyImport";
|
|
5
|
-
var SHOULD_REVALIDATE = "shouldRevalidate";
|
|
6
|
-
var PRIVATE_COMPONENT = "_component";
|
|
7
|
-
var ELEMENT = "element";
|
|
8
|
-
var LOADER = "loader";
|
|
9
|
-
export {
|
|
10
|
-
COMPONENT,
|
|
11
|
-
ELEMENT,
|
|
12
|
-
ID,
|
|
13
|
-
IS_ROOT,
|
|
14
|
-
LAZY_COMPONENT,
|
|
15
|
-
LOADER,
|
|
16
|
-
PRIVATE_COMPONENT,
|
|
17
|
-
SHOULD_REVALIDATE
|
|
18
|
-
};
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
2
|
-
import fs from "fs";
|
|
3
|
-
import traverse from "@babel/traverse";
|
|
4
|
-
import * as babelParser from "@babel/parser";
|
|
5
|
-
import generate from "@babel/generator";
|
|
6
|
-
import * as t from "@babel/types";
|
|
7
|
-
function findTargetKeyNode(nodeProperties, key) {
|
|
8
|
-
return nodeProperties.find(function(p) {
|
|
9
|
-
return t.isObjectProperty(p) && t.isStringLiteral(p.key) && p.key.value === key;
|
|
10
|
-
});
|
|
11
|
-
}
|
|
12
|
-
function generateRoutes(param) {
|
|
13
|
-
var sourceCode = param.sourceCode, filePath = param.filePath;
|
|
14
|
-
var ast = babelParser.parse(sourceCode, {
|
|
15
|
-
sourceType: "module"
|
|
16
|
-
});
|
|
17
|
-
var lazyComponentDeclarations = [];
|
|
18
|
-
var componentDeclarations = [];
|
|
19
|
-
var componentId = 0;
|
|
20
|
-
traverse(ast, {
|
|
21
|
-
// ImportDeclaration(path) {
|
|
22
|
-
// const source = path.node.source.value;
|
|
23
|
-
// const routeIdMatch = source.match(/routeId=([^&]+)/);
|
|
24
|
-
// if (routeIdMatch) {
|
|
25
|
-
// const originalRouteId = routeIdMatch[1];
|
|
26
|
-
// const newRouteId = `${prefix}${originalRouteId}`;
|
|
27
|
-
// const newSource = source.replace(
|
|
28
|
-
// /routeId=[^&]+/,
|
|
29
|
-
// `routeId=${newRouteId}`,
|
|
30
|
-
// );
|
|
31
|
-
// path.node.source = t.stringLiteral(newSource);
|
|
32
|
-
// }
|
|
33
|
-
// },
|
|
34
|
-
ObjectExpression: function ObjectExpression(path) {
|
|
35
|
-
var componentName = "";
|
|
36
|
-
var lazyComponentName = "";
|
|
37
|
-
if (!Array.isArray(path.node.properties)) {
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
var idNode = findTargetKeyNode(path.node.properties, "id");
|
|
41
|
-
if (idNode && t.isObjectProperty(idNode) && t.isStringLiteral(idNode.value)) {
|
|
42
|
-
}
|
|
43
|
-
var isRootNode = findTargetKeyNode(path.node.properties, "isRoot");
|
|
44
|
-
if (isRootNode && t.isObjectProperty(isRootNode) && t.isBooleanLiteral(isRootNode.value)) {
|
|
45
|
-
isRootNode.value.value = false;
|
|
46
|
-
}
|
|
47
|
-
if (!isRootNode) {
|
|
48
|
-
var lazyComponentNode = findTargetKeyNode(path.node.properties, "lazyImport");
|
|
49
|
-
if (lazyComponentNode && t.isObjectProperty(lazyComponentNode) && t.isArrowFunctionExpression(lazyComponentNode.value)) {
|
|
50
|
-
lazyComponentName = "LazyComponent_".concat(componentId);
|
|
51
|
-
var lazyDeclaration = t.variableDeclaration("const", [
|
|
52
|
-
t.variableDeclarator(t.identifier(lazyComponentName), lazyComponentNode.value)
|
|
53
|
-
]);
|
|
54
|
-
lazyComponentNode.value = t.identifier(lazyComponentName);
|
|
55
|
-
var componentNode = findTargetKeyNode(path.node.properties, "component");
|
|
56
|
-
if (componentNode && t.isObjectProperty(componentNode) && t.isCallExpression(componentNode.value) && t.isIdentifier(componentNode.value.callee)) {
|
|
57
|
-
componentNode.value = t.callExpression(t.identifier("lazy"), [
|
|
58
|
-
t.identifier(lazyComponentName)
|
|
59
|
-
]);
|
|
60
|
-
}
|
|
61
|
-
lazyComponentDeclarations.push(lazyDeclaration);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
var componentNode1 = findTargetKeyNode(path.node.properties, "component");
|
|
65
|
-
if (componentNode1 && t.isObjectProperty(componentNode1) && t.isCallExpression(componentNode1.value) && t.isIdentifier(componentNode1.value.callee)) {
|
|
66
|
-
componentName = "Component_".concat(componentId);
|
|
67
|
-
var componentDeclaration = t.variableDeclaration("const", [
|
|
68
|
-
t.variableDeclarator(t.identifier(componentName), lazyComponentName ? t.callExpression(t.identifier("lazy"), [
|
|
69
|
-
t.identifier(lazyComponentName)
|
|
70
|
-
]) : componentNode1.value)
|
|
71
|
-
]);
|
|
72
|
-
componentDeclarations.push(componentDeclaration);
|
|
73
|
-
componentNode1.value = t.identifier(componentName);
|
|
74
|
-
}
|
|
75
|
-
if (lazyComponentName || componentName) {
|
|
76
|
-
componentId++;
|
|
77
|
-
if (componentName) {
|
|
78
|
-
var upperFirstName = componentName.slice(0, 1).toUpperCase() + componentName.slice(1);
|
|
79
|
-
var jsxElement = t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier(upperFirstName), [], true), null, [], true);
|
|
80
|
-
path.node.properties.push(t.objectProperty(t.identifier("element"), jsxElement));
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
traverse(ast, {
|
|
86
|
-
Program: function Program(path) {
|
|
87
|
-
var lastImportIndex = path.get("body").reduce(function(lastIndex, p, index) {
|
|
88
|
-
if (t.isImportDeclaration(p.node)) {
|
|
89
|
-
lastIndex = index;
|
|
90
|
-
}
|
|
91
|
-
return lastIndex;
|
|
92
|
-
}, -1);
|
|
93
|
-
if (lastImportIndex >= 0) {
|
|
94
|
-
var lastImport = path.get("body.".concat(lastImportIndex));
|
|
95
|
-
_to_consumable_array(componentDeclarations).concat(_to_consumable_array(lazyComponentDeclarations)).forEach(function(declaration) {
|
|
96
|
-
if ("insertAfter" in lastImport) {
|
|
97
|
-
lastImport.insertAfter(declaration);
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
var code = generate(ast).code;
|
|
104
|
-
fs.writeFileSync(filePath, code);
|
|
105
|
-
}
|
|
106
|
-
export {
|
|
107
|
-
generateRoutes
|
|
108
|
-
};
|
|
@@ -1,293 +0,0 @@
|
|
|
1
|
-
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
-
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
3
|
-
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
4
|
-
import { fs, normalizeToPosixPath } from "@modern-js/utils";
|
|
5
|
-
import path from "path";
|
|
6
|
-
import { MF_JSON } from "../../constant";
|
|
7
|
-
import { getRemoteLayoutId } from "../../runtime/dataLoader/utils";
|
|
8
|
-
var ROUTES = "routes";
|
|
9
|
-
function calcPathName(filePath, rootDir, entry) {
|
|
10
|
-
var relativePath = path.relative(rootDir, filePath);
|
|
11
|
-
var conditionalReg = /^\[(.*)\]$/;
|
|
12
|
-
var splitPaths = relativePath.split("/").reduce(function(sum, cur) {
|
|
13
|
-
if (cur.includes(".")) {
|
|
14
|
-
var _sum;
|
|
15
|
-
var split = cur.split(".").map(function(i) {
|
|
16
|
-
var match2 = i.match(conditionalReg);
|
|
17
|
-
if (match2) {
|
|
18
|
-
return ":".concat(match2[1]);
|
|
19
|
-
}
|
|
20
|
-
return i.startsWith("__") ? "" : i;
|
|
21
|
-
});
|
|
22
|
-
(_sum = sum).push.apply(_sum, _to_consumable_array(split));
|
|
23
|
-
} else {
|
|
24
|
-
var match = cur.match(conditionalReg);
|
|
25
|
-
if (match) {
|
|
26
|
-
sum.push(":".concat(match[1]));
|
|
27
|
-
} else {
|
|
28
|
-
if (!cur.startsWith("__")) {
|
|
29
|
-
sum.push(cur);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return sum;
|
|
34
|
-
}, []);
|
|
35
|
-
return path.join(entry, splitPaths.filter(function(p) {
|
|
36
|
-
return !!p;
|
|
37
|
-
}).join("/"));
|
|
38
|
-
}
|
|
39
|
-
function generateRouteFile(options) {
|
|
40
|
-
return _generateRouteFile.apply(this, arguments);
|
|
41
|
-
}
|
|
42
|
-
function _generateRouteFile() {
|
|
43
|
-
_generateRouteFile = _async_to_generator(function(options) {
|
|
44
|
-
var appDirectory, entries, remotePathMap, srcPath, traverse, traverseRoutes, normalizedEntries;
|
|
45
|
-
return _ts_generator(this, function(_state) {
|
|
46
|
-
switch (_state.label) {
|
|
47
|
-
case 0:
|
|
48
|
-
appDirectory = options.appDirectory;
|
|
49
|
-
entries = [];
|
|
50
|
-
remotePathMap = {};
|
|
51
|
-
srcPath = "".concat(appDirectory, "/src");
|
|
52
|
-
traverse = function() {
|
|
53
|
-
var _ref = _async_to_generator(function(filepath, rootDir, entry) {
|
|
54
|
-
var isDirectory, items, mfJson, mfJsonContent, remoteName, remoteLayoutId, writeFile, pathname;
|
|
55
|
-
return _ts_generator(this, function(_state2) {
|
|
56
|
-
switch (_state2.label) {
|
|
57
|
-
case 0:
|
|
58
|
-
return [
|
|
59
|
-
4,
|
|
60
|
-
fs.pathExists(filepath)
|
|
61
|
-
];
|
|
62
|
-
case 1:
|
|
63
|
-
if (!_state2.sent()) {
|
|
64
|
-
return [
|
|
65
|
-
2,
|
|
66
|
-
null
|
|
67
|
-
];
|
|
68
|
-
}
|
|
69
|
-
return [
|
|
70
|
-
4,
|
|
71
|
-
fs.stat(filepath)
|
|
72
|
-
];
|
|
73
|
-
case 2:
|
|
74
|
-
isDirectory = _state2.sent().isDirectory();
|
|
75
|
-
if (!isDirectory) {
|
|
76
|
-
return [
|
|
77
|
-
2,
|
|
78
|
-
null
|
|
79
|
-
];
|
|
80
|
-
}
|
|
81
|
-
return [
|
|
82
|
-
4,
|
|
83
|
-
fs.readdir(filepath)
|
|
84
|
-
];
|
|
85
|
-
case 3:
|
|
86
|
-
items = _state2.sent();
|
|
87
|
-
mfJson = items.find(function(i) {
|
|
88
|
-
return i === MF_JSON;
|
|
89
|
-
});
|
|
90
|
-
if (!mfJson)
|
|
91
|
-
return [
|
|
92
|
-
3,
|
|
93
|
-
5
|
|
94
|
-
];
|
|
95
|
-
return [
|
|
96
|
-
4,
|
|
97
|
-
fs.readJSON(normalizeToPosixPath(path.join(filepath, mfJson)))
|
|
98
|
-
];
|
|
99
|
-
case 4:
|
|
100
|
-
mfJsonContent = _state2.sent();
|
|
101
|
-
remoteName = mfJsonContent.remoteName;
|
|
102
|
-
remoteLayoutId = getRemoteLayoutId(remoteName);
|
|
103
|
-
writeFile = function() {
|
|
104
|
-
var _ref2 = _async_to_generator(function(id, type, pathname2) {
|
|
105
|
-
var generatedFilePath, content;
|
|
106
|
-
return _ts_generator(this, function(_state3) {
|
|
107
|
-
switch (_state3.label) {
|
|
108
|
-
case 0:
|
|
109
|
-
generatedFilePath = normalizeToPosixPath(path.join(filepath, "".concat(type, ".tsx")));
|
|
110
|
-
content = [
|
|
111
|
-
"// Do not edit this file. It is a auto generated.",
|
|
112
|
-
"// It will render ".concat(remoteName, "'s ").concat(type, "."),
|
|
113
|
-
"// The ".concat(type, " pathname is: ").concat(pathname2),
|
|
114
|
-
"// The ".concat(type, " route id is: ").concat(id)
|
|
115
|
-
];
|
|
116
|
-
return [
|
|
117
|
-
4,
|
|
118
|
-
fs.writeFile(generatedFilePath, content.join("\n"))
|
|
119
|
-
];
|
|
120
|
-
case 1:
|
|
121
|
-
_state3.sent();
|
|
122
|
-
return [
|
|
123
|
-
2
|
|
124
|
-
];
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
});
|
|
128
|
-
return function writeFile2(id, type, pathname2) {
|
|
129
|
-
return _ref2.apply(this, arguments);
|
|
130
|
-
};
|
|
131
|
-
}();
|
|
132
|
-
if (remoteLayoutId) {
|
|
133
|
-
pathname = calcPathName(filepath, rootDir, entry);
|
|
134
|
-
writeFile(remoteLayoutId, "layout", pathname);
|
|
135
|
-
remotePathMap[pathname] = remoteName;
|
|
136
|
-
}
|
|
137
|
-
return [
|
|
138
|
-
2
|
|
139
|
-
];
|
|
140
|
-
case 5:
|
|
141
|
-
return [
|
|
142
|
-
4,
|
|
143
|
-
Promise.all(items.map(function() {
|
|
144
|
-
var _ref2 = _async_to_generator(function(item) {
|
|
145
|
-
return _ts_generator(this, function(_state3) {
|
|
146
|
-
switch (_state3.label) {
|
|
147
|
-
case 0:
|
|
148
|
-
return [
|
|
149
|
-
4,
|
|
150
|
-
traverse(normalizeToPosixPath(path.join(filepath, item)), rootDir, entry)
|
|
151
|
-
];
|
|
152
|
-
case 1:
|
|
153
|
-
_state3.sent();
|
|
154
|
-
return [
|
|
155
|
-
2
|
|
156
|
-
];
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
});
|
|
160
|
-
return function(item) {
|
|
161
|
-
return _ref2.apply(this, arguments);
|
|
162
|
-
};
|
|
163
|
-
}()))
|
|
164
|
-
];
|
|
165
|
-
case 6:
|
|
166
|
-
_state2.sent();
|
|
167
|
-
return [
|
|
168
|
-
2
|
|
169
|
-
];
|
|
170
|
-
}
|
|
171
|
-
});
|
|
172
|
-
});
|
|
173
|
-
return function traverse2(filepath, rootDir, entry) {
|
|
174
|
-
return _ref.apply(this, arguments);
|
|
175
|
-
};
|
|
176
|
-
}();
|
|
177
|
-
traverseRoutes = function() {
|
|
178
|
-
var _ref = _async_to_generator(function(dir) {
|
|
179
|
-
var level, isDirectory, items, entry, rootDir;
|
|
180
|
-
var _arguments = arguments;
|
|
181
|
-
return _ts_generator(this, function(_state2) {
|
|
182
|
-
switch (_state2.label) {
|
|
183
|
-
case 0:
|
|
184
|
-
level = _arguments.length > 1 && _arguments[1] !== void 0 ? _arguments[1] : 1;
|
|
185
|
-
if (level > 3) {
|
|
186
|
-
return [
|
|
187
|
-
2
|
|
188
|
-
];
|
|
189
|
-
}
|
|
190
|
-
return [
|
|
191
|
-
4,
|
|
192
|
-
fs.stat(dir)
|
|
193
|
-
];
|
|
194
|
-
case 1:
|
|
195
|
-
isDirectory = _state2.sent().isDirectory();
|
|
196
|
-
if (!isDirectory) {
|
|
197
|
-
return [
|
|
198
|
-
2
|
|
199
|
-
];
|
|
200
|
-
}
|
|
201
|
-
return [
|
|
202
|
-
4,
|
|
203
|
-
fs.readdir(dir)
|
|
204
|
-
];
|
|
205
|
-
case 2:
|
|
206
|
-
items = _state2.sent();
|
|
207
|
-
if (!items.find(function(i) {
|
|
208
|
-
return i === ROUTES;
|
|
209
|
-
}))
|
|
210
|
-
return [
|
|
211
|
-
3,
|
|
212
|
-
4
|
|
213
|
-
];
|
|
214
|
-
entry = path.relative(srcPath, dir);
|
|
215
|
-
entries.push(entry);
|
|
216
|
-
rootDir = "".concat(dir, "/").concat(ROUTES);
|
|
217
|
-
return [
|
|
218
|
-
4,
|
|
219
|
-
traverse(rootDir, rootDir, entry)
|
|
220
|
-
];
|
|
221
|
-
case 3:
|
|
222
|
-
_state2.sent();
|
|
223
|
-
return [
|
|
224
|
-
3,
|
|
225
|
-
6
|
|
226
|
-
];
|
|
227
|
-
case 4:
|
|
228
|
-
return [
|
|
229
|
-
4,
|
|
230
|
-
Promise.all(items.map(function() {
|
|
231
|
-
var _ref2 = _async_to_generator(function(item) {
|
|
232
|
-
return _ts_generator(this, function(_state3) {
|
|
233
|
-
switch (_state3.label) {
|
|
234
|
-
case 0:
|
|
235
|
-
return [
|
|
236
|
-
4,
|
|
237
|
-
traverseRoutes(path.join(dir, item), level++)
|
|
238
|
-
];
|
|
239
|
-
case 1:
|
|
240
|
-
_state3.sent();
|
|
241
|
-
return [
|
|
242
|
-
2
|
|
243
|
-
];
|
|
244
|
-
}
|
|
245
|
-
});
|
|
246
|
-
});
|
|
247
|
-
return function(item) {
|
|
248
|
-
return _ref2.apply(this, arguments);
|
|
249
|
-
};
|
|
250
|
-
}()))
|
|
251
|
-
];
|
|
252
|
-
case 5:
|
|
253
|
-
_state2.sent();
|
|
254
|
-
_state2.label = 6;
|
|
255
|
-
case 6:
|
|
256
|
-
return [
|
|
257
|
-
2
|
|
258
|
-
];
|
|
259
|
-
}
|
|
260
|
-
});
|
|
261
|
-
});
|
|
262
|
-
return function traverseRoutes2(dir) {
|
|
263
|
-
return _ref.apply(this, arguments);
|
|
264
|
-
};
|
|
265
|
-
}();
|
|
266
|
-
return [
|
|
267
|
-
4,
|
|
268
|
-
traverseRoutes(srcPath)
|
|
269
|
-
];
|
|
270
|
-
case 1:
|
|
271
|
-
_state.sent();
|
|
272
|
-
normalizedEntries = entries.map(function(entry) {
|
|
273
|
-
if (entry === "") {
|
|
274
|
-
return "main";
|
|
275
|
-
}
|
|
276
|
-
return entry;
|
|
277
|
-
});
|
|
278
|
-
return [
|
|
279
|
-
2,
|
|
280
|
-
{
|
|
281
|
-
entries: normalizedEntries,
|
|
282
|
-
remotePathMap
|
|
283
|
-
}
|
|
284
|
-
];
|
|
285
|
-
}
|
|
286
|
-
});
|
|
287
|
-
});
|
|
288
|
-
return _generateRouteFile.apply(this, arguments);
|
|
289
|
-
}
|
|
290
|
-
export {
|
|
291
|
-
calcPathName,
|
|
292
|
-
generateRouteFile
|
|
293
|
-
};
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import { fs } from "@modern-js/utils";
|
|
4
|
-
import { MODERN_JS_ROUTE_SERVER_LOADER } from "../../constant";
|
|
5
|
-
import { MF_ROUTES, MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME } from "../../constant";
|
|
6
|
-
import { generateRoutes } from "./ast";
|
|
7
|
-
function generateExtraExposeFiles(options) {
|
|
8
|
-
var internalDirectory = options.internalDirectory, mfConfig = options.mfConfig, isServer = options.isServer, entries = options.entries;
|
|
9
|
-
var entryMap = {};
|
|
10
|
-
var outputDir = path.resolve(process.cwd(), "node_modules/.federation/data-loader");
|
|
11
|
-
fs.ensureDirSync(outputDir);
|
|
12
|
-
var addSuffix = function(fileName) {
|
|
13
|
-
var ext = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : ".jsx";
|
|
14
|
-
if (!isServer) {
|
|
15
|
-
return "".concat(fileName).concat(ext);
|
|
16
|
-
}
|
|
17
|
-
return "".concat(fileName, ".server").concat(ext);
|
|
18
|
-
};
|
|
19
|
-
var generateEntryRoutes = function(entry) {
|
|
20
|
-
var outputEntryDir = path.resolve(outputDir, entry);
|
|
21
|
-
fs.ensureDirSync(outputEntryDir);
|
|
22
|
-
var sourceDir = path.resolve(internalDirectory, entry);
|
|
23
|
-
var routesFilePath = path.resolve(sourceDir, addSuffix(MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME, ".js"));
|
|
24
|
-
var routesFileContent = fs.readFileSync(routesFilePath, "utf-8");
|
|
25
|
-
var outputFullRoutesPath = path.resolve(outputEntryDir, addSuffix(MF_ROUTES));
|
|
26
|
-
generateRoutes({
|
|
27
|
-
sourceCode: routesFileContent,
|
|
28
|
-
filePath: outputFullRoutesPath
|
|
29
|
-
});
|
|
30
|
-
var routeServerLoaderPath = path.resolve(sourceDir, "".concat(MODERN_JS_ROUTE_SERVER_LOADER, ".js"));
|
|
31
|
-
var outputRouteServerLoaderPath = path.resolve(outputEntryDir, "".concat(MODERN_JS_ROUTE_SERVER_LOADER, ".js"));
|
|
32
|
-
if (isServer) {
|
|
33
|
-
var routeServerLoaderContent = fs.readFileSync(routeServerLoaderPath, "utf-8");
|
|
34
|
-
generateRoutes({
|
|
35
|
-
sourceCode: routeServerLoaderContent,
|
|
36
|
-
filePath: outputRouteServerLoaderPath
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
entryMap[entry] = {
|
|
40
|
-
routesPath: outputFullRoutesPath,
|
|
41
|
-
routeServerLoaderPath: outputRouteServerLoaderPath
|
|
42
|
-
};
|
|
43
|
-
};
|
|
44
|
-
entries.forEach(function(entry) {
|
|
45
|
-
generateEntryRoutes(entry);
|
|
46
|
-
});
|
|
47
|
-
return {
|
|
48
|
-
entryMap
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
function addExpose(options) {
|
|
52
|
-
var mfConfig = options.mfConfig, isServer = options.isServer;
|
|
53
|
-
if (mfConfig.remotes && mfConfig.exposes) {
|
|
54
|
-
throw new Error("dataLoader only support for pure provider/consumer !");
|
|
55
|
-
}
|
|
56
|
-
var entryMap = generateExtraExposeFiles(options).entryMap;
|
|
57
|
-
var addExposeByEntry = function(entry, routesPath, routeServerLoaderPath) {
|
|
58
|
-
if (!mfConfig.exposes) {
|
|
59
|
-
mfConfig.exposes = {};
|
|
60
|
-
}
|
|
61
|
-
var routesKey = "./".concat(entry, "/").concat(MF_ROUTES);
|
|
62
|
-
if (!mfConfig.exposes[routesKey]) {
|
|
63
|
-
mfConfig.exposes[routesKey] = routesPath;
|
|
64
|
-
}
|
|
65
|
-
var routeServerLoaderKey = "./".concat(entry, "/").concat(MODERN_JS_ROUTE_SERVER_LOADER);
|
|
66
|
-
if (isServer && !mfConfig.exposes[routeServerLoaderKey]) {
|
|
67
|
-
mfConfig.exposes[routeServerLoaderKey] = routeServerLoaderPath;
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
Object.keys(entryMap).forEach(function(entry) {
|
|
71
|
-
var _entryMap_entry = entryMap[entry], routesPath = _entryMap_entry.routesPath, routeServerLoaderPath = _entryMap_entry.routeServerLoaderPath;
|
|
72
|
-
addExposeByEntry(entry, routesPath, routeServerLoaderPath);
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
function addShared(options) {
|
|
76
|
-
var metaName = options.metaName, mfConfig = options.mfConfig;
|
|
77
|
-
var alias = "@".concat(metaName, "/runtime/router");
|
|
78
|
-
if (!mfConfig.shared) {
|
|
79
|
-
mfConfig.shared = _define_property({}, alias, {
|
|
80
|
-
singleton: true
|
|
81
|
-
});
|
|
82
|
-
} else {
|
|
83
|
-
if (!Array.isArray(mfConfig.shared)) {
|
|
84
|
-
mfConfig.shared[alias] = {
|
|
85
|
-
singleton: true
|
|
86
|
-
};
|
|
87
|
-
} else {
|
|
88
|
-
mfConfig.shared.push(alias);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
function patchMFConfig(options) {
|
|
93
|
-
addShared(options);
|
|
94
|
-
addExpose(options);
|
|
95
|
-
}
|
|
96
|
-
export {
|
|
97
|
-
patchMFConfig
|
|
98
|
-
};
|