@module-federation/modern-js 0.0.0-next-20240909092952 → 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.
Files changed (91) hide show
  1. package/dist/cjs/cli/{dataLoader/ast/index.js → constant.js} +6 -6
  2. package/dist/cjs/cli/index.js +1 -5
  3. package/dist/cjs/cli/mfRuntimePlugins/inject-node-fetch.js +10 -7
  4. package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +38 -40
  5. package/dist/cjs/cli/ssrPlugin.js +1 -1
  6. package/dist/cjs/cli/utils.js +4 -8
  7. package/dist/cjs/cli/utils.spec.js +1 -1
  8. package/dist/cjs/constant.js +2 -32
  9. package/dist/cjs/runtime/index.js +1 -4
  10. package/dist/cjs/ssr-runtime/plugin.js +7 -7
  11. package/dist/esm/cli/constant.js +4 -0
  12. package/dist/esm/cli/index.js +1 -4
  13. package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js +10 -7
  14. package/dist/esm/cli/ssrPlugin.js +1 -1
  15. package/dist/esm/cli/utils.js +3 -6
  16. package/dist/esm/cli/utils.spec.js +1 -1
  17. package/dist/esm/constant.js +1 -21
  18. package/dist/esm/runtime/index.js +1 -3
  19. package/dist/esm/ssr-runtime/plugin.js +10 -7
  20. package/dist/esm-node/cli/constant.js +4 -0
  21. package/dist/esm-node/cli/index.js +1 -5
  22. package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.js +10 -7
  23. package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +38 -40
  24. package/dist/esm-node/cli/ssrPlugin.js +1 -1
  25. package/dist/esm-node/cli/utils.js +3 -6
  26. package/dist/esm-node/cli/utils.spec.js +1 -1
  27. package/dist/esm-node/constant.js +1 -21
  28. package/dist/esm-node/runtime/index.js +1 -3
  29. package/dist/esm-node/ssr-runtime/plugin.js +7 -7
  30. package/dist/types/cli/constant.d.ts +1 -0
  31. package/dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts +2 -2
  32. package/dist/types/cli/utils.d.ts +0 -2
  33. package/dist/types/constant.d.ts +0 -10
  34. package/dist/types/runtime/index.d.ts +0 -1
  35. package/dist/types/types/index.d.ts +0 -9
  36. package/package.json +11 -40
  37. package/dist/cjs/cli/dataLoader/ast/constant.js +0 -49
  38. package/dist/cjs/cli/dataLoader/ast/generateRoutes.js +0 -141
  39. package/dist/cjs/cli/dataLoader/generateRouteFile.js +0 -143
  40. package/dist/cjs/cli/dataLoader/patchMFConfig.js +0 -132
  41. package/dist/cjs/cli/dataLoader/plugin.js +0 -194
  42. package/dist/cjs/cli/mfRuntimePlugins/node.js +0 -44
  43. package/dist/cjs/cli/server/dataLoaderPlugin.js +0 -82
  44. package/dist/cjs/cli/server/fetchRemoteFullRoutesMap.js +0 -69
  45. package/dist/cjs/interfaces/route.js +0 -16
  46. package/dist/cjs/runtime/dataLoader/plugin.js +0 -73
  47. package/dist/cjs/runtime/dataLoader/utils.js +0 -89
  48. package/dist/cjs/runtime/utils.js +0 -38
  49. package/dist/cjs/runtime/withMFRouteId.js +0 -31
  50. package/dist/esm/cli/dataLoader/ast/constant.js +0 -18
  51. package/dist/esm/cli/dataLoader/ast/generateRoutes.js +0 -108
  52. package/dist/esm/cli/dataLoader/ast/index.js +0 -4
  53. package/dist/esm/cli/dataLoader/generateRouteFile.js +0 -293
  54. package/dist/esm/cli/dataLoader/patchMFConfig.js +0 -98
  55. package/dist/esm/cli/dataLoader/plugin.js +0 -209
  56. package/dist/esm/cli/mfRuntimePlugins/node.js +0 -16
  57. package/dist/esm/cli/server/dataLoaderPlugin.js +0 -105
  58. package/dist/esm/cli/server/fetchRemoteFullRoutesMap.js +0 -88
  59. package/dist/esm/interfaces/route.js +0 -0
  60. package/dist/esm/runtime/dataLoader/plugin.js +0 -92
  61. package/dist/esm/runtime/dataLoader/utils.js +0 -63
  62. package/dist/esm/runtime/utils.js +0 -13
  63. package/dist/esm/runtime/withMFRouteId.js +0 -7
  64. package/dist/esm-node/cli/dataLoader/ast/constant.js +0 -18
  65. package/dist/esm-node/cli/dataLoader/ast/generateRoutes.js +0 -107
  66. package/dist/esm-node/cli/dataLoader/ast/index.js +0 -4
  67. package/dist/esm-node/cli/dataLoader/generateRouteFile.js +0 -108
  68. package/dist/esm-node/cli/dataLoader/patchMFConfig.js +0 -98
  69. package/dist/esm-node/cli/dataLoader/plugin.js +0 -159
  70. package/dist/esm-node/cli/mfRuntimePlugins/node.js +0 -14
  71. package/dist/esm-node/cli/server/dataLoaderPlugin.js +0 -62
  72. package/dist/esm-node/cli/server/fetchRemoteFullRoutesMap.js +0 -44
  73. package/dist/esm-node/interfaces/route.js +0 -0
  74. package/dist/esm-node/runtime/dataLoader/plugin.js +0 -49
  75. package/dist/esm-node/runtime/dataLoader/utils.js +0 -63
  76. package/dist/esm-node/runtime/utils.js +0 -13
  77. package/dist/esm-node/runtime/withMFRouteId.js +0 -7
  78. package/dist/types/cli/dataLoader/ast/constant.d.ts +0 -8
  79. package/dist/types/cli/dataLoader/ast/generateRoutes.d.ts +0 -5
  80. package/dist/types/cli/dataLoader/ast/index.d.ts +0 -1
  81. package/dist/types/cli/dataLoader/generateRouteFile.d.ts +0 -7
  82. package/dist/types/cli/dataLoader/patchMFConfig.d.ts +0 -10
  83. package/dist/types/cli/dataLoader/plugin.d.ts +0 -6
  84. package/dist/types/cli/mfRuntimePlugins/node.d.ts +0 -3
  85. package/dist/types/cli/server/dataLoaderPlugin.d.ts +0 -10
  86. package/dist/types/cli/server/fetchRemoteFullRoutesMap.d.ts +0 -10
  87. package/dist/types/interfaces/route.d.ts +0 -12
  88. package/dist/types/runtime/dataLoader/plugin.d.ts +0 -2
  89. package/dist/types/runtime/dataLoader/utils.d.ts +0 -19
  90. package/dist/types/runtime/utils.d.ts +0 -2
  91. package/dist/types/runtime/withMFRouteId.d.ts +0 -1
@@ -1,107 +0,0 @@
1
- import fs from "fs";
2
- import traverse from "@babel/traverse";
3
- import * as babelParser from "@babel/parser";
4
- import generate from "@babel/generator";
5
- import * as t from "@babel/types";
6
- function findTargetKeyNode(nodeProperties, key) {
7
- return nodeProperties.find((p) => t.isObjectProperty(p) && t.isStringLiteral(p.key) && p.key.value === key);
8
- }
9
- function generateRoutes({ sourceCode, filePath }) {
10
- const ast = babelParser.parse(sourceCode, {
11
- sourceType: "module"
12
- });
13
- const lazyComponentDeclarations = [];
14
- const componentDeclarations = [];
15
- let componentId = 0;
16
- traverse(ast, {
17
- // ImportDeclaration(path) {
18
- // const source = path.node.source.value;
19
- // const routeIdMatch = source.match(/routeId=([^&]+)/);
20
- // if (routeIdMatch) {
21
- // const originalRouteId = routeIdMatch[1];
22
- // const newRouteId = `${prefix}${originalRouteId}`;
23
- // const newSource = source.replace(
24
- // /routeId=[^&]+/,
25
- // `routeId=${newRouteId}`,
26
- // );
27
- // path.node.source = t.stringLiteral(newSource);
28
- // }
29
- // },
30
- ObjectExpression(path) {
31
- let componentName = "";
32
- let lazyComponentName = "";
33
- if (!Array.isArray(path.node.properties)) {
34
- return;
35
- }
36
- const idNode = findTargetKeyNode(path.node.properties, "id");
37
- if (idNode && t.isObjectProperty(idNode) && t.isStringLiteral(idNode.value)) {
38
- }
39
- const isRootNode = findTargetKeyNode(path.node.properties, "isRoot");
40
- if (isRootNode && t.isObjectProperty(isRootNode) && t.isBooleanLiteral(isRootNode.value)) {
41
- isRootNode.value.value = false;
42
- }
43
- if (!isRootNode) {
44
- const lazyComponentNode = findTargetKeyNode(path.node.properties, "lazyImport");
45
- if (lazyComponentNode && t.isObjectProperty(lazyComponentNode) && t.isArrowFunctionExpression(lazyComponentNode.value)) {
46
- lazyComponentName = `LazyComponent_${componentId}`;
47
- const lazyDeclaration = t.variableDeclaration("const", [
48
- t.variableDeclarator(t.identifier(lazyComponentName), lazyComponentNode.value)
49
- ]);
50
- lazyComponentNode.value = t.identifier(lazyComponentName);
51
- const componentNode2 = findTargetKeyNode(path.node.properties, "component");
52
- if (componentNode2 && t.isObjectProperty(componentNode2) && t.isCallExpression(componentNode2.value) && t.isIdentifier(componentNode2.value.callee)) {
53
- componentNode2.value = t.callExpression(t.identifier("lazy"), [
54
- t.identifier(lazyComponentName)
55
- ]);
56
- }
57
- lazyComponentDeclarations.push(lazyDeclaration);
58
- }
59
- }
60
- const componentNode = findTargetKeyNode(path.node.properties, "component");
61
- if (componentNode && t.isObjectProperty(componentNode) && t.isCallExpression(componentNode.value) && t.isIdentifier(componentNode.value.callee)) {
62
- componentName = `Component_${componentId}`;
63
- const componentDeclaration = t.variableDeclaration("const", [
64
- t.variableDeclarator(t.identifier(componentName), lazyComponentName ? t.callExpression(t.identifier("lazy"), [
65
- t.identifier(lazyComponentName)
66
- ]) : componentNode.value)
67
- ]);
68
- componentDeclarations.push(componentDeclaration);
69
- componentNode.value = t.identifier(componentName);
70
- }
71
- if (lazyComponentName || componentName) {
72
- componentId++;
73
- if (componentName) {
74
- const upperFirstName = componentName.slice(0, 1).toUpperCase() + componentName.slice(1);
75
- const jsxElement = t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier(upperFirstName), [], true), null, [], true);
76
- path.node.properties.push(t.objectProperty(t.identifier("element"), jsxElement));
77
- }
78
- }
79
- }
80
- });
81
- traverse(ast, {
82
- Program(path) {
83
- const lastImportIndex = path.get("body").reduce((lastIndex, p, index) => {
84
- if (t.isImportDeclaration(p.node)) {
85
- lastIndex = index;
86
- }
87
- return lastIndex;
88
- }, -1);
89
- if (lastImportIndex >= 0) {
90
- const lastImport = path.get(`body.${lastImportIndex}`);
91
- [
92
- ...componentDeclarations,
93
- ...lazyComponentDeclarations
94
- ].forEach((declaration) => {
95
- if ("insertAfter" in lastImport) {
96
- lastImport.insertAfter(declaration);
97
- }
98
- });
99
- }
100
- }
101
- });
102
- const { code } = generate(ast);
103
- fs.writeFileSync(filePath, code);
104
- }
105
- export {
106
- generateRoutes
107
- };
@@ -1,4 +0,0 @@
1
- import { generateRoutes } from "./generateRoutes";
2
- export {
3
- generateRoutes
4
- };
@@ -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,98 +0,0 @@
1
- import path from "path";
2
- import { fs } from "@modern-js/utils";
3
- import { MODERN_JS_ROUTE_SERVER_LOADER } from "../../constant";
4
- import { MF_ROUTES, MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME } from "../../constant";
5
- import { generateRoutes } from "./ast";
6
- function generateExtraExposeFiles(options) {
7
- const { internalDirectory, mfConfig, isServer, entries } = options;
8
- const entryMap = {};
9
- const outputDir = path.resolve(process.cwd(), "node_modules/.federation/data-loader");
10
- fs.ensureDirSync(outputDir);
11
- const addSuffix = (fileName, ext = ".jsx") => {
12
- if (!isServer) {
13
- return `${fileName}${ext}`;
14
- }
15
- return `${fileName}.server${ext}`;
16
- };
17
- const generateEntryRoutes = (entry) => {
18
- const outputEntryDir = path.resolve(outputDir, entry);
19
- fs.ensureDirSync(outputEntryDir);
20
- const sourceDir = path.resolve(internalDirectory, entry);
21
- const routesFilePath = path.resolve(sourceDir, addSuffix(MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME, ".js"));
22
- const routesFileContent = fs.readFileSync(routesFilePath, "utf-8");
23
- const outputFullRoutesPath = path.resolve(outputEntryDir, addSuffix(MF_ROUTES));
24
- generateRoutes({
25
- sourceCode: routesFileContent,
26
- filePath: outputFullRoutesPath
27
- });
28
- const routeServerLoaderPath = path.resolve(sourceDir, `${MODERN_JS_ROUTE_SERVER_LOADER}.js`);
29
- const outputRouteServerLoaderPath = path.resolve(outputEntryDir, `${MODERN_JS_ROUTE_SERVER_LOADER}.js`);
30
- if (isServer) {
31
- const routeServerLoaderContent = fs.readFileSync(routeServerLoaderPath, "utf-8");
32
- generateRoutes({
33
- sourceCode: routeServerLoaderContent,
34
- filePath: outputRouteServerLoaderPath
35
- });
36
- }
37
- entryMap[entry] = {
38
- routesPath: outputFullRoutesPath,
39
- routeServerLoaderPath: outputRouteServerLoaderPath
40
- };
41
- };
42
- entries.forEach((entry) => {
43
- generateEntryRoutes(entry);
44
- });
45
- return {
46
- entryMap
47
- };
48
- }
49
- function addExpose(options) {
50
- const { mfConfig, isServer } = options;
51
- if (mfConfig.remotes && mfConfig.exposes) {
52
- throw new Error(`dataLoader only support for pure provider/consumer !`);
53
- }
54
- const { entryMap } = generateExtraExposeFiles(options);
55
- const addExposeByEntry = (entry, routesPath, routeServerLoaderPath) => {
56
- if (!mfConfig.exposes) {
57
- mfConfig.exposes = {};
58
- }
59
- const routesKey = `./${entry}/${MF_ROUTES}`;
60
- if (!mfConfig.exposes[routesKey]) {
61
- mfConfig.exposes[routesKey] = routesPath;
62
- }
63
- const routeServerLoaderKey = `./${entry}/${MODERN_JS_ROUTE_SERVER_LOADER}`;
64
- if (isServer && !mfConfig.exposes[routeServerLoaderKey]) {
65
- mfConfig.exposes[routeServerLoaderKey] = routeServerLoaderPath;
66
- }
67
- };
68
- Object.keys(entryMap).forEach((entry) => {
69
- const { routesPath, routeServerLoaderPath } = entryMap[entry];
70
- addExposeByEntry(entry, routesPath, routeServerLoaderPath);
71
- });
72
- }
73
- function addShared(options) {
74
- const { metaName, mfConfig } = options;
75
- const alias = `@${metaName}/runtime/router`;
76
- if (!mfConfig.shared) {
77
- mfConfig.shared = {
78
- [alias]: {
79
- singleton: true
80
- }
81
- };
82
- } else {
83
- if (!Array.isArray(mfConfig.shared)) {
84
- mfConfig.shared[alias] = {
85
- singleton: true
86
- };
87
- } else {
88
- mfConfig.shared.push(alias);
89
- }
90
- }
91
- }
92
- function patchMFConfig(options) {
93
- addShared(options);
94
- addExpose(options);
95
- }
96
- export {
97
- patchMFConfig
98
- };
@@ -1,159 +0,0 @@
1
- import { transformName2Prefix } from "../../runtime/utils";
2
- import { DEFAULT_ENTRY } from "../../constant";
3
- import { META_NAME } from "../../constant";
4
- import { generateRoutes } 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 ({ useAppContext }) => {
34
- if (!enable) {
35
- return;
36
- }
37
- const {
38
- // patchMFConfig,
39
- metaName = META_NAME,
40
- serverPlugin = "@module-federation/modern-js/data-loader-server",
41
- transformRuntimeOptions
42
- } = userConfig;
43
- const appContext = useAppContext();
44
- const name = internalOptions.csrConfig.name;
45
- const internalDirectory = appContext.internalDirectory.replace(META_NAME, metaName || META_NAME);
46
- const transformRuntimeFn = transformRuntimeOptions || _transformRuntimeOptions;
47
- const partialSSRRemotes = [];
48
- const entries = /* @__PURE__ */ new Set();
49
- let remotePathMap = {};
50
- const ssrByRouteIdsMap = {};
51
- const serverPluginOptions = {
52
- runtimeOptions: transformRuntimeFn(internalOptions.csrConfig),
53
- partialSSRRemotes,
54
- ssrByRouteIdsMap
55
- };
56
- const csrConfig = internalOptions.csrConfig;
57
- if (typeof csrConfig.remotes === "object" && Object.keys(csrConfig.remotes).length) {
58
- const { entries: scanEntries, remotePathMap: scanRemotePathMap } = await generateRouteFile({
59
- appDirectory: appContext.appDirectory
60
- });
61
- scanEntries.forEach((e) => entries.add(e));
62
- remotePathMap = scanRemotePathMap;
63
- } else {
64
- entries.add(DEFAULT_ENTRY);
65
- }
66
- return {
67
- _internalRuntimePlugins: ({ entrypoint, plugins }) => {
68
- plugins.push({
69
- name: "ssrDataLoader",
70
- path: "@module-federation/modern-js/data-loader",
71
- config: {}
72
- });
73
- return {
74
- entrypoint,
75
- plugins
76
- };
77
- },
78
- _internalServerPlugins({ plugins }) {
79
- plugins.push({
80
- name: serverPlugin,
81
- options: serverPluginOptions
82
- });
83
- return {
84
- plugins
85
- };
86
- },
87
- async modifyFileSystemRoutes(options) {
88
- const { routes, entrypoint } = options;
89
- const { entryName } = entrypoint;
90
- const prefix = transformName2Prefix(internalOptions.csrConfig.name);
91
- const modifyRouteIds = (route, pathName) => {
92
- const currentPathName = route.path ? path.join(pathName, route.path) : pathName;
93
- if (route.id) {
94
- var _route_children;
95
- const originalId = route.id;
96
- route.originalId = originalId;
97
- const remoteName = remotePathMap[currentPathName];
98
- if (remoteName) {
99
- const remoteIdPrefix = transformName2Prefix(remoteName);
100
- const isLayout = Boolean(route.children);
101
- const newId = `${remoteIdPrefix}${isLayout ? "layout" : "page"}`;
102
- ssrByRouteIdsMap[originalId] = newId;
103
- route.id = newId;
104
- if (!partialSSRRemotes.includes(remoteName)) {
105
- partialSSRRemotes.push(remoteName);
106
- }
107
- } else {
108
- route.id = `${prefix}${route.id}`;
109
- }
110
- (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.forEach((r) => {
111
- modifyRouteIds(r, currentPathName);
112
- });
113
- }
114
- };
115
- routes.forEach((route) => {
116
- modifyRouteIds(route, entryName);
117
- });
118
- return options;
119
- },
120
- config: async () => {
121
- return {
122
- tools: {
123
- // bundlerChain can not keep target order
124
- rspack(_config, { isServer }) {
125
- patchMFConfig({
126
- mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
127
- metaName,
128
- isServer,
129
- internalDirectory,
130
- entries
131
- });
132
- },
133
- // bundlerChain can not keep target order
134
- webpack(_config, { isServer }) {
135
- patchMFConfig({
136
- mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
137
- metaName,
138
- isServer,
139
- internalDirectory,
140
- entries
141
- });
142
- }
143
- },
144
- source: {
145
- define: {
146
- MODERN_ROUTER_ID_PREFIX: JSON.stringify(transformName2Prefix(name))
147
- }
148
- }
149
- };
150
- }
151
- };
152
- }
153
- });
154
- var plugin_default = moduleFederationDataLoaderPlugin;
155
- export {
156
- plugin_default as default,
157
- generateRoutes,
158
- moduleFederationDataLoaderPlugin
159
- };
@@ -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.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
- };
@@ -1,44 +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
- 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
- };
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
- };