@module-federation/modern-js 0.0.0-next-20240909083238 → 0.0.0-next-20240909101028
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/{runtime/withMFRouteId.js → cli/constant.js} +6 -9
- package/dist/cjs/cli/index.js +1 -6
- package/dist/cjs/cli/mfRuntimePlugins/inject-node-fetch.js +10 -7
- package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +38 -40
- package/dist/cjs/cli/ssrPlugin.js +1 -1
- package/dist/cjs/cli/utils.js +4 -8
- package/dist/cjs/cli/utils.spec.js +1 -1
- package/dist/cjs/constant.js +2 -32
- package/dist/cjs/runtime/index.js +1 -4
- package/dist/cjs/ssr-runtime/plugin.js +7 -7
- package/dist/esm/cli/constant.js +4 -0
- package/dist/esm/cli/index.js +1 -6
- package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js +10 -7
- package/dist/esm/cli/ssrPlugin.js +1 -1
- package/dist/esm/cli/utils.js +3 -6
- package/dist/esm/cli/utils.spec.js +1 -1
- package/dist/esm/constant.js +1 -21
- package/dist/esm/runtime/index.js +1 -3
- package/dist/esm/ssr-runtime/plugin.js +10 -7
- package/dist/esm-node/cli/constant.js +4 -0
- package/dist/esm-node/cli/index.js +1 -6
- package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.js +10 -7
- package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +38 -40
- package/dist/esm-node/cli/ssrPlugin.js +1 -1
- package/dist/esm-node/cli/utils.js +3 -6
- package/dist/esm-node/cli/utils.spec.js +1 -1
- package/dist/esm-node/constant.js +1 -21
- package/dist/esm-node/runtime/index.js +1 -3
- package/dist/esm-node/ssr-runtime/plugin.js +7 -7
- package/dist/types/cli/constant.d.ts +1 -0
- package/dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts +2 -2
- package/dist/types/cli/utils.d.ts +0 -2
- package/dist/types/constant.d.ts +0 -10
- package/dist/types/runtime/index.d.ts +0 -1
- package/dist/types/types/index.d.ts +0 -10
- package/package.json +11 -40
- package/dist/cjs/cli/dataLoader/ast/constant.js +0 -49
- package/dist/cjs/cli/dataLoader/ast/generateRoutes.js +0 -142
- package/dist/cjs/cli/dataLoader/ast/generateSelfRoutes.js +0 -48
- package/dist/cjs/cli/dataLoader/ast/generateSerializableRoutes.js +0 -90
- package/dist/cjs/cli/dataLoader/ast/generateSlimRoutes.js +0 -104
- package/dist/cjs/cli/dataLoader/ast/index.js +0 -37
- package/dist/cjs/cli/dataLoader/generateRouteFile.js +0 -143
- package/dist/cjs/cli/dataLoader/patchMFConfig.js +0 -137
- package/dist/cjs/cli/dataLoader/plugin.js +0 -205
- package/dist/cjs/cli/mfRuntimePlugins/node.js +0 -44
- package/dist/cjs/cli/server/dataLoaderPlugin.js +0 -82
- package/dist/cjs/cli/server/fetchRemoteFullRoutesMap.js +0 -68
- package/dist/cjs/interfaces/route.js +0 -16
- package/dist/cjs/runtime/dataLoader/plugin.js +0 -73
- package/dist/cjs/runtime/dataLoader/utils.js +0 -89
- package/dist/cjs/runtime/utils.js +0 -38
- package/dist/esm/cli/dataLoader/ast/constant.js +0 -18
- package/dist/esm/cli/dataLoader/ast/generateRoutes.js +0 -109
- package/dist/esm/cli/dataLoader/ast/generateSelfRoutes.js +0 -17
- package/dist/esm/cli/dataLoader/ast/generateSerializableRoutes.js +0 -57
- package/dist/esm/cli/dataLoader/ast/generateSlimRoutes.js +0 -73
- package/dist/esm/cli/dataLoader/ast/index.js +0 -10
- package/dist/esm/cli/dataLoader/generateRouteFile.js +0 -293
- package/dist/esm/cli/dataLoader/patchMFConfig.js +0 -103
- package/dist/esm/cli/dataLoader/plugin.js +0 -216
- package/dist/esm/cli/mfRuntimePlugins/node.js +0 -16
- package/dist/esm/cli/server/dataLoaderPlugin.js +0 -105
- package/dist/esm/cli/server/fetchRemoteFullRoutesMap.js +0 -86
- package/dist/esm/interfaces/route.js +0 -0
- package/dist/esm/runtime/dataLoader/plugin.js +0 -92
- package/dist/esm/runtime/dataLoader/utils.js +0 -63
- package/dist/esm/runtime/utils.js +0 -13
- package/dist/esm/runtime/withMFRouteId.js +0 -7
- package/dist/esm-node/cli/dataLoader/ast/constant.js +0 -18
- package/dist/esm-node/cli/dataLoader/ast/generateRoutes.js +0 -108
- package/dist/esm-node/cli/dataLoader/ast/generateSelfRoutes.js +0 -14
- package/dist/esm-node/cli/dataLoader/ast/generateSerializableRoutes.js +0 -56
- package/dist/esm-node/cli/dataLoader/ast/generateSlimRoutes.js +0 -70
- package/dist/esm-node/cli/dataLoader/ast/index.js +0 -10
- package/dist/esm-node/cli/dataLoader/generateRouteFile.js +0 -108
- package/dist/esm-node/cli/dataLoader/patchMFConfig.js +0 -103
- package/dist/esm-node/cli/dataLoader/plugin.js +0 -169
- package/dist/esm-node/cli/mfRuntimePlugins/node.js +0 -14
- package/dist/esm-node/cli/server/dataLoaderPlugin.js +0 -62
- package/dist/esm-node/cli/server/fetchRemoteFullRoutesMap.js +0 -43
- package/dist/esm-node/interfaces/route.js +0 -0
- package/dist/esm-node/runtime/dataLoader/plugin.js +0 -49
- package/dist/esm-node/runtime/dataLoader/utils.js +0 -63
- package/dist/esm-node/runtime/utils.js +0 -13
- package/dist/esm-node/runtime/withMFRouteId.js +0 -7
- package/dist/types/cli/dataLoader/ast/constant.d.ts +0 -8
- package/dist/types/cli/dataLoader/ast/generateRoutes.d.ts +0 -7
- package/dist/types/cli/dataLoader/ast/generateSelfRoutes.d.ts +0 -7
- package/dist/types/cli/dataLoader/ast/generateSerializableRoutes.d.ts +0 -5
- package/dist/types/cli/dataLoader/ast/generateSlimRoutes.d.ts +0 -7
- package/dist/types/cli/dataLoader/ast/index.d.ts +0 -4
- package/dist/types/cli/dataLoader/generateRouteFile.d.ts +0 -7
- package/dist/types/cli/dataLoader/patchMFConfig.d.ts +0 -11
- package/dist/types/cli/dataLoader/plugin.d.ts +0 -6
- package/dist/types/cli/mfRuntimePlugins/node.d.ts +0 -3
- package/dist/types/cli/server/dataLoaderPlugin.d.ts +0 -10
- package/dist/types/cli/server/fetchRemoteFullRoutesMap.d.ts +0 -10
- package/dist/types/interfaces/route.d.ts +0 -13
- package/dist/types/runtime/dataLoader/plugin.d.ts +0 -2
- package/dist/types/runtime/dataLoader/utils.d.ts +0 -19
- package/dist/types/runtime/utils.d.ts +0 -2
- package/dist/types/runtime/withMFRouteId.d.ts +0 -1
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import { fs, normalizeToPosixPath } from "@modern-js/utils";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import { MF_JSON } from "../../constant";
|
|
4
|
-
import { getRemoteLayoutId } from "../../runtime/dataLoader/utils";
|
|
5
|
-
const ROUTES = "routes";
|
|
6
|
-
function calcPathName(filePath, rootDir, entry) {
|
|
7
|
-
const relativePath = path.relative(rootDir, filePath);
|
|
8
|
-
const conditionalReg = /^\[(.*)\]$/;
|
|
9
|
-
const splitPaths = relativePath.split("/").reduce((sum, cur) => {
|
|
10
|
-
if (cur.includes(".")) {
|
|
11
|
-
const split = cur.split(".").map((i) => {
|
|
12
|
-
const match = i.match(conditionalReg);
|
|
13
|
-
if (match) {
|
|
14
|
-
return `:${match[1]}`;
|
|
15
|
-
}
|
|
16
|
-
return i.startsWith("__") ? "" : i;
|
|
17
|
-
});
|
|
18
|
-
sum.push(...split);
|
|
19
|
-
} else {
|
|
20
|
-
const match = cur.match(conditionalReg);
|
|
21
|
-
if (match) {
|
|
22
|
-
sum.push(`:${match[1]}`);
|
|
23
|
-
} else {
|
|
24
|
-
if (!cur.startsWith("__")) {
|
|
25
|
-
sum.push(cur);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return sum;
|
|
30
|
-
}, []);
|
|
31
|
-
return path.join(entry, splitPaths.filter((p) => !!p).join("/"));
|
|
32
|
-
}
|
|
33
|
-
async function generateRouteFile(options) {
|
|
34
|
-
const { appDirectory } = options;
|
|
35
|
-
const entries = [];
|
|
36
|
-
const remotePathMap = {};
|
|
37
|
-
const srcPath = `${appDirectory}/src`;
|
|
38
|
-
const traverse = async (filepath, rootDir, entry) => {
|
|
39
|
-
if (!await fs.pathExists(filepath)) {
|
|
40
|
-
return null;
|
|
41
|
-
}
|
|
42
|
-
const isDirectory = (await fs.stat(filepath)).isDirectory();
|
|
43
|
-
if (!isDirectory) {
|
|
44
|
-
return null;
|
|
45
|
-
}
|
|
46
|
-
const items = await fs.readdir(filepath);
|
|
47
|
-
const mfJson = items.find((i) => i === MF_JSON);
|
|
48
|
-
if (mfJson) {
|
|
49
|
-
const mfJsonContent = await fs.readJSON(normalizeToPosixPath(path.join(filepath, mfJson)));
|
|
50
|
-
const { remoteName } = mfJsonContent;
|
|
51
|
-
const remoteLayoutId = getRemoteLayoutId(remoteName);
|
|
52
|
-
const writeFile = async (id, type, pathname) => {
|
|
53
|
-
const generatedFilePath = normalizeToPosixPath(path.join(filepath, `${type}.tsx`));
|
|
54
|
-
const content = [
|
|
55
|
-
"// Do not edit this file. It is a auto generated.",
|
|
56
|
-
`// It will render ${remoteName}'s ${type}.`,
|
|
57
|
-
`// The ${type} pathname is: ${pathname}`,
|
|
58
|
-
`// The ${type} route id is: ${id}`
|
|
59
|
-
];
|
|
60
|
-
await fs.writeFile(generatedFilePath, content.join("\n"));
|
|
61
|
-
};
|
|
62
|
-
if (remoteLayoutId) {
|
|
63
|
-
const pathname = calcPathName(filepath, rootDir, entry);
|
|
64
|
-
writeFile(remoteLayoutId, "layout", pathname);
|
|
65
|
-
remotePathMap[pathname] = remoteName;
|
|
66
|
-
}
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
await Promise.all(items.map(async (item) => {
|
|
70
|
-
await traverse(normalizeToPosixPath(path.join(filepath, item)), rootDir, entry);
|
|
71
|
-
}));
|
|
72
|
-
};
|
|
73
|
-
const traverseRoutes = async (dir, level = 1) => {
|
|
74
|
-
if (level > 3) {
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
const isDirectory = (await fs.stat(dir)).isDirectory();
|
|
78
|
-
if (!isDirectory) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
const items = await fs.readdir(dir);
|
|
82
|
-
if (items.find((i) => i === ROUTES)) {
|
|
83
|
-
const entry = path.relative(srcPath, dir);
|
|
84
|
-
entries.push(entry);
|
|
85
|
-
const rootDir = `${dir}/${ROUTES}`;
|
|
86
|
-
await traverse(rootDir, rootDir, entry);
|
|
87
|
-
} else {
|
|
88
|
-
await Promise.all(items.map(async (item) => {
|
|
89
|
-
await traverseRoutes(path.join(dir, item), level++);
|
|
90
|
-
}));
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
await traverseRoutes(srcPath);
|
|
94
|
-
const normalizedEntries = entries.map((entry) => {
|
|
95
|
-
if (entry === "") {
|
|
96
|
-
return "main";
|
|
97
|
-
}
|
|
98
|
-
return entry;
|
|
99
|
-
});
|
|
100
|
-
return {
|
|
101
|
-
entries: normalizedEntries,
|
|
102
|
-
remotePathMap
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
export {
|
|
106
|
-
calcPathName,
|
|
107
|
-
generateRouteFile
|
|
108
|
-
};
|
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import { fs } from "@modern-js/utils";
|
|
3
|
-
import { transformName2Prefix } from "../../runtime/utils";
|
|
4
|
-
import { MODERN_JS_ROUTE_SERVER_LOADER } from "../../constant";
|
|
5
|
-
import { MF_ROUTES, MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME } from "../../constant";
|
|
6
|
-
import { generateRoutes } from "./ast";
|
|
7
|
-
function generateExtraExposeFiles(options) {
|
|
8
|
-
const { internalDirectory, mfConfig, isServer, baseName, entries } = options;
|
|
9
|
-
const entryMap = {};
|
|
10
|
-
const outputDir = path.resolve(process.cwd(), "node_modules/.federation/data-loader");
|
|
11
|
-
fs.ensureDirSync(outputDir);
|
|
12
|
-
const addSuffix = (fileName, ext = ".jsx") => {
|
|
13
|
-
if (!isServer) {
|
|
14
|
-
return `${fileName}${ext}`;
|
|
15
|
-
}
|
|
16
|
-
return `${fileName}.server${ext}`;
|
|
17
|
-
};
|
|
18
|
-
const generateEntryRoutes = (entry) => {
|
|
19
|
-
const outputEntryDir = path.resolve(outputDir, entry);
|
|
20
|
-
fs.ensureDirSync(outputEntryDir);
|
|
21
|
-
const sourceDir = path.resolve(internalDirectory, entry);
|
|
22
|
-
const routesFilePath = path.resolve(sourceDir, addSuffix(MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME, ".js"));
|
|
23
|
-
const routesFileContent = fs.readFileSync(routesFilePath, "utf-8");
|
|
24
|
-
const outputFullRoutesPath = path.resolve(outputEntryDir, addSuffix(MF_ROUTES));
|
|
25
|
-
generateRoutes({
|
|
26
|
-
sourceCode: routesFileContent,
|
|
27
|
-
filePath: outputFullRoutesPath,
|
|
28
|
-
prefix: transformName2Prefix(mfConfig.name),
|
|
29
|
-
baseName
|
|
30
|
-
});
|
|
31
|
-
const routeServerLoaderPath = path.resolve(sourceDir, `${MODERN_JS_ROUTE_SERVER_LOADER}.js`);
|
|
32
|
-
const outputRouteServerLoaderPath = path.resolve(outputEntryDir, `${MODERN_JS_ROUTE_SERVER_LOADER}.js`);
|
|
33
|
-
if (isServer) {
|
|
34
|
-
const routeServerLoaderContent = fs.readFileSync(routeServerLoaderPath, "utf-8");
|
|
35
|
-
generateRoutes({
|
|
36
|
-
sourceCode: routeServerLoaderContent,
|
|
37
|
-
filePath: outputRouteServerLoaderPath,
|
|
38
|
-
prefix: transformName2Prefix(mfConfig.name),
|
|
39
|
-
baseName
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
entryMap[entry] = {
|
|
43
|
-
routesPath: outputFullRoutesPath,
|
|
44
|
-
routeServerLoaderPath: outputRouteServerLoaderPath
|
|
45
|
-
};
|
|
46
|
-
};
|
|
47
|
-
entries.forEach((entry) => {
|
|
48
|
-
generateEntryRoutes(entry);
|
|
49
|
-
});
|
|
50
|
-
return {
|
|
51
|
-
entryMap
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
function addExpose(options) {
|
|
55
|
-
const { mfConfig, isServer } = options;
|
|
56
|
-
if (mfConfig.remotes && mfConfig.exposes) {
|
|
57
|
-
throw new Error(`dataLoader only support for pure provider/consumer !`);
|
|
58
|
-
}
|
|
59
|
-
const { entryMap } = generateExtraExposeFiles(options);
|
|
60
|
-
const addExposeByEntry = (entry, routesPath, routeServerLoaderPath) => {
|
|
61
|
-
if (!mfConfig.exposes) {
|
|
62
|
-
mfConfig.exposes = {};
|
|
63
|
-
}
|
|
64
|
-
const routesKey = `./${entry}/${MF_ROUTES}`;
|
|
65
|
-
if (!mfConfig.exposes[routesKey]) {
|
|
66
|
-
mfConfig.exposes[routesKey] = routesPath;
|
|
67
|
-
}
|
|
68
|
-
const routeServerLoaderKey = `./${entry}/${MODERN_JS_ROUTE_SERVER_LOADER}`;
|
|
69
|
-
if (isServer && !mfConfig.exposes[routeServerLoaderKey]) {
|
|
70
|
-
mfConfig.exposes[routeServerLoaderKey] = routeServerLoaderPath;
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
Object.keys(entryMap).forEach((entry) => {
|
|
74
|
-
const { routesPath, routeServerLoaderPath } = entryMap[entry];
|
|
75
|
-
addExposeByEntry(entry, routesPath, routeServerLoaderPath);
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
function addShared(options) {
|
|
79
|
-
const { metaName, mfConfig } = options;
|
|
80
|
-
const alias = `@${metaName}/runtime/router`;
|
|
81
|
-
if (!mfConfig.shared) {
|
|
82
|
-
mfConfig.shared = {
|
|
83
|
-
[alias]: {
|
|
84
|
-
singleton: true
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
} else {
|
|
88
|
-
if (!Array.isArray(mfConfig.shared)) {
|
|
89
|
-
mfConfig.shared[alias] = {
|
|
90
|
-
singleton: true
|
|
91
|
-
};
|
|
92
|
-
} else {
|
|
93
|
-
mfConfig.shared.push(alias);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
function patchMFConfig(options) {
|
|
98
|
-
addShared(options);
|
|
99
|
-
addExpose(options);
|
|
100
|
-
}
|
|
101
|
-
export {
|
|
102
|
-
patchMFConfig
|
|
103
|
-
};
|
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
import { transformName2Prefix } from "../../runtime/utils";
|
|
2
|
-
import { DEFAULT_ENTRY, PLUGIN_IDENTIFIER } from "../../constant";
|
|
3
|
-
import { META_NAME } from "../../constant";
|
|
4
|
-
import { generateRoutes, generateSlimRoutes } from "./ast";
|
|
5
|
-
import { generateRouteFile } from "./generateRouteFile";
|
|
6
|
-
import { patchMFConfig } from "./patchMFConfig";
|
|
7
|
-
import path from "path";
|
|
8
|
-
function _transformRuntimeOptions(buildOptions) {
|
|
9
|
-
const remotes = buildOptions.remotes || {};
|
|
10
|
-
const runtimeRemotes = Object.entries(remotes).map((remote) => {
|
|
11
|
-
const [alias, nameAndEntry] = remote;
|
|
12
|
-
const [name, entry] = nameAndEntry.split("@");
|
|
13
|
-
return {
|
|
14
|
-
name,
|
|
15
|
-
entry,
|
|
16
|
-
alias
|
|
17
|
-
};
|
|
18
|
-
});
|
|
19
|
-
return {
|
|
20
|
-
name: buildOptions.name,
|
|
21
|
-
remotes: runtimeRemotes
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
const moduleFederationDataLoaderPlugin = (enable, internalOptions, userConfig) => ({
|
|
25
|
-
name: "@modern-js/plugin-module-federation-data-loader",
|
|
26
|
-
pre: [
|
|
27
|
-
"@modern-js/plugin-module-federation-config"
|
|
28
|
-
],
|
|
29
|
-
post: [
|
|
30
|
-
"@modern-js/plugin-router",
|
|
31
|
-
"@modern-js/plugin-module-federation"
|
|
32
|
-
],
|
|
33
|
-
setup: async ({ useConfigContext, useAppContext }) => {
|
|
34
|
-
var _modernjsConfig_server, _modernjsConfig_server1, _modernjsConfig_server2, _modernjsConfig_server3, _modernjsConfig_server4;
|
|
35
|
-
if (!enable) {
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
const {
|
|
39
|
-
baseName,
|
|
40
|
-
partialSSRRemotes = [],
|
|
41
|
-
// patchMFConfig,
|
|
42
|
-
metaName = META_NAME,
|
|
43
|
-
serverPlugin = "@module-federation/modern-js/data-loader-server",
|
|
44
|
-
transformRuntimeOptions
|
|
45
|
-
} = userConfig;
|
|
46
|
-
if (!baseName) {
|
|
47
|
-
throw new Error(`${PLUGIN_IDENTIFIER} 'baseName' is required if you enable 'dataLoader'!`);
|
|
48
|
-
}
|
|
49
|
-
const modernjsConfig = useConfigContext();
|
|
50
|
-
const appContext = useAppContext();
|
|
51
|
-
const enableSSR = Boolean(modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
|
|
52
|
-
const name = internalOptions.csrConfig.name;
|
|
53
|
-
const internalDirectory = appContext.internalDirectory.replace(META_NAME, metaName || META_NAME);
|
|
54
|
-
const transformRuntimeFn = transformRuntimeOptions || _transformRuntimeOptions;
|
|
55
|
-
const baseUrls = ((_modernjsConfig_server1 = modernjsConfig.server) === null || _modernjsConfig_server1 === void 0 ? void 0 : _modernjsConfig_server1.baseUrl) ? Array.isArray((_modernjsConfig_server2 = modernjsConfig.server) === null || _modernjsConfig_server2 === void 0 ? void 0 : _modernjsConfig_server2.baseUrl) ? (_modernjsConfig_server3 = modernjsConfig.server) === null || _modernjsConfig_server3 === void 0 ? void 0 : _modernjsConfig_server3.baseUrl : [
|
|
56
|
-
(_modernjsConfig_server4 = modernjsConfig.server) === null || _modernjsConfig_server4 === void 0 ? void 0 : _modernjsConfig_server4.baseUrl
|
|
57
|
-
] : [];
|
|
58
|
-
const entries = /* @__PURE__ */ new Set();
|
|
59
|
-
let remotePathMap = {};
|
|
60
|
-
const ssrByRouteIdsMap = {};
|
|
61
|
-
const serverPluginOptions = {
|
|
62
|
-
runtimeOptions: transformRuntimeFn(internalOptions.csrConfig),
|
|
63
|
-
partialSSRRemotes,
|
|
64
|
-
ssrByRouteIdsMap
|
|
65
|
-
};
|
|
66
|
-
const csrConfig = internalOptions.csrConfig;
|
|
67
|
-
if (typeof csrConfig.remotes === "object" && Object.keys(csrConfig.remotes).length) {
|
|
68
|
-
const { entries: scanEntries, remotePathMap: scanRemotePathMap } = await generateRouteFile({
|
|
69
|
-
appDirectory: appContext.appDirectory
|
|
70
|
-
});
|
|
71
|
-
scanEntries.forEach((e) => entries.add(e));
|
|
72
|
-
remotePathMap = scanRemotePathMap;
|
|
73
|
-
} else {
|
|
74
|
-
entries.add(DEFAULT_ENTRY);
|
|
75
|
-
}
|
|
76
|
-
return {
|
|
77
|
-
_internalRuntimePlugins: ({ entrypoint, plugins }) => {
|
|
78
|
-
plugins.push({
|
|
79
|
-
name: "ssrDataLoader",
|
|
80
|
-
path: "@module-federation/modern-js/data-loader",
|
|
81
|
-
config: {}
|
|
82
|
-
});
|
|
83
|
-
return {
|
|
84
|
-
entrypoint,
|
|
85
|
-
plugins
|
|
86
|
-
};
|
|
87
|
-
},
|
|
88
|
-
_internalServerPlugins({ plugins }) {
|
|
89
|
-
plugins.push({
|
|
90
|
-
name: serverPlugin,
|
|
91
|
-
options: serverPluginOptions
|
|
92
|
-
});
|
|
93
|
-
return {
|
|
94
|
-
plugins
|
|
95
|
-
};
|
|
96
|
-
},
|
|
97
|
-
async modifyFileSystemRoutes(options) {
|
|
98
|
-
const { routes, entrypoint } = options;
|
|
99
|
-
const { entryName } = entrypoint;
|
|
100
|
-
const prefix = transformName2Prefix(internalOptions.csrConfig.name);
|
|
101
|
-
const modifyRouteIds = (route, pathName) => {
|
|
102
|
-
const currentPathName = route.path ? path.join(pathName, route.path) : pathName;
|
|
103
|
-
if (route.id) {
|
|
104
|
-
var _route_children;
|
|
105
|
-
const originalId = route.id;
|
|
106
|
-
route.originalId = originalId;
|
|
107
|
-
const remoteName = remotePathMap[currentPathName];
|
|
108
|
-
if (remoteName) {
|
|
109
|
-
const remoteIdPrefix = transformName2Prefix(remoteName);
|
|
110
|
-
const isLayout = Boolean(route.children);
|
|
111
|
-
const newId = `${remoteIdPrefix}${isLayout ? "layout" : "page"}`;
|
|
112
|
-
ssrByRouteIdsMap[originalId] = newId;
|
|
113
|
-
route.id = newId;
|
|
114
|
-
} else {
|
|
115
|
-
route.id = `${prefix}${route.id}`;
|
|
116
|
-
}
|
|
117
|
-
(_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.forEach((r) => {
|
|
118
|
-
modifyRouteIds(r, currentPathName);
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
routes.forEach((route) => {
|
|
123
|
-
modifyRouteIds(route, entryName);
|
|
124
|
-
});
|
|
125
|
-
return options;
|
|
126
|
-
},
|
|
127
|
-
config: async () => {
|
|
128
|
-
return {
|
|
129
|
-
tools: {
|
|
130
|
-
// bundlerChain can not keep target order
|
|
131
|
-
rspack(config, { isServer }) {
|
|
132
|
-
patchMFConfig({
|
|
133
|
-
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
134
|
-
baseName,
|
|
135
|
-
metaName,
|
|
136
|
-
isServer,
|
|
137
|
-
internalDirectory,
|
|
138
|
-
entries
|
|
139
|
-
});
|
|
140
|
-
},
|
|
141
|
-
// bundlerChain can not keep target order
|
|
142
|
-
webpack(config, { isServer }) {
|
|
143
|
-
patchMFConfig({
|
|
144
|
-
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
145
|
-
baseName,
|
|
146
|
-
metaName,
|
|
147
|
-
isServer,
|
|
148
|
-
internalDirectory,
|
|
149
|
-
entries
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
},
|
|
153
|
-
source: {
|
|
154
|
-
define: {
|
|
155
|
-
MODERN_ROUTER_ID_PREFIX: JSON.stringify(transformName2Prefix(name))
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
var plugin_default = moduleFederationDataLoaderPlugin;
|
|
164
|
-
export {
|
|
165
|
-
plugin_default as default,
|
|
166
|
-
generateRoutes,
|
|
167
|
-
generateSlimRoutes,
|
|
168
|
-
moduleFederationDataLoaderPlugin
|
|
169
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,62 +0,0 @@
|
|
|
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) {
|
|
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
|
-
};
|
|
@@ -1,43 +0,0 @@
|
|
|
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
|
-
if (!partialSSRRemotes.length) {
|
|
19
|
-
return void 0;
|
|
20
|
-
}
|
|
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.values(remoteRoutesMap).forEach((item) => {
|
|
31
|
-
const { routes } = item;
|
|
32
|
-
routes.forEach((route) => {
|
|
33
|
-
collectIds(route);
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
return [
|
|
37
|
-
...remoteProviderRouteIds
|
|
38
|
-
];
|
|
39
|
-
}
|
|
40
|
-
export {
|
|
41
|
-
fetchRemoteFullRoutesMap,
|
|
42
|
-
getSSRByRouteIds
|
|
43
|
-
};
|
|
File without changes
|
|
@@ -1,49 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,63 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,13 +0,0 @@
|
|
|
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,8 +0,0 @@
|
|
|
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";
|