@module-federation/modern-js 0.14.2 → 0.15.0

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 (122) hide show
  1. package/dist/LICENSE +21 -0
  2. package/dist/cjs/cli/configPlugin.js +381 -0
  3. package/dist/cjs/cli/configPlugin.spec.js +114 -0
  4. package/dist/cjs/cli/index.js +95 -0
  5. package/dist/cjs/cli/mfRuntimePlugins/auto-fetch-data.js +98 -0
  6. package/dist/cjs/cli/mfRuntimePlugins/inject-node-fetch.js +44 -0
  7. package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +82 -0
  8. package/dist/cjs/cli/mfRuntimePlugins/shared-strategy.js +43 -0
  9. package/dist/cjs/cli/server/data-fetch-server-plugin.js +189 -0
  10. package/dist/cjs/cli/ssrPlugin.js +236 -0
  11. package/dist/cjs/cli/utils.js +90 -0
  12. package/dist/cjs/constant.js +78 -0
  13. package/dist/cjs/interfaces/bundler.js +16 -0
  14. package/dist/cjs/interfaces/global.js +16 -0
  15. package/dist/cjs/logger.js +27 -0
  16. package/dist/cjs/runtime/AwaitDataFetch.js +144 -0
  17. package/dist/cjs/runtime/createRemoteComponent.js +327 -0
  18. package/dist/cjs/runtime/index.js +50 -0
  19. package/dist/cjs/runtime/wrapNoSSR.js +35 -0
  20. package/dist/cjs/server/fileCache.js +84 -0
  21. package/dist/cjs/server/fileCache.spec.js +28 -0
  22. package/dist/cjs/server/index.js +58 -0
  23. package/dist/cjs/server/staticMiddleware.js +77 -0
  24. package/dist/cjs/server/staticMiddleware.spec.js +185 -0
  25. package/dist/cjs/ssr-runtime/SSRLiveReload.js +43 -0
  26. package/dist/cjs/ssr-runtime/devPlugin.js +71 -0
  27. package/dist/cjs/ssr-runtime/downgrade.js +114 -0
  28. package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +136 -0
  29. package/dist/cjs/types/index.js +16 -0
  30. package/dist/cjs/utils/dataFetch.js +211 -0
  31. package/dist/cjs/utils/index.js +54 -0
  32. package/dist/esm/cli/configPlugin.js +388 -0
  33. package/dist/esm/cli/configPlugin.spec.js +110 -0
  34. package/dist/esm/cli/index.js +90 -0
  35. package/dist/esm/cli/mfRuntimePlugins/auto-fetch-data.js +74 -0
  36. package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js +16 -0
  37. package/dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.js +74 -0
  38. package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +25 -0
  39. package/dist/esm/cli/server/data-fetch-server-plugin.js +237 -0
  40. package/dist/esm/cli/ssrPlugin.js +219 -0
  41. package/dist/esm/cli/utils.js +53 -0
  42. package/dist/esm/constant.js +42 -0
  43. package/dist/esm/interfaces/bundler.js +0 -0
  44. package/dist/esm/interfaces/global.js +0 -0
  45. package/dist/esm/logger.js +7 -0
  46. package/dist/esm/runtime/AwaitDataFetch.js +131 -0
  47. package/dist/esm/runtime/createRemoteComponent.js +417 -0
  48. package/dist/esm/runtime/index.js +22 -0
  49. package/dist/esm/runtime/wrapNoSSR.js +12 -0
  50. package/dist/esm/server/fileCache.js +98 -0
  51. package/dist/esm/server/fileCache.spec.js +50 -0
  52. package/dist/esm/server/index.js +36 -0
  53. package/dist/esm/server/staticMiddleware.js +81 -0
  54. package/dist/esm/server/staticMiddleware.spec.js +328 -0
  55. package/dist/esm/ssr-runtime/SSRLiveReload.js +26 -0
  56. package/dist/esm/ssr-runtime/devPlugin.js +73 -0
  57. package/dist/esm/ssr-runtime/downgrade.js +150 -0
  58. package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js +177 -0
  59. package/dist/esm/types/index.js +0 -0
  60. package/dist/esm/utils/dataFetch.js +237 -0
  61. package/dist/esm/utils/index.js +28 -0
  62. package/dist/esm-node/cli/configPlugin.js +340 -0
  63. package/dist/esm-node/cli/configPlugin.spec.js +91 -0
  64. package/dist/esm-node/cli/index.js +70 -0
  65. package/dist/esm-node/cli/mfRuntimePlugins/auto-fetch-data.js +68 -0
  66. package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.js +14 -0
  67. package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +62 -0
  68. package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +23 -0
  69. package/dist/esm-node/cli/server/data-fetch-server-plugin.js +159 -0
  70. package/dist/esm-node/cli/ssrPlugin.js +200 -0
  71. package/dist/esm-node/cli/utils.js +53 -0
  72. package/dist/esm-node/constant.js +42 -0
  73. package/dist/esm-node/interfaces/bundler.js +0 -0
  74. package/dist/esm-node/interfaces/global.js +0 -0
  75. package/dist/esm-node/logger.js +7 -0
  76. package/dist/esm-node/runtime/AwaitDataFetch.js +109 -0
  77. package/dist/esm-node/runtime/createRemoteComponent.js +291 -0
  78. package/dist/esm-node/runtime/index.js +22 -0
  79. package/dist/esm-node/runtime/wrapNoSSR.js +11 -0
  80. package/dist/esm-node/server/fileCache.js +49 -0
  81. package/dist/esm-node/server/fileCache.spec.js +27 -0
  82. package/dist/esm-node/server/index.js +34 -0
  83. package/dist/esm-node/server/staticMiddleware.js +43 -0
  84. package/dist/esm-node/server/staticMiddleware.spec.js +162 -0
  85. package/dist/esm-node/ssr-runtime/SSRLiveReload.js +19 -0
  86. package/dist/esm-node/ssr-runtime/devPlugin.js +37 -0
  87. package/dist/esm-node/ssr-runtime/downgrade.js +88 -0
  88. package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.js +101 -0
  89. package/dist/esm-node/types/index.js +0 -0
  90. package/dist/esm-node/utils/dataFetch.js +166 -0
  91. package/dist/esm-node/utils/index.js +27 -0
  92. package/dist/types/cli/configPlugin.d.ts +21 -0
  93. package/dist/types/cli/configPlugin.spec.d.ts +1 -0
  94. package/dist/types/cli/index.d.ts +6 -0
  95. package/dist/types/cli/mfRuntimePlugins/auto-fetch-data.d.ts +3 -0
  96. package/dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts +3 -0
  97. package/dist/types/cli/mfRuntimePlugins/resolve-entry-ipv4.d.ts +3 -0
  98. package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +3 -0
  99. package/dist/types/cli/server/data-fetch-server-plugin.d.ts +3 -0
  100. package/dist/types/cli/ssrPlugin.d.ts +6 -0
  101. package/dist/types/cli/utils.d.ts +6 -0
  102. package/dist/types/constant.d.ts +25 -0
  103. package/dist/types/interfaces/bundler.d.ts +18 -0
  104. package/dist/types/interfaces/global.d.ts +27 -0
  105. package/dist/types/logger.d.ts +2 -0
  106. package/dist/types/runtime/AwaitDataFetch.d.ts +19 -0
  107. package/dist/types/runtime/createRemoteComponent.d.ts +26 -0
  108. package/dist/types/runtime/index.d.ts +10 -0
  109. package/dist/types/runtime/wrapNoSSR.d.ts +9 -0
  110. package/dist/types/server/fileCache.d.ts +14 -0
  111. package/dist/types/server/fileCache.spec.d.ts +1 -0
  112. package/dist/types/server/index.d.ts +4 -0
  113. package/dist/types/server/staticMiddleware.d.ts +6 -0
  114. package/dist/types/server/staticMiddleware.spec.d.ts +1 -0
  115. package/dist/types/ssr-runtime/SSRLiveReload.d.ts +1 -0
  116. package/dist/types/ssr-runtime/devPlugin.d.ts +2 -0
  117. package/dist/types/ssr-runtime/downgrade.d.ts +4 -0
  118. package/dist/types/ssr-runtime/injectDataFetchFunctionPlugin.d.ts +7 -0
  119. package/dist/types/types/index.d.ts +24 -0
  120. package/dist/types/utils/dataFetch.d.ts +26 -0
  121. package/dist/types/utils/index.d.ts +15 -0
  122. package/package.json +37 -12
@@ -0,0 +1,110 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
+ import { it, expect, describe } from "vitest";
4
+ import { patchMFConfig } from "./configPlugin";
5
+ import { getIPV4 } from "./utils";
6
+ var mfConfig = {
7
+ name: "host",
8
+ filename: "remoteEntry.js",
9
+ remotes: {
10
+ remote: "http://localhost:3000/remoteEntry.js"
11
+ },
12
+ shared: {
13
+ react: {
14
+ singleton: true,
15
+ eager: true
16
+ },
17
+ "react-dom": {
18
+ singleton: true,
19
+ eager: true
20
+ }
21
+ }
22
+ };
23
+ describe("patchMFConfig", /* @__PURE__ */ _async_to_generator(function() {
24
+ return _ts_generator(this, function(_state) {
25
+ it("patchMFConfig: server", /* @__PURE__ */ _async_to_generator(function() {
26
+ var patchedConfig, ipv4;
27
+ return _ts_generator(this, function(_state2) {
28
+ patchedConfig = JSON.parse(JSON.stringify(mfConfig));
29
+ patchMFConfig(patchedConfig, true);
30
+ ipv4 = getIPV4();
31
+ expect(patchedConfig).toStrictEqual({
32
+ dev: false,
33
+ dts: false,
34
+ filename: "remoteEntry.js",
35
+ library: {
36
+ name: "host",
37
+ type: "commonjs-module"
38
+ },
39
+ name: "host",
40
+ remotes: {
41
+ remote: "http://".concat(ipv4, ":3000/remoteEntry.js")
42
+ },
43
+ remoteType: "script",
44
+ runtimePlugins: [
45
+ require.resolve("@module-federation/modern-js/shared-strategy"),
46
+ require.resolve("@module-federation/modern-js/auto-fetch-data"),
47
+ require.resolve("@module-federation/node/runtimePlugin"),
48
+ require.resolve("@module-federation/modern-js/inject-node-fetch")
49
+ ],
50
+ shared: {
51
+ react: {
52
+ eager: true,
53
+ singleton: true
54
+ },
55
+ "react-dom": {
56
+ eager: true,
57
+ singleton: true
58
+ }
59
+ }
60
+ });
61
+ return [
62
+ 2
63
+ ];
64
+ });
65
+ }));
66
+ it("patchMFConfig: client", /* @__PURE__ */ _async_to_generator(function() {
67
+ var patchedConfig, ipv4;
68
+ return _ts_generator(this, function(_state2) {
69
+ patchedConfig = JSON.parse(JSON.stringify(mfConfig));
70
+ patchMFConfig(patchedConfig, false);
71
+ ipv4 = getIPV4();
72
+ expect(patchedConfig).toStrictEqual({
73
+ filename: "remoteEntry.js",
74
+ name: "host",
75
+ remotes: {
76
+ remote: "http://".concat(ipv4, ":3000/remoteEntry.js")
77
+ },
78
+ remoteType: "script",
79
+ runtimePlugins: [
80
+ require.resolve("@module-federation/modern-js/shared-strategy"),
81
+ require.resolve("@module-federation/modern-js/auto-fetch-data")
82
+ ],
83
+ shared: {
84
+ react: {
85
+ eager: true,
86
+ singleton: true
87
+ },
88
+ "react-dom": {
89
+ eager: true,
90
+ singleton: true
91
+ }
92
+ },
93
+ dts: {
94
+ consumeTypes: {
95
+ runtimePkgs: [
96
+ "@module-federation/modern-js/runtime"
97
+ ]
98
+ }
99
+ }
100
+ });
101
+ return [
102
+ 2
103
+ ];
104
+ });
105
+ }));
106
+ return [
107
+ 2
108
+ ];
109
+ });
110
+ }));
@@ -0,0 +1,90 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _type_of } from "@swc/helpers/_/_type_of";
3
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
+ import { ModuleFederationPlugin as WebpackModuleFederationPlugin, AsyncBoundaryPlugin } from "@module-federation/enhanced";
5
+ import { ModuleFederationPlugin as RspackModuleFederationPlugin } from "@module-federation/enhanced/rspack";
6
+ import { moduleFederationConfigPlugin } from "./configPlugin";
7
+ import { moduleFederationSSRPlugin } from "./ssrPlugin";
8
+ import { isWebTarget } from "./utils";
9
+ var moduleFederationPlugin = function() {
10
+ var userConfig = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
11
+ var _userConfig_fetchServerQuery;
12
+ var internalModernPluginOptions = {
13
+ csrConfig: void 0,
14
+ ssrConfig: void 0,
15
+ browserPlugin: void 0,
16
+ nodePlugin: void 0,
17
+ distOutputDir: "",
18
+ originPluginOptions: userConfig,
19
+ remoteIpStrategy: userConfig === null || userConfig === void 0 ? void 0 : userConfig.remoteIpStrategy,
20
+ userConfig: userConfig || {},
21
+ fetchServerQuery: (_userConfig_fetchServerQuery = userConfig.fetchServerQuery) !== null && _userConfig_fetchServerQuery !== void 0 ? _userConfig_fetchServerQuery : void 0
22
+ };
23
+ return {
24
+ name: "@modern-js/plugin-module-federation",
25
+ setup: function() {
26
+ var _ref = _async_to_generator(function(api) {
27
+ var modernjsConfig;
28
+ return _ts_generator(this, function(_state) {
29
+ modernjsConfig = api.getConfig();
30
+ api.modifyBundlerChain(function(chain) {
31
+ var bundlerType = api.getAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
32
+ var browserPluginOptions = internalModernPluginOptions.csrConfig;
33
+ var MFPlugin = bundlerType === "webpack" ? WebpackModuleFederationPlugin : RspackModuleFederationPlugin;
34
+ if (isWebTarget(chain.get("target"))) {
35
+ chain.plugin("plugin-module-federation").use(MFPlugin, [
36
+ browserPluginOptions
37
+ ]).init(function(Plugin, args) {
38
+ internalModernPluginOptions.browserPlugin = new Plugin(args[0]);
39
+ return internalModernPluginOptions.browserPlugin;
40
+ });
41
+ }
42
+ if (bundlerType === "webpack") {
43
+ var _modernjsConfig_source;
44
+ var enableAsyncEntry = (_modernjsConfig_source = modernjsConfig.source) === null || _modernjsConfig_source === void 0 ? void 0 : _modernjsConfig_source.enableAsyncEntry;
45
+ if (!enableAsyncEntry && browserPluginOptions.async !== false) {
46
+ var asyncBoundaryPluginOptions = _type_of(browserPluginOptions.async) === "object" ? browserPluginOptions.async : {
47
+ eager: function(module) {
48
+ return module && /\.federation/.test((module === null || module === void 0 ? void 0 : module.request) || "");
49
+ },
50
+ excludeChunk: function(chunk) {
51
+ return chunk.name === browserPluginOptions.name;
52
+ }
53
+ };
54
+ chain.plugin("async-boundary-plugin").use(AsyncBoundaryPlugin, [
55
+ asyncBoundaryPluginOptions
56
+ ]);
57
+ }
58
+ }
59
+ });
60
+ api._internalServerPlugins(function(param) {
61
+ var plugins = param.plugins;
62
+ plugins.push({
63
+ name: "@module-federation/modern-js/server"
64
+ });
65
+ return {
66
+ plugins
67
+ };
68
+ });
69
+ return [
70
+ 2
71
+ ];
72
+ });
73
+ });
74
+ return function(api) {
75
+ return _ref.apply(this, arguments);
76
+ };
77
+ }(),
78
+ usePlugins: [
79
+ moduleFederationConfigPlugin(internalModernPluginOptions),
80
+ moduleFederationSSRPlugin(internalModernPluginOptions)
81
+ ]
82
+ };
83
+ };
84
+ var cli_default = moduleFederationPlugin;
85
+ import { createModuleFederationConfig } from "@module-federation/enhanced";
86
+ export {
87
+ createModuleFederationConfig,
88
+ cli_default as default,
89
+ moduleFederationPlugin
90
+ };
@@ -0,0 +1,74 @@
1
+ import { getDataFetchInfo, initDataFetchMap, getDataFetchItem, getDataFetchMap, isCSROnly } from "../../utils";
2
+ import logger from "../../logger";
3
+ import { getDataFetchMapKey, isDataLoaderExpose, loadDataFetchModule } from "../../utils/dataFetch";
4
+ import { MF_DATA_FETCH_TYPE, MF_DATA_FETCH_STATUS } from "../../constant";
5
+ import { DATA_FETCH_CLIENT_SUFFIX } from "@module-federation/rsbuild-plugin/constant";
6
+ var autoFetchData = function() {
7
+ return {
8
+ name: "auto-fetch-data-plugin",
9
+ beforeInit: function beforeInit(args) {
10
+ initDataFetchMap();
11
+ return args;
12
+ },
13
+ afterLoadSnapshot: function afterLoadSnapshot(args) {
14
+ var id = args.id, moduleInfo = args.moduleInfo, remoteSnapshot = args.remoteSnapshot, host = args.host;
15
+ if (typeof id === "string" && isDataLoaderExpose(id)) {
16
+ return args;
17
+ }
18
+ if (!remoteSnapshot || !id || !("modules" in remoteSnapshot)) {
19
+ return args;
20
+ }
21
+ var name = moduleInfo.name, alias = moduleInfo.alias;
22
+ var dataFetchInfo = getDataFetchInfo({
23
+ name,
24
+ alias,
25
+ id,
26
+ remoteSnapshot
27
+ });
28
+ if (!dataFetchInfo) {
29
+ return args;
30
+ }
31
+ var dataFetchId = dataFetchInfo.dataFetchId, dataFetchName = dataFetchInfo.dataFetchName;
32
+ var dataFetchMapKey = getDataFetchMapKey(dataFetchInfo, {
33
+ name: host.name,
34
+ version: host.options.version
35
+ });
36
+ logger.debug("======= auto fetch plugin dataFetchMapKey: ", dataFetchMapKey);
37
+ if (!dataFetchMapKey) {
38
+ return args;
39
+ }
40
+ var dataFetchItem = getDataFetchItem(dataFetchMapKey);
41
+ if (dataFetchItem) {
42
+ return args;
43
+ }
44
+ var dataFetchMap = getDataFetchMap();
45
+ var downgradeType = remoteSnapshot.modules.find(function(module) {
46
+ return module.moduleName === "".concat(dataFetchName).concat(DATA_FETCH_CLIENT_SUFFIX);
47
+ }) ? MF_DATA_FETCH_TYPE.FETCH_CLIENT : MF_DATA_FETCH_TYPE.FETCH_SERVER;
48
+ var finalDataFetchId = dataFetchId;
49
+ if (typeof window !== "undefined") {
50
+ finalDataFetchId = downgradeType === MF_DATA_FETCH_TYPE.FETCH_CLIENT ? "".concat(dataFetchId).concat(DATA_FETCH_CLIENT_SUFFIX) : dataFetchId;
51
+ }
52
+ var getDataFetchGetter = function() {
53
+ return loadDataFetchModule(host, finalDataFetchId);
54
+ };
55
+ var dataFetchFnItem = [
56
+ getDataFetchGetter,
57
+ downgradeType
58
+ ];
59
+ if (typeof window === "undefined" || isCSROnly()) {
60
+ dataFetchFnItem.push(getDataFetchGetter());
61
+ }
62
+ dataFetchMap[dataFetchMapKey] = [
63
+ dataFetchFnItem,
64
+ void 0,
65
+ MF_DATA_FETCH_STATUS.AWAIT
66
+ ];
67
+ return args;
68
+ }
69
+ };
70
+ };
71
+ var auto_fetch_data_default = autoFetchData;
72
+ export {
73
+ auto_fetch_data_default as default
74
+ };
@@ -0,0 +1,16 @@
1
+ import nodeFetch from "node-fetch";
2
+ var injectNodeFetchPlugin = function() {
3
+ return {
4
+ name: "inject-node-fetch-plugin",
5
+ beforeInit: function beforeInit(args) {
6
+ if (!globalThis.fetch) {
7
+ globalThis.fetch = nodeFetch;
8
+ }
9
+ return args;
10
+ }
11
+ };
12
+ };
13
+ var inject_node_fetch_default = injectNodeFetchPlugin;
14
+ export {
15
+ inject_node_fetch_default as default
16
+ };
@@ -0,0 +1,74 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
+ import { LOCALHOST } from "../../constant";
4
+ var ipv4 = typeof FEDERATION_IPV4 !== "undefined" ? FEDERATION_IPV4 : "127.0.0.1";
5
+ var remoteIpStrategy = typeof REMOTE_IP_STRATEGY !== "undefined" ? REMOTE_IP_STRATEGY : "inherit";
6
+ function replaceObjectLocalhost(key, obj) {
7
+ if (remoteIpStrategy !== "ipv4") {
8
+ return;
9
+ }
10
+ if (!(key in obj)) {
11
+ return;
12
+ }
13
+ var remote = obj[key];
14
+ if (remote && typeof remote === "string" && remote.includes(LOCALHOST)) {
15
+ obj[key] = replaceLocalhost(remote);
16
+ }
17
+ }
18
+ function replaceLocalhost(url) {
19
+ return url.replace(LOCALHOST, ipv4);
20
+ }
21
+ var resolveEntryIpv4Plugin = function() {
22
+ return {
23
+ name: "resolve-entry-ipv4",
24
+ beforeRegisterRemote: function beforeRegisterRemote(args) {
25
+ var remote = args.remote;
26
+ replaceObjectLocalhost("entry", remote);
27
+ return args;
28
+ },
29
+ afterResolve: function afterResolve(args) {
30
+ return _async_to_generator(function() {
31
+ var remoteInfo;
32
+ return _ts_generator(this, function(_state) {
33
+ remoteInfo = args.remoteInfo;
34
+ replaceObjectLocalhost("entry", remoteInfo);
35
+ return [
36
+ 2,
37
+ args
38
+ ];
39
+ });
40
+ })();
41
+ },
42
+ beforeLoadRemoteSnapshot: function beforeLoadRemoteSnapshot(args) {
43
+ var moduleInfo = args.moduleInfo;
44
+ if ("entry" in moduleInfo) {
45
+ replaceObjectLocalhost("entry", moduleInfo);
46
+ return args;
47
+ }
48
+ if ("version" in moduleInfo) {
49
+ replaceObjectLocalhost("version", moduleInfo);
50
+ }
51
+ return args;
52
+ },
53
+ loadRemoteSnapshot: function loadRemoteSnapshot(args) {
54
+ var remoteSnapshot = args.remoteSnapshot;
55
+ if ("publicPath" in remoteSnapshot) {
56
+ replaceObjectLocalhost("publicPath", remoteSnapshot);
57
+ }
58
+ if ("getPublicPath" in remoteSnapshot) {
59
+ replaceObjectLocalhost("getPublicPath", remoteSnapshot);
60
+ }
61
+ if (remoteSnapshot.remotesInfo) {
62
+ Object.keys(remoteSnapshot.remotesInfo).forEach(function(key) {
63
+ var remoteInfo = remoteSnapshot.remotesInfo[key];
64
+ replaceObjectLocalhost("matchedVersion", remoteInfo);
65
+ });
66
+ }
67
+ return args;
68
+ }
69
+ };
70
+ };
71
+ var resolve_entry_ipv4_default = resolveEntryIpv4Plugin;
72
+ export {
73
+ resolve_entry_ipv4_default as default
74
+ };
@@ -0,0 +1,25 @@
1
+ var sharedStrategy = function() {
2
+ return {
3
+ name: "shared-strategy-plugin",
4
+ beforeInit: function beforeInit(args) {
5
+ var userOptions = args.userOptions;
6
+ var shared = userOptions.shared;
7
+ if (shared) {
8
+ Object.keys(shared).forEach(function(sharedKey) {
9
+ var sharedConfigs = shared[sharedKey];
10
+ var arraySharedConfigs = Array.isArray(sharedConfigs) ? sharedConfigs : [
11
+ sharedConfigs
12
+ ];
13
+ arraySharedConfigs.forEach(function(s) {
14
+ s.strategy = "loaded-first";
15
+ });
16
+ });
17
+ }
18
+ return args;
19
+ }
20
+ };
21
+ };
22
+ var shared_strategy_default = sharedStrategy;
23
+ export {
24
+ shared_strategy_default as default
25
+ };
@@ -0,0 +1,237 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
3
+ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
4
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
5
+ import { DATA_FETCH_QUERY, MF_DATA_FETCH_STATUS } from "../../constant";
6
+ import logger from "../../logger";
7
+ import { getDataFetchMap } from "../../utils";
8
+ import { fetchData, initDataFetchMap, loadDataFetchModule } from "../../utils/dataFetch";
9
+ import { SEPARATOR, MANIFEST_EXT } from "@module-federation/sdk";
10
+ function wrapSetTimeout(targetPromise) {
11
+ var delay = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 2e4, id = arguments.length > 2 ? arguments[2] : void 0;
12
+ if (targetPromise && typeof targetPromise.then === "function") {
13
+ return new Promise(function(resolve, reject) {
14
+ var timeoutId = setTimeout(function() {
15
+ logger.warn("Data fetch for ID ".concat(id, " timed out after 20 seconds."));
16
+ reject(new Error("Data fetch for ID ".concat(id, " timed out after 20 seconds")));
17
+ }, delay);
18
+ targetPromise.then(function(value) {
19
+ clearTimeout(timeoutId);
20
+ resolve(value);
21
+ }).catch(function(err) {
22
+ clearTimeout(timeoutId);
23
+ reject(err);
24
+ });
25
+ });
26
+ }
27
+ }
28
+ function addProtocol(url) {
29
+ if (url.startsWith("//")) {
30
+ return "https:" + url;
31
+ }
32
+ return url;
33
+ }
34
+ var getDecodeQuery = function(url, name) {
35
+ var res = url.searchParams.get(name);
36
+ if (!res) {
37
+ return null;
38
+ }
39
+ return decodeURIComponent(res);
40
+ };
41
+ var middleware = function() {
42
+ var _ref = _async_to_generator(function(ctx, next) {
43
+ var _globalThis___FEDERATION__, url, dataFetchId, params, remoteInfo, remoteInfoQuery, _dataFetchMap_dataFetchId, _dataFetchMap_dataFetchId1, dataFetchMap, fetchDataPromise, targetPromise, wrappedPromise, res, hostInstance, remoteEntry, remote, _hostInstance_snapshotHandler_getGlobalRemoteInfo, hostGlobalSnapshot, remoteSnapshot, dataFetchItem, callFetchDataPromise, wrappedPromise1, res1, remoteId, hostInstance1, dataFetchFn, data, e;
44
+ return _ts_generator(this, function(_state) {
45
+ switch (_state.label) {
46
+ case 0:
47
+ try {
48
+ url = new URL(ctx.req.url);
49
+ dataFetchId = getDecodeQuery(url, DATA_FETCH_QUERY);
50
+ params = JSON.parse(getDecodeQuery(url, "params") || "{}");
51
+ remoteInfoQuery = getDecodeQuery(url, "remoteInfo");
52
+ remoteInfo = remoteInfoQuery ? JSON.parse(remoteInfoQuery) : null;
53
+ } catch (e2) {
54
+ logger.error("fetch data from server, error: ", e2);
55
+ return [
56
+ 2,
57
+ next()
58
+ ];
59
+ }
60
+ if (!dataFetchId) {
61
+ return [
62
+ 2,
63
+ next()
64
+ ];
65
+ }
66
+ logger.log("fetch data from server, dataFetchId: ", dataFetchId);
67
+ logger.debug("fetch data from server, moduleInfo: ", (_globalThis___FEDERATION__ = globalThis.__FEDERATION__) === null || _globalThis___FEDERATION__ === void 0 ? void 0 : _globalThis___FEDERATION__.moduleInfo);
68
+ _state.label = 1;
69
+ case 1:
70
+ _state.trys.push([
71
+ 1,
72
+ 9,
73
+ ,
74
+ 10
75
+ ]);
76
+ dataFetchMap = getDataFetchMap();
77
+ if (!dataFetchMap) {
78
+ initDataFetchMap();
79
+ }
80
+ fetchDataPromise = (_dataFetchMap_dataFetchId = dataFetchMap[dataFetchId]) === null || _dataFetchMap_dataFetchId === void 0 ? void 0 : _dataFetchMap_dataFetchId[1];
81
+ logger.debug("fetch data from server, fetchDataPromise: ", fetchDataPromise);
82
+ if (!(fetchDataPromise && ((_dataFetchMap_dataFetchId1 = dataFetchMap[dataFetchId]) === null || _dataFetchMap_dataFetchId1 === void 0 ? void 0 : _dataFetchMap_dataFetchId1[2]) !== MF_DATA_FETCH_STATUS.ERROR))
83
+ return [
84
+ 3,
85
+ 4
86
+ ];
87
+ targetPromise = fetchDataPromise[0];
88
+ wrappedPromise = wrapSetTimeout(targetPromise, 2e4, dataFetchId);
89
+ if (!wrappedPromise)
90
+ return [
91
+ 3,
92
+ 3
93
+ ];
94
+ return [
95
+ 4,
96
+ wrappedPromise
97
+ ];
98
+ case 2:
99
+ res = _state.sent();
100
+ logger.log("fetch data from server, fetchDataPromise res: ", res);
101
+ return [
102
+ 2,
103
+ ctx.json(res)
104
+ ];
105
+ case 3:
106
+ logger.error("Expected a Promise from fetchDataPromise[0] for dataFetchId ".concat(dataFetchId, ", but received:"), targetPromise, "Will try call new dataFetch again...");
107
+ _state.label = 4;
108
+ case 4:
109
+ if (remoteInfo) {
110
+ try {
111
+ hostInstance = globalThis.__FEDERATION__.__INSTANCES__[0];
112
+ remoteEntry = "".concat(addProtocol(remoteInfo.ssrPublicPath) + remoteInfo.ssrRemoteEntry);
113
+ if (!hostInstance) {
114
+ throw new Error("host instance not found!");
115
+ }
116
+ remote = hostInstance.options.remotes.find(function(remote2) {
117
+ return remote2.name === remoteInfo.name;
118
+ });
119
+ logger.debug("find remote: ", JSON.stringify(remote));
120
+ if (!remote) {
121
+ hostInstance.registerRemotes([
122
+ {
123
+ name: remoteInfo.name,
124
+ entry: remoteEntry,
125
+ entryGlobalName: remoteInfo.globalName
126
+ }
127
+ ]);
128
+ } else if (!("entry" in remote) || !remote.entry.includes(MANIFEST_EXT)) {
129
+ _hostInstance_snapshotHandler_getGlobalRemoteInfo = hostInstance.snapshotHandler.getGlobalRemoteInfo(remoteInfo), hostGlobalSnapshot = _hostInstance_snapshotHandler_getGlobalRemoteInfo.hostGlobalSnapshot, remoteSnapshot = _hostInstance_snapshotHandler_getGlobalRemoteInfo.remoteSnapshot;
130
+ logger.debug("find hostGlobalSnapshot: ", JSON.stringify(hostGlobalSnapshot));
131
+ logger.debug("find remoteSnapshot: ", JSON.stringify(remoteSnapshot));
132
+ if (!hostGlobalSnapshot || !remoteSnapshot) {
133
+ if ("version" in remote) {
134
+ delete remote.version;
135
+ }
136
+ remote.entry = remoteEntry;
137
+ remote.entryGlobalName = remoteInfo.globalName;
138
+ }
139
+ }
140
+ } catch (e2) {
141
+ ctx.status(500);
142
+ return [
143
+ 2,
144
+ ctx.text("failed to fetch ".concat(remoteInfo.name, " data, error:\n ").concat(e2))
145
+ ];
146
+ }
147
+ }
148
+ dataFetchItem = dataFetchMap[dataFetchId];
149
+ logger.debug("fetch data from server, dataFetchItem: ", dataFetchItem);
150
+ if (!dataFetchItem)
151
+ return [
152
+ 3,
153
+ 6
154
+ ];
155
+ callFetchDataPromise = fetchData(dataFetchId, _object_spread_props(_object_spread({}, params), {
156
+ isDowngrade: !remoteInfo
157
+ }));
158
+ wrappedPromise1 = wrapSetTimeout(callFetchDataPromise, 2e4, dataFetchId);
159
+ if (!wrappedPromise1)
160
+ return [
161
+ 3,
162
+ 6
163
+ ];
164
+ return [
165
+ 4,
166
+ wrappedPromise1
167
+ ];
168
+ case 5:
169
+ res1 = _state.sent();
170
+ logger.log("fetch data from server, dataFetchItem res: ", res1);
171
+ return [
172
+ 2,
173
+ ctx.json(res1)
174
+ ];
175
+ case 6:
176
+ remoteId = dataFetchId.split(SEPARATOR)[0];
177
+ hostInstance1 = globalThis.__FEDERATION__.__INSTANCES__[0];
178
+ if (!hostInstance1) {
179
+ throw new Error("host instance not found!");
180
+ }
181
+ return [
182
+ 4,
183
+ loadDataFetchModule(hostInstance1, remoteId)
184
+ ];
185
+ case 7:
186
+ dataFetchFn = _state.sent();
187
+ return [
188
+ 4,
189
+ dataFetchFn(_object_spread_props(_object_spread({}, params), {
190
+ isDowngrade: !remoteInfo
191
+ }))
192
+ ];
193
+ case 8:
194
+ data = _state.sent();
195
+ logger.log("fetch data from server, loadDataFetchModule res: ", data);
196
+ return [
197
+ 2,
198
+ ctx.json(data)
199
+ ];
200
+ case 9:
201
+ e = _state.sent();
202
+ logger.error("server plugin data fetch error: ", e);
203
+ ctx.status(500);
204
+ return [
205
+ 2,
206
+ ctx.text("failed to fetch ".concat(remoteInfo.name, " data, error:\n ").concat(e))
207
+ ];
208
+ case 10:
209
+ return [
210
+ 2
211
+ ];
212
+ }
213
+ });
214
+ });
215
+ return function middleware2(ctx, next) {
216
+ return _ref.apply(this, arguments);
217
+ };
218
+ }();
219
+ var dataFetchServePlugin = function() {
220
+ return {
221
+ name: "mf-data-fetch-server-plugin",
222
+ setup: function(api) {
223
+ api.onPrepare(function() {
224
+ var middlewares = api.getServerContext().middlewares;
225
+ middlewares.push({
226
+ name: "module-federation-serve-manifest",
227
+ // @ts-ignore type error
228
+ handler: middleware
229
+ });
230
+ });
231
+ }
232
+ };
233
+ };
234
+ var data_fetch_server_plugin_default = dataFetchServePlugin;
235
+ export {
236
+ data_fetch_server_plugin_default as default
237
+ };