@module-federation/modern-js 0.0.0-next-20240913081127 → 0.0.0-next-20240913095223
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/clearMFCache.js +57 -0
- 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 +204 -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 +84 -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 +94 -0
- package/dist/cjs/runtime/dataLoader/plugin.js +74 -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 +9 -22
- 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/clearMFCache.js +34 -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 +232 -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 +107 -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 +73 -0
- package/dist/esm/runtime/dataLoader/plugin.js +94 -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 +10 -26
- 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/clearMFCache.js +33 -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 +169 -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 +64 -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 +70 -0
- package/dist/esm-node/runtime/dataLoader/plugin.js +50 -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 +9 -22
- 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/clearMFCache.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
|
@@ -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,64 @@
|
|
|
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
|
+
console.log("server plugin ", isHandled, loaderBundles);
|
|
26
|
+
if (isHandled && !globalThis.FORCE_MF_REFRESH) {
|
|
27
|
+
await next();
|
|
28
|
+
} else {
|
|
29
|
+
var _api_useConfigContext_server;
|
|
30
|
+
const instance = init({
|
|
31
|
+
name,
|
|
32
|
+
remotes
|
|
33
|
+
});
|
|
34
|
+
const remoteFullRoutesMap = await fetchRemoteFullRoutesMap({
|
|
35
|
+
instance,
|
|
36
|
+
remotePath: isBrowserEnv() ? `${DEFAULT_ENTRY}/${MF_ROUTES}` : `${DEFAULT_ENTRY}/${MODERN_JS_ROUTE_SERVER_LOADER}`
|
|
37
|
+
});
|
|
38
|
+
const originalSSRByRouteIds = (_api_useConfigContext_server = api.useConfigContext().server) === null || _api_useConfigContext_server === void 0 ? void 0 : _api_useConfigContext_server.ssrByRouteIds;
|
|
39
|
+
if (originalSSRByRouteIds && partialSSRRemotes.length) {
|
|
40
|
+
const remoteSSRByRouteIds = getSSRByRouteIds(partialSSRRemotes, remoteFullRoutesMap) || [];
|
|
41
|
+
const ssrByRouteIds = /* @__PURE__ */ new Set([
|
|
42
|
+
...originalSSRByRouteIds,
|
|
43
|
+
...remoteSSRByRouteIds.map((id) => ssrByRouteIdsMap[id] || id)
|
|
44
|
+
]);
|
|
45
|
+
api.useConfigContext().server.ssrByRouteIds = Array.from(ssrByRouteIds);
|
|
46
|
+
}
|
|
47
|
+
injectRemoteRoutes(loaderBundles, remoteFullRoutesMap);
|
|
48
|
+
injectRemoteRoutes(nestedRoutesJson, remoteFullRoutesMap);
|
|
49
|
+
isHandled = true;
|
|
50
|
+
globalThis.FORCE_MF_REFRESH = false;
|
|
51
|
+
await next();
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
before: [
|
|
55
|
+
"render"
|
|
56
|
+
]
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
export {
|
|
63
|
+
dataLoaderPlugin_default as default
|
|
64
|
+
};
|
|
@@ -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,70 @@
|
|
|
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-data-loader-inject-assets",
|
|
16
|
+
pre: [
|
|
17
|
+
"@modern-js/plugin-mf-data-loader"
|
|
18
|
+
],
|
|
19
|
+
post: [
|
|
20
|
+
"@module-federation/modern-js",
|
|
21
|
+
`@${metaName}/plugin-router`
|
|
22
|
+
],
|
|
23
|
+
setup: () => {
|
|
24
|
+
return {
|
|
25
|
+
wrapRoot(App) {
|
|
26
|
+
const AppWrapper = (props) => {
|
|
27
|
+
const instance = getInstance();
|
|
28
|
+
if (!instance || !instance.options.remotes.length) {
|
|
29
|
+
console.log("plugin-mf-data-loader-inject-assets no instance!");
|
|
30
|
+
return /* @__PURE__ */ _jsx(_Fragment, {
|
|
31
|
+
children: props.children
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
const context = useContext(RuntimeReactContext);
|
|
35
|
+
const { remotes } = instance.options;
|
|
36
|
+
const remoteNames = /* @__PURE__ */ new Set();
|
|
37
|
+
traverseRoutes(context.routes, remoteNames);
|
|
38
|
+
const remoteIds = [
|
|
39
|
+
...remoteNames
|
|
40
|
+
].reduce((sum, cur) => {
|
|
41
|
+
const matchRemote = remotes.find((r) => r.name === cur);
|
|
42
|
+
if (matchRemote) {
|
|
43
|
+
sum.add(getRemoteId(cur));
|
|
44
|
+
}
|
|
45
|
+
return sum;
|
|
46
|
+
}, /* @__PURE__ */ new Set());
|
|
47
|
+
const assets = [];
|
|
48
|
+
remoteIds.forEach((remoteId) => {
|
|
49
|
+
assets.push(...collectSSRAssets({
|
|
50
|
+
id: remoteId,
|
|
51
|
+
injectScript: false
|
|
52
|
+
}));
|
|
53
|
+
});
|
|
54
|
+
console.log("plugin-mf-data-loader-inject-assets has assets: ", assets);
|
|
55
|
+
return /* @__PURE__ */ _jsxs(_Fragment, {
|
|
56
|
+
children: [
|
|
57
|
+
assets,
|
|
58
|
+
props.children
|
|
59
|
+
]
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
return AppWrapper;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
export {
|
|
69
|
+
ssrDataLoaderInjectAssetsPlugin
|
|
70
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
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
|
+
"@module-federation/modern-js"
|
|
10
|
+
],
|
|
11
|
+
setup: () => {
|
|
12
|
+
const remoteRoutesMap = {};
|
|
13
|
+
return {
|
|
14
|
+
async beforeRender() {
|
|
15
|
+
console.log("init");
|
|
16
|
+
if (Object.keys(remoteRoutesMap).length) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const instance = getInstance();
|
|
20
|
+
if (!instance) {
|
|
21
|
+
console.log("no instance!");
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (!instance.options.remotes.length) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
await Promise.all(instance.options.remotes.map(async (remote) => {
|
|
28
|
+
const remoteId = `${remote.name}/${DEFAULT_ENTRY}/${MF_ROUTES}`;
|
|
29
|
+
const { routes } = await instance.loadRemote(remoteId);
|
|
30
|
+
remoteRoutesMap[remote.name] = {
|
|
31
|
+
routes
|
|
32
|
+
};
|
|
33
|
+
}));
|
|
34
|
+
},
|
|
35
|
+
// runtime plugin not save route , so it needs to inject again when trigger
|
|
36
|
+
modifyRoutes: (routes) => {
|
|
37
|
+
injectRemoteRoutes({
|
|
38
|
+
entry: {
|
|
39
|
+
routes
|
|
40
|
+
}
|
|
41
|
+
}, remoteRoutesMap);
|
|
42
|
+
return routes;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
export {
|
|
49
|
+
ssrDataLoaderPlugin
|
|
50
|
+
};
|
|
@@ -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,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { SSRLiveReload } from "./SSRLiveReload";
|
|
4
|
-
const mfSSRPlugin = () => ({
|
|
1
|
+
console.log("mfSSRPlugin trigger");
|
|
2
|
+
const mfSSRPlugin = ({ metaName }) => ({
|
|
5
3
|
name: "@module-federation/modern-js",
|
|
4
|
+
pre: [
|
|
5
|
+
`@${metaName}/plugin-router`
|
|
6
|
+
],
|
|
6
7
|
setup: () => {
|
|
7
8
|
return {
|
|
8
|
-
async
|
|
9
|
+
async beforeRender() {
|
|
10
|
+
console.log("live reload beforeRender");
|
|
9
11
|
if (typeof window !== "undefined") {
|
|
10
|
-
return
|
|
11
|
-
context
|
|
12
|
-
});
|
|
12
|
+
return;
|
|
13
13
|
}
|
|
14
14
|
globalThis.shouldUpdate = false;
|
|
15
15
|
const nodeUtils = await import("@module-federation/node/utils");
|
|
@@ -19,20 +19,7 @@ const mfSSRPlugin = () => ({
|
|
|
19
19
|
await nodeUtils.flushChunks();
|
|
20
20
|
globalThis.shouldUpdate = true;
|
|
21
21
|
}
|
|
22
|
-
return
|
|
23
|
-
context
|
|
24
|
-
});
|
|
25
|
-
},
|
|
26
|
-
wrapRoot(App) {
|
|
27
|
-
const AppWrapper = (props) => /* @__PURE__ */ _jsxs(_Fragment, {
|
|
28
|
-
children: [
|
|
29
|
-
/* @__PURE__ */ _jsx(SSRLiveReload, {}),
|
|
30
|
-
/* @__PURE__ */ _jsx(App, {
|
|
31
|
-
...props
|
|
32
|
-
})
|
|
33
|
-
]
|
|
34
|
-
});
|
|
35
|
-
return hoistNonReactStatics(AppWrapper, App);
|
|
22
|
+
return;
|
|
36
23
|
}
|
|
37
24
|
};
|
|
38
25
|
}
|
|
@@ -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,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[];
|