@module-federation/modern-js 0.0.0-next-20241106033151 → 0.0.0-next-20241106063644
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/index.js +7 -2
- package/dist/cjs/cli/mfRuntimePlugins/{inject-node-fetch.js → node-fetch.js} +6 -6
- package/dist/cjs/cli/mfRuntimePlugins/node.js +41 -0
- package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +40 -38
- package/dist/cjs/cli/routes/ast/constant.js +49 -0
- package/dist/cjs/cli/routes/ast/generateRoutes.js +143 -0
- package/dist/cjs/cli/{constant.js → routes/ast/index.js} +6 -6
- package/dist/cjs/cli/routes/clearMFCache.js +57 -0
- package/dist/cjs/cli/routes/exportRoutePlugin.js +77 -0
- package/dist/cjs/cli/routes/importRoutesPlugin.js +120 -0
- package/dist/cjs/cli/routes/plugin.js +157 -0
- package/dist/cjs/cli/routes/traverseRouteFiles.js +148 -0
- package/dist/cjs/cli/routes/utils.js +168 -0
- package/dist/cjs/cli/server/fetchRemoteFullRoutesMap.js +72 -0
- package/dist/cjs/cli/server/fetchRoutePlugin.js +82 -0
- package/dist/cjs/cli/ssrPlugin.js +5 -2
- package/dist/cjs/cli/utils.js +11 -6
- package/dist/cjs/cli/utils.spec.js +5 -6
- package/dist/cjs/constant.js +35 -2
- package/dist/cjs/interfaces/route.js +16 -0
- package/dist/cjs/runtime/index.js +4 -1
- package/dist/cjs/runtime/routes/plugin-inject-assets.js +92 -0
- package/dist/cjs/runtime/routes/plugin.js +75 -0
- package/dist/cjs/runtime/routes/utils.js +115 -0
- package/dist/cjs/{cli/mfRuntimePlugins/shared-strategy.js → runtime/withMFRouteId.js} +11 -23
- package/dist/cjs/ssr-runtime/plugin.js +7 -8
- package/dist/cjs/types/modern.js +16 -0
- package/dist/cjs/types/routes.js +16 -0
- package/dist/esm/cli/index.js +7 -2
- package/dist/esm/cli/mfRuntimePlugins/{inject-node-fetch.js → node-fetch.js} +3 -3
- package/dist/esm/cli/mfRuntimePlugins/node.js +13 -0
- package/dist/esm/cli/routes/ast/constant.js +18 -0
- package/dist/esm/cli/routes/ast/generateRoutes.js +110 -0
- package/dist/esm/cli/routes/ast/index.js +4 -0
- package/dist/esm/cli/routes/clearMFCache.js +34 -0
- package/dist/esm/cli/routes/exportRoutePlugin.js +79 -0
- package/dist/esm/cli/routes/importRoutesPlugin.js +122 -0
- package/dist/esm/cli/routes/plugin.js +266 -0
- package/dist/esm/cli/routes/traverseRouteFiles.js +313 -0
- package/dist/esm/cli/routes/utils.js +130 -0
- package/dist/esm/cli/server/fetchRemoteFullRoutesMap.js +93 -0
- package/dist/esm/cli/server/fetchRoutePlugin.js +105 -0
- package/dist/esm/cli/ssrPlugin.js +6 -3
- package/dist/esm/cli/utils.js +9 -5
- package/dist/esm/cli/utils.spec.js +5 -6
- package/dist/esm/constant.js +23 -1
- package/dist/esm/interfaces/route.js +0 -0
- package/dist/esm/runtime/index.js +3 -1
- package/dist/esm/runtime/routes/plugin-inject-assets.js +71 -0
- package/dist/esm/runtime/routes/plugin.js +97 -0
- package/dist/esm/runtime/routes/utils.js +86 -0
- package/dist/esm/runtime/withMFRouteId.js +7 -0
- package/dist/esm/ssr-runtime/plugin.js +8 -11
- package/dist/esm/types/modern.js +0 -0
- package/dist/esm/types/routes.js +0 -0
- package/dist/esm-node/cli/index.js +7 -2
- package/dist/esm-node/cli/mfRuntimePlugins/{inject-node-fetch.js → node-fetch.js} +3 -3
- package/dist/esm-node/cli/mfRuntimePlugins/node.js +11 -0
- package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +40 -38
- package/dist/esm-node/cli/routes/ast/constant.js +18 -0
- package/dist/esm-node/cli/routes/ast/generateRoutes.js +109 -0
- package/dist/esm-node/cli/routes/ast/index.js +4 -0
- package/dist/esm-node/cli/routes/clearMFCache.js +33 -0
- package/dist/esm-node/cli/routes/exportRoutePlugin.js +53 -0
- package/dist/esm-node/cli/routes/importRoutesPlugin.js +96 -0
- package/dist/esm-node/cli/routes/plugin.js +123 -0
- package/dist/esm-node/cli/routes/traverseRouteFiles.js +113 -0
- package/dist/esm-node/cli/routes/utils.js +133 -0
- package/dist/esm-node/cli/server/fetchRemoteFullRoutesMap.js +47 -0
- package/dist/esm-node/cli/server/fetchRoutePlugin.js +62 -0
- package/dist/esm-node/cli/ssrPlugin.js +5 -2
- package/dist/esm-node/cli/utils.js +9 -5
- package/dist/esm-node/cli/utils.spec.js +5 -6
- package/dist/esm-node/constant.js +23 -1
- package/dist/esm-node/interfaces/route.js +0 -0
- package/dist/esm-node/runtime/index.js +3 -1
- package/dist/esm-node/runtime/routes/plugin-inject-assets.js +68 -0
- package/dist/esm-node/runtime/routes/plugin.js +51 -0
- package/dist/esm-node/runtime/routes/utils.js +86 -0
- package/dist/esm-node/runtime/withMFRouteId.js +7 -0
- package/dist/esm-node/ssr-runtime/plugin.js +7 -8
- package/dist/esm-node/types/modern.js +0 -0
- package/dist/esm-node/types/routes.js +0 -0
- package/dist/types/cli/mfRuntimePlugins/node.d.ts +3 -0
- package/dist/types/cli/routes/ast/constant.d.ts +8 -0
- package/dist/types/cli/routes/ast/generateRoutes.d.ts +5 -0
- package/dist/types/cli/routes/ast/index.d.ts +1 -0
- package/dist/types/cli/routes/clearMFCache.d.ts +1 -0
- package/dist/types/cli/routes/exportRoutePlugin.d.ts +4 -0
- package/dist/types/cli/routes/importRoutesPlugin.d.ts +4 -0
- package/dist/types/cli/routes/plugin.d.ts +4 -0
- package/dist/types/cli/routes/traverseRouteFiles.d.ts +11 -0
- package/dist/types/cli/routes/utils.d.ts +11 -0
- package/dist/types/cli/server/fetchRemoteFullRoutesMap.d.ts +11 -0
- package/dist/types/cli/server/fetchRoutePlugin.d.ts +10 -0
- package/dist/types/cli/utils.d.ts +2 -0
- package/dist/types/constant.d.ts +11 -0
- package/dist/types/interfaces/route.d.ts +12 -0
- package/dist/types/runtime/index.d.ts +1 -0
- package/dist/types/runtime/routes/plugin-inject-assets.d.ts +4 -0
- package/dist/types/runtime/routes/plugin.d.ts +5 -0
- package/dist/types/runtime/routes/utils.d.ts +22 -0
- package/dist/types/runtime/withMFRouteId.d.ts +1 -0
- package/dist/types/ssr-runtime/plugin.d.ts +3 -1
- package/dist/types/types/index.d.ts +3 -1
- package/dist/types/types/modern.d.ts +12 -0
- package/dist/types/types/routes.d.ts +19 -0
- package/package.json +42 -5
- package/dist/esm/cli/constant.js +0 -4
- package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +0 -25
- package/dist/esm-node/cli/constant.js +0 -4
- package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +0 -23
- package/dist/types/cli/constant.d.ts +0 -1
- package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +0 -3
- /package/dist/types/cli/mfRuntimePlugins/{inject-node-fetch.d.ts → node-fetch.d.ts} +0 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
async function fetchRemoteFullRoutesMap(options) {
|
|
2
|
+
const { instance, remotePath, dataLoaderRemotes } = 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
|
+
if (!dataLoaderRemotes.includes(remote.name)) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const remoteId = `${remote.name}/${remotePath}`;
|
|
13
|
+
const { routes } = await instance.loadRemote(remoteId);
|
|
14
|
+
remoteRoutesMap[remote.name] = {
|
|
15
|
+
routes
|
|
16
|
+
};
|
|
17
|
+
}));
|
|
18
|
+
return remoteRoutesMap;
|
|
19
|
+
}
|
|
20
|
+
function getSSRByRouteIds(dataLoaderRemotes, remoteRoutesMap) {
|
|
21
|
+
const remoteProviderRouteIds = /* @__PURE__ */ new Set();
|
|
22
|
+
const collectIds = (route) => {
|
|
23
|
+
remoteProviderRouteIds.add(route.id);
|
|
24
|
+
if (route.children) {
|
|
25
|
+
route.children.forEach((r) => {
|
|
26
|
+
collectIds(r);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
Object.entries(remoteRoutesMap).forEach((item) => {
|
|
31
|
+
const [remoteName, routesObj] = item;
|
|
32
|
+
if (!dataLoaderRemotes.includes(remoteName)) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const { routes } = routesObj;
|
|
36
|
+
routes.forEach((route) => {
|
|
37
|
+
collectIds(route);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
return [
|
|
41
|
+
...remoteProviderRouteIds
|
|
42
|
+
];
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
fetchRemoteFullRoutesMap,
|
|
46
|
+
getSSRByRouteIds
|
|
47
|
+
};
|
|
@@ -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/routes/utils";
|
|
5
|
+
import { MF_ROUTES, DEFAULT_ENTRY, MODERN_JS_ROUTE_SERVER_LOADER } from "../../constant";
|
|
6
|
+
var fetchRoutePlugin_default = ({ runtimeOptions, dataLoaderRemotes, ssrByRouteIdsMap }) => ({
|
|
7
|
+
name: "@module-federation/modern-js-fetch-route",
|
|
8
|
+
pre: [
|
|
9
|
+
"@modern-js/plugin-inject-resource"
|
|
10
|
+
],
|
|
11
|
+
setup(api) {
|
|
12
|
+
const { remotes, name } = runtimeOptions;
|
|
13
|
+
let isHandled = false;
|
|
14
|
+
return {
|
|
15
|
+
prepare() {
|
|
16
|
+
const { middlewares } = api.useAppContext();
|
|
17
|
+
middlewares.push({
|
|
18
|
+
name: "MFPatchRouteMiddleWare",
|
|
19
|
+
handler: async (c, next) => {
|
|
20
|
+
console.log("MFPatchRouteMiddleWare apply");
|
|
21
|
+
const serverManifest = c.get("serverManifest");
|
|
22
|
+
const { loaderBundles, nestedRoutesJson } = serverManifest;
|
|
23
|
+
if (isHandled && !globalThis.FORCE_MF_REFRESH) {
|
|
24
|
+
await next();
|
|
25
|
+
} else {
|
|
26
|
+
var _api_useConfigContext_server;
|
|
27
|
+
const instance = init({
|
|
28
|
+
name,
|
|
29
|
+
remotes
|
|
30
|
+
});
|
|
31
|
+
const remoteFullRoutesMap = await fetchRemoteFullRoutesMap({
|
|
32
|
+
instance,
|
|
33
|
+
remotePath: isBrowserEnv() ? `${DEFAULT_ENTRY}/${MF_ROUTES}` : `${DEFAULT_ENTRY}/${MODERN_JS_ROUTE_SERVER_LOADER}`,
|
|
34
|
+
dataLoaderRemotes
|
|
35
|
+
});
|
|
36
|
+
const originalSSRByRouteIds = (_api_useConfigContext_server = api.useConfigContext().server) === null || _api_useConfigContext_server === void 0 ? void 0 : _api_useConfigContext_server.ssrByRouteIds;
|
|
37
|
+
if (originalSSRByRouteIds && dataLoaderRemotes.length) {
|
|
38
|
+
const remoteSSRByRouteIds = getSSRByRouteIds(dataLoaderRemotes, remoteFullRoutesMap) || [];
|
|
39
|
+
const ssrByRouteIds = /* @__PURE__ */ new Set([
|
|
40
|
+
...originalSSRByRouteIds,
|
|
41
|
+
...remoteSSRByRouteIds.map((id) => ssrByRouteIdsMap[id] || id)
|
|
42
|
+
]);
|
|
43
|
+
api.useConfigContext().server.ssrByRouteIds = Array.from(ssrByRouteIds);
|
|
44
|
+
}
|
|
45
|
+
injectRemoteRoutes(loaderBundles, remoteFullRoutesMap);
|
|
46
|
+
injectRemoteRoutes(nestedRoutesJson, remoteFullRoutesMap);
|
|
47
|
+
isHandled = true;
|
|
48
|
+
globalThis.FORCE_MF_REFRESH = false;
|
|
49
|
+
await next();
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
before: [
|
|
53
|
+
"render"
|
|
54
|
+
]
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
export {
|
|
61
|
+
fetchRoutePlugin_default as default
|
|
62
|
+
};
|
|
@@ -4,8 +4,8 @@ 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 "./constant";
|
|
8
7
|
import logger from "./logger";
|
|
8
|
+
import { isDev, META_NAME } from "../constant";
|
|
9
9
|
function setEnv() {
|
|
10
10
|
process.env["MF_DISABLE_EMIT_STATS"] = "true";
|
|
11
11
|
process.env["MF_SSR_PRJ"] = "true";
|
|
@@ -19,6 +19,7 @@ const moduleFederationSSRPlugin = (userConfig) => ({
|
|
|
19
19
|
setup: async ({ useConfigContext, useAppContext }) => {
|
|
20
20
|
var _modernjsConfig_server;
|
|
21
21
|
const modernjsConfig = useConfigContext();
|
|
22
|
+
const { metaName = META_NAME } = userConfig;
|
|
22
23
|
const enableSSR = Boolean(modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
|
|
23
24
|
if (!enableSSR) {
|
|
24
25
|
return {};
|
|
@@ -35,7 +36,9 @@ const moduleFederationSSRPlugin = (userConfig) => ({
|
|
|
35
36
|
plugins.push({
|
|
36
37
|
name: "mfSSR",
|
|
37
38
|
path: "@module-federation/modern-js/ssr-runtime",
|
|
38
|
-
config: {
|
|
39
|
+
config: {
|
|
40
|
+
metaName
|
|
41
|
+
}
|
|
39
42
|
});
|
|
40
43
|
return {
|
|
41
44
|
entrypoint,
|
|
@@ -48,7 +48,9 @@ const replaceRemoteUrl = (mfConfig, remoteIpStrategy) => {
|
|
|
48
48
|
if (typeof remoteObject === "string") {
|
|
49
49
|
return;
|
|
50
50
|
}
|
|
51
|
-
|
|
51
|
+
if ("entry" in remoteObject && typeof remoteObject.entry === "string") {
|
|
52
|
+
remoteObject.entry = remoteObject.entry.replace(LOCALHOST, ipv4);
|
|
53
|
+
}
|
|
52
54
|
});
|
|
53
55
|
} else if (typeof mfConfig.remotes !== "string") {
|
|
54
56
|
handleRemoteObject(mfConfig.remotes);
|
|
@@ -97,17 +99,18 @@ const patchMFConfig = (mfConfig, isServer, remoteIpStrategy) => {
|
|
|
97
99
|
const runtimePlugins = [
|
|
98
100
|
...mfConfig.runtimePlugins || []
|
|
99
101
|
];
|
|
102
|
+
var _mfConfig_shareStrategy;
|
|
103
|
+
mfConfig.shareStrategy = (_mfConfig_shareStrategy = mfConfig.shareStrategy) !== null && _mfConfig_shareStrategy !== void 0 ? _mfConfig_shareStrategy : "loaded-first";
|
|
100
104
|
patchDTSConfig(mfConfig, isServer);
|
|
101
|
-
injectRuntimePlugins(path.resolve(__dirname, "./mfRuntimePlugins/shared-strategy.js"), runtimePlugins);
|
|
102
105
|
if (isDev) {
|
|
103
106
|
injectRuntimePlugins(path.resolve(__dirname, "./mfRuntimePlugins/resolve-entry-ipv4.js"), runtimePlugins);
|
|
104
107
|
}
|
|
105
108
|
if (isServer) {
|
|
106
|
-
injectRuntimePlugins(
|
|
109
|
+
injectRuntimePlugins(path.resolve(__dirname, "./mfRuntimePlugins/node.js"), runtimePlugins);
|
|
107
110
|
if (isDev) {
|
|
108
111
|
injectRuntimePlugins(require.resolve("@module-federation/node/record-dynamic-remote-entry-hash-plugin"), runtimePlugins);
|
|
109
112
|
}
|
|
110
|
-
injectRuntimePlugins(path.resolve(__dirname, "./mfRuntimePlugins/
|
|
113
|
+
injectRuntimePlugins(path.resolve(__dirname, "./mfRuntimePlugins/node-fetch.js"), runtimePlugins);
|
|
111
114
|
if (!mfConfig.library) {
|
|
112
115
|
mfConfig.library = {
|
|
113
116
|
type: "commonjs-module",
|
|
@@ -292,5 +295,6 @@ export {
|
|
|
292
295
|
getMFConfig,
|
|
293
296
|
patchBundlerConfig,
|
|
294
297
|
patchIgnoreWarning,
|
|
295
|
-
patchMFConfig
|
|
298
|
+
patchMFConfig,
|
|
299
|
+
replaceRemoteUrl
|
|
296
300
|
};
|
|
@@ -36,10 +36,10 @@ describe("patchMFConfig", async () => {
|
|
|
36
36
|
remote: `http://${ipv4}:3000/remoteEntry.js`
|
|
37
37
|
},
|
|
38
38
|
remoteType: "script",
|
|
39
|
+
shareStrategy: "loaded-first",
|
|
39
40
|
runtimePlugins: [
|
|
40
|
-
path.resolve(__dirname, "./mfRuntimePlugins/
|
|
41
|
-
|
|
42
|
-
path.resolve(__dirname, "./mfRuntimePlugins/inject-node-fetch.js")
|
|
41
|
+
path.resolve(__dirname, "./mfRuntimePlugins/node.js"),
|
|
42
|
+
path.resolve(__dirname, "./mfRuntimePlugins/node-fetch.js")
|
|
43
43
|
],
|
|
44
44
|
shared: {
|
|
45
45
|
react: {
|
|
@@ -64,9 +64,8 @@ describe("patchMFConfig", async () => {
|
|
|
64
64
|
remote: `http://${ipv4}:3000/remoteEntry.js`
|
|
65
65
|
},
|
|
66
66
|
remoteType: "script",
|
|
67
|
-
runtimePlugins: [
|
|
68
|
-
|
|
69
|
-
],
|
|
67
|
+
runtimePlugins: [],
|
|
68
|
+
shareStrategy: "loaded-first",
|
|
70
69
|
shared: {
|
|
71
70
|
react: {
|
|
72
71
|
eager: true,
|
|
@@ -1,8 +1,30 @@
|
|
|
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";
|
|
14
|
+
const ROUTE_ID = "ROUTE_ID";
|
|
4
15
|
export {
|
|
16
|
+
DEFAULT_ENTRY,
|
|
17
|
+
DEFAULT_LAYOUT,
|
|
18
|
+
FOLD_IDENTIFIER,
|
|
5
19
|
LOCALHOST,
|
|
20
|
+
META_NAME,
|
|
21
|
+
MF_JSON,
|
|
22
|
+
MF_ROUTES,
|
|
23
|
+
MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME,
|
|
24
|
+
MODERN_JS_ROUTE_SERVER_LOADER,
|
|
6
25
|
MODERN_JS_SERVER_DIR,
|
|
7
|
-
PLUGIN_IDENTIFIER
|
|
26
|
+
PLUGIN_IDENTIFIER,
|
|
27
|
+
ROUTE_ID,
|
|
28
|
+
SEPARATOR,
|
|
29
|
+
isDev
|
|
8
30
|
};
|
|
File without changes
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { useContext } from "react";
|
|
3
|
+
import { getInstance } from "@module-federation/enhanced/runtime";
|
|
4
|
+
import { collectSSRAssets } from "../createRemoteSSRComponent";
|
|
5
|
+
import { RuntimeReactContext } from "@meta/runtime";
|
|
6
|
+
import { decodeId, getRemoteId } from "./utils";
|
|
7
|
+
function traverseRoutes(routes, remoteNames) {
|
|
8
|
+
routes.forEach((route) => {
|
|
9
|
+
route.id && remoteNames.add(decodeId(route.id));
|
|
10
|
+
route.children && traverseRoutes(route.children, remoteNames);
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
const ssrDataLoaderInjectAssetsPlugin = ({ metaName }) => {
|
|
14
|
+
return {
|
|
15
|
+
name: "@modern-js/plugin-mf-routes-inject-assets",
|
|
16
|
+
pre: [
|
|
17
|
+
"@modern-js/plugin-mf-routes"
|
|
18
|
+
],
|
|
19
|
+
post: [
|
|
20
|
+
"@module-federation/modern-js",
|
|
21
|
+
`@${metaName}/plugin-router`
|
|
22
|
+
],
|
|
23
|
+
setup: () => {
|
|
24
|
+
return {
|
|
25
|
+
wrapRoot() {
|
|
26
|
+
const AppWrapper = (props) => {
|
|
27
|
+
const instance = getInstance();
|
|
28
|
+
if (!instance || !instance.options.remotes.length) {
|
|
29
|
+
return /* @__PURE__ */ _jsx(_Fragment, {
|
|
30
|
+
children: props.children
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
const context = useContext(RuntimeReactContext);
|
|
34
|
+
const { remotes } = instance.options;
|
|
35
|
+
const remoteNames = /* @__PURE__ */ new Set();
|
|
36
|
+
traverseRoutes(context.routes, remoteNames);
|
|
37
|
+
const remoteIds = [
|
|
38
|
+
...remoteNames
|
|
39
|
+
].reduce((sum, cur) => {
|
|
40
|
+
const matchRemote = remotes.find((r) => r.name === cur);
|
|
41
|
+
if (matchRemote) {
|
|
42
|
+
sum.add(getRemoteId(cur));
|
|
43
|
+
}
|
|
44
|
+
return sum;
|
|
45
|
+
}, /* @__PURE__ */ new Set());
|
|
46
|
+
const assets = [];
|
|
47
|
+
remoteIds.forEach((remoteId) => {
|
|
48
|
+
assets.push(...collectSSRAssets({
|
|
49
|
+
id: remoteId,
|
|
50
|
+
injectScript: false
|
|
51
|
+
}));
|
|
52
|
+
});
|
|
53
|
+
return /* @__PURE__ */ _jsxs(_Fragment, {
|
|
54
|
+
children: [
|
|
55
|
+
assets,
|
|
56
|
+
props.children
|
|
57
|
+
]
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
return AppWrapper;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
export {
|
|
67
|
+
ssrDataLoaderInjectAssetsPlugin
|
|
68
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { getInstance } from "@module-federation/enhanced/runtime";
|
|
2
|
+
import { MF_ROUTES, DEFAULT_ENTRY } from "../../constant";
|
|
3
|
+
import { injectRemoteRoutes } from "./utils";
|
|
4
|
+
const ssrDataLoaderPlugin = ({ metaName, dataLoaderRemotes }) => {
|
|
5
|
+
return {
|
|
6
|
+
name: "@modern-js/plugin-mf-routes",
|
|
7
|
+
post: [
|
|
8
|
+
`@${metaName}/plugin-router`,
|
|
9
|
+
"@module-federation/modern-js"
|
|
10
|
+
],
|
|
11
|
+
setup: () => {
|
|
12
|
+
const remoteRoutesMap = {};
|
|
13
|
+
return {
|
|
14
|
+
async beforeRender() {
|
|
15
|
+
if (Object.keys(remoteRoutesMap).length) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const instance = getInstance();
|
|
19
|
+
if (!instance) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (!instance.options.remotes.length) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
await Promise.all(instance.options.remotes.map(async (remote) => {
|
|
26
|
+
if (!dataLoaderRemotes.includes(remote.name)) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const remoteId = `${remote.name}/${DEFAULT_ENTRY}/${MF_ROUTES}`;
|
|
30
|
+
const { routes } = await instance.loadRemote(remoteId);
|
|
31
|
+
remoteRoutesMap[remote.name] = {
|
|
32
|
+
routes
|
|
33
|
+
};
|
|
34
|
+
}));
|
|
35
|
+
},
|
|
36
|
+
// runtime plugin not save route , so it needs to inject again when trigger
|
|
37
|
+
modifyRoutes: (routes) => {
|
|
38
|
+
injectRemoteRoutes({
|
|
39
|
+
entry: {
|
|
40
|
+
routes
|
|
41
|
+
}
|
|
42
|
+
}, remoteRoutesMap);
|
|
43
|
+
return routes;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
export {
|
|
50
|
+
ssrDataLoaderPlugin
|
|
51
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { encodeName, decodeName } from "@module-federation/sdk";
|
|
2
|
+
import { DEFAULT_ENTRY, DEFAULT_LAYOUT, MF_ROUTES } from "../../constant";
|
|
3
|
+
const SPLIT_SYMBOL = "@";
|
|
4
|
+
function transformName2Prefix(name) {
|
|
5
|
+
return `${encodeName(name, "", false)}${SPLIT_SYMBOL}`;
|
|
6
|
+
}
|
|
7
|
+
function decodeId(prefix) {
|
|
8
|
+
const realPrefix = prefix.split(SPLIT_SYMBOL)[0];
|
|
9
|
+
return decodeName(realPrefix, "", false);
|
|
10
|
+
}
|
|
11
|
+
function getRemoteRoutesInfos(route, remoteRoutesMap) {
|
|
12
|
+
if (!route.id) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const remoteName = decodeId(route.id);
|
|
16
|
+
const remoteRouteObj = remoteRoutesMap[remoteName];
|
|
17
|
+
if (!remoteRouteObj) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const remoteRoutes = remoteRouteObj.routes[0];
|
|
21
|
+
return {
|
|
22
|
+
routes: remoteRoutes,
|
|
23
|
+
name: remoteName,
|
|
24
|
+
pathName: route.path
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function getRemoteLayoutId(remoteName) {
|
|
28
|
+
const prefix = transformName2Prefix(remoteName);
|
|
29
|
+
return `${prefix}${DEFAULT_LAYOUT}`;
|
|
30
|
+
}
|
|
31
|
+
function getRemoteId(remoteName) {
|
|
32
|
+
return `${remoteName}/${DEFAULT_ENTRY}/${MF_ROUTES}`;
|
|
33
|
+
}
|
|
34
|
+
function injectRemoteRoutes(initialRoutes, remoteRoutesMap) {
|
|
35
|
+
const traverse = (route) => {
|
|
36
|
+
var _route_children;
|
|
37
|
+
const remoteRoutesInfos = getRemoteRoutesInfos(route, remoteRoutesMap);
|
|
38
|
+
if (remoteRoutesInfos) {
|
|
39
|
+
const { routes, pathName } = remoteRoutesInfos;
|
|
40
|
+
route.id = routes.id;
|
|
41
|
+
route.path = pathName;
|
|
42
|
+
if (routes.loader) {
|
|
43
|
+
route.loader = routes.loader;
|
|
44
|
+
}
|
|
45
|
+
if (routes.children) {
|
|
46
|
+
route.children = routes.children;
|
|
47
|
+
}
|
|
48
|
+
if (routes.Component) {
|
|
49
|
+
route.Component = routes.Component;
|
|
50
|
+
if (route.element) {
|
|
51
|
+
delete route.element;
|
|
52
|
+
}
|
|
53
|
+
} else if (routes.element) {
|
|
54
|
+
route.element = routes.element;
|
|
55
|
+
if (route.Component) {
|
|
56
|
+
delete route.Component;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
(_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.forEach((r) => {
|
|
62
|
+
traverse(r);
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
Object.keys(initialRoutes).forEach((entryName) => {
|
|
66
|
+
const routes = initialRoutes[entryName];
|
|
67
|
+
if (Array.isArray(routes)) {
|
|
68
|
+
routes.forEach((route) => {
|
|
69
|
+
traverse(route);
|
|
70
|
+
});
|
|
71
|
+
} else {
|
|
72
|
+
var _routes_routes;
|
|
73
|
+
routes === null || routes === void 0 ? void 0 : (_routes_routes = routes.routes) === null || _routes_routes === void 0 ? void 0 : _routes_routes.forEach((r) => {
|
|
74
|
+
traverse(r);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
export {
|
|
80
|
+
decodeId,
|
|
81
|
+
getRemoteId,
|
|
82
|
+
getRemoteLayoutId,
|
|
83
|
+
getRemoteRoutesInfos,
|
|
84
|
+
injectRemoteRoutes,
|
|
85
|
+
transformName2Prefix
|
|
86
|
+
};
|
|
@@ -1,15 +1,16 @@
|
|
|
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
|
-
const mfSSRPlugin = () => ({
|
|
4
|
+
const mfSSRPlugin = ({ metaName }) => ({
|
|
5
5
|
name: "@module-federation/modern-js",
|
|
6
|
+
pre: [
|
|
7
|
+
`@${metaName}/plugin-router`
|
|
8
|
+
],
|
|
6
9
|
setup: () => {
|
|
7
10
|
return {
|
|
8
|
-
async
|
|
11
|
+
async beforeRender() {
|
|
9
12
|
if (typeof window !== "undefined") {
|
|
10
|
-
return
|
|
11
|
-
context
|
|
12
|
-
});
|
|
13
|
+
return;
|
|
13
14
|
}
|
|
14
15
|
globalThis.shouldUpdate = false;
|
|
15
16
|
const nodeUtils = await import("@module-federation/node/utils");
|
|
@@ -19,9 +20,7 @@ const mfSSRPlugin = () => ({
|
|
|
19
20
|
await nodeUtils.flushChunks();
|
|
20
21
|
globalThis.shouldUpdate = true;
|
|
21
22
|
}
|
|
22
|
-
return
|
|
23
|
-
context
|
|
24
|
-
});
|
|
23
|
+
return;
|
|
25
24
|
},
|
|
26
25
|
wrapRoot(App) {
|
|
27
26
|
const AppWrapper = (props) => /* @__PURE__ */ _jsxs(_Fragment, {
|
|
File without changes
|
|
File without changes
|
|
@@ -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 @@
|
|
|
1
|
+
export declare function clearMFCache(): void;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { CliPlugin, AppTools } from '@modern-js/app-tools';
|
|
2
|
+
import type { InternalRoutesPluginOptions } from '../../types/routes';
|
|
3
|
+
export declare const moduleFederationExportRoutePlugin: ({ userConfig, internalOptions, entries, }: InternalRoutesPluginOptions) => CliPlugin<AppTools>;
|
|
4
|
+
export default moduleFederationExportRoutePlugin;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { CliPlugin, AppTools } from '@modern-js/app-tools';
|
|
2
|
+
import type { InternalRoutesPluginOptions } from '../../types/routes';
|
|
3
|
+
export declare const moduleFederationImportRoutePlugin: ({ userConfig, internalOptions, remotePathMap, serverPlugin, ssrByRouteIdsMap, transformRuntimeOptions, }: InternalRoutesPluginOptions) => CliPlugin<AppTools>;
|
|
4
|
+
export default moduleFederationImportRoutePlugin;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { CliPlugin, AppTools } from '@modern-js/app-tools';
|
|
2
|
+
import type { RoutesPluginOptions } from '../../types/routes';
|
|
3
|
+
export declare const moduleFederationRoutesPlugin: ({ userConfig, internalOptions, serverPlugin, transformRuntimeOptions, }: RoutesPluginOptions) => CliPlugin<AppTools>;
|
|
4
|
+
export default moduleFederationRoutesPlugin;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare function calcPathName(filePath: string, rootDir: string, entry: string): string;
|
|
2
|
+
export declare function traverseRouteFiles(options: {
|
|
3
|
+
appDirectory: string;
|
|
4
|
+
remotePathMap: Record<string, {
|
|
5
|
+
name: string;
|
|
6
|
+
path: string;
|
|
7
|
+
}>;
|
|
8
|
+
generateRouteFile?: boolean;
|
|
9
|
+
}): Promise<{
|
|
10
|
+
entries: string[];
|
|
11
|
+
}>;
|
|
@@ -0,0 +1,11 @@
|
|
|
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 addExpose(options: PatchMFConfigOptions): void;
|
|
10
|
+
export declare function addShared(options: PatchMFConfigOptions): void;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
dataLoaderRemotes: string[];
|
|
10
|
+
}): Promise<RemoteRoutesMap>;
|
|
11
|
+
export declare function getSSRByRouteIds(dataLoaderRemotes: string[], remoteRoutesMap: RemoteRoutesMap): undefined | string[];
|
|
@@ -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 FetchRouteServerPluginOptions = {
|
|
5
|
+
runtimeOptions: MFRuntimeOptions;
|
|
6
|
+
dataLoaderRemotes: string[];
|
|
7
|
+
ssrByRouteIdsMap: Record<string, string>;
|
|
8
|
+
};
|
|
9
|
+
declare const _default: ({ runtimeOptions, dataLoaderRemotes, ssrByRouteIdsMap, }: FetchRouteServerPluginOptions) => ServerPlugin;
|
|
10
|
+
export default _default;
|
|
@@ -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,14 @@
|
|
|
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";
|
|
14
|
+
export declare const ROUTE_ID = "ROUTE_ID";
|