@module-federation/modern-js 0.0.0-next-20240913095223 → 0.0.0-next-20240918073359

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 (28) hide show
  1. package/dist/cjs/cli/dataLoader/generateRouteFile.js +7 -3
  2. package/dist/cjs/cli/dataLoader/patchMFConfig.js +0 -3
  3. package/dist/cjs/cli/dataLoader/plugin.js +29 -25
  4. package/dist/cjs/cli/server/dataLoaderPlugin.js +7 -5
  5. package/dist/cjs/cli/server/fetchRemoteFullRoutesMap.js +6 -3
  6. package/dist/cjs/constant.js +3 -0
  7. package/dist/cjs/runtime/dataLoader/plugin.js +5 -2
  8. package/dist/esm/cli/dataLoader/generateRouteFile.js +26 -12
  9. package/dist/esm/cli/dataLoader/patchMFConfig.js +0 -3
  10. package/dist/esm/cli/dataLoader/plugin.js +140 -44
  11. package/dist/esm/cli/server/dataLoaderPlugin.js +7 -5
  12. package/dist/esm/cli/server/fetchRemoteFullRoutesMap.js +9 -4
  13. package/dist/esm/constant.js +2 -0
  14. package/dist/esm/runtime/dataLoader/plugin.js +7 -2
  15. package/dist/esm-node/cli/dataLoader/generateRouteFile.js +8 -4
  16. package/dist/esm-node/cli/dataLoader/patchMFConfig.js +0 -3
  17. package/dist/esm-node/cli/dataLoader/plugin.js +27 -23
  18. package/dist/esm-node/cli/server/dataLoaderPlugin.js +7 -5
  19. package/dist/esm-node/cli/server/fetchRemoteFullRoutesMap.js +6 -3
  20. package/dist/esm-node/constant.js +2 -0
  21. package/dist/esm-node/runtime/dataLoader/plugin.js +5 -2
  22. package/dist/types/cli/dataLoader/generateRouteFile.d.ts +4 -1
  23. package/dist/types/cli/server/dataLoaderPlugin.d.ts +2 -2
  24. package/dist/types/cli/server/fetchRemoteFullRoutesMap.d.ts +2 -1
  25. package/dist/types/constant.d.ts +1 -0
  26. package/dist/types/runtime/dataLoader/plugin.d.ts +2 -1
  27. package/dist/types/types/index.d.ts +0 -1
  28. package/package.json +5 -5
@@ -89,14 +89,18 @@ async function generateRouteFile(options) {
89
89
  "// Do not edit this file. It is a auto generated.",
90
90
  `// It will render ${remoteName}'s ${type}.`,
91
91
  `// The ${type} pathname is: ${pathname}`,
92
- `// The ${type} route id is: ${id}`
92
+ `// The ${type} route id is: ${import_constant.ROUTE_ID}`
93
93
  ];
94
94
  await import_utils.fs.writeFile(generatedFilePath, content.join("\n"));
95
+ return generatedFilePath;
95
96
  };
96
97
  if (remoteLayoutId) {
97
98
  const pathname = calcPathName(filepath, rootDir, entry);
98
- writeFile(remoteLayoutId, "layout", pathname);
99
- remotePathMap[pathname] = remoteName;
99
+ const filePath = await writeFile(remoteLayoutId, "layout", pathname);
100
+ remotePathMap[pathname] = {
101
+ name: remoteName,
102
+ path: filePath
103
+ };
100
104
  }
101
105
  return;
102
106
  }
@@ -85,9 +85,6 @@ function generateExtraExposeFiles(options) {
85
85
  }
86
86
  function addExpose(options) {
87
87
  const { mfConfig, isServer } = options;
88
- if (mfConfig.remotes && mfConfig.exposes) {
89
- throw new Error(`dataLoader only support for pure provider/consumer!`);
90
- }
91
88
  const { entryMap } = generateExtraExposeFiles(options);
92
89
  const addExposeByEntry = (entry, routesPath, routeServerLoaderPath, clientRouteServerLoaderPath) => {
93
90
  if (!mfConfig.exposes) {
@@ -33,7 +33,8 @@ __export(plugin_exports, {
33
33
  moduleFederationDataLoaderPlugin: () => moduleFederationDataLoaderPlugin
34
34
  });
35
35
  module.exports = __toCommonJS(plugin_exports);
36
- var import_utils = require("../../runtime/dataLoader/utils");
36
+ var import_utils = require("@modern-js/utils");
37
+ var import_utils2 = require("../../runtime/dataLoader/utils");
37
38
  var import_constant = require("../../constant");
38
39
  var import_constant2 = require("../../constant");
39
40
  var import_ast = require("./ast");
@@ -76,15 +77,9 @@ const moduleFederationDataLoaderPlugin = (enable, internalOptions, userConfig) =
76
77
  const { metaName = import_constant2.META_NAME } = internalOptions;
77
78
  const internalDirectory = appContext.internalDirectory.replace(import_constant2.META_NAME, metaName || import_constant2.META_NAME);
78
79
  const transformRuntimeFn = transformRuntimeOptions || _transformRuntimeOptions;
79
- const partialSSRRemotes = [];
80
80
  const entries = /* @__PURE__ */ new Set();
81
81
  let remotePathMap = {};
82
82
  const ssrByRouteIdsMap = {};
83
- const serverPluginOptions = {
84
- runtimeOptions: transformRuntimeFn(internalOptions.csrConfig),
85
- partialSSRRemotes,
86
- ssrByRouteIdsMap
87
- };
88
83
  const csrConfig = internalOptions.csrConfig;
89
84
  if (typeof csrConfig.remotes === "object" && Object.keys(csrConfig.remotes).length) {
90
85
  const { entries: scanEntries, remotePathMap: scanRemotePathMap } = await (0, import_generateRouteFile.generateRouteFile)({
@@ -95,20 +90,30 @@ const moduleFederationDataLoaderPlugin = (enable, internalOptions, userConfig) =
95
90
  } else {
96
91
  entries.add(import_constant.DEFAULT_ENTRY);
97
92
  }
93
+ const dataLoaderRemotes = [
94
+ ...new Set(Object.values(remotePathMap).map((i) => i.name))
95
+ ];
96
+ const serverPluginOptions = {
97
+ runtimeOptions: transformRuntimeFn(internalOptions.csrConfig),
98
+ dataLoaderRemotes,
99
+ ssrByRouteIdsMap
100
+ };
98
101
  return {
99
102
  _internalRuntimePlugins: ({ entrypoint, plugins }) => {
100
103
  plugins.push({
101
104
  name: "ssrDataLoader",
102
105
  path: "@module-federation/modern-js/data-loader",
103
106
  config: {
104
- metaName
107
+ metaName,
108
+ dataLoaderRemotes
105
109
  }
106
110
  });
107
111
  plugins.push({
108
112
  name: "ssrDataLoaderInjectAssets",
109
113
  path: "@module-federation/modern-js/data-loader-inject-assets",
110
114
  config: {
111
- metaName
115
+ metaName,
116
+ dataLoaderRemotes
112
117
  }
113
118
  });
114
119
  return {
@@ -128,34 +133,33 @@ const moduleFederationDataLoaderPlugin = (enable, internalOptions, userConfig) =
128
133
  async modifyFileSystemRoutes(options) {
129
134
  const { routes, entrypoint } = options;
130
135
  const { entryName } = entrypoint;
131
- const prefix = (0, import_utils.transformName2Prefix)(internalOptions.csrConfig.name);
132
- const modifyRouteIds = (route, pathName) => {
136
+ const prefix = (0, import_utils2.transformName2Prefix)(internalOptions.csrConfig.name);
137
+ const modifyRouteIds = async (route, pathName) => {
133
138
  const currentPathName = route.path ? import_path.default.join(pathName, route.path) : pathName;
134
139
  if (route.id) {
135
- var _route_children;
136
140
  const originalId = route.id;
137
141
  route.originalId = originalId;
138
- const remoteName = remotePathMap[currentPathName];
139
- if (remoteName) {
140
- const remoteIdPrefix = (0, import_utils.transformName2Prefix)(remoteName);
142
+ const remoteInfo = remotePathMap[currentPathName];
143
+ if (remoteInfo) {
144
+ const { path: path2, name: name2 } = remoteInfo;
145
+ const remoteIdPrefix = (0, import_utils2.transformName2Prefix)(name2);
141
146
  const isLayout = Boolean(route.children);
142
147
  const newId = `${remoteIdPrefix}${isLayout ? "layout" : "page"}`;
143
148
  ssrByRouteIdsMap[originalId] = newId;
149
+ const content = import_utils.fs.readFileSync(path2, "utf-8");
150
+ await import_utils.fs.writeFile(path2, content.replace(import_constant.ROUTE_ID, originalId));
144
151
  route.id = newId;
145
- if (!partialSSRRemotes.includes(remoteName)) {
146
- partialSSRRemotes.push(remoteName);
147
- }
148
152
  } else {
149
153
  route.id = `${prefix}${route.id}`;
150
154
  }
151
- (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.forEach((r) => {
152
- modifyRouteIds(r, currentPathName);
153
- });
155
+ route.children && await Promise.all(route.children.map(async (r) => {
156
+ return await modifyRouteIds(r, currentPathName);
157
+ }));
154
158
  }
155
159
  };
156
- routes.forEach((route) => {
157
- modifyRouteIds(route, entryName);
158
- });
160
+ await Promise.all(routes.map(async (route) => {
161
+ await modifyRouteIds(route, entryName);
162
+ }));
159
163
  return options;
160
164
  },
161
165
  config: async () => {
@@ -184,7 +188,7 @@ const moduleFederationDataLoaderPlugin = (enable, internalOptions, userConfig) =
184
188
  },
185
189
  source: {
186
190
  define: {
187
- MODERN_ROUTER_ID_PREFIX: JSON.stringify((0, import_utils.transformName2Prefix)(name))
191
+ MODERN_ROUTER_ID_PREFIX: JSON.stringify((0, import_utils2.transformName2Prefix)(name))
188
192
  }
189
193
  }
190
194
  };
@@ -26,14 +26,15 @@ var import_sdk = require("@module-federation/sdk");
26
26
  var import_fetchRemoteFullRoutesMap = require("./fetchRemoteFullRoutesMap");
27
27
  var import_utils = require("../../runtime/dataLoader/utils");
28
28
  var import_constant = require("../../constant");
29
- var dataLoaderPlugin_default = ({ runtimeOptions, partialSSRRemotes, ssrByRouteIdsMap }) => ({
29
+ var dataLoaderPlugin_default = ({ runtimeOptions, dataLoaderRemotes, ssrByRouteIdsMap }) => ({
30
30
  name: "MFDataLoaderServerPlugin",
31
31
  pre: [
32
32
  "@modern-js/plugin-inject-resource"
33
33
  ],
34
34
  setup(api) {
35
35
  const { remotes, name } = runtimeOptions;
36
- if (!remotes.length) {
36
+ console.log("server ", dataLoaderRemotes);
37
+ if (!dataLoaderRemotes.length) {
37
38
  return {};
38
39
  }
39
40
  let isHandled = false;
@@ -56,11 +57,12 @@ var dataLoaderPlugin_default = ({ runtimeOptions, partialSSRRemotes, ssrByRouteI
56
57
  });
57
58
  const remoteFullRoutesMap = await (0, import_fetchRemoteFullRoutesMap.fetchRemoteFullRoutesMap)({
58
59
  instance,
59
- remotePath: (0, import_sdk.isBrowserEnv)() ? `${import_constant.DEFAULT_ENTRY}/${import_constant.MF_ROUTES}` : `${import_constant.DEFAULT_ENTRY}/${import_constant.MODERN_JS_ROUTE_SERVER_LOADER}`
60
+ remotePath: (0, import_sdk.isBrowserEnv)() ? `${import_constant.DEFAULT_ENTRY}/${import_constant.MF_ROUTES}` : `${import_constant.DEFAULT_ENTRY}/${import_constant.MODERN_JS_ROUTE_SERVER_LOADER}`,
61
+ dataLoaderRemotes
60
62
  });
61
63
  const originalSSRByRouteIds = (_api_useConfigContext_server = api.useConfigContext().server) === null || _api_useConfigContext_server === void 0 ? void 0 : _api_useConfigContext_server.ssrByRouteIds;
62
- if (originalSSRByRouteIds && partialSSRRemotes.length) {
63
- const remoteSSRByRouteIds = (0, import_fetchRemoteFullRoutesMap.getSSRByRouteIds)(partialSSRRemotes, remoteFullRoutesMap) || [];
64
+ if (originalSSRByRouteIds && dataLoaderRemotes.length) {
65
+ const remoteSSRByRouteIds = (0, import_fetchRemoteFullRoutesMap.getSSRByRouteIds)(dataLoaderRemotes, remoteFullRoutesMap) || [];
64
66
  const ssrByRouteIds = /* @__PURE__ */ new Set([
65
67
  ...originalSSRByRouteIds,
66
68
  ...remoteSSRByRouteIds.map((id) => ssrByRouteIdsMap[id] || id)
@@ -23,13 +23,16 @@ __export(fetchRemoteFullRoutesMap_exports, {
23
23
  });
24
24
  module.exports = __toCommonJS(fetchRemoteFullRoutesMap_exports);
25
25
  async function fetchRemoteFullRoutesMap(options) {
26
- const { instance, remotePath } = options;
26
+ const { instance, remotePath, dataLoaderRemotes } = options;
27
27
  const { remotes } = instance.options;
28
28
  const remoteRoutesMap = {};
29
29
  if (!remotes.length) {
30
30
  return remoteRoutesMap;
31
31
  }
32
32
  await Promise.all(remotes.map(async (remote) => {
33
+ if (!dataLoaderRemotes.includes(remote.name)) {
34
+ return;
35
+ }
33
36
  const remoteId = `${remote.name}/${remotePath}`;
34
37
  const { routes } = await instance.loadRemote(remoteId);
35
38
  remoteRoutesMap[remote.name] = {
@@ -38,7 +41,7 @@ async function fetchRemoteFullRoutesMap(options) {
38
41
  }));
39
42
  return remoteRoutesMap;
40
43
  }
41
- function getSSRByRouteIds(partialSSRRemotes, remoteRoutesMap) {
44
+ function getSSRByRouteIds(dataLoaderRemotes, remoteRoutesMap) {
42
45
  const remoteProviderRouteIds = /* @__PURE__ */ new Set();
43
46
  const collectIds = (route) => {
44
47
  remoteProviderRouteIds.add(route.id);
@@ -50,7 +53,7 @@ function getSSRByRouteIds(partialSSRRemotes, remoteRoutesMap) {
50
53
  };
51
54
  Object.entries(remoteRoutesMap).forEach((item) => {
52
55
  const [remoteName, routesObj] = item;
53
- if (!partialSSRRemotes.includes(remoteName)) {
56
+ if (!dataLoaderRemotes.includes(remoteName)) {
54
57
  return;
55
58
  }
56
59
  const { routes } = routesObj;
@@ -29,6 +29,7 @@ __export(constant_exports, {
29
29
  MODERN_JS_ROUTE_SERVER_LOADER: () => MODERN_JS_ROUTE_SERVER_LOADER,
30
30
  MODERN_JS_SERVER_DIR: () => MODERN_JS_SERVER_DIR,
31
31
  PLUGIN_IDENTIFIER: () => PLUGIN_IDENTIFIER,
32
+ ROUTE_ID: () => ROUTE_ID,
32
33
  SEPARATOR: () => SEPARATOR,
33
34
  isDev: () => isDev
34
35
  });
@@ -46,6 +47,7 @@ const MF_ROUTES = "mf-routes";
46
47
  const DEFAULT_ENTRY = "main";
47
48
  const DEFAULT_LAYOUT = "layout";
48
49
  const MF_JSON = "mf.json";
50
+ const ROUTE_ID = "ROUTE_ID";
49
51
  // Annotate the CommonJS export names for ESM import in node:
50
52
  0 && (module.exports = {
51
53
  DEFAULT_ENTRY,
@@ -59,6 +61,7 @@ const MF_JSON = "mf.json";
59
61
  MODERN_JS_ROUTE_SERVER_LOADER,
60
62
  MODERN_JS_SERVER_DIR,
61
63
  PLUGIN_IDENTIFIER,
64
+ ROUTE_ID,
62
65
  SEPARATOR,
63
66
  isDev
64
67
  });
@@ -24,7 +24,7 @@ module.exports = __toCommonJS(plugin_exports);
24
24
  var import_runtime = require("@module-federation/enhanced/runtime");
25
25
  var import_constant = require("../../constant");
26
26
  var import_utils = require("./utils");
27
- const ssrDataLoaderPlugin = ({ metaName }) => {
27
+ const ssrDataLoaderPlugin = ({ metaName, dataLoaderRemotes }) => {
28
28
  return {
29
29
  name: "@modern-js/plugin-mf-data-loader",
30
30
  post: [
@@ -35,7 +35,7 @@ const ssrDataLoaderPlugin = ({ metaName }) => {
35
35
  const remoteRoutesMap = {};
36
36
  return {
37
37
  async beforeRender() {
38
- console.log("init");
38
+ console.log("init ", dataLoaderRemotes);
39
39
  if (Object.keys(remoteRoutesMap).length) {
40
40
  return;
41
41
  }
@@ -48,6 +48,9 @@ const ssrDataLoaderPlugin = ({ metaName }) => {
48
48
  return;
49
49
  }
50
50
  await Promise.all(instance.options.remotes.map(async (remote) => {
51
+ if (!dataLoaderRemotes.includes(remote.name)) {
52
+ return;
53
+ }
51
54
  const remoteId = `${remote.name}/${import_constant.DEFAULT_ENTRY}/${import_constant.MF_ROUTES}`;
52
55
  const { routes } = await instance.loadRemote(remoteId);
53
56
  remoteRoutesMap[remote.name] = {
@@ -3,7 +3,7 @@ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
3
3
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
4
  import { fs, normalizeToPosixPath } from "@modern-js/utils";
5
5
  import path from "path";
6
- import { MF_JSON } from "../../constant";
6
+ import { MF_JSON, ROUTE_ID } from "../../constant";
7
7
  import { getRemoteLayoutId } from "../../runtime/dataLoader/utils";
8
8
  var ROUTES = "routes";
9
9
  function calcPathName(filePath, rootDir, entry) {
@@ -51,7 +51,7 @@ function _generateRouteFile() {
51
51
  srcPath = "".concat(appDirectory, "/src");
52
52
  traverse = function() {
53
53
  var _ref = _async_to_generator(function(filepath, rootDir, entry) {
54
- var isDirectory, items, mfJson, mfJsonContent, remoteName, remoteLayoutId, writeFile, pathname;
54
+ var isDirectory, items, mfJson, mfJsonContent, remoteName, remoteLayoutId, writeFile, pathname, filePath;
55
55
  return _ts_generator(this, function(_state2) {
56
56
  switch (_state2.label) {
57
57
  case 0:
@@ -90,7 +90,7 @@ function _generateRouteFile() {
90
90
  if (!mfJson)
91
91
  return [
92
92
  3,
93
- 5
93
+ 7
94
94
  ];
95
95
  return [
96
96
  4,
@@ -111,7 +111,7 @@ function _generateRouteFile() {
111
111
  "// Do not edit this file. It is a auto generated.",
112
112
  "// It will render ".concat(remoteName, "'s ").concat(type, "."),
113
113
  "// The ".concat(type, " pathname is: ").concat(pathname2),
114
- "// The ".concat(type, " route id is: ").concat(id)
114
+ "// The ".concat(type, " route id is: ").concat(ROUTE_ID)
115
115
  ];
116
116
  return [
117
117
  4,
@@ -120,7 +120,8 @@ function _generateRouteFile() {
120
120
  case 1:
121
121
  _state3.sent();
122
122
  return [
123
- 2
123
+ 2,
124
+ generatedFilePath
124
125
  ];
125
126
  }
126
127
  });
@@ -129,15 +130,28 @@ function _generateRouteFile() {
129
130
  return _ref2.apply(this, arguments);
130
131
  };
131
132
  }();
132
- if (remoteLayoutId) {
133
- pathname = calcPathName(filepath, rootDir, entry);
134
- writeFile(remoteLayoutId, "layout", pathname);
135
- remotePathMap[pathname] = remoteName;
136
- }
133
+ if (!remoteLayoutId)
134
+ return [
135
+ 3,
136
+ 6
137
+ ];
138
+ pathname = calcPathName(filepath, rootDir, entry);
137
139
  return [
138
- 2
140
+ 4,
141
+ writeFile(remoteLayoutId, "layout", pathname)
139
142
  ];
140
143
  case 5:
144
+ filePath = _state2.sent();
145
+ remotePathMap[pathname] = {
146
+ name: remoteName,
147
+ path: filePath
148
+ };
149
+ _state2.label = 6;
150
+ case 6:
151
+ return [
152
+ 2
153
+ ];
154
+ case 7:
141
155
  return [
142
156
  4,
143
157
  Promise.all(items.map(function() {
@@ -162,7 +176,7 @@ function _generateRouteFile() {
162
176
  };
163
177
  }()))
164
178
  ];
165
- case 6:
179
+ case 8:
166
180
  _state2.sent();
167
181
  return [
168
182
  2
@@ -54,9 +54,6 @@ function generateExtraExposeFiles(options) {
54
54
  }
55
55
  function addExpose(options) {
56
56
  var mfConfig = options.mfConfig, isServer = options.isServer;
57
- if (mfConfig.remotes && mfConfig.exposes) {
58
- throw new Error("dataLoader only support for pure provider/consumer!");
59
- }
60
57
  var entryMap = generateExtraExposeFiles(options).entryMap;
61
58
  var addExposeByEntry = function(entry, routesPath, routeServerLoaderPath, clientRouteServerLoaderPath) {
62
59
  if (!mfConfig.exposes) {
@@ -1,9 +1,11 @@
1
1
  import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
2
  import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
3
+ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
3
4
  import { _ as _type_of } from "@swc/helpers/_/_type_of";
4
5
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
6
+ import { fs } from "@modern-js/utils";
5
7
  import { transformName2Prefix } from "../../runtime/dataLoader/utils";
6
- import { DEFAULT_ENTRY } from "../../constant";
8
+ import { DEFAULT_ENTRY, ROUTE_ID } from "../../constant";
7
9
  import { META_NAME } from "../../constant";
8
10
  import { generateRoutes } from "./ast";
9
11
  import { generateRouteFile } from "./generateRouteFile";
@@ -38,7 +40,7 @@ var moduleFederationDataLoaderPlugin = function(enable, internalOptions, userCon
38
40
  ],
39
41
  setup: function() {
40
42
  var _ref = _async_to_generator(function(param) {
41
- var useAppContext, _userConfig_serverPlugin, serverPlugin, transformRuntimeOptions, appContext, name, _internalOptions_metaName, metaName, internalDirectory, transformRuntimeFn, partialSSRRemotes, entries, remotePathMap, ssrByRouteIdsMap, serverPluginOptions, csrConfig, _ref2, scanEntries, scanRemotePathMap;
43
+ var useAppContext, _userConfig_serverPlugin, serverPlugin, transformRuntimeOptions, appContext, name, _internalOptions_metaName, metaName, internalDirectory, transformRuntimeFn, entries, remotePathMap, ssrByRouteIdsMap, csrConfig, _ref2, scanEntries, scanRemotePathMap, dataLoaderRemotes, serverPluginOptions;
42
44
  return _ts_generator(this, function(_state) {
43
45
  switch (_state.label) {
44
46
  case 0:
@@ -54,15 +56,9 @@ var moduleFederationDataLoaderPlugin = function(enable, internalOptions, userCon
54
56
  _internalOptions_metaName = internalOptions.metaName, metaName = _internalOptions_metaName === void 0 ? META_NAME : _internalOptions_metaName;
55
57
  internalDirectory = appContext.internalDirectory.replace(META_NAME, metaName || META_NAME);
56
58
  transformRuntimeFn = transformRuntimeOptions || _transformRuntimeOptions;
57
- partialSSRRemotes = [];
58
59
  entries = /* @__PURE__ */ new Set();
59
60
  remotePathMap = {};
60
61
  ssrByRouteIdsMap = {};
61
- serverPluginOptions = {
62
- runtimeOptions: transformRuntimeFn(internalOptions.csrConfig),
63
- partialSSRRemotes,
64
- ssrByRouteIdsMap
65
- };
66
62
  csrConfig = internalOptions.csrConfig;
67
63
  if (!(_type_of(csrConfig.remotes) === "object" && Object.keys(csrConfig.remotes).length))
68
64
  return [
@@ -89,6 +85,14 @@ var moduleFederationDataLoaderPlugin = function(enable, internalOptions, userCon
89
85
  entries.add(DEFAULT_ENTRY);
90
86
  _state.label = 3;
91
87
  case 3:
88
+ dataLoaderRemotes = _to_consumable_array(new Set(Object.values(remotePathMap).map(function(i) {
89
+ return i.name;
90
+ })));
91
+ serverPluginOptions = {
92
+ runtimeOptions: transformRuntimeFn(internalOptions.csrConfig),
93
+ dataLoaderRemotes,
94
+ ssrByRouteIdsMap
95
+ };
92
96
  return [
93
97
  2,
94
98
  {
@@ -98,14 +102,16 @@ var moduleFederationDataLoaderPlugin = function(enable, internalOptions, userCon
98
102
  name: "ssrDataLoader",
99
103
  path: "@module-federation/modern-js/data-loader",
100
104
  config: {
101
- metaName
105
+ metaName,
106
+ dataLoaderRemotes
102
107
  }
103
108
  });
104
109
  plugins.push({
105
110
  name: "ssrDataLoaderInjectAssets",
106
111
  path: "@module-federation/modern-js/data-loader-inject-assets",
107
112
  config: {
108
- metaName
113
+ metaName,
114
+ dataLoaderRemotes
109
115
  }
110
116
  });
111
117
  return {
@@ -127,40 +133,130 @@ var moduleFederationDataLoaderPlugin = function(enable, internalOptions, userCon
127
133
  return _async_to_generator(function() {
128
134
  var routes, entrypoint, entryName, prefix, modifyRouteIds;
129
135
  return _ts_generator(this, function(_state2) {
130
- routes = options.routes, entrypoint = options.entrypoint;
131
- entryName = entrypoint.entryName;
132
- prefix = transformName2Prefix(internalOptions.csrConfig.name);
133
- modifyRouteIds = function(route, pathName) {
134
- var currentPathName = route.path ? path.join(pathName, route.path) : pathName;
135
- if (route.id) {
136
- var _route_children;
137
- var originalId = route.id;
138
- route.originalId = originalId;
139
- var remoteName = remotePathMap[currentPathName];
140
- if (remoteName) {
141
- var remoteIdPrefix = transformName2Prefix(remoteName);
142
- var isLayout = Boolean(route.children);
143
- var newId = "".concat(remoteIdPrefix).concat(isLayout ? "layout" : "page");
144
- ssrByRouteIdsMap[originalId] = newId;
145
- route.id = newId;
146
- if (!partialSSRRemotes.includes(remoteName)) {
147
- partialSSRRemotes.push(remoteName);
148
- }
149
- } else {
150
- route.id = "".concat(prefix).concat(route.id);
151
- }
152
- (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.forEach(function(r) {
153
- modifyRouteIds(r, currentPathName);
154
- });
155
- }
156
- };
157
- routes.forEach(function(route) {
158
- modifyRouteIds(route, entryName);
159
- });
160
- return [
161
- 2,
162
- options
163
- ];
136
+ switch (_state2.label) {
137
+ case 0:
138
+ routes = options.routes, entrypoint = options.entrypoint;
139
+ entryName = entrypoint.entryName;
140
+ prefix = transformName2Prefix(internalOptions.csrConfig.name);
141
+ modifyRouteIds = function() {
142
+ var _ref3 = _async_to_generator(function(route, pathName) {
143
+ var currentPathName, originalId, remoteInfo, _$path, name2, remoteIdPrefix, isLayout, newId, content, _tmp;
144
+ return _ts_generator(this, function(_state3) {
145
+ switch (_state3.label) {
146
+ case 0:
147
+ currentPathName = route.path ? path.join(pathName, route.path) : pathName;
148
+ if (!route.id)
149
+ return [
150
+ 3,
151
+ 6
152
+ ];
153
+ originalId = route.id;
154
+ route.originalId = originalId;
155
+ remoteInfo = remotePathMap[currentPathName];
156
+ if (!remoteInfo)
157
+ return [
158
+ 3,
159
+ 2
160
+ ];
161
+ _$path = remoteInfo.path, name2 = remoteInfo.name;
162
+ remoteIdPrefix = transformName2Prefix(name2);
163
+ isLayout = Boolean(route.children);
164
+ newId = "".concat(remoteIdPrefix).concat(isLayout ? "layout" : "page");
165
+ ssrByRouteIdsMap[originalId] = newId;
166
+ content = fs.readFileSync(_$path, "utf-8");
167
+ return [
168
+ 4,
169
+ fs.writeFile(_$path, content.replace(ROUTE_ID, originalId))
170
+ ];
171
+ case 1:
172
+ _state3.sent();
173
+ route.id = newId;
174
+ return [
175
+ 3,
176
+ 3
177
+ ];
178
+ case 2:
179
+ route.id = "".concat(prefix).concat(route.id);
180
+ _state3.label = 3;
181
+ case 3:
182
+ _tmp = route.children;
183
+ if (!_tmp)
184
+ return [
185
+ 3,
186
+ 5
187
+ ];
188
+ return [
189
+ 4,
190
+ Promise.all(route.children.map(function() {
191
+ var _ref4 = _async_to_generator(function(r) {
192
+ return _ts_generator(this, function(_state4) {
193
+ switch (_state4.label) {
194
+ case 0:
195
+ return [
196
+ 4,
197
+ modifyRouteIds(r, currentPathName)
198
+ ];
199
+ case 1:
200
+ return [
201
+ 2,
202
+ _state4.sent()
203
+ ];
204
+ }
205
+ });
206
+ });
207
+ return function(r) {
208
+ return _ref4.apply(this, arguments);
209
+ };
210
+ }()))
211
+ ];
212
+ case 4:
213
+ _tmp = _state3.sent();
214
+ _state3.label = 5;
215
+ case 5:
216
+ _tmp;
217
+ _state3.label = 6;
218
+ case 6:
219
+ return [
220
+ 2
221
+ ];
222
+ }
223
+ });
224
+ });
225
+ return function modifyRouteIds2(route, pathName) {
226
+ return _ref3.apply(this, arguments);
227
+ };
228
+ }();
229
+ return [
230
+ 4,
231
+ Promise.all(routes.map(function() {
232
+ var _ref3 = _async_to_generator(function(route) {
233
+ return _ts_generator(this, function(_state3) {
234
+ switch (_state3.label) {
235
+ case 0:
236
+ return [
237
+ 4,
238
+ modifyRouteIds(route, entryName)
239
+ ];
240
+ case 1:
241
+ _state3.sent();
242
+ return [
243
+ 2
244
+ ];
245
+ }
246
+ });
247
+ });
248
+ return function(route) {
249
+ return _ref3.apply(this, arguments);
250
+ };
251
+ }()))
252
+ ];
253
+ case 1:
254
+ _state2.sent();
255
+ return [
256
+ 2,
257
+ options
258
+ ];
259
+ }
164
260
  });
165
261
  })();
166
262
  },
@@ -7,7 +7,7 @@ import { fetchRemoteFullRoutesMap, getSSRByRouteIds } from "./fetchRemoteFullRou
7
7
  import { injectRemoteRoutes } from "../../runtime/dataLoader/utils";
8
8
  import { MF_ROUTES, DEFAULT_ENTRY, MODERN_JS_ROUTE_SERVER_LOADER } from "../../constant";
9
9
  function dataLoaderPlugin_default(param) {
10
- var runtimeOptions = param.runtimeOptions, partialSSRRemotes = param.partialSSRRemotes, ssrByRouteIdsMap = param.ssrByRouteIdsMap;
10
+ var runtimeOptions = param.runtimeOptions, dataLoaderRemotes = param.dataLoaderRemotes, ssrByRouteIdsMap = param.ssrByRouteIdsMap;
11
11
  return {
12
12
  name: "MFDataLoaderServerPlugin",
13
13
  pre: [
@@ -15,7 +15,8 @@ function dataLoaderPlugin_default(param) {
15
15
  ],
16
16
  setup: function setup(api) {
17
17
  var remotes = runtimeOptions.remotes, name = runtimeOptions.name;
18
- if (!remotes.length) {
18
+ console.log("server ", dataLoaderRemotes);
19
+ if (!dataLoaderRemotes.length) {
19
20
  return {};
20
21
  }
21
22
  var isHandled = false;
@@ -57,14 +58,15 @@ function dataLoaderPlugin_default(param) {
57
58
  4,
58
59
  fetchRemoteFullRoutesMap({
59
60
  instance,
60
- remotePath: isBrowserEnv() ? "".concat(DEFAULT_ENTRY, "/").concat(MF_ROUTES) : "".concat(DEFAULT_ENTRY, "/").concat(MODERN_JS_ROUTE_SERVER_LOADER)
61
+ remotePath: isBrowserEnv() ? "".concat(DEFAULT_ENTRY, "/").concat(MF_ROUTES) : "".concat(DEFAULT_ENTRY, "/").concat(MODERN_JS_ROUTE_SERVER_LOADER),
62
+ dataLoaderRemotes
61
63
  })
62
64
  ];
63
65
  case 3:
64
66
  remoteFullRoutesMap = _state.sent();
65
67
  originalSSRByRouteIds = (_api_useConfigContext_server = api.useConfigContext().server) === null || _api_useConfigContext_server === void 0 ? void 0 : _api_useConfigContext_server.ssrByRouteIds;
66
- if (originalSSRByRouteIds && partialSSRRemotes.length) {
67
- remoteSSRByRouteIds = getSSRByRouteIds(partialSSRRemotes, remoteFullRoutesMap) || [];
68
+ if (originalSSRByRouteIds && dataLoaderRemotes.length) {
69
+ remoteSSRByRouteIds = getSSRByRouteIds(dataLoaderRemotes, remoteFullRoutesMap) || [];
68
70
  ssrByRouteIds = new Set(_to_consumable_array(originalSSRByRouteIds).concat(_to_consumable_array(remoteSSRByRouteIds.map(function(id) {
69
71
  return ssrByRouteIdsMap[id] || id;
70
72
  }))));
@@ -7,11 +7,11 @@ function fetchRemoteFullRoutesMap(options) {
7
7
  }
8
8
  function _fetchRemoteFullRoutesMap() {
9
9
  _fetchRemoteFullRoutesMap = _async_to_generator(function(options) {
10
- var instance, remotePath, remotes, remoteRoutesMap;
10
+ var instance, remotePath, dataLoaderRemotes, remotes, remoteRoutesMap;
11
11
  return _ts_generator(this, function(_state) {
12
12
  switch (_state.label) {
13
13
  case 0:
14
- instance = options.instance, remotePath = options.remotePath;
14
+ instance = options.instance, remotePath = options.remotePath, dataLoaderRemotes = options.dataLoaderRemotes;
15
15
  remotes = instance.options.remotes;
16
16
  remoteRoutesMap = {};
17
17
  if (!remotes.length) {
@@ -28,6 +28,11 @@ function _fetchRemoteFullRoutesMap() {
28
28
  return _ts_generator(this, function(_state2) {
29
29
  switch (_state2.label) {
30
30
  case 0:
31
+ if (!dataLoaderRemotes.includes(remote.name)) {
32
+ return [
33
+ 2
34
+ ];
35
+ }
31
36
  remoteId = "".concat(remote.name, "/").concat(remotePath);
32
37
  return [
33
38
  4,
@@ -60,7 +65,7 @@ function _fetchRemoteFullRoutesMap() {
60
65
  });
61
66
  return _fetchRemoteFullRoutesMap.apply(this, arguments);
62
67
  }
63
- function getSSRByRouteIds(partialSSRRemotes, remoteRoutesMap) {
68
+ function getSSRByRouteIds(dataLoaderRemotes, remoteRoutesMap) {
64
69
  var remoteProviderRouteIds = /* @__PURE__ */ new Set();
65
70
  var collectIds = function(route) {
66
71
  remoteProviderRouteIds.add(route.id);
@@ -72,7 +77,7 @@ function getSSRByRouteIds(partialSSRRemotes, remoteRoutesMap) {
72
77
  };
73
78
  Object.entries(remoteRoutesMap).forEach(function(item) {
74
79
  var _item = _sliced_to_array(item, 2), remoteName = _item[0], routesObj = _item[1];
75
- if (!partialSSRRemotes.includes(remoteName)) {
80
+ if (!dataLoaderRemotes.includes(remoteName)) {
76
81
  return;
77
82
  }
78
83
  var routes = routesObj.routes;
@@ -11,6 +11,7 @@ var MF_ROUTES = "mf-routes";
11
11
  var DEFAULT_ENTRY = "main";
12
12
  var DEFAULT_LAYOUT = "layout";
13
13
  var MF_JSON = "mf.json";
14
+ var ROUTE_ID = "ROUTE_ID";
14
15
  export {
15
16
  DEFAULT_ENTRY,
16
17
  DEFAULT_LAYOUT,
@@ -23,6 +24,7 @@ export {
23
24
  MODERN_JS_ROUTE_SERVER_LOADER,
24
25
  MODERN_JS_SERVER_DIR,
25
26
  PLUGIN_IDENTIFIER,
27
+ ROUTE_ID,
26
28
  SEPARATOR,
27
29
  isDev
28
30
  };
@@ -4,7 +4,7 @@ import { getInstance } from "@module-federation/enhanced/runtime";
4
4
  import { MF_ROUTES, DEFAULT_ENTRY } from "../../constant";
5
5
  import { injectRemoteRoutes } from "./utils";
6
6
  var ssrDataLoaderPlugin = function(param) {
7
- var metaName = param.metaName;
7
+ var metaName = param.metaName, dataLoaderRemotes = param.dataLoaderRemotes;
8
8
  return {
9
9
  name: "@modern-js/plugin-mf-data-loader",
10
10
  post: [
@@ -20,7 +20,7 @@ var ssrDataLoaderPlugin = function(param) {
20
20
  return _ts_generator(this, function(_state) {
21
21
  switch (_state.label) {
22
22
  case 0:
23
- console.log("init");
23
+ console.log("init ", dataLoaderRemotes);
24
24
  if (Object.keys(remoteRoutesMap).length) {
25
25
  return [
26
26
  2
@@ -46,6 +46,11 @@ var ssrDataLoaderPlugin = function(param) {
46
46
  return _ts_generator(this, function(_state2) {
47
47
  switch (_state2.label) {
48
48
  case 0:
49
+ if (!dataLoaderRemotes.includes(remote.name)) {
50
+ return [
51
+ 2
52
+ ];
53
+ }
49
54
  remoteId = "".concat(remote.name, "/").concat(DEFAULT_ENTRY, "/").concat(MF_ROUTES);
50
55
  return [
51
56
  4,
@@ -1,6 +1,6 @@
1
1
  import { fs, normalizeToPosixPath } from "@modern-js/utils";
2
2
  import path from "path";
3
- import { MF_JSON } from "../../constant";
3
+ import { MF_JSON, ROUTE_ID } from "../../constant";
4
4
  import { getRemoteLayoutId } from "../../runtime/dataLoader/utils";
5
5
  const ROUTES = "routes";
6
6
  function calcPathName(filePath, rootDir, entry) {
@@ -55,14 +55,18 @@ async function generateRouteFile(options) {
55
55
  "// Do not edit this file. It is a auto generated.",
56
56
  `// It will render ${remoteName}'s ${type}.`,
57
57
  `// The ${type} pathname is: ${pathname}`,
58
- `// The ${type} route id is: ${id}`
58
+ `// The ${type} route id is: ${ROUTE_ID}`
59
59
  ];
60
60
  await fs.writeFile(generatedFilePath, content.join("\n"));
61
+ return generatedFilePath;
61
62
  };
62
63
  if (remoteLayoutId) {
63
64
  const pathname = calcPathName(filepath, rootDir, entry);
64
- writeFile(remoteLayoutId, "layout", pathname);
65
- remotePathMap[pathname] = remoteName;
65
+ const filePath = await writeFile(remoteLayoutId, "layout", pathname);
66
+ remotePathMap[pathname] = {
67
+ name: remoteName,
68
+ path: filePath
69
+ };
66
70
  }
67
71
  return;
68
72
  }
@@ -52,9 +52,6 @@ function generateExtraExposeFiles(options) {
52
52
  }
53
53
  function addExpose(options) {
54
54
  const { mfConfig, isServer } = options;
55
- if (mfConfig.remotes && mfConfig.exposes) {
56
- throw new Error(`dataLoader only support for pure provider/consumer!`);
57
- }
58
55
  const { entryMap } = generateExtraExposeFiles(options);
59
56
  const addExposeByEntry = (entry, routesPath, routeServerLoaderPath, clientRouteServerLoaderPath) => {
60
57
  if (!mfConfig.exposes) {
@@ -1,5 +1,6 @@
1
+ import { fs } from "@modern-js/utils";
1
2
  import { transformName2Prefix } from "../../runtime/dataLoader/utils";
2
- import { DEFAULT_ENTRY } from "../../constant";
3
+ import { DEFAULT_ENTRY, ROUTE_ID } from "../../constant";
3
4
  import { META_NAME } from "../../constant";
4
5
  import { generateRoutes } from "./ast";
5
6
  import { generateRouteFile } from "./generateRouteFile";
@@ -41,15 +42,9 @@ const moduleFederationDataLoaderPlugin = (enable, internalOptions, userConfig) =
41
42
  const { metaName = META_NAME } = internalOptions;
42
43
  const internalDirectory = appContext.internalDirectory.replace(META_NAME, metaName || META_NAME);
43
44
  const transformRuntimeFn = transformRuntimeOptions || _transformRuntimeOptions;
44
- const partialSSRRemotes = [];
45
45
  const entries = /* @__PURE__ */ new Set();
46
46
  let remotePathMap = {};
47
47
  const ssrByRouteIdsMap = {};
48
- const serverPluginOptions = {
49
- runtimeOptions: transformRuntimeFn(internalOptions.csrConfig),
50
- partialSSRRemotes,
51
- ssrByRouteIdsMap
52
- };
53
48
  const csrConfig = internalOptions.csrConfig;
54
49
  if (typeof csrConfig.remotes === "object" && Object.keys(csrConfig.remotes).length) {
55
50
  const { entries: scanEntries, remotePathMap: scanRemotePathMap } = await generateRouteFile({
@@ -60,20 +55,30 @@ const moduleFederationDataLoaderPlugin = (enable, internalOptions, userConfig) =
60
55
  } else {
61
56
  entries.add(DEFAULT_ENTRY);
62
57
  }
58
+ const dataLoaderRemotes = [
59
+ ...new Set(Object.values(remotePathMap).map((i) => i.name))
60
+ ];
61
+ const serverPluginOptions = {
62
+ runtimeOptions: transformRuntimeFn(internalOptions.csrConfig),
63
+ dataLoaderRemotes,
64
+ ssrByRouteIdsMap
65
+ };
63
66
  return {
64
67
  _internalRuntimePlugins: ({ entrypoint, plugins }) => {
65
68
  plugins.push({
66
69
  name: "ssrDataLoader",
67
70
  path: "@module-federation/modern-js/data-loader",
68
71
  config: {
69
- metaName
72
+ metaName,
73
+ dataLoaderRemotes
70
74
  }
71
75
  });
72
76
  plugins.push({
73
77
  name: "ssrDataLoaderInjectAssets",
74
78
  path: "@module-federation/modern-js/data-loader-inject-assets",
75
79
  config: {
76
- metaName
80
+ metaName,
81
+ dataLoaderRemotes
77
82
  }
78
83
  });
79
84
  return {
@@ -94,33 +99,32 @@ const moduleFederationDataLoaderPlugin = (enable, internalOptions, userConfig) =
94
99
  const { routes, entrypoint } = options;
95
100
  const { entryName } = entrypoint;
96
101
  const prefix = transformName2Prefix(internalOptions.csrConfig.name);
97
- const modifyRouteIds = (route, pathName) => {
102
+ const modifyRouteIds = async (route, pathName) => {
98
103
  const currentPathName = route.path ? path.join(pathName, route.path) : pathName;
99
104
  if (route.id) {
100
- var _route_children;
101
105
  const originalId = route.id;
102
106
  route.originalId = originalId;
103
- const remoteName = remotePathMap[currentPathName];
104
- if (remoteName) {
105
- const remoteIdPrefix = transformName2Prefix(remoteName);
107
+ const remoteInfo = remotePathMap[currentPathName];
108
+ if (remoteInfo) {
109
+ const { path: path2, name: name2 } = remoteInfo;
110
+ const remoteIdPrefix = transformName2Prefix(name2);
106
111
  const isLayout = Boolean(route.children);
107
112
  const newId = `${remoteIdPrefix}${isLayout ? "layout" : "page"}`;
108
113
  ssrByRouteIdsMap[originalId] = newId;
114
+ const content = fs.readFileSync(path2, "utf-8");
115
+ await fs.writeFile(path2, content.replace(ROUTE_ID, originalId));
109
116
  route.id = newId;
110
- if (!partialSSRRemotes.includes(remoteName)) {
111
- partialSSRRemotes.push(remoteName);
112
- }
113
117
  } else {
114
118
  route.id = `${prefix}${route.id}`;
115
119
  }
116
- (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.forEach((r) => {
117
- modifyRouteIds(r, currentPathName);
118
- });
120
+ route.children && await Promise.all(route.children.map(async (r) => {
121
+ return await modifyRouteIds(r, currentPathName);
122
+ }));
119
123
  }
120
124
  };
121
- routes.forEach((route) => {
122
- modifyRouteIds(route, entryName);
123
- });
125
+ await Promise.all(routes.map(async (route) => {
126
+ await modifyRouteIds(route, entryName);
127
+ }));
124
128
  return options;
125
129
  },
126
130
  config: async () => {
@@ -3,14 +3,15 @@ import { isBrowserEnv } from "@module-federation/sdk";
3
3
  import { fetchRemoteFullRoutesMap, getSSRByRouteIds } from "./fetchRemoteFullRoutesMap";
4
4
  import { injectRemoteRoutes } from "../../runtime/dataLoader/utils";
5
5
  import { MF_ROUTES, DEFAULT_ENTRY, MODERN_JS_ROUTE_SERVER_LOADER } from "../../constant";
6
- var dataLoaderPlugin_default = ({ runtimeOptions, partialSSRRemotes, ssrByRouteIdsMap }) => ({
6
+ var dataLoaderPlugin_default = ({ runtimeOptions, dataLoaderRemotes, ssrByRouteIdsMap }) => ({
7
7
  name: "MFDataLoaderServerPlugin",
8
8
  pre: [
9
9
  "@modern-js/plugin-inject-resource"
10
10
  ],
11
11
  setup(api) {
12
12
  const { remotes, name } = runtimeOptions;
13
- if (!remotes.length) {
13
+ console.log("server ", dataLoaderRemotes);
14
+ if (!dataLoaderRemotes.length) {
14
15
  return {};
15
16
  }
16
17
  let isHandled = false;
@@ -33,11 +34,12 @@ var dataLoaderPlugin_default = ({ runtimeOptions, partialSSRRemotes, ssrByRouteI
33
34
  });
34
35
  const remoteFullRoutesMap = await fetchRemoteFullRoutesMap({
35
36
  instance,
36
- remotePath: isBrowserEnv() ? `${DEFAULT_ENTRY}/${MF_ROUTES}` : `${DEFAULT_ENTRY}/${MODERN_JS_ROUTE_SERVER_LOADER}`
37
+ remotePath: isBrowserEnv() ? `${DEFAULT_ENTRY}/${MF_ROUTES}` : `${DEFAULT_ENTRY}/${MODERN_JS_ROUTE_SERVER_LOADER}`,
38
+ dataLoaderRemotes
37
39
  });
38
40
  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
+ if (originalSSRByRouteIds && dataLoaderRemotes.length) {
42
+ const remoteSSRByRouteIds = getSSRByRouteIds(dataLoaderRemotes, remoteFullRoutesMap) || [];
41
43
  const ssrByRouteIds = /* @__PURE__ */ new Set([
42
44
  ...originalSSRByRouteIds,
43
45
  ...remoteSSRByRouteIds.map((id) => ssrByRouteIdsMap[id] || id)
@@ -1,11 +1,14 @@
1
1
  async function fetchRemoteFullRoutesMap(options) {
2
- const { instance, remotePath } = options;
2
+ const { instance, remotePath, dataLoaderRemotes } = options;
3
3
  const { remotes } = instance.options;
4
4
  const remoteRoutesMap = {};
5
5
  if (!remotes.length) {
6
6
  return remoteRoutesMap;
7
7
  }
8
8
  await Promise.all(remotes.map(async (remote) => {
9
+ if (!dataLoaderRemotes.includes(remote.name)) {
10
+ return;
11
+ }
9
12
  const remoteId = `${remote.name}/${remotePath}`;
10
13
  const { routes } = await instance.loadRemote(remoteId);
11
14
  remoteRoutesMap[remote.name] = {
@@ -14,7 +17,7 @@ async function fetchRemoteFullRoutesMap(options) {
14
17
  }));
15
18
  return remoteRoutesMap;
16
19
  }
17
- function getSSRByRouteIds(partialSSRRemotes, remoteRoutesMap) {
20
+ function getSSRByRouteIds(dataLoaderRemotes, remoteRoutesMap) {
18
21
  const remoteProviderRouteIds = /* @__PURE__ */ new Set();
19
22
  const collectIds = (route) => {
20
23
  remoteProviderRouteIds.add(route.id);
@@ -26,7 +29,7 @@ function getSSRByRouteIds(partialSSRRemotes, remoteRoutesMap) {
26
29
  };
27
30
  Object.entries(remoteRoutesMap).forEach((item) => {
28
31
  const [remoteName, routesObj] = item;
29
- if (!partialSSRRemotes.includes(remoteName)) {
32
+ if (!dataLoaderRemotes.includes(remoteName)) {
30
33
  return;
31
34
  }
32
35
  const { routes } = routesObj;
@@ -11,6 +11,7 @@ const MF_ROUTES = "mf-routes";
11
11
  const DEFAULT_ENTRY = "main";
12
12
  const DEFAULT_LAYOUT = "layout";
13
13
  const MF_JSON = "mf.json";
14
+ const ROUTE_ID = "ROUTE_ID";
14
15
  export {
15
16
  DEFAULT_ENTRY,
16
17
  DEFAULT_LAYOUT,
@@ -23,6 +24,7 @@ export {
23
24
  MODERN_JS_ROUTE_SERVER_LOADER,
24
25
  MODERN_JS_SERVER_DIR,
25
26
  PLUGIN_IDENTIFIER,
27
+ ROUTE_ID,
26
28
  SEPARATOR,
27
29
  isDev
28
30
  };
@@ -1,7 +1,7 @@
1
1
  import { getInstance } from "@module-federation/enhanced/runtime";
2
2
  import { MF_ROUTES, DEFAULT_ENTRY } from "../../constant";
3
3
  import { injectRemoteRoutes } from "./utils";
4
- const ssrDataLoaderPlugin = ({ metaName }) => {
4
+ const ssrDataLoaderPlugin = ({ metaName, dataLoaderRemotes }) => {
5
5
  return {
6
6
  name: "@modern-js/plugin-mf-data-loader",
7
7
  post: [
@@ -12,7 +12,7 @@ const ssrDataLoaderPlugin = ({ metaName }) => {
12
12
  const remoteRoutesMap = {};
13
13
  return {
14
14
  async beforeRender() {
15
- console.log("init");
15
+ console.log("init ", dataLoaderRemotes);
16
16
  if (Object.keys(remoteRoutesMap).length) {
17
17
  return;
18
18
  }
@@ -25,6 +25,9 @@ const ssrDataLoaderPlugin = ({ metaName }) => {
25
25
  return;
26
26
  }
27
27
  await Promise.all(instance.options.remotes.map(async (remote) => {
28
+ if (!dataLoaderRemotes.includes(remote.name)) {
29
+ return;
30
+ }
28
31
  const remoteId = `${remote.name}/${DEFAULT_ENTRY}/${MF_ROUTES}`;
29
32
  const { routes } = await instance.loadRemote(remoteId);
30
33
  remoteRoutesMap[remote.name] = {
@@ -3,5 +3,8 @@ export declare function generateRouteFile(options: {
3
3
  appDirectory: string;
4
4
  }): Promise<{
5
5
  entries: string[];
6
- remotePathMap: Record<string, string>;
6
+ remotePathMap: Record<string, {
7
+ name: string;
8
+ path: string;
9
+ }>;
7
10
  }>;
@@ -3,8 +3,8 @@ import { init } from '@module-federation/enhanced/runtime';
3
3
  type MFRuntimeOptions = Parameters<typeof init>[0];
4
4
  export type DataLoaderServerPluginOptions = {
5
5
  runtimeOptions: MFRuntimeOptions;
6
- partialSSRRemotes: string[];
6
+ dataLoaderRemotes: string[];
7
7
  ssrByRouteIdsMap: Record<string, string>;
8
8
  };
9
- declare const _default: ({ runtimeOptions, partialSSRRemotes, ssrByRouteIdsMap, }: DataLoaderServerPluginOptions) => ServerPlugin;
9
+ declare const _default: ({ runtimeOptions, dataLoaderRemotes, ssrByRouteIdsMap, }: DataLoaderServerPluginOptions) => ServerPlugin;
10
10
  export default _default;
@@ -6,5 +6,6 @@ export type RemoteRoutesMap = Record<string, {
6
6
  export declare function fetchRemoteFullRoutesMap(options: {
7
7
  remotePath: string;
8
8
  instance: FederationHost;
9
+ dataLoaderRemotes: string[];
9
10
  }): Promise<RemoteRoutesMap>;
10
- export declare function getSSRByRouteIds(partialSSRRemotes: string[], remoteRoutesMap: RemoteRoutesMap): undefined | string[];
11
+ export declare function getSSRByRouteIds(dataLoaderRemotes: string[], remoteRoutesMap: RemoteRoutesMap): undefined | string[];
@@ -11,3 +11,4 @@ export declare const MF_ROUTES = "mf-routes";
11
11
  export declare const DEFAULT_ENTRY = "main";
12
12
  export declare const DEFAULT_LAYOUT = "layout";
13
13
  export declare const MF_JSON = "mf.json";
14
+ export declare const ROUTE_ID = "ROUTE_ID";
@@ -1,4 +1,5 @@
1
1
  import type { Plugin } from '@modern-js/runtime';
2
- export declare const ssrDataLoaderPlugin: ({ metaName, }: {
2
+ export declare const ssrDataLoaderPlugin: ({ metaName, dataLoaderRemotes, }: {
3
3
  metaName: string;
4
+ dataLoaderRemotes: string[];
4
5
  }) => Plugin;
@@ -21,7 +21,6 @@ export interface InternalModernPluginOptions {
21
21
  export type BundlerPlugin = WebpackModuleFederationPlugin | RspackModuleFederationPlugin;
22
22
  export type TransformRuntimeOptions = (mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions) => Parameters<typeof init>[0];
23
23
  export type DataLoaderOptions = {
24
- partialSSRRemotes?: string[];
25
24
  serverPlugin?: string;
26
25
  transformRuntimeOptions?: TransformRuntimeOptions;
27
26
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/modern-js",
3
- "version": "0.0.0-next-20240913095223",
3
+ "version": "0.0.0-next-20240918073359",
4
4
  "files": [
5
5
  "dist/",
6
6
  "types.d.ts",
@@ -94,9 +94,9 @@
94
94
  "@babel/parser": "7.25.3",
95
95
  "@babel/traverse": "7.25.3",
96
96
  "@babel/types": "7.25.2",
97
- "@module-federation/sdk": "0.0.0-next-20240913095223",
98
- "@module-federation/enhanced": "0.0.0-next-20240913095223",
99
- "@module-federation/node": "0.0.0-next-20240913095223"
97
+ "@module-federation/sdk": "0.0.0-next-20240918073359",
98
+ "@module-federation/enhanced": "0.0.0-next-20240918073359",
99
+ "@module-federation/node": "0.0.0-next-20240918073359"
100
100
  },
101
101
  "devDependencies": {
102
102
  "@types/babel__traverse": "7.20.6",
@@ -108,7 +108,7 @@
108
108
  "@modern-js/runtime": "^2.59.0",
109
109
  "@modern-js/module-tools": "^2.59.0",
110
110
  "@modern-js/tsconfig": "^2.59.0",
111
- "@module-federation/manifest": "0.0.0-next-20240913095223"
111
+ "@module-federation/manifest": "0.0.0-next-20240918073359"
112
112
  },
113
113
  "peerDependencies": {
114
114
  "@modern-js/runtime": "^2.59.0",