@module-federation/modern-js 0.0.0-next-20240909073915 → 0.0.0-next-20240909092952
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/constant.js +49 -0
- package/dist/cjs/cli/dataLoader/ast/generateRoutes.js +141 -0
- package/dist/cjs/cli/{constant.js → dataLoader/ast/index.js} +6 -6
- package/dist/cjs/cli/dataLoader/generateRouteFile.js +143 -0
- package/dist/cjs/cli/dataLoader/patchMFConfig.js +132 -0
- package/dist/cjs/cli/dataLoader/plugin.js +194 -0
- package/dist/cjs/cli/index.js +5 -1
- package/dist/cjs/cli/mfRuntimePlugins/inject-node-fetch.js +7 -10
- package/dist/cjs/cli/mfRuntimePlugins/node.js +44 -0
- package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +40 -38
- package/dist/cjs/cli/server/dataLoaderPlugin.js +82 -0
- package/dist/cjs/cli/server/fetchRemoteFullRoutesMap.js +69 -0
- package/dist/cjs/cli/ssrPlugin.js +1 -1
- package/dist/cjs/cli/utils.js +8 -4
- package/dist/cjs/cli/utils.spec.js +1 -1
- package/dist/cjs/constant.js +32 -2
- package/dist/cjs/interfaces/route.js +16 -0
- package/dist/cjs/runtime/dataLoader/plugin.js +73 -0
- package/dist/cjs/runtime/dataLoader/utils.js +89 -0
- package/dist/cjs/runtime/index.js +4 -1
- package/dist/cjs/runtime/utils.js +38 -0
- package/dist/cjs/runtime/withMFRouteId.js +31 -0
- package/dist/cjs/ssr-runtime/plugin.js +7 -7
- package/dist/esm/cli/dataLoader/ast/constant.js +18 -0
- package/dist/esm/cli/dataLoader/ast/generateRoutes.js +108 -0
- package/dist/esm/cli/dataLoader/ast/index.js +4 -0
- package/dist/esm/cli/dataLoader/generateRouteFile.js +293 -0
- package/dist/esm/cli/dataLoader/patchMFConfig.js +98 -0
- package/dist/esm/cli/dataLoader/plugin.js +209 -0
- package/dist/esm/cli/index.js +4 -1
- package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js +7 -10
- package/dist/esm/cli/mfRuntimePlugins/node.js +16 -0
- package/dist/esm/cli/server/dataLoaderPlugin.js +105 -0
- package/dist/esm/cli/server/fetchRemoteFullRoutesMap.js +88 -0
- package/dist/esm/cli/ssrPlugin.js +1 -1
- package/dist/esm/cli/utils.js +6 -3
- package/dist/esm/cli/utils.spec.js +1 -1
- package/dist/esm/constant.js +21 -1
- package/dist/esm/interfaces/route.js +0 -0
- package/dist/esm/runtime/dataLoader/plugin.js +92 -0
- package/dist/esm/runtime/dataLoader/utils.js +63 -0
- package/dist/esm/runtime/index.js +3 -1
- package/dist/esm/runtime/utils.js +13 -0
- package/dist/esm/runtime/withMFRouteId.js +7 -0
- package/dist/esm/ssr-runtime/plugin.js +7 -10
- package/dist/esm-node/cli/dataLoader/ast/constant.js +18 -0
- package/dist/esm-node/cli/dataLoader/ast/generateRoutes.js +107 -0
- package/dist/esm-node/cli/dataLoader/ast/index.js +4 -0
- package/dist/esm-node/cli/dataLoader/generateRouteFile.js +108 -0
- package/dist/esm-node/cli/dataLoader/patchMFConfig.js +98 -0
- package/dist/esm-node/cli/dataLoader/plugin.js +159 -0
- package/dist/esm-node/cli/index.js +5 -1
- package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.js +7 -10
- package/dist/esm-node/cli/mfRuntimePlugins/node.js +14 -0
- package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +40 -38
- package/dist/esm-node/cli/server/dataLoaderPlugin.js +62 -0
- package/dist/esm-node/cli/server/fetchRemoteFullRoutesMap.js +44 -0
- package/dist/esm-node/cli/ssrPlugin.js +1 -1
- package/dist/esm-node/cli/utils.js +6 -3
- package/dist/esm-node/cli/utils.spec.js +1 -1
- package/dist/esm-node/constant.js +21 -1
- package/dist/esm-node/interfaces/route.js +0 -0
- package/dist/esm-node/runtime/dataLoader/plugin.js +49 -0
- package/dist/esm-node/runtime/dataLoader/utils.js +63 -0
- package/dist/esm-node/runtime/index.js +3 -1
- package/dist/esm-node/runtime/utils.js +13 -0
- package/dist/esm-node/runtime/withMFRouteId.js +7 -0
- package/dist/esm-node/ssr-runtime/plugin.js +7 -7
- package/dist/types/cli/dataLoader/ast/constant.d.ts +8 -0
- package/dist/types/cli/dataLoader/ast/generateRoutes.d.ts +5 -0
- package/dist/types/cli/dataLoader/ast/index.d.ts +1 -0
- package/dist/types/cli/dataLoader/generateRouteFile.d.ts +7 -0
- package/dist/types/cli/dataLoader/patchMFConfig.d.ts +10 -0
- package/dist/types/cli/dataLoader/plugin.d.ts +6 -0
- package/dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts +2 -2
- package/dist/types/cli/mfRuntimePlugins/node.d.ts +3 -0
- package/dist/types/cli/server/dataLoaderPlugin.d.ts +10 -0
- package/dist/types/cli/server/fetchRemoteFullRoutesMap.d.ts +10 -0
- package/dist/types/cli/utils.d.ts +2 -0
- package/dist/types/constant.d.ts +10 -0
- package/dist/types/interfaces/route.d.ts +12 -0
- package/dist/types/runtime/dataLoader/plugin.d.ts +2 -0
- package/dist/types/runtime/dataLoader/utils.d.ts +19 -0
- package/dist/types/runtime/index.d.ts +1 -0
- package/dist/types/runtime/utils.d.ts +2 -0
- package/dist/types/runtime/withMFRouteId.d.ts +1 -0
- package/dist/types/types/index.d.ts +9 -0
- package/package.json +40 -11
- package/dist/esm/cli/constant.js +0 -4
- package/dist/esm-node/cli/constant.js +0 -4
- package/dist/types/cli/constant.d.ts +0 -1
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import nodeFetch from "node-fetch";
|
|
2
|
+
const injectNodeFetchPlugin = () => ({
|
|
3
|
+
name: "inject-node-fetch-plugin",
|
|
4
|
+
beforeInit(args) {
|
|
5
|
+
if (!globalThis.fetch) {
|
|
6
|
+
globalThis.fetch = nodeFetch;
|
|
7
|
+
}
|
|
8
|
+
return args;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
var node_default = injectNodeFetchPlugin;
|
|
12
|
+
export {
|
|
13
|
+
node_default as default
|
|
14
|
+
};
|
|
@@ -16,46 +16,48 @@ function replaceObjectLocalhost(key, obj) {
|
|
|
16
16
|
function replaceLocalhost(url) {
|
|
17
17
|
return url.replace(LOCALHOST, ipv4);
|
|
18
18
|
}
|
|
19
|
-
const resolveEntryIpv4Plugin = () =>
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
19
|
+
const resolveEntryIpv4Plugin = () => {
|
|
20
|
+
return {
|
|
21
|
+
name: "resolve-entry-ipv4",
|
|
22
|
+
beforeRegisterRemote(args) {
|
|
23
|
+
const { remote } = args;
|
|
24
|
+
replaceObjectLocalhost("entry", remote);
|
|
25
|
+
return args;
|
|
26
|
+
},
|
|
27
|
+
async afterResolve(args) {
|
|
28
|
+
const { remoteInfo } = args;
|
|
29
|
+
replaceObjectLocalhost("entry", remoteInfo);
|
|
30
|
+
return args;
|
|
31
|
+
},
|
|
32
|
+
beforeLoadRemoteSnapshot(args) {
|
|
33
|
+
const { moduleInfo } = args;
|
|
34
|
+
if ("entry" in moduleInfo) {
|
|
35
|
+
replaceObjectLocalhost("entry", moduleInfo);
|
|
36
|
+
return args;
|
|
37
|
+
}
|
|
38
|
+
if ("version" in moduleInfo) {
|
|
39
|
+
replaceObjectLocalhost("version", moduleInfo);
|
|
40
|
+
}
|
|
41
|
+
return args;
|
|
42
|
+
},
|
|
43
|
+
loadRemoteSnapshot(args) {
|
|
44
|
+
const { remoteSnapshot } = args;
|
|
45
|
+
if ("publicPath" in remoteSnapshot) {
|
|
46
|
+
replaceObjectLocalhost("publicPath", remoteSnapshot);
|
|
47
|
+
}
|
|
48
|
+
if ("getPublicPath" in remoteSnapshot) {
|
|
49
|
+
replaceObjectLocalhost("getPublicPath", remoteSnapshot);
|
|
50
|
+
}
|
|
51
|
+
if (remoteSnapshot.remotesInfo) {
|
|
52
|
+
Object.keys(remoteSnapshot.remotesInfo).forEach((key) => {
|
|
53
|
+
const remoteInfo = remoteSnapshot.remotesInfo[key];
|
|
54
|
+
replaceObjectLocalhost("matchedVersion", remoteInfo);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
35
57
|
return args;
|
|
36
58
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
return args;
|
|
41
|
-
},
|
|
42
|
-
loadRemoteSnapshot(args) {
|
|
43
|
-
const { remoteSnapshot } = args;
|
|
44
|
-
if ("publicPath" in remoteSnapshot) {
|
|
45
|
-
replaceObjectLocalhost("publicPath", remoteSnapshot);
|
|
46
|
-
}
|
|
47
|
-
if ("getPublicPath" in remoteSnapshot) {
|
|
48
|
-
replaceObjectLocalhost("getPublicPath", remoteSnapshot);
|
|
49
|
-
}
|
|
50
|
-
if (remoteSnapshot.remotesInfo) {
|
|
51
|
-
Object.keys(remoteSnapshot.remotesInfo).forEach((key) => {
|
|
52
|
-
const remoteInfo = remoteSnapshot.remotesInfo[key];
|
|
53
|
-
replaceObjectLocalhost("matchedVersion", remoteInfo);
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
return args;
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
+
};
|
|
60
|
+
};
|
|
59
61
|
var resolve_entry_ipv4_default = resolveEntryIpv4Plugin;
|
|
60
62
|
export {
|
|
61
63
|
resolve_entry_ipv4_default as default
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { init } from "@module-federation/enhanced/runtime";
|
|
2
|
+
import { isBrowserEnv } from "@module-federation/sdk";
|
|
3
|
+
import { fetchRemoteFullRoutesMap, getSSRByRouteIds } from "./fetchRemoteFullRoutesMap";
|
|
4
|
+
import { injectRemoteRoutes } from "../../runtime/dataLoader/utils";
|
|
5
|
+
import { MF_ROUTES, DEFAULT_ENTRY, MODERN_JS_ROUTE_SERVER_LOADER } from "../../constant";
|
|
6
|
+
var dataLoaderPlugin_default = ({ runtimeOptions, partialSSRRemotes, ssrByRouteIdsMap }) => ({
|
|
7
|
+
name: "MFDataLoaderServerPlugin",
|
|
8
|
+
pre: [
|
|
9
|
+
"@modern-js/plugin-inject-resource"
|
|
10
|
+
],
|
|
11
|
+
setup(api) {
|
|
12
|
+
const { remotes, name } = runtimeOptions;
|
|
13
|
+
if (!remotes.length) {
|
|
14
|
+
return {};
|
|
15
|
+
}
|
|
16
|
+
let isHandled = false;
|
|
17
|
+
return {
|
|
18
|
+
prepare() {
|
|
19
|
+
const { middlewares } = api.useAppContext();
|
|
20
|
+
middlewares.push({
|
|
21
|
+
name: "MFDataLoaderServerPlugin",
|
|
22
|
+
handler: async (c, next) => {
|
|
23
|
+
const serverManifest = c.get("serverManifest");
|
|
24
|
+
const { loaderBundles, nestedRoutesJson } = serverManifest;
|
|
25
|
+
if (isHandled) {
|
|
26
|
+
await next();
|
|
27
|
+
} else {
|
|
28
|
+
var _api_useConfigContext_server;
|
|
29
|
+
const instance = init({
|
|
30
|
+
name,
|
|
31
|
+
remotes
|
|
32
|
+
});
|
|
33
|
+
const remoteFullRoutesMap = await fetchRemoteFullRoutesMap({
|
|
34
|
+
instance,
|
|
35
|
+
remotePath: isBrowserEnv() ? `${DEFAULT_ENTRY}/${MF_ROUTES}` : `${DEFAULT_ENTRY}/${MODERN_JS_ROUTE_SERVER_LOADER}`
|
|
36
|
+
});
|
|
37
|
+
const originalSSRByRouteIds = (_api_useConfigContext_server = api.useConfigContext().server) === null || _api_useConfigContext_server === void 0 ? void 0 : _api_useConfigContext_server.ssrByRouteIds;
|
|
38
|
+
if (originalSSRByRouteIds && partialSSRRemotes.length) {
|
|
39
|
+
const remoteSSRByRouteIds = getSSRByRouteIds(partialSSRRemotes, remoteFullRoutesMap) || [];
|
|
40
|
+
const ssrByRouteIds = /* @__PURE__ */ new Set([
|
|
41
|
+
...originalSSRByRouteIds,
|
|
42
|
+
...remoteSSRByRouteIds.map((id) => ssrByRouteIdsMap[id] || id)
|
|
43
|
+
]);
|
|
44
|
+
api.useConfigContext().server.ssrByRouteIds = Array.from(ssrByRouteIds);
|
|
45
|
+
}
|
|
46
|
+
injectRemoteRoutes(loaderBundles, remoteFullRoutesMap);
|
|
47
|
+
injectRemoteRoutes(nestedRoutesJson, remoteFullRoutesMap);
|
|
48
|
+
isHandled = true;
|
|
49
|
+
await next();
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
before: [
|
|
53
|
+
"render"
|
|
54
|
+
]
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
export {
|
|
61
|
+
dataLoaderPlugin_default as default
|
|
62
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
async function fetchRemoteFullRoutesMap(options) {
|
|
2
|
+
const { instance, remotePath } = options;
|
|
3
|
+
const { remotes } = instance.options;
|
|
4
|
+
const remoteRoutesMap = {};
|
|
5
|
+
if (!remotes.length) {
|
|
6
|
+
return remoteRoutesMap;
|
|
7
|
+
}
|
|
8
|
+
await Promise.all(remotes.map(async (remote) => {
|
|
9
|
+
const remoteId = `${remote.name}/${remotePath}`;
|
|
10
|
+
const { routes } = await instance.loadRemote(remoteId);
|
|
11
|
+
remoteRoutesMap[remote.name] = {
|
|
12
|
+
routes
|
|
13
|
+
};
|
|
14
|
+
}));
|
|
15
|
+
return remoteRoutesMap;
|
|
16
|
+
}
|
|
17
|
+
function getSSRByRouteIds(partialSSRRemotes, remoteRoutesMap) {
|
|
18
|
+
const remoteProviderRouteIds = /* @__PURE__ */ new Set();
|
|
19
|
+
const collectIds = (route) => {
|
|
20
|
+
remoteProviderRouteIds.add(route.id);
|
|
21
|
+
if (route.children) {
|
|
22
|
+
route.children.forEach((r) => {
|
|
23
|
+
collectIds(r);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
Object.entries(remoteRoutesMap).forEach((item) => {
|
|
28
|
+
const [remoteName, routesObj] = item;
|
|
29
|
+
if (!partialSSRRemotes.includes(remoteName)) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const { routes } = routesObj;
|
|
33
|
+
routes.forEach((route) => {
|
|
34
|
+
collectIds(route);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
return [
|
|
38
|
+
...remoteProviderRouteIds
|
|
39
|
+
];
|
|
40
|
+
}
|
|
41
|
+
export {
|
|
42
|
+
fetchRemoteFullRoutesMap,
|
|
43
|
+
getSSRByRouteIds
|
|
44
|
+
};
|
|
@@ -4,7 +4,7 @@ import { ModuleFederationPlugin } from "@module-federation/enhanced/webpack";
|
|
|
4
4
|
import { ModuleFederationPlugin as RspackModuleFederationPlugin } from "@module-federation/enhanced/rspack";
|
|
5
5
|
import UniverseEntryChunkTrackerPlugin from "@module-federation/node/universe-entry-chunk-tracker-plugin";
|
|
6
6
|
import { updateStatsAndManifest } from "./manifest";
|
|
7
|
-
import { isDev } from "
|
|
7
|
+
import { isDev } from "../constant";
|
|
8
8
|
function setEnv() {
|
|
9
9
|
process.env["MF_DISABLE_EMIT_STATS"] = "true";
|
|
10
10
|
process.env["MF_SSR_PRJ"] = "true";
|
|
@@ -47,7 +47,9 @@ const replaceRemoteUrl = (mfConfig, remoteIpStrategy) => {
|
|
|
47
47
|
if (typeof remoteObject === "string") {
|
|
48
48
|
return;
|
|
49
49
|
}
|
|
50
|
-
|
|
50
|
+
if ("entry" in remoteObject && typeof remoteObject.entry === "string") {
|
|
51
|
+
remoteObject.entry = remoteObject.entry.replace(LOCALHOST, ipv4);
|
|
52
|
+
}
|
|
51
53
|
});
|
|
52
54
|
} else if (typeof mfConfig.remotes !== "string") {
|
|
53
55
|
handleRemoteObject(mfConfig.remotes);
|
|
@@ -102,7 +104,7 @@ const patchMFConfig = (mfConfig, isServer, remoteIpStrategy) => {
|
|
|
102
104
|
injectRuntimePlugins(path.resolve(__dirname, "./mfRuntimePlugins/resolve-entry-ipv4.js"), runtimePlugins);
|
|
103
105
|
}
|
|
104
106
|
if (isServer) {
|
|
105
|
-
injectRuntimePlugins(require.resolve("
|
|
107
|
+
injectRuntimePlugins(require.resolve("./mfRuntimePlugins/node.js"), runtimePlugins);
|
|
106
108
|
if (isDev) {
|
|
107
109
|
injectRuntimePlugins(require.resolve("@module-federation/node/record-dynamic-remote-entry-hash-plugin"), runtimePlugins);
|
|
108
110
|
}
|
|
@@ -291,5 +293,6 @@ export {
|
|
|
291
293
|
getMFConfig,
|
|
292
294
|
patchBundlerConfig,
|
|
293
295
|
patchIgnoreWarning,
|
|
294
|
-
patchMFConfig
|
|
296
|
+
patchMFConfig,
|
|
297
|
+
replaceRemoteUrl
|
|
295
298
|
};
|
|
@@ -38,7 +38,7 @@ describe("patchMFConfig", async () => {
|
|
|
38
38
|
remoteType: "script",
|
|
39
39
|
runtimePlugins: [
|
|
40
40
|
path.resolve(__dirname, "./mfRuntimePlugins/shared-strategy.js"),
|
|
41
|
-
require.resolve("
|
|
41
|
+
require.resolve("./mfRuntimePlugins/node.js"),
|
|
42
42
|
path.resolve(__dirname, "./mfRuntimePlugins/inject-node-fetch.js")
|
|
43
43
|
],
|
|
44
44
|
shared: {
|
|
@@ -1,8 +1,28 @@
|
|
|
1
1
|
const MODERN_JS_SERVER_DIR = "bundles";
|
|
2
2
|
const LOCALHOST = "localhost";
|
|
3
3
|
const PLUGIN_IDENTIFIER = "[ Modern.js Module Federation ]";
|
|
4
|
+
const isDev = process.env.NODE_ENV === "development";
|
|
5
|
+
const MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME = "routes";
|
|
6
|
+
const MODERN_JS_ROUTE_SERVER_LOADER = "route-server-loaders";
|
|
7
|
+
const META_NAME = "modern-js";
|
|
8
|
+
const FOLD_IDENTIFIER = "@mf";
|
|
9
|
+
const SEPARATOR = "_";
|
|
10
|
+
const MF_ROUTES = "mf-routes";
|
|
11
|
+
const DEFAULT_ENTRY = "main";
|
|
12
|
+
const DEFAULT_LAYOUT = "layout";
|
|
13
|
+
const MF_JSON = "mf.json";
|
|
4
14
|
export {
|
|
15
|
+
DEFAULT_ENTRY,
|
|
16
|
+
DEFAULT_LAYOUT,
|
|
17
|
+
FOLD_IDENTIFIER,
|
|
5
18
|
LOCALHOST,
|
|
19
|
+
META_NAME,
|
|
20
|
+
MF_JSON,
|
|
21
|
+
MF_ROUTES,
|
|
22
|
+
MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME,
|
|
23
|
+
MODERN_JS_ROUTE_SERVER_LOADER,
|
|
6
24
|
MODERN_JS_SERVER_DIR,
|
|
7
|
-
PLUGIN_IDENTIFIER
|
|
25
|
+
PLUGIN_IDENTIFIER,
|
|
26
|
+
SEPARATOR,
|
|
27
|
+
isDev
|
|
8
28
|
};
|
|
File without changes
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { getInstance } from "@module-federation/enhanced/runtime";
|
|
2
|
+
import { MF_ROUTES, DEFAULT_ENTRY } from "../../constant";
|
|
3
|
+
import { injectRemoteRoutes } from "./utils";
|
|
4
|
+
const ssrDataLoaderPlugin = () => {
|
|
5
|
+
return {
|
|
6
|
+
name: "@modern-js/plugin-mf-data-loader",
|
|
7
|
+
post: [
|
|
8
|
+
"@modern-js/plugin-router"
|
|
9
|
+
],
|
|
10
|
+
setup: () => {
|
|
11
|
+
const remoteRoutesMap = {};
|
|
12
|
+
return {
|
|
13
|
+
async beforeRender() {
|
|
14
|
+
console.log("init");
|
|
15
|
+
if (Object.keys(remoteRoutesMap).length) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const instance = getInstance();
|
|
19
|
+
if (!instance) {
|
|
20
|
+
console.log("no instance!");
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (!instance.options.remotes.length) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
await Promise.all(instance.options.remotes.map(async (remote) => {
|
|
27
|
+
const remoteId = `${remote.name}/${DEFAULT_ENTRY}/${MF_ROUTES}`;
|
|
28
|
+
const { routes } = await instance.loadRemote(remoteId);
|
|
29
|
+
remoteRoutesMap[remote.name] = {
|
|
30
|
+
routes
|
|
31
|
+
};
|
|
32
|
+
}));
|
|
33
|
+
},
|
|
34
|
+
// runtime plugin not save route , so it needs to inject again when trigger
|
|
35
|
+
modifyRoutes: (routes) => {
|
|
36
|
+
injectRemoteRoutes({
|
|
37
|
+
entry: {
|
|
38
|
+
routes
|
|
39
|
+
}
|
|
40
|
+
}, remoteRoutesMap);
|
|
41
|
+
return routes;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
export {
|
|
48
|
+
ssrDataLoaderPlugin
|
|
49
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { DEFAULT_LAYOUT } from "../../constant";
|
|
2
|
+
import { transformName2Prefix, transformPrefix2Name } from "../utils";
|
|
3
|
+
function getRemoteRoutesInfos(route, remoteRoutesMap) {
|
|
4
|
+
if (!route.id) {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
const remoteName = transformPrefix2Name(route.id);
|
|
8
|
+
const remoteRouteObj = remoteRoutesMap[remoteName];
|
|
9
|
+
if (!remoteRouteObj) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const remoteRoutes = remoteRouteObj.routes[0];
|
|
13
|
+
return {
|
|
14
|
+
routes: remoteRoutes,
|
|
15
|
+
name: remoteName,
|
|
16
|
+
pathName: route.path
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function getRemoteLayoutId(remoteName) {
|
|
20
|
+
const prefix = transformName2Prefix(remoteName);
|
|
21
|
+
return `${prefix}${DEFAULT_LAYOUT}`;
|
|
22
|
+
}
|
|
23
|
+
function injectRemoteRoutes(initialRoutes, remoteRoutesMap) {
|
|
24
|
+
const traverse = (route) => {
|
|
25
|
+
var _route_children;
|
|
26
|
+
const remoteRoutesInfos = getRemoteRoutesInfos(route, remoteRoutesMap);
|
|
27
|
+
if (remoteRoutesInfos) {
|
|
28
|
+
const { routes, pathName } = remoteRoutesInfos;
|
|
29
|
+
route.id = routes.id;
|
|
30
|
+
route.path = pathName;
|
|
31
|
+
if (routes.loader) {
|
|
32
|
+
route.loader = routes.loader;
|
|
33
|
+
}
|
|
34
|
+
if (routes.children) {
|
|
35
|
+
route.children = routes.children;
|
|
36
|
+
}
|
|
37
|
+
route.element = routes.element;
|
|
38
|
+
route.Component = routes.Component;
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
(_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.forEach((r) => {
|
|
42
|
+
traverse(r);
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
Object.keys(initialRoutes).forEach((entryName) => {
|
|
46
|
+
const routes = initialRoutes[entryName];
|
|
47
|
+
if (Array.isArray(routes)) {
|
|
48
|
+
routes.forEach((route) => {
|
|
49
|
+
traverse(route);
|
|
50
|
+
});
|
|
51
|
+
} else {
|
|
52
|
+
var _routes_routes;
|
|
53
|
+
routes === null || routes === void 0 ? void 0 : (_routes_routes = routes.routes) === null || _routes_routes === void 0 ? void 0 : _routes_routes.forEach((r) => {
|
|
54
|
+
traverse(r);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
export {
|
|
60
|
+
getRemoteLayoutId,
|
|
61
|
+
getRemoteRoutesInfos,
|
|
62
|
+
injectRemoteRoutes
|
|
63
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { encodeName, decodeName } from "@module-federation/sdk";
|
|
2
|
+
const SPLIT_SYMBOL = "@";
|
|
3
|
+
function transformName2Prefix(name) {
|
|
4
|
+
return `${encodeName(name, "", false)}${SPLIT_SYMBOL}`;
|
|
5
|
+
}
|
|
6
|
+
function transformPrefix2Name(prefix) {
|
|
7
|
+
const realPrefix = prefix.split(SPLIT_SYMBOL)[0];
|
|
8
|
+
return decodeName(realPrefix, "", false);
|
|
9
|
+
}
|
|
10
|
+
export {
|
|
11
|
+
transformName2Prefix,
|
|
12
|
+
transformPrefix2Name
|
|
13
|
+
};
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
2
|
import hoistNonReactStatics from "hoist-non-react-statics";
|
|
3
3
|
import { SSRLiveReload } from "./SSRLiveReload";
|
|
4
|
+
console.log("mfSSRPlugin trigger");
|
|
4
5
|
const mfSSRPlugin = () => ({
|
|
5
6
|
name: "@module-federation/modern-js",
|
|
7
|
+
pre: [
|
|
8
|
+
"@modern-js/plugin-router"
|
|
9
|
+
],
|
|
6
10
|
setup: () => {
|
|
7
11
|
return {
|
|
8
|
-
async
|
|
12
|
+
async beforeRender() {
|
|
9
13
|
if (typeof window !== "undefined") {
|
|
10
|
-
return
|
|
11
|
-
context
|
|
12
|
-
});
|
|
14
|
+
return;
|
|
13
15
|
}
|
|
14
16
|
globalThis.shouldUpdate = false;
|
|
15
17
|
const nodeUtils = await import("@module-federation/node/utils");
|
|
@@ -19,9 +21,7 @@ const mfSSRPlugin = () => ({
|
|
|
19
21
|
await nodeUtils.flushChunks();
|
|
20
22
|
globalThis.shouldUpdate = true;
|
|
21
23
|
}
|
|
22
|
-
return
|
|
23
|
-
context
|
|
24
|
-
});
|
|
24
|
+
return;
|
|
25
25
|
},
|
|
26
26
|
wrapRoot(App) {
|
|
27
27
|
const AppWrapper = (props) => /* @__PURE__ */ _jsxs(_Fragment, {
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const IS_ROOT = "isRoot";
|
|
2
|
+
export declare const ID = "id";
|
|
3
|
+
export declare const COMPONENT = "component";
|
|
4
|
+
export declare const LAZY_COMPONENT = "lazyImport";
|
|
5
|
+
export declare const SHOULD_REVALIDATE = "shouldRevalidate";
|
|
6
|
+
export declare const PRIVATE_COMPONENT = "_component";
|
|
7
|
+
export declare const ELEMENT = "element";
|
|
8
|
+
export declare const LOADER = "loader";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { generateRoutes } from './generateRoutes';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type moduleFederationPlugin } from '@module-federation/sdk';
|
|
2
|
+
type PatchMFConfigOptions = {
|
|
3
|
+
mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions;
|
|
4
|
+
metaName: string;
|
|
5
|
+
isServer: boolean;
|
|
6
|
+
internalDirectory: string;
|
|
7
|
+
entries: Set<string>;
|
|
8
|
+
};
|
|
9
|
+
export declare function patchMFConfig(options: PatchMFConfigOptions): void;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { CliPlugin, AppTools } from '@modern-js/app-tools';
|
|
2
|
+
import type { DataLoaderOptions, InternalModernPluginOptions } from '../../types';
|
|
3
|
+
import { generateRoutes } from './ast';
|
|
4
|
+
export declare const moduleFederationDataLoaderPlugin: (enable: boolean, internalOptions: InternalModernPluginOptions, userConfig: DataLoaderOptions) => CliPlugin<AppTools>;
|
|
5
|
+
export default moduleFederationDataLoaderPlugin;
|
|
6
|
+
export { generateRoutes };
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime';
|
|
2
|
-
declare const
|
|
3
|
-
export default
|
|
2
|
+
declare const nodePlugin: () => FederationRuntimePlugin;
|
|
3
|
+
export default nodePlugin;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ServerPlugin } from '@modern-js/server-core';
|
|
2
|
+
import { init } from '@module-federation/enhanced/runtime';
|
|
3
|
+
type MFRuntimeOptions = Parameters<typeof init>[0];
|
|
4
|
+
export type DataLoaderServerPluginOptions = {
|
|
5
|
+
runtimeOptions: MFRuntimeOptions;
|
|
6
|
+
partialSSRRemotes: string[];
|
|
7
|
+
ssrByRouteIdsMap: Record<string, string>;
|
|
8
|
+
};
|
|
9
|
+
declare const _default: ({ runtimeOptions, partialSSRRemotes, ssrByRouteIdsMap, }: DataLoaderServerPluginOptions) => ServerPlugin;
|
|
10
|
+
export default _default;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { FederationHost } from '@module-federation/enhanced/runtime';
|
|
2
|
+
import type { RouteObject } from '@modern-js/runtime/router';
|
|
3
|
+
export type RemoteRoutesMap = Record<string, {
|
|
4
|
+
routes: RouteObject[];
|
|
5
|
+
}>;
|
|
6
|
+
export declare function fetchRemoteFullRoutesMap(options: {
|
|
7
|
+
remotePath: string;
|
|
8
|
+
instance: FederationHost;
|
|
9
|
+
}): Promise<RemoteRoutesMap>;
|
|
10
|
+
export declare function getSSRByRouteIds(partialSSRRemotes: string[], remoteRoutesMap: RemoteRoutesMap): undefined | string[];
|
|
@@ -2,8 +2,10 @@ import type { webpack, UserConfig, AppTools, Rspack, Bundler } from '@modern-js/
|
|
|
2
2
|
import { moduleFederationPlugin } from '@module-federation/sdk';
|
|
3
3
|
import { PluginOptions } from '../types';
|
|
4
4
|
import { BundlerConfig } from '../interfaces/bundler';
|
|
5
|
+
import type { init } from '@module-federation/enhanced/runtime';
|
|
5
6
|
export type ConfigType<T> = T extends 'webpack' ? webpack.Configuration : T extends 'rspack' ? Rspack.Configuration : never;
|
|
6
7
|
export declare const getMFConfig: (userConfig: PluginOptions) => Promise<moduleFederationPlugin.ModuleFederationPluginOptions>;
|
|
8
|
+
export declare const replaceRemoteUrl: (mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions | Parameters<typeof init>[0], remoteIpStrategy?: "ipv4" | "inherit") => void;
|
|
7
9
|
export declare const patchMFConfig: (mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions, isServer: boolean, remoteIpStrategy?: "ipv4" | "inherit") => moduleFederationPlugin.ModuleFederationPluginOptions;
|
|
8
10
|
export declare function patchIgnoreWarning<T extends Bundler>(bundlerConfig: BundlerConfig<T>): void;
|
|
9
11
|
export declare function patchBundlerConfig<T extends Bundler>(options: {
|
package/dist/types/constant.d.ts
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
1
|
export declare const MODERN_JS_SERVER_DIR = "bundles";
|
|
2
2
|
export declare const LOCALHOST = "localhost";
|
|
3
3
|
export declare const PLUGIN_IDENTIFIER = "[ Modern.js Module Federation ]";
|
|
4
|
+
export declare const isDev: boolean;
|
|
5
|
+
export declare const MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME = "routes";
|
|
6
|
+
export declare const MODERN_JS_ROUTE_SERVER_LOADER = "route-server-loaders";
|
|
7
|
+
export declare const META_NAME = "modern-js";
|
|
8
|
+
export declare const FOLD_IDENTIFIER = "@mf";
|
|
9
|
+
export declare const SEPARATOR = "_";
|
|
10
|
+
export declare const MF_ROUTES = "mf-routes";
|
|
11
|
+
export declare const DEFAULT_ENTRY = "main";
|
|
12
|
+
export declare const DEFAULT_LAYOUT = "layout";
|
|
13
|
+
export declare const MF_JSON = "mf.json";
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { RouteObject } from '@modern-js/runtime/router';
|
|
2
|
+
export type RemoteRoutesMap = Record<string, {
|
|
3
|
+
routes: RouteObject[];
|
|
4
|
+
}>;
|
|
5
|
+
export type LoaderBundlesRoutes = {
|
|
6
|
+
[entryName: string]: {
|
|
7
|
+
routes: RouteObject[];
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
export type NestedRoutes = {
|
|
11
|
+
[entryName: string]: RouteObject[];
|
|
12
|
+
};
|
|
13
|
+
export declare function getRemoteRoutesInfos(route: RouteObject, remoteRoutesMap: RemoteRoutesMap): {
|
|
14
|
+
routes: RouteObject;
|
|
15
|
+
name: string;
|
|
16
|
+
pathName: string | undefined;
|
|
17
|
+
} | undefined;
|
|
18
|
+
export declare function getRemoteLayoutId(remoteName: string): string;
|
|
19
|
+
export declare function injectRemoteRoutes(initialRoutes: LoaderBundlesRoutes | NestedRoutes, remoteRoutesMap: RemoteRoutesMap): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function withMFRouteId(id: string): string;
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { moduleFederationPlugin } from '@module-federation/sdk';
|
|
2
2
|
import type { ModuleFederationPlugin as WebpackModuleFederationPlugin } from '@module-federation/enhanced';
|
|
3
3
|
import type { ModuleFederationPlugin as RspackModuleFederationPlugin } from '@module-federation/enhanced/rspack';
|
|
4
|
+
import type { init } from '@module-federation/enhanced/runtime';
|
|
4
5
|
export interface PluginOptions {
|
|
5
6
|
config?: moduleFederationPlugin.ModuleFederationPluginOptions;
|
|
6
7
|
configPath?: string;
|
|
7
8
|
remoteIpStrategy?: 'ipv4' | 'inherit';
|
|
9
|
+
dataLoader?: boolean | DataLoaderOptions;
|
|
8
10
|
}
|
|
9
11
|
export interface InternalModernPluginOptions {
|
|
10
12
|
csrConfig?: moduleFederationPlugin.ModuleFederationPluginOptions;
|
|
@@ -16,3 +18,10 @@ export interface InternalModernPluginOptions {
|
|
|
16
18
|
remoteIpStrategy?: 'ipv4' | 'inherit';
|
|
17
19
|
}
|
|
18
20
|
export type BundlerPlugin = WebpackModuleFederationPlugin | RspackModuleFederationPlugin;
|
|
21
|
+
export type TransformRuntimeOptions = (mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions) => Parameters<typeof init>[0];
|
|
22
|
+
export type DataLoaderOptions = {
|
|
23
|
+
partialSSRRemotes?: string[];
|
|
24
|
+
metaName?: string;
|
|
25
|
+
serverPlugin?: string;
|
|
26
|
+
transformRuntimeOptions?: TransformRuntimeOptions;
|
|
27
|
+
};
|