@module-federation/modern-js 0.0.0-next-20240812114132 → 0.0.0-next-20240814073142
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/cli/ast/constant.js +46 -0
- package/dist/cjs/cli/ast/generateRoutes.js +140 -0
- package/dist/cjs/cli/ast/generateSlimRoutes.js +106 -0
- package/dist/cjs/cli/ast/index.js +31 -0
- package/dist/cjs/cli/configPlugin.js +1 -0
- package/dist/cjs/cli/constant.js +3 -0
- package/dist/cjs/cli/dataLoaderPlugin.js +205 -0
- package/dist/cjs/cli/index.js +6 -1
- package/dist/cjs/cli/server/dataLoaderPlugin.js +87 -0
- package/dist/cjs/interfaces/route.js +16 -0
- package/dist/cjs/runtime/constant.js +34 -0
- package/dist/cjs/runtime/dataLoader.js +87 -0
- package/dist/cjs/runtime/index.js +4 -1
- package/dist/cjs/runtime/utils.js +43 -0
- package/dist/cjs/runtime/withMFRouteId.js +31 -0
- package/dist/cjs/ssr-runtime/plugin.js +5 -7
- package/dist/esm/cli/ast/constant.js +16 -0
- package/dist/esm/cli/ast/generateRoutes.js +107 -0
- package/dist/esm/cli/ast/generateSlimRoutes.js +75 -0
- package/dist/esm/cli/ast/index.js +6 -0
- package/dist/esm/cli/configPlugin.js +1 -0
- package/dist/esm/cli/constant.js +2 -0
- package/dist/esm/cli/dataLoaderPlugin.js +191 -0
- package/dist/esm/cli/index.js +6 -1
- package/dist/esm/cli/server/dataLoaderPlugin.js +132 -0
- package/dist/esm/interfaces/route.js +0 -0
- package/dist/esm/runtime/constant.js +8 -0
- package/dist/esm/runtime/dataLoader.js +125 -0
- package/dist/esm/runtime/index.js +3 -1
- package/dist/esm/runtime/utils.js +16 -0
- package/dist/esm/runtime/withMFRouteId.js +7 -0
- package/dist/esm/ssr-runtime/plugin.js +5 -10
- package/dist/esm-node/cli/ast/constant.js +16 -0
- package/dist/esm-node/cli/ast/generateRoutes.js +106 -0
- package/dist/esm-node/cli/ast/generateSlimRoutes.js +72 -0
- package/dist/esm-node/cli/ast/index.js +6 -0
- package/dist/esm-node/cli/configPlugin.js +1 -0
- package/dist/esm-node/cli/constant.js +2 -0
- package/dist/esm-node/cli/dataLoaderPlugin.js +169 -0
- package/dist/esm-node/cli/index.js +6 -1
- package/dist/esm-node/cli/server/dataLoaderPlugin.js +67 -0
- package/dist/esm-node/interfaces/route.js +0 -0
- package/dist/esm-node/runtime/constant.js +8 -0
- package/dist/esm-node/runtime/dataLoader.js +63 -0
- package/dist/esm-node/runtime/index.js +3 -1
- package/dist/esm-node/runtime/utils.js +19 -0
- package/dist/esm-node/runtime/withMFRouteId.js +7 -0
- package/dist/esm-node/ssr-runtime/plugin.js +5 -7
- package/dist/types/cli/ast/constant.d.ts +7 -0
- package/dist/types/cli/ast/generateRoutes.d.ts +7 -0
- package/dist/types/cli/ast/generateSlimRoutes.d.ts +7 -0
- package/dist/types/cli/ast/index.d.ts +2 -0
- package/dist/types/cli/constant.d.ts +1 -0
- package/dist/types/cli/dataLoaderPlugin.d.ts +6 -0
- package/dist/types/cli/server/dataLoaderPlugin.d.ts +4 -0
- package/dist/types/interfaces/route.d.ts +13 -0
- package/dist/types/runtime/constant.d.ts +3 -0
- package/dist/types/runtime/dataLoader.d.ts +7 -0
- package/dist/types/runtime/index.d.ts +1 -0
- package/dist/types/runtime/utils.d.ts +1 -0
- package/dist/types/runtime/withMFRouteId.d.ts +1 -0
- package/dist/types/types/index.d.ts +15 -0
- package/package.json +36 -10
|
@@ -0,0 +1,34 @@
|
|
|
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 constant_exports = {};
|
|
20
|
+
__export(constant_exports, {
|
|
21
|
+
MF_FULL_ROUTES: () => MF_FULL_ROUTES,
|
|
22
|
+
MF_ROUTES_META: () => MF_ROUTES_META,
|
|
23
|
+
MF_SLIM_ROUTES: () => MF_SLIM_ROUTES
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(constant_exports);
|
|
26
|
+
const MF_SLIM_ROUTES = "mf-slim-routes";
|
|
27
|
+
const MF_FULL_ROUTES = "mf-full-routes";
|
|
28
|
+
const MF_ROUTES_META = "mf-routes-meta";
|
|
29
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
30
|
+
0 && (module.exports = {
|
|
31
|
+
MF_FULL_ROUTES,
|
|
32
|
+
MF_ROUTES_META,
|
|
33
|
+
MF_SLIM_ROUTES
|
|
34
|
+
});
|
|
@@ -0,0 +1,87 @@
|
|
|
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 dataLoader_exports = {};
|
|
20
|
+
__export(dataLoader_exports, {
|
|
21
|
+
ssrDataLoaderPlugin: () => ssrDataLoaderPlugin
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(dataLoader_exports);
|
|
24
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
25
|
+
var import_runtime = require("@module-federation/enhanced/runtime");
|
|
26
|
+
var import_constant = require("./constant");
|
|
27
|
+
globalThis.mfRemoteRoutes = globalThis.mfRemoteRoutes || [];
|
|
28
|
+
var _globalThis_mfHasLoadedRemoteRoutes;
|
|
29
|
+
globalThis.mfHasLoadedRemoteRoutes = (_globalThis_mfHasLoadedRemoteRoutes = globalThis.mfHasLoadedRemoteRoutes) !== null && _globalThis_mfHasLoadedRemoteRoutes !== void 0 ? _globalThis_mfHasLoadedRemoteRoutes : false;
|
|
30
|
+
async function loadRoutes() {
|
|
31
|
+
var _getInstance;
|
|
32
|
+
if (globalThis.mfHasLoadedRemoteRoutes) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const remotes = (_getInstance = (0, import_runtime.getInstance)()) === null || _getInstance === void 0 ? void 0 : _getInstance.options.remotes;
|
|
36
|
+
if (!remotes) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const remoteModuleIds = remotes.map((remote) => {
|
|
40
|
+
return `${remote.name}/${import_constant.MF_FULL_ROUTES}`;
|
|
41
|
+
});
|
|
42
|
+
const traverse = (cRoutes, prefix) => {
|
|
43
|
+
cRoutes.forEach((i) => {
|
|
44
|
+
i.id = prefix + i.id;
|
|
45
|
+
const Comp = i.component;
|
|
46
|
+
i.element = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Comp, {});
|
|
47
|
+
if (i.children) {
|
|
48
|
+
traverse(i.children, prefix);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
await Promise.all(remoteModuleIds.map(async (remoteModuleId) => {
|
|
53
|
+
const remoteName = remoteModuleId.split(`/${import_constant.MF_FULL_ROUTES}`)[0];
|
|
54
|
+
const { routes, baseName } = await (0, import_runtime.loadRemote)(remoteModuleId);
|
|
55
|
+
routes[0].path = `/${baseName}`;
|
|
56
|
+
routes[0].isRoot = false;
|
|
57
|
+
globalThis.mfRemoteRoutes.push(...routes);
|
|
58
|
+
}));
|
|
59
|
+
globalThis.mfHasLoadedRemoteRoutes = true;
|
|
60
|
+
return globalThis.mfRemoteRoutes;
|
|
61
|
+
}
|
|
62
|
+
const ssrDataLoaderPlugin = () => {
|
|
63
|
+
return {
|
|
64
|
+
name: "@modern-js/plugin-mf-data-loader",
|
|
65
|
+
post: [
|
|
66
|
+
"@modern-js/plugin-router"
|
|
67
|
+
],
|
|
68
|
+
setup: () => {
|
|
69
|
+
return {
|
|
70
|
+
async beforeRender({ context }) {
|
|
71
|
+
console.log("init");
|
|
72
|
+
await loadRoutes();
|
|
73
|
+
},
|
|
74
|
+
modifyRoutes: (routes) => {
|
|
75
|
+
console.log("modifyRoutes");
|
|
76
|
+
routes.push(...globalThis.mfRemoteRoutes);
|
|
77
|
+
console.log("routes: ", routes);
|
|
78
|
+
return routes;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
85
|
+
0 && (module.exports = {
|
|
86
|
+
ssrDataLoaderPlugin
|
|
87
|
+
});
|
|
@@ -19,13 +19,16 @@ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "defau
|
|
|
19
19
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
20
|
var runtime_exports = {};
|
|
21
21
|
__export(runtime_exports, {
|
|
22
|
-
createRemoteSSRComponent: () => import_createRemoteSSRComponent.createRemoteSSRComponent
|
|
22
|
+
createRemoteSSRComponent: () => import_createRemoteSSRComponent.createRemoteSSRComponent,
|
|
23
|
+
withMFRouteId: () => import_withMFRouteId.withMFRouteId
|
|
23
24
|
});
|
|
24
25
|
module.exports = __toCommonJS(runtime_exports);
|
|
25
26
|
__reExport(runtime_exports, require("@module-federation/enhanced/runtime"), module.exports);
|
|
26
27
|
var import_createRemoteSSRComponent = require("./createRemoteSSRComponent");
|
|
28
|
+
var import_withMFRouteId = require("./withMFRouteId");
|
|
27
29
|
// Annotate the CommonJS export names for ESM import in node:
|
|
28
30
|
0 && (module.exports = {
|
|
29
31
|
createRemoteSSRComponent,
|
|
32
|
+
withMFRouteId,
|
|
30
33
|
...require("@module-federation/enhanced/runtime")
|
|
31
34
|
});
|
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(utils_exports);
|
|
24
|
+
function transformName2Prefix(name) {
|
|
25
|
+
const NameTransformSymbol = {
|
|
26
|
+
AT: "@",
|
|
27
|
+
HYPHEN: "-",
|
|
28
|
+
SLASH: "/",
|
|
29
|
+
UNDERLINE: "_"
|
|
30
|
+
};
|
|
31
|
+
const NameTransformMap = {
|
|
32
|
+
[NameTransformSymbol.AT]: "SCOPE",
|
|
33
|
+
[NameTransformSymbol.HYPHEN]: "HYPHEN",
|
|
34
|
+
[NameTransformSymbol.SLASH]: "SLASH",
|
|
35
|
+
[NameTransformSymbol.UNDERLINE]: "UNDERLINE"
|
|
36
|
+
};
|
|
37
|
+
const SPLIT_SYMBOL = "@";
|
|
38
|
+
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}`;
|
|
39
|
+
}
|
|
40
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
41
|
+
0 && (module.exports = {
|
|
42
|
+
transformName2Prefix
|
|
43
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
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
|
+
});
|
|
@@ -34,15 +34,15 @@ module.exports = __toCommonJS(plugin_exports);
|
|
|
34
34
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
35
35
|
var import_hoist_non_react_statics = __toESM(require("hoist-non-react-statics"));
|
|
36
36
|
var import_SSRLiveReload = require("./SSRLiveReload");
|
|
37
|
+
console.log("mfSSRPlugin trigger");
|
|
37
38
|
const mfSSRPlugin = () => ({
|
|
38
39
|
name: "@module-federation/modern-js",
|
|
39
40
|
setup: () => {
|
|
40
41
|
return {
|
|
41
|
-
async
|
|
42
|
+
async beforeRender() {
|
|
43
|
+
console.log(111, "beforeRender");
|
|
42
44
|
if (typeof window !== "undefined") {
|
|
43
|
-
return
|
|
44
|
-
context
|
|
45
|
-
});
|
|
45
|
+
return;
|
|
46
46
|
}
|
|
47
47
|
globalThis.shouldUpdate = false;
|
|
48
48
|
const nodeUtils = await Promise.resolve().then(() => __toESM(require("@module-federation/node/utils")));
|
|
@@ -52,9 +52,7 @@ const mfSSRPlugin = () => ({
|
|
|
52
52
|
await nodeUtils.flushChunks();
|
|
53
53
|
globalThis.shouldUpdate = true;
|
|
54
54
|
}
|
|
55
|
-
return
|
|
56
|
-
context
|
|
57
|
-
});
|
|
55
|
+
return;
|
|
58
56
|
},
|
|
59
57
|
wrapRoot(App) {
|
|
60
58
|
const AppWrapper = (props) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, {
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
export {
|
|
9
|
+
COMPONENT,
|
|
10
|
+
ELEMENT,
|
|
11
|
+
ID,
|
|
12
|
+
IS_ROOT,
|
|
13
|
+
LAZY_COMPONENT,
|
|
14
|
+
PRIVATE_COMPONENT,
|
|
15
|
+
SHOULD_REVALIDATE
|
|
16
|
+
};
|
|
@@ -0,0 +1,107 @@
|
|
|
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, prefix = param.prefix, baseName = param.baseName;
|
|
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: function ImportDeclaration(path) {
|
|
22
|
+
var source = path.node.source.value;
|
|
23
|
+
var routeIdMatch = source.match(/routeId=([^&]+)/);
|
|
24
|
+
if (routeIdMatch) {
|
|
25
|
+
var originalRouteId = routeIdMatch[1];
|
|
26
|
+
var newRouteId = "".concat(prefix).concat(originalRouteId);
|
|
27
|
+
var newSource = source.replace(/routeId=[^&]+/, "routeId=".concat(newRouteId));
|
|
28
|
+
path.node.source = t.stringLiteral(newSource);
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
ObjectExpression: function ObjectExpression(path) {
|
|
32
|
+
var componentName = "";
|
|
33
|
+
var lazyComponentName = "";
|
|
34
|
+
if (!Array.isArray(path.node.properties)) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
var idNode = findTargetKeyNode(path.node.properties, "id");
|
|
38
|
+
if (idNode && t.isObjectProperty(idNode) && t.isStringLiteral(idNode.value)) {
|
|
39
|
+
idNode.value = t.stringLiteral("".concat(prefix).concat(idNode.value.value));
|
|
40
|
+
}
|
|
41
|
+
var isRootNode = findTargetKeyNode(path.node.properties, "isRoot");
|
|
42
|
+
if (isRootNode && t.isObjectProperty(isRootNode) && t.isBooleanLiteral(isRootNode.value)) {
|
|
43
|
+
isRootNode.value.value = false;
|
|
44
|
+
}
|
|
45
|
+
if (!isRootNode) {
|
|
46
|
+
var lazyComponentNode = findTargetKeyNode(path.node.properties, "lazyImport");
|
|
47
|
+
if (lazyComponentNode && t.isObjectProperty(lazyComponentNode) && t.isArrowFunctionExpression(lazyComponentNode.value)) {
|
|
48
|
+
lazyComponentName = "LazyComponent_".concat(componentId);
|
|
49
|
+
var lazyDeclaration = t.variableDeclaration("const", [
|
|
50
|
+
t.variableDeclarator(t.identifier(lazyComponentName), lazyComponentNode.value)
|
|
51
|
+
]);
|
|
52
|
+
lazyComponentNode.value = t.identifier(lazyComponentName);
|
|
53
|
+
var componentNode = findTargetKeyNode(path.node.properties, "component");
|
|
54
|
+
if (componentNode && t.isObjectProperty(componentNode) && t.isCallExpression(componentNode.value) && t.isIdentifier(componentNode.value.callee)) {
|
|
55
|
+
componentNode.value = t.callExpression(t.identifier("lazy"), [
|
|
56
|
+
t.identifier(lazyComponentName)
|
|
57
|
+
]);
|
|
58
|
+
}
|
|
59
|
+
lazyComponentDeclarations.push(lazyDeclaration);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
var componentNode1 = findTargetKeyNode(path.node.properties, "component");
|
|
63
|
+
if (componentNode1 && t.isObjectProperty(componentNode1) && t.isCallExpression(componentNode1.value) && t.isIdentifier(componentNode1.value.callee)) {
|
|
64
|
+
componentName = "Component_".concat(componentId);
|
|
65
|
+
var componentDeclaration = t.variableDeclaration("const", [
|
|
66
|
+
t.variableDeclarator(t.identifier(componentName), lazyComponentName ? t.callExpression(t.identifier("lazy"), [
|
|
67
|
+
t.identifier(lazyComponentName)
|
|
68
|
+
]) : componentNode1.value)
|
|
69
|
+
]);
|
|
70
|
+
componentDeclarations.push(componentDeclaration);
|
|
71
|
+
componentNode1.value = t.identifier(componentName);
|
|
72
|
+
}
|
|
73
|
+
if (lazyComponentName || componentName) {
|
|
74
|
+
componentId++;
|
|
75
|
+
if (componentName) {
|
|
76
|
+
var upperFirstName = componentName.slice(0, 1).toUpperCase() + componentName.slice(1);
|
|
77
|
+
var jsxElement = t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier(upperFirstName), [], true), null, [], true);
|
|
78
|
+
path.node.properties.push(t.objectProperty(t.identifier("element"), jsxElement));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
traverse(ast, {
|
|
84
|
+
Program: function Program(path) {
|
|
85
|
+
var lastImportIndex = path.get("body").reduce(function(lastIndex, p, index) {
|
|
86
|
+
if (t.isImportDeclaration(p.node)) {
|
|
87
|
+
lastIndex = index;
|
|
88
|
+
}
|
|
89
|
+
return lastIndex;
|
|
90
|
+
}, -1);
|
|
91
|
+
if (lastImportIndex >= 0) {
|
|
92
|
+
var lastImport = path.get("body.".concat(lastImportIndex));
|
|
93
|
+
_to_consumable_array(componentDeclarations).concat(_to_consumable_array(lazyComponentDeclarations)).forEach(function(declaration) {
|
|
94
|
+
if ("insertAfter" in lastImport) {
|
|
95
|
+
lastImport.insertAfter(declaration);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
var _generate = generate(ast), newCode = _generate.code;
|
|
102
|
+
var finalCode = "".concat(newCode, "export const baseName = '").concat(baseName, "';");
|
|
103
|
+
fs.writeFileSync(filePath, finalCode);
|
|
104
|
+
}
|
|
105
|
+
export {
|
|
106
|
+
generateRoutes
|
|
107
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
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(param) {
|
|
8
|
+
var sourceCode = param.sourceCode, filePath = param.filePath, prefix = param.prefix, baseName = param.baseName;
|
|
9
|
+
var ast = babelParser.parse(sourceCode, {
|
|
10
|
+
sourceType: "module"
|
|
11
|
+
});
|
|
12
|
+
var removedKeys = [
|
|
13
|
+
COMPONENT,
|
|
14
|
+
SHOULD_REVALIDATE,
|
|
15
|
+
LAZY_COMPONENT,
|
|
16
|
+
PRIVATE_COMPONENT
|
|
17
|
+
];
|
|
18
|
+
traverse(ast, {
|
|
19
|
+
ImportDeclaration: function ImportDeclaration(path) {
|
|
20
|
+
var source = path.node.source.value;
|
|
21
|
+
var routeIdMatch = source.match(/routeId=([^&]+)/);
|
|
22
|
+
if (routeIdMatch) {
|
|
23
|
+
var originalRouteId = routeIdMatch[1];
|
|
24
|
+
var newRouteId = "".concat(prefix).concat(originalRouteId);
|
|
25
|
+
var newSource = source.replace(/routeId=[^&]+/, "routeId=".concat(newRouteId));
|
|
26
|
+
path.node.source = t.stringLiteral(newSource);
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
ObjectExpression: function ObjectExpression(path) {
|
|
30
|
+
if (!Array.isArray(path.node.properties)) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
path.node.properties.forEach(function(prop) {
|
|
34
|
+
if (t.isObjectProperty(prop) && t.isStringLiteral(prop.key) && t.isStringLiteral(prop.value) && prop.key.value === ID) {
|
|
35
|
+
prop.value = t.stringLiteral("".concat(prefix).concat(prop.value.value));
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
path.node.properties = path.node.properties.filter(function(p) {
|
|
39
|
+
if (t.isObjectProperty(p) && t.isStringLiteral(p.key)) {
|
|
40
|
+
return !removedKeys.includes(p.key.value);
|
|
41
|
+
} else {
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
var tempCode = generate(ast).code;
|
|
48
|
+
var tempAst = babelParser.parse(tempCode, {
|
|
49
|
+
sourceType: "module"
|
|
50
|
+
});
|
|
51
|
+
var usedIdentifiers = /* @__PURE__ */ new Set();
|
|
52
|
+
traverse(tempAst, {
|
|
53
|
+
Identifier: function Identifier(path) {
|
|
54
|
+
if (t.isProperty(path.parent)) {
|
|
55
|
+
usedIdentifiers.add(path.node.name);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
traverse(tempAst, {
|
|
60
|
+
ImportDeclaration: function ImportDeclaration(path) {
|
|
61
|
+
path.node.specifiers = path.node.specifiers.filter(function(specifier) {
|
|
62
|
+
return usedIdentifiers.has(specifier.local.name);
|
|
63
|
+
});
|
|
64
|
+
if (!path.node.specifiers.length) {
|
|
65
|
+
path.remove();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
var _generate = generate(tempAst), newCode = _generate.code;
|
|
70
|
+
var finalCode = "".concat(newCode, "export const baseName = '").concat(baseName, "';");
|
|
71
|
+
fs.writeFileSync(filePath, finalCode);
|
|
72
|
+
}
|
|
73
|
+
export {
|
|
74
|
+
generateSlimRoutes
|
|
75
|
+
};
|
|
@@ -49,6 +49,7 @@ var moduleFederationConfigPlugin = function(userConfig) {
|
|
|
49
49
|
config: /* @__PURE__ */ _async_to_generator(function() {
|
|
50
50
|
var _modernjsConfig_server, _modernjsConfig_dev, bundlerType, ipv4, enableSSR;
|
|
51
51
|
return _ts_generator(this, function(_state2) {
|
|
52
|
+
console.log("config plugin config");
|
|
52
53
|
bundlerType = useAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
|
|
53
54
|
ipv4 = getIPV4();
|
|
54
55
|
enableSSR = Boolean(modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
|
package/dist/esm/cli/constant.js
CHANGED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
3
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import { fs } from "@modern-js/utils";
|
|
6
|
+
import { transformName2Prefix } from "../runtime/utils";
|
|
7
|
+
import { PLUGIN_IDENTIFIER } from "../constant";
|
|
8
|
+
import { MF_FULL_ROUTES, MF_SLIM_ROUTES, MF_ROUTES_META } from "../runtime/constant";
|
|
9
|
+
import { generateRoutes, generateSlimRoutes } from "./ast";
|
|
10
|
+
import { MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME } from "./constant";
|
|
11
|
+
function generateExtraExposeFiles(options) {
|
|
12
|
+
var routesFilePath = options.routesFilePath, mfConfig = options.mfConfig, isServer = options.isServer, baseName = options.baseName;
|
|
13
|
+
var outputDir = path.resolve(process.cwd(), "node_modules/.federation");
|
|
14
|
+
fs.ensureDirSync(outputDir);
|
|
15
|
+
var addSuffix = function(fileName) {
|
|
16
|
+
if (!isServer) {
|
|
17
|
+
return "".concat(fileName, ".jsx");
|
|
18
|
+
}
|
|
19
|
+
return "".concat(fileName, ".server.jsx");
|
|
20
|
+
};
|
|
21
|
+
var routesFileContent = fs.readFileSync(routesFilePath, "utf-8");
|
|
22
|
+
var outputSlimRoutesPath = path.resolve(outputDir, addSuffix(MF_SLIM_ROUTES));
|
|
23
|
+
var outputFullRoutesPath = path.resolve(outputDir, addSuffix(MF_FULL_ROUTES));
|
|
24
|
+
var outputRoutesMetaPath = path.resolve(outputDir, "".concat(MF_ROUTES_META, ".js"));
|
|
25
|
+
generateSlimRoutes({
|
|
26
|
+
sourceCode: routesFileContent,
|
|
27
|
+
filePath: outputSlimRoutesPath,
|
|
28
|
+
prefix: transformName2Prefix(mfConfig.name),
|
|
29
|
+
baseName
|
|
30
|
+
});
|
|
31
|
+
generateRoutes({
|
|
32
|
+
sourceCode: routesFileContent,
|
|
33
|
+
filePath: outputFullRoutesPath,
|
|
34
|
+
prefix: transformName2Prefix(mfConfig.name),
|
|
35
|
+
baseName
|
|
36
|
+
});
|
|
37
|
+
fs.writeFileSync(outputRoutesMetaPath, "export const baseName = '".concat(baseName, "';"));
|
|
38
|
+
return {
|
|
39
|
+
outputSlimRoutesPath,
|
|
40
|
+
outputFullRoutesPath,
|
|
41
|
+
outputRoutesMetaPath
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function addExpose(options) {
|
|
45
|
+
var mfConfig = options.mfConfig;
|
|
46
|
+
var _generateExtraExposeFiles = generateExtraExposeFiles(options), outputSlimRoutesPath = _generateExtraExposeFiles.outputSlimRoutesPath, outputFullRoutesPath = _generateExtraExposeFiles.outputFullRoutesPath, outputRoutesMetaPath = _generateExtraExposeFiles.outputRoutesMetaPath;
|
|
47
|
+
var fullRoutesKey = "./".concat(MF_FULL_ROUTES);
|
|
48
|
+
var slimRoutesKey = "./".concat(MF_SLIM_ROUTES);
|
|
49
|
+
var routeMetaKey = "./".concat(MF_ROUTES_META);
|
|
50
|
+
if (!mfConfig.exposes) {
|
|
51
|
+
var _obj;
|
|
52
|
+
mfConfig.exposes = (_obj = {}, _define_property(_obj, fullRoutesKey, outputFullRoutesPath), _define_property(_obj, slimRoutesKey, outputSlimRoutesPath), _define_property(_obj, routeMetaKey, outputRoutesMetaPath), _obj);
|
|
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
|
+
var pkgName = options.pkgName, mfConfig = options.mfConfig;
|
|
69
|
+
var alias = "@".concat(pkgName, "/runtime/router");
|
|
70
|
+
if (!mfConfig.shared) {
|
|
71
|
+
mfConfig.shared = _define_property({}, alias, {
|
|
72
|
+
singleton: true
|
|
73
|
+
});
|
|
74
|
+
} else {
|
|
75
|
+
if (!Array.isArray(mfConfig.shared)) {
|
|
76
|
+
mfConfig.shared[alias] = {
|
|
77
|
+
singleton: true
|
|
78
|
+
};
|
|
79
|
+
} else {
|
|
80
|
+
mfConfig.shared.push(alias);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function _pathMfConfig(options) {
|
|
85
|
+
addShared(options);
|
|
86
|
+
addExpose(options);
|
|
87
|
+
}
|
|
88
|
+
var moduleFederationDataLoaderPlugin = function(enable, internalOptions, userConfig) {
|
|
89
|
+
return {
|
|
90
|
+
name: "@modern-js/plugin-module-federation-data-loader",
|
|
91
|
+
pre: [
|
|
92
|
+
"@modern-js/plugin-module-federation-config"
|
|
93
|
+
],
|
|
94
|
+
post: [
|
|
95
|
+
"@modern-js/plugin-router",
|
|
96
|
+
"@modern-js/plugin-module-federation"
|
|
97
|
+
],
|
|
98
|
+
setup: function() {
|
|
99
|
+
var _ref = _async_to_generator(function(param) {
|
|
100
|
+
var useConfigContext, useAppContext, _modernjsConfig_server, _internalOptions_csrConfig, baseName, _userConfig_partialSSRRemotes, partialSSRRemotes, fetchSSRByRouteIds, patchMFConfig, _userConfig_pkgName, pkgName, _userConfig_serverPlugin, serverPlugin, modernjsConfig, appContext, enableSSR, name, routesFilePath;
|
|
101
|
+
return _ts_generator(this, function(_state) {
|
|
102
|
+
useConfigContext = param.useConfigContext, useAppContext = param.useAppContext;
|
|
103
|
+
if (!enable) {
|
|
104
|
+
return [
|
|
105
|
+
2
|
|
106
|
+
];
|
|
107
|
+
}
|
|
108
|
+
baseName = userConfig.baseName, _userConfig_partialSSRRemotes = userConfig.partialSSRRemotes, partialSSRRemotes = _userConfig_partialSSRRemotes === void 0 ? [] : _userConfig_partialSSRRemotes, fetchSSRByRouteIds = userConfig.fetchSSRByRouteIds, patchMFConfig = userConfig.patchMFConfig, _userConfig_pkgName = userConfig.pkgName, pkgName = _userConfig_pkgName === void 0 ? "modern-js" : _userConfig_pkgName, _userConfig_serverPlugin = userConfig.serverPlugin, serverPlugin = _userConfig_serverPlugin === void 0 ? "@module-federation/modern-js/data-loader-server" : _userConfig_serverPlugin;
|
|
109
|
+
if (!baseName) {
|
|
110
|
+
throw new Error("".concat(PLUGIN_IDENTIFIER, " 'baseName' is required if you enable 'dataLoader'!"));
|
|
111
|
+
}
|
|
112
|
+
modernjsConfig = useConfigContext();
|
|
113
|
+
appContext = useAppContext();
|
|
114
|
+
enableSSR = Boolean(modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
|
|
115
|
+
name = (_internalOptions_csrConfig = internalOptions.csrConfig) === null || _internalOptions_csrConfig === void 0 ? void 0 : _internalOptions_csrConfig.name;
|
|
116
|
+
routesFilePath = path.resolve(appContext.internalDirectory, "./main/".concat(MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME));
|
|
117
|
+
return [
|
|
118
|
+
2,
|
|
119
|
+
{
|
|
120
|
+
_internalRuntimePlugins: function(param2) {
|
|
121
|
+
var entrypoint = param2.entrypoint, plugins = param2.plugins;
|
|
122
|
+
plugins.push({
|
|
123
|
+
name: "ssrDataLoader",
|
|
124
|
+
path: "@module-federation/modern-js/data-loader",
|
|
125
|
+
config: {}
|
|
126
|
+
});
|
|
127
|
+
return {
|
|
128
|
+
entrypoint,
|
|
129
|
+
plugins
|
|
130
|
+
};
|
|
131
|
+
},
|
|
132
|
+
_internalServerPlugins: function _internalServerPlugins(param2) {
|
|
133
|
+
var plugins = param2.plugins;
|
|
134
|
+
plugins.push({
|
|
135
|
+
name: serverPlugin,
|
|
136
|
+
options: internalOptions
|
|
137
|
+
});
|
|
138
|
+
return {
|
|
139
|
+
plugins
|
|
140
|
+
};
|
|
141
|
+
},
|
|
142
|
+
config: /* @__PURE__ */ _async_to_generator(function() {
|
|
143
|
+
var patchMFConfigFn;
|
|
144
|
+
return _ts_generator(this, function(_state2) {
|
|
145
|
+
console.log("dataloader plugin config");
|
|
146
|
+
patchMFConfigFn = patchMFConfig || _pathMfConfig;
|
|
147
|
+
return [
|
|
148
|
+
2,
|
|
149
|
+
{
|
|
150
|
+
// server: {
|
|
151
|
+
// ssrByRouteIds: ssrByRouteIds,
|
|
152
|
+
// },
|
|
153
|
+
tools: {
|
|
154
|
+
rspack: function rspack(_config, param2) {
|
|
155
|
+
var isServer = param2.isServer;
|
|
156
|
+
patchMFConfigFn({
|
|
157
|
+
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
158
|
+
baseName,
|
|
159
|
+
pkgName,
|
|
160
|
+
isServer,
|
|
161
|
+
routesFilePath
|
|
162
|
+
});
|
|
163
|
+
console.log("dataloader plugin rspack");
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
source: {
|
|
167
|
+
define: {
|
|
168
|
+
MODERN_ROUTER_ID_PREFIX: JSON.stringify(transformName2Prefix(name))
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
];
|
|
173
|
+
});
|
|
174
|
+
})
|
|
175
|
+
}
|
|
176
|
+
];
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
return function(_) {
|
|
180
|
+
return _ref.apply(this, arguments);
|
|
181
|
+
};
|
|
182
|
+
}()
|
|
183
|
+
};
|
|
184
|
+
};
|
|
185
|
+
var dataLoaderPlugin_default = moduleFederationDataLoaderPlugin;
|
|
186
|
+
export {
|
|
187
|
+
dataLoaderPlugin_default as default,
|
|
188
|
+
generateRoutes,
|
|
189
|
+
generateSlimRoutes,
|
|
190
|
+
moduleFederationDataLoaderPlugin
|
|
191
|
+
};
|