@module-federation/modern-js 0.0.0-next-20240910102933 → 0.0.0-next-20240911025149

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 (94) 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/{node-fetch.js → inject-node-fetch.js} +6 -6
  4. package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +38 -40
  5. package/dist/cjs/{runtime/withMFRouteId.js → cli/mfRuntimePlugins/shared-strategy.js} +23 -11
  6. package/dist/cjs/cli/ssrPlugin.js +1 -1
  7. package/dist/cjs/cli/utils.js +6 -11
  8. package/dist/cjs/cli/utils.spec.js +6 -5
  9. package/dist/cjs/constant.js +2 -32
  10. package/dist/cjs/runtime/index.js +1 -4
  11. package/dist/cjs/ssr-runtime/plugin.js +7 -7
  12. package/dist/esm/cli/constant.js +4 -0
  13. package/dist/esm/cli/index.js +1 -4
  14. package/dist/esm/cli/mfRuntimePlugins/{node-fetch.js → inject-node-fetch.js} +3 -3
  15. package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +25 -0
  16. package/dist/esm/cli/ssrPlugin.js +1 -1
  17. package/dist/esm/cli/utils.js +5 -9
  18. package/dist/esm/cli/utils.spec.js +6 -5
  19. package/dist/esm/constant.js +1 -21
  20. package/dist/esm/runtime/index.js +1 -3
  21. package/dist/esm/ssr-runtime/plugin.js +10 -7
  22. package/dist/esm-node/cli/constant.js +4 -0
  23. package/dist/esm-node/cli/index.js +1 -5
  24. package/dist/esm-node/cli/mfRuntimePlugins/{node-fetch.js → inject-node-fetch.js} +3 -3
  25. package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +38 -40
  26. package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +23 -0
  27. package/dist/esm-node/cli/ssrPlugin.js +1 -1
  28. package/dist/esm-node/cli/utils.js +5 -9
  29. package/dist/esm-node/cli/utils.spec.js +6 -5
  30. package/dist/esm-node/constant.js +1 -21
  31. package/dist/esm-node/runtime/index.js +1 -3
  32. package/dist/esm-node/ssr-runtime/plugin.js +7 -7
  33. package/dist/types/cli/constant.d.ts +1 -0
  34. package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +3 -0
  35. package/dist/types/cli/utils.d.ts +0 -2
  36. package/dist/types/constant.d.ts +0 -10
  37. package/dist/types/runtime/index.d.ts +0 -1
  38. package/dist/types/types/index.d.ts +0 -9
  39. package/package.json +11 -48
  40. package/dist/cjs/cli/dataLoader/ast/constant.js +0 -49
  41. package/dist/cjs/cli/dataLoader/ast/generateRoutes.js +0 -141
  42. package/dist/cjs/cli/dataLoader/generateRouteFile.js +0 -143
  43. package/dist/cjs/cli/dataLoader/patchMFConfig.js +0 -140
  44. package/dist/cjs/cli/dataLoader/plugin.js +0 -199
  45. package/dist/cjs/cli/mfRuntimePlugins/node.js +0 -41
  46. package/dist/cjs/cli/server/dataLoaderPlugin.js +0 -82
  47. package/dist/cjs/cli/server/fetchRemoteFullRoutesMap.js +0 -69
  48. package/dist/cjs/interfaces/route.js +0 -16
  49. package/dist/cjs/runtime/dataLoader/plugin-inject-assets.js +0 -84
  50. package/dist/cjs/runtime/dataLoader/plugin.js +0 -74
  51. package/dist/cjs/runtime/dataLoader/utils.js +0 -106
  52. package/dist/esm/cli/dataLoader/ast/constant.js +0 -18
  53. package/dist/esm/cli/dataLoader/ast/generateRoutes.js +0 -108
  54. package/dist/esm/cli/dataLoader/ast/index.js +0 -4
  55. package/dist/esm/cli/dataLoader/generateRouteFile.js +0 -293
  56. package/dist/esm/cli/dataLoader/patchMFConfig.js +0 -106
  57. package/dist/esm/cli/dataLoader/plugin.js +0 -214
  58. package/dist/esm/cli/mfRuntimePlugins/node.js +0 -13
  59. package/dist/esm/cli/server/dataLoaderPlugin.js +0 -105
  60. package/dist/esm/cli/server/fetchRemoteFullRoutesMap.js +0 -88
  61. package/dist/esm/interfaces/route.js +0 -0
  62. package/dist/esm/runtime/dataLoader/plugin-inject-assets.js +0 -63
  63. package/dist/esm/runtime/dataLoader/plugin.js +0 -93
  64. package/dist/esm/runtime/dataLoader/utils.js +0 -77
  65. package/dist/esm/runtime/withMFRouteId.js +0 -7
  66. package/dist/esm-node/cli/dataLoader/ast/constant.js +0 -18
  67. package/dist/esm-node/cli/dataLoader/ast/generateRoutes.js +0 -107
  68. package/dist/esm-node/cli/dataLoader/ast/index.js +0 -4
  69. package/dist/esm-node/cli/dataLoader/generateRouteFile.js +0 -108
  70. package/dist/esm-node/cli/dataLoader/patchMFConfig.js +0 -106
  71. package/dist/esm-node/cli/dataLoader/plugin.js +0 -164
  72. package/dist/esm-node/cli/mfRuntimePlugins/node.js +0 -11
  73. package/dist/esm-node/cli/server/dataLoaderPlugin.js +0 -62
  74. package/dist/esm-node/cli/server/fetchRemoteFullRoutesMap.js +0 -44
  75. package/dist/esm-node/interfaces/route.js +0 -0
  76. package/dist/esm-node/runtime/dataLoader/plugin-inject-assets.js +0 -60
  77. package/dist/esm-node/runtime/dataLoader/plugin.js +0 -50
  78. package/dist/esm-node/runtime/dataLoader/utils.js +0 -77
  79. package/dist/esm-node/runtime/withMFRouteId.js +0 -7
  80. package/dist/types/cli/dataLoader/ast/constant.d.ts +0 -8
  81. package/dist/types/cli/dataLoader/ast/generateRoutes.d.ts +0 -5
  82. package/dist/types/cli/dataLoader/ast/index.d.ts +0 -1
  83. package/dist/types/cli/dataLoader/generateRouteFile.d.ts +0 -7
  84. package/dist/types/cli/dataLoader/patchMFConfig.d.ts +0 -10
  85. package/dist/types/cli/dataLoader/plugin.d.ts +0 -6
  86. package/dist/types/cli/mfRuntimePlugins/node.d.ts +0 -3
  87. package/dist/types/cli/server/dataLoaderPlugin.d.ts +0 -10
  88. package/dist/types/cli/server/fetchRemoteFullRoutesMap.d.ts +0 -10
  89. package/dist/types/interfaces/route.d.ts +0 -12
  90. package/dist/types/runtime/dataLoader/plugin-inject-assets.d.ts +0 -2
  91. package/dist/types/runtime/dataLoader/plugin.d.ts +0 -2
  92. package/dist/types/runtime/dataLoader/utils.d.ts +0 -22
  93. package/dist/types/runtime/withMFRouteId.d.ts +0 -1
  94. /package/dist/types/cli/mfRuntimePlugins/{node-fetch.d.ts → inject-node-fetch.d.ts} +0 -0
@@ -1,106 +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
- const clientRouteServerLoaderPath = outputRouteServerLoaderPath.replace(MODERN_JS_ROUTE_SERVER_LOADER, `${MODERN_JS_ROUTE_SERVER_LOADER}-client`);
31
- if (isServer) {
32
- const routeServerLoaderContent = fs.readFileSync(routeServerLoaderPath, "utf-8");
33
- generateRoutes({
34
- sourceCode: routeServerLoaderContent,
35
- filePath: outputRouteServerLoaderPath
36
- });
37
- } else {
38
- fs.writeFileSync(clientRouteServerLoaderPath, `export const routes = []`);
39
- }
40
- entryMap[entry] = {
41
- routesPath: outputFullRoutesPath,
42
- routeServerLoaderPath: outputRouteServerLoaderPath,
43
- clientRouteServerLoaderPath
44
- };
45
- };
46
- entries.forEach((entry) => {
47
- generateEntryRoutes(entry);
48
- });
49
- return {
50
- entryMap
51
- };
52
- }
53
- function addExpose(options) {
54
- const { mfConfig, isServer } = options;
55
- if (mfConfig.remotes && mfConfig.exposes) {
56
- throw new Error(`dataLoader only support for pure provider/consumer!`);
57
- }
58
- const { entryMap } = generateExtraExposeFiles(options);
59
- const addExposeByEntry = (entry, routesPath, routeServerLoaderPath, clientRouteServerLoaderPath) => {
60
- if (!mfConfig.exposes) {
61
- mfConfig.exposes = {};
62
- }
63
- const routesKey = `./${entry}/${MF_ROUTES}`;
64
- if (!mfConfig.exposes[routesKey]) {
65
- mfConfig.exposes[routesKey] = routesPath;
66
- }
67
- const routeServerLoaderKey = `./${entry}/${MODERN_JS_ROUTE_SERVER_LOADER}`;
68
- if (!mfConfig.exposes[routeServerLoaderKey]) {
69
- if (isServer) {
70
- mfConfig.exposes[routeServerLoaderKey] = routeServerLoaderPath;
71
- } else {
72
- mfConfig.exposes[routeServerLoaderKey] = clientRouteServerLoaderPath;
73
- }
74
- }
75
- };
76
- Object.keys(entryMap).forEach((entry) => {
77
- const { routesPath, routeServerLoaderPath, clientRouteServerLoaderPath } = entryMap[entry];
78
- addExposeByEntry(entry, routesPath, routeServerLoaderPath, clientRouteServerLoaderPath);
79
- });
80
- }
81
- function addShared(options) {
82
- const { metaName, mfConfig } = options;
83
- const alias = `@${metaName}/runtime/router`;
84
- if (!mfConfig.shared) {
85
- mfConfig.shared = {
86
- [alias]: {
87
- singleton: true
88
- }
89
- };
90
- } else {
91
- if (!Array.isArray(mfConfig.shared)) {
92
- mfConfig.shared[alias] = {
93
- singleton: true
94
- };
95
- } else {
96
- mfConfig.shared.push(alias);
97
- }
98
- }
99
- }
100
- function patchMFConfig(options) {
101
- addShared(options);
102
- addExpose(options);
103
- }
104
- export {
105
- patchMFConfig
106
- };
@@ -1,164 +0,0 @@
1
- import { transformName2Prefix } from "../../runtime/dataLoader/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
- plugins.push({
74
- name: "ssrDataLoaderInjectAssets",
75
- path: "@module-federation/modern-js/data-loader-inject-assets",
76
- config: {}
77
- });
78
- return {
79
- entrypoint,
80
- plugins
81
- };
82
- },
83
- _internalServerPlugins({ plugins }) {
84
- plugins.push({
85
- name: serverPlugin,
86
- options: serverPluginOptions
87
- });
88
- return {
89
- plugins
90
- };
91
- },
92
- async modifyFileSystemRoutes(options) {
93
- const { routes, entrypoint } = options;
94
- const { entryName } = entrypoint;
95
- const prefix = transformName2Prefix(internalOptions.csrConfig.name);
96
- const modifyRouteIds = (route, pathName) => {
97
- const currentPathName = route.path ? path.join(pathName, route.path) : pathName;
98
- if (route.id) {
99
- var _route_children;
100
- const originalId = route.id;
101
- route.originalId = originalId;
102
- const remoteName = remotePathMap[currentPathName];
103
- if (remoteName) {
104
- const remoteIdPrefix = transformName2Prefix(remoteName);
105
- const isLayout = Boolean(route.children);
106
- const newId = `${remoteIdPrefix}${isLayout ? "layout" : "page"}`;
107
- ssrByRouteIdsMap[originalId] = newId;
108
- route.id = newId;
109
- if (!partialSSRRemotes.includes(remoteName)) {
110
- partialSSRRemotes.push(remoteName);
111
- }
112
- } else {
113
- route.id = `${prefix}${route.id}`;
114
- }
115
- (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.forEach((r) => {
116
- modifyRouteIds(r, currentPathName);
117
- });
118
- }
119
- };
120
- routes.forEach((route) => {
121
- modifyRouteIds(route, entryName);
122
- });
123
- return options;
124
- },
125
- config: async () => {
126
- return {
127
- tools: {
128
- // bundlerChain can not keep target order
129
- rspack(_config, { isServer }) {
130
- patchMFConfig({
131
- mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
132
- metaName,
133
- isServer,
134
- internalDirectory,
135
- entries
136
- });
137
- },
138
- // bundlerChain can not keep target order
139
- webpack(_config, { isServer }) {
140
- patchMFConfig({
141
- mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
142
- metaName,
143
- isServer,
144
- internalDirectory,
145
- entries
146
- });
147
- }
148
- },
149
- source: {
150
- define: {
151
- MODERN_ROUTER_ID_PREFIX: JSON.stringify(transformName2Prefix(name))
152
- }
153
- }
154
- };
155
- }
156
- };
157
- }
158
- });
159
- var plugin_default = moduleFederationDataLoaderPlugin;
160
- export {
161
- plugin_default as default,
162
- generateRoutes,
163
- moduleFederationDataLoaderPlugin
164
- };
@@ -1,11 +0,0 @@
1
- import nodeRuntimePlugin from "@module-federation/node/runtimePlugin";
2
- const isContainer = typeof __filename === "string" ? __filename.includes("remote") ? true : false : false;
3
- const pluginName = `node-plugin-${isContainer}`;
4
- const nodePlugin = () => ({
5
- name: pluginName,
6
- beforeInit: nodeRuntimePlugin().beforeInit
7
- });
8
- var node_default = nodePlugin;
9
- export {
10
- node_default as default
11
- };
@@ -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,60 +0,0 @@
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 = () => {
8
- return {
9
- name: "@modern-js/plugin-mf-data-loader-inject-assets",
10
- pre: [
11
- "@modern-js/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
- return /* @__PURE__ */ _jsx(_Fragment, {
22
- children: /* @__PURE__ */ _jsx(App, {
23
- ...props
24
- })
25
- });
26
- }
27
- const context = useContext(RuntimeReactContext);
28
- const { remotes } = instance.options;
29
- const remoteNames = ((_context_routerContext = context.routerContext) === null || _context_routerContext === void 0 ? void 0 : _context_routerContext.matches.map((match) => decodeId(match.route.id))) || [];
30
- const remoteIds = remoteNames.reduce((sum, cur) => {
31
- const matchRemote = remotes.find((r) => r.name === cur);
32
- if (matchRemote) {
33
- sum.add(getRemoteId(cur));
34
- }
35
- return sum;
36
- }, /* @__PURE__ */ new Set());
37
- const assets = [];
38
- remoteIds.forEach((remoteId) => {
39
- assets.push(...collectSSRAssets({
40
- id: remoteId
41
- }));
42
- });
43
- return /* @__PURE__ */ _jsxs(_Fragment, {
44
- children: [
45
- assets,
46
- /* @__PURE__ */ _jsx(App, {
47
- ...props
48
- })
49
- ]
50
- });
51
- };
52
- return AppWrapper;
53
- }
54
- };
55
- }
56
- };
57
- };
58
- export {
59
- ssrDataLoaderInjectAssetsPlugin
60
- };
@@ -1,50 +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
- const pathRemoteMap = {};
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
- };
@@ -1,77 +0,0 @@
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,7 +0,0 @@
1
- function withMFRouteId(id) {
2
- const prefix = typeof MODERN_ROUTER_ID_PREFIX === "string" ? MODERN_ROUTER_ID_PREFIX : "";
3
- return prefix + id;
4
- }
5
- export {
6
- withMFRouteId
7
- };
@@ -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";
@@ -1,5 +0,0 @@
1
- declare function generateRoutes({ sourceCode, filePath, }: {
2
- sourceCode: string;
3
- filePath: string;
4
- }): void;
5
- export { generateRoutes };
@@ -1 +0,0 @@
1
- export { generateRoutes } from './generateRoutes';
@@ -1,7 +0,0 @@
1
- export declare function calcPathName(filePath: string, rootDir: string, entry: string): string;
2
- export declare function generateRouteFile(options: {
3
- appDirectory: string;
4
- }): Promise<{
5
- entries: string[];
6
- remotePathMap: Record<string, string>;
7
- }>;
@@ -1,10 +0,0 @@
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 {};
@@ -1,6 +0,0 @@
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 };
@@ -1,3 +0,0 @@
1
- import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime';
2
- declare const nodePlugin: () => FederationRuntimePlugin;
3
- export default nodePlugin;
@@ -1,10 +0,0 @@
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;
@@ -1,10 +0,0 @@
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[];
@@ -1,12 +0,0 @@
1
- export type Route = {
2
- id: string;
3
- type: string;
4
- loader?: boolean;
5
- index?: boolean;
6
- isRoot?: boolean;
7
- children?: Route[];
8
- };
9
- export type MFModernRouteJson = {
10
- routes: Record<string, Route[]>;
11
- prefix: string;
12
- };