@module-federation/modern-js 0.0.0-next-20240911095451 → 0.0.0-next-20240912070754
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 +140 -0
- package/dist/cjs/cli/dataLoader/plugin.js +199 -0
- package/dist/cjs/cli/index.js +5 -1
- 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/server/dataLoaderPlugin.js +82 -0
- package/dist/cjs/cli/server/fetchRemoteFullRoutesMap.js +69 -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 +32 -2
- package/dist/cjs/interfaces/route.js +16 -0
- package/dist/cjs/runtime/dataLoader/plugin-inject-assets.js +87 -0
- package/dist/cjs/runtime/dataLoader/plugin.js +73 -0
- package/dist/cjs/runtime/dataLoader/utils.js +106 -0
- package/dist/cjs/runtime/index.js +4 -1
- package/dist/cjs/{cli/mfRuntimePlugins/shared-strategy.js → runtime/withMFRouteId.js} +11 -23
- package/dist/cjs/ssr-runtime/plugin.js +8 -8
- 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 +106 -0
- package/dist/esm/cli/dataLoader/plugin.js +220 -0
- package/dist/esm/cli/index.js +6 -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/server/dataLoaderPlugin.js +105 -0
- package/dist/esm/cli/server/fetchRemoteFullRoutesMap.js +88 -0
- package/dist/esm/cli/ssrPlugin.js +6 -3
- package/dist/esm/cli/utils.js +13 -8
- package/dist/esm/cli/utils.spec.js +5 -6
- package/dist/esm/constant.js +21 -1
- package/dist/esm/interfaces/route.js +0 -0
- package/dist/esm/runtime/dataLoader/plugin-inject-assets.js +67 -0
- package/dist/esm/runtime/dataLoader/plugin.js +93 -0
- package/dist/esm/runtime/dataLoader/utils.js +77 -0
- package/dist/esm/runtime/index.js +3 -1
- package/dist/esm/runtime/withMFRouteId.js +7 -0
- package/dist/esm/ssr-runtime/plugin.js +9 -11
- 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 +106 -0
- package/dist/esm-node/cli/dataLoader/plugin.js +164 -0
- package/dist/esm-node/cli/index.js +5 -1
- 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/server/dataLoaderPlugin.js +62 -0
- package/dist/esm-node/cli/server/fetchRemoteFullRoutesMap.js +44 -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 +21 -1
- package/dist/esm-node/interfaces/route.js +0 -0
- package/dist/esm-node/runtime/dataLoader/plugin-inject-assets.js +63 -0
- package/dist/esm-node/runtime/dataLoader/plugin.js +49 -0
- package/dist/esm-node/runtime/dataLoader/utils.js +77 -0
- package/dist/esm-node/runtime/index.js +3 -1
- package/dist/esm-node/runtime/withMFRouteId.js +7 -0
- package/dist/esm-node/ssr-runtime/plugin.js +8 -8
- 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/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-inject-assets.d.ts +4 -0
- package/dist/types/runtime/dataLoader/plugin.d.ts +4 -0
- package/dist/types/runtime/dataLoader/utils.d.ts +22 -0
- package/dist/types/runtime/index.d.ts +1 -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 +9 -0
- package/package.json +48 -11
- 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,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, META_NAME } from "../constant";
|
|
8
8
|
function setEnv() {
|
|
9
9
|
process.env["MF_DISABLE_EMIT_STATS"] = "true";
|
|
10
10
|
process.env["MF_SSR_PRJ"] = "true";
|
|
@@ -18,6 +18,7 @@ const moduleFederationSSRPlugin = (userConfig) => ({
|
|
|
18
18
|
setup: async ({ useConfigContext, useAppContext }) => {
|
|
19
19
|
var _modernjsConfig_server;
|
|
20
20
|
const modernjsConfig = useConfigContext();
|
|
21
|
+
const { metaName = META_NAME } = userConfig;
|
|
21
22
|
const enableSSR = Boolean(modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
|
|
22
23
|
if (!enableSSR) {
|
|
23
24
|
return {};
|
|
@@ -34,7 +35,9 @@ const moduleFederationSSRPlugin = (userConfig) => ({
|
|
|
34
35
|
plugins.push({
|
|
35
36
|
name: "mfSSR",
|
|
36
37
|
path: "@module-federation/modern-js/ssr-runtime",
|
|
37
|
-
config: {
|
|
38
|
+
config: {
|
|
39
|
+
metaName
|
|
40
|
+
}
|
|
38
41
|
});
|
|
39
42
|
return {
|
|
40
43
|
entrypoint,
|
|
@@ -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);
|
|
@@ -96,17 +98,18 @@ const patchMFConfig = (mfConfig, isServer, remoteIpStrategy) => {
|
|
|
96
98
|
const runtimePlugins = [
|
|
97
99
|
...mfConfig.runtimePlugins || []
|
|
98
100
|
];
|
|
101
|
+
var _mfConfig_shareStrategy;
|
|
102
|
+
mfConfig.shareStrategy = (_mfConfig_shareStrategy = mfConfig.shareStrategy) !== null && _mfConfig_shareStrategy !== void 0 ? _mfConfig_shareStrategy : "loaded-first";
|
|
99
103
|
patchDTSConfig(mfConfig, isServer);
|
|
100
|
-
injectRuntimePlugins(path.resolve(__dirname, "./mfRuntimePlugins/shared-strategy.js"), runtimePlugins);
|
|
101
104
|
if (isDev) {
|
|
102
105
|
injectRuntimePlugins(path.resolve(__dirname, "./mfRuntimePlugins/resolve-entry-ipv4.js"), runtimePlugins);
|
|
103
106
|
}
|
|
104
107
|
if (isServer) {
|
|
105
|
-
injectRuntimePlugins(
|
|
108
|
+
injectRuntimePlugins(path.resolve(__dirname, "./mfRuntimePlugins/node.js"), runtimePlugins);
|
|
106
109
|
if (isDev) {
|
|
107
110
|
injectRuntimePlugins(require.resolve("@module-federation/node/record-dynamic-remote-entry-hash-plugin"), runtimePlugins);
|
|
108
111
|
}
|
|
109
|
-
injectRuntimePlugins(path.resolve(__dirname, "./mfRuntimePlugins/
|
|
112
|
+
injectRuntimePlugins(path.resolve(__dirname, "./mfRuntimePlugins/node-fetch.js"), runtimePlugins);
|
|
110
113
|
if (!mfConfig.library) {
|
|
111
114
|
mfConfig.library = {
|
|
112
115
|
type: "commonjs-module",
|
|
@@ -291,5 +294,6 @@ export {
|
|
|
291
294
|
getMFConfig,
|
|
292
295
|
patchBundlerConfig,
|
|
293
296
|
patchIgnoreWarning,
|
|
294
|
-
patchMFConfig
|
|
297
|
+
patchMFConfig,
|
|
298
|
+
replaceRemoteUrl
|
|
295
299
|
};
|
|
@@ -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,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,63 @@
|
|
|
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
|
+
const ssrDataLoaderInjectAssetsPlugin = ({ metaName }) => {
|
|
8
|
+
return {
|
|
9
|
+
name: "@modern-js/plugin-mf-data-loader-inject-assets",
|
|
10
|
+
pre: [
|
|
11
|
+
`@${metaName}/plugin-router`,
|
|
12
|
+
"@modern-js/plugin-mf-data-loader"
|
|
13
|
+
],
|
|
14
|
+
setup: () => {
|
|
15
|
+
return {
|
|
16
|
+
wrapRoot(App) {
|
|
17
|
+
const AppWrapper = (props) => {
|
|
18
|
+
var _context_routerContext;
|
|
19
|
+
const instance = getInstance();
|
|
20
|
+
if (!instance || !instance.options.remotes.length) {
|
|
21
|
+
console.log("plugin-mf-data-loader-inject-assets no instance!");
|
|
22
|
+
return /* @__PURE__ */ _jsx(_Fragment, {
|
|
23
|
+
children: /* @__PURE__ */ _jsx(App, {
|
|
24
|
+
...props
|
|
25
|
+
})
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
const context = useContext(RuntimeReactContext);
|
|
29
|
+
const { remotes } = instance.options;
|
|
30
|
+
const remoteNames = ((_context_routerContext = context.routerContext) === null || _context_routerContext === void 0 ? void 0 : _context_routerContext.matches.map((match) => decodeId(match.route.id))) || [];
|
|
31
|
+
const remoteIds = remoteNames.reduce((sum, cur) => {
|
|
32
|
+
const matchRemote = remotes.find((r) => r.name === cur);
|
|
33
|
+
if (matchRemote) {
|
|
34
|
+
sum.add(getRemoteId(cur));
|
|
35
|
+
}
|
|
36
|
+
return sum;
|
|
37
|
+
}, /* @__PURE__ */ new Set());
|
|
38
|
+
const assets = [];
|
|
39
|
+
remoteIds.forEach((remoteId) => {
|
|
40
|
+
assets.push(...collectSSRAssets({
|
|
41
|
+
id: remoteId,
|
|
42
|
+
injectScript: false
|
|
43
|
+
}));
|
|
44
|
+
});
|
|
45
|
+
console.log("plugin-mf-data-loader-inject-assets has assets: ", assets);
|
|
46
|
+
return /* @__PURE__ */ _jsxs(_Fragment, {
|
|
47
|
+
children: [
|
|
48
|
+
assets,
|
|
49
|
+
/* @__PURE__ */ _jsx(App, {
|
|
50
|
+
...props
|
|
51
|
+
})
|
|
52
|
+
]
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
return AppWrapper;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
export {
|
|
62
|
+
ssrDataLoaderInjectAssetsPlugin
|
|
63
|
+
};
|
|
@@ -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 = ({ metaName }) => {
|
|
5
|
+
return {
|
|
6
|
+
name: "@modern-js/plugin-mf-data-loader",
|
|
7
|
+
post: [
|
|
8
|
+
`@${metaName}/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,77 @@
|
|
|
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
|
+
route.element = routes.element;
|
|
49
|
+
route.Component = routes.Component;
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
(_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.forEach((r) => {
|
|
53
|
+
traverse(r);
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
Object.keys(initialRoutes).forEach((entryName) => {
|
|
57
|
+
const routes = initialRoutes[entryName];
|
|
58
|
+
if (Array.isArray(routes)) {
|
|
59
|
+
routes.forEach((route) => {
|
|
60
|
+
traverse(route);
|
|
61
|
+
});
|
|
62
|
+
} else {
|
|
63
|
+
var _routes_routes;
|
|
64
|
+
routes === null || routes === void 0 ? void 0 : (_routes_routes = routes.routes) === null || _routes_routes === void 0 ? void 0 : _routes_routes.forEach((r) => {
|
|
65
|
+
traverse(r);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
export {
|
|
71
|
+
decodeId,
|
|
72
|
+
getRemoteId,
|
|
73
|
+
getRemoteLayoutId,
|
|
74
|
+
getRemoteRoutesInfos,
|
|
75
|
+
injectRemoteRoutes,
|
|
76
|
+
transformName2Prefix
|
|
77
|
+
};
|
|
@@ -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
|
-
|
|
4
|
+
console.log("mfSSRPlugin trigger");
|
|
5
|
+
const mfSSRPlugin = ({ metaName }) => ({
|
|
5
6
|
name: "@module-federation/modern-js",
|
|
7
|
+
pre: [
|
|
8
|
+
`@${metaName}/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 };
|
|
@@ -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,22 @@
|
|
|
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 transformName2Prefix(name: string): string;
|
|
14
|
+
export declare function decodeId(prefix: string): string;
|
|
15
|
+
export declare function getRemoteRoutesInfos(route: RouteObject, remoteRoutesMap: RemoteRoutesMap): {
|
|
16
|
+
routes: RouteObject;
|
|
17
|
+
name: string;
|
|
18
|
+
pathName: string | undefined;
|
|
19
|
+
} | undefined;
|
|
20
|
+
export declare function getRemoteLayoutId(remoteName: string): string;
|
|
21
|
+
export declare function getRemoteId(remoteName: string): string;
|
|
22
|
+
export declare function injectRemoteRoutes(initialRoutes: LoaderBundlesRoutes | NestedRoutes, remoteRoutesMap: RemoteRoutesMap): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function withMFRouteId(id: string): string;
|