@module-federation/modern-js 0.0.0-next-20250526074234 → 0.0.0-next-20250527094526

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 (109) hide show
  1. package/dist/cjs/cli/configPlugin.js +6 -8
  2. package/dist/cjs/cli/configPlugin.spec.js +1 -3
  3. package/dist/cjs/{runtime/wrapNoSSR.js → cli/constant.js} +6 -13
  4. package/dist/cjs/cli/index.js +9 -3
  5. package/dist/cjs/{logger.js → cli/logger.js} +1 -1
  6. package/dist/cjs/cli/manifest.js +3 -3
  7. package/dist/cjs/cli/ssrPlugin.js +15 -34
  8. package/dist/cjs/cli/utils.js +0 -5
  9. package/dist/cjs/constant.js +2 -49
  10. package/dist/cjs/runtime/createRemoteSSRComponent.js +201 -0
  11. package/dist/cjs/runtime/index.js +3 -15
  12. package/dist/cjs/server/fileCache.js +84 -0
  13. package/dist/cjs/server/fileCache.spec.js +28 -0
  14. package/dist/cjs/server/index.js +58 -0
  15. package/dist/cjs/server/staticMiddleware.js +77 -0
  16. package/dist/cjs/server/staticMiddleware.spec.js +185 -0
  17. package/dist/cjs/{interfaces/global.js → ssr-runtime/index.js} +8 -2
  18. package/dist/cjs/ssr-runtime/{devPlugin.js → plugin.js} +6 -9
  19. package/dist/esm/cli/configPlugin.js +7 -9
  20. package/dist/esm/cli/configPlugin.spec.js +1 -3
  21. package/dist/esm/cli/constant.js +4 -0
  22. package/dist/esm/cli/index.js +10 -3
  23. package/dist/esm/{logger.js → cli/logger.js} +1 -1
  24. package/dist/esm/cli/manifest.js +1 -1
  25. package/dist/esm/cli/ssrPlugin.js +8 -28
  26. package/dist/esm/cli/utils.js +0 -4
  27. package/dist/esm/constant.js +1 -37
  28. package/dist/esm/runtime/createRemoteSSRComponent.js +191 -0
  29. package/dist/esm/runtime/index.js +3 -13
  30. package/dist/esm/server/fileCache.js +98 -0
  31. package/dist/esm/server/fileCache.spec.js +50 -0
  32. package/dist/esm/server/index.js +36 -0
  33. package/dist/esm/server/staticMiddleware.js +81 -0
  34. package/dist/esm/server/staticMiddleware.spec.js +328 -0
  35. package/dist/esm/ssr-runtime/index.js +1 -0
  36. package/dist/esm/ssr-runtime/{devPlugin.js → plugin.js} +2 -5
  37. package/dist/esm-node/cli/configPlugin.js +7 -9
  38. package/dist/esm-node/cli/configPlugin.spec.js +1 -3
  39. package/dist/esm-node/cli/constant.js +4 -0
  40. package/dist/esm-node/cli/index.js +9 -3
  41. package/dist/esm-node/{logger.js → cli/logger.js} +1 -1
  42. package/dist/esm-node/cli/manifest.js +1 -1
  43. package/dist/esm-node/cli/ssrPlugin.js +8 -27
  44. package/dist/esm-node/cli/utils.js +0 -4
  45. package/dist/esm-node/constant.js +1 -37
  46. package/dist/esm-node/runtime/createRemoteSSRComponent.js +166 -0
  47. package/dist/esm-node/runtime/index.js +3 -13
  48. package/dist/esm-node/server/fileCache.js +49 -0
  49. package/dist/esm-node/server/fileCache.spec.js +27 -0
  50. package/dist/esm-node/server/index.js +34 -0
  51. package/dist/esm-node/server/staticMiddleware.js +43 -0
  52. package/dist/esm-node/server/staticMiddleware.spec.js +162 -0
  53. package/dist/esm-node/ssr-runtime/index.js +1 -0
  54. package/dist/esm-node/ssr-runtime/{devPlugin.js → plugin.js} +2 -5
  55. package/dist/types/cli/constant.d.ts +1 -0
  56. package/dist/types/cli/index.d.ts +0 -1
  57. package/dist/types/cli/utils.d.ts +0 -1
  58. package/dist/types/constant.d.ts +0 -23
  59. package/dist/types/runtime/createRemoteSSRComponent.d.ts +18 -0
  60. package/dist/types/runtime/index.d.ts +2 -7
  61. package/dist/types/server/fileCache.d.ts +14 -0
  62. package/dist/types/server/fileCache.spec.d.ts +1 -0
  63. package/dist/types/server/index.d.ts +4 -0
  64. package/dist/types/server/staticMiddleware.d.ts +6 -0
  65. package/dist/types/server/staticMiddleware.spec.d.ts +1 -0
  66. package/dist/types/ssr-runtime/index.d.ts +1 -0
  67. package/dist/types/ssr-runtime/plugin.d.ts +2 -0
  68. package/dist/types/types/index.d.ts +0 -2
  69. package/package.json +23 -39
  70. package/dist/cjs/cli/mfRuntimePlugins/auto-fetch-data.js +0 -98
  71. package/dist/cjs/cli/server/data-fetch-server-plugin.js +0 -192
  72. package/dist/cjs/runtime/AwaitDataFetch.js +0 -144
  73. package/dist/cjs/runtime/createRemoteComponent.js +0 -327
  74. package/dist/cjs/ssr-runtime/downgrade.js +0 -114
  75. package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -136
  76. package/dist/cjs/utils/dataFetch.js +0 -211
  77. package/dist/cjs/utils/index.js +0 -54
  78. package/dist/esm/cli/mfRuntimePlugins/auto-fetch-data.js +0 -74
  79. package/dist/esm/cli/server/data-fetch-server-plugin.js +0 -240
  80. package/dist/esm/interfaces/global.js +0 -0
  81. package/dist/esm/runtime/AwaitDataFetch.js +0 -131
  82. package/dist/esm/runtime/createRemoteComponent.js +0 -417
  83. package/dist/esm/runtime/wrapNoSSR.js +0 -12
  84. package/dist/esm/ssr-runtime/downgrade.js +0 -150
  85. package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -177
  86. package/dist/esm/utils/dataFetch.js +0 -237
  87. package/dist/esm/utils/index.js +0 -28
  88. package/dist/esm-node/cli/mfRuntimePlugins/auto-fetch-data.js +0 -68
  89. package/dist/esm-node/cli/server/data-fetch-server-plugin.js +0 -162
  90. package/dist/esm-node/interfaces/global.js +0 -0
  91. package/dist/esm-node/runtime/AwaitDataFetch.js +0 -109
  92. package/dist/esm-node/runtime/createRemoteComponent.js +0 -291
  93. package/dist/esm-node/runtime/wrapNoSSR.js +0 -11
  94. package/dist/esm-node/ssr-runtime/downgrade.js +0 -88
  95. package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -101
  96. package/dist/esm-node/utils/dataFetch.js +0 -166
  97. package/dist/esm-node/utils/index.js +0 -27
  98. package/dist/types/cli/mfRuntimePlugins/auto-fetch-data.d.ts +0 -3
  99. package/dist/types/cli/server/data-fetch-server-plugin.d.ts +0 -3
  100. package/dist/types/interfaces/global.d.ts +0 -27
  101. package/dist/types/runtime/AwaitDataFetch.d.ts +0 -19
  102. package/dist/types/runtime/createRemoteComponent.d.ts +0 -26
  103. package/dist/types/runtime/wrapNoSSR.d.ts +0 -9
  104. package/dist/types/ssr-runtime/devPlugin.d.ts +0 -2
  105. package/dist/types/ssr-runtime/downgrade.d.ts +0 -4
  106. package/dist/types/ssr-runtime/injectDataFetchFunctionPlugin.d.ts +0 -7
  107. package/dist/types/utils/dataFetch.d.ts +0 -26
  108. package/dist/types/utils/index.d.ts +0 -15
  109. /package/dist/types/{logger.d.ts → cli/logger.d.ts} +0 -0
@@ -2,13 +2,13 @@ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
2
  import { _ as _type_of } from "@swc/helpers/_/_type_of";
3
3
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
4
  import path from "path";
5
- import { fs } from "@modern-js/utils";
5
+ import fs from "fs-extra";
6
6
  import { ModuleFederationPlugin } from "@module-federation/enhanced/webpack";
7
7
  import { ModuleFederationPlugin as RspackModuleFederationPlugin } from "@module-federation/enhanced/rspack";
8
8
  import UniverseEntryChunkTrackerPlugin from "@module-federation/node/universe-entry-chunk-tracker-plugin";
9
9
  import { updateStatsAndManifest } from "./manifest";
10
- import logger from "../logger";
11
- import { isDev } from "./utils";
10
+ import { isDev } from "./constant";
11
+ import logger from "./logger";
12
12
  import { isWebTarget, skipByTarget } from "./utils";
13
13
  function setEnv() {
14
14
  process.env["MF_DISABLE_EMIT_STATS"] = "true";
@@ -85,23 +85,15 @@ var moduleFederationSSRPlugin = function(pluginOptions) {
85
85
  setEnv();
86
86
  api._internalRuntimePlugins(function(param) {
87
87
  var entrypoint = param.entrypoint, plugins = param.plugins;
88
- var fetchServerQuery = pluginOptions.fetchServerQuery;
89
- plugins.push({
90
- name: "injectDataFetchFunction",
91
- path: "@module-federation/modern-js/ssr-inject-data-fetch-function-plugin",
92
- config: {
93
- fetchServerQuery
94
- }
95
- });
96
- if (!isDev()) {
88
+ if (!isDev) {
97
89
  return {
98
90
  entrypoint,
99
91
  plugins
100
92
  };
101
93
  }
102
94
  plugins.push({
103
- name: "mfSSRDev",
104
- path: "@module-federation/modern-js/ssr-dev-plugin",
95
+ name: "mfSSR",
96
+ path: "@module-federation/modern-js/ssr-runtime",
105
97
  config: {}
106
98
  });
107
99
  return {
@@ -109,18 +101,6 @@ var moduleFederationSSRPlugin = function(pluginOptions) {
109
101
  plugins
110
102
  };
111
103
  });
112
- if (pluginOptions.ssrConfig.remotes) {
113
- api._internalServerPlugins(function(param) {
114
- var plugins = param.plugins;
115
- plugins.push({
116
- name: "@module-federation/modern-js/data-fetch-server-plugin",
117
- options: {}
118
- });
119
- return {
120
- plugins
121
- };
122
- });
123
- }
124
104
  api.modifyBundlerChain(function(chain) {
125
105
  var target = chain.get("target");
126
106
  if (skipByTarget(target)) {
@@ -141,7 +121,7 @@ var moduleFederationSSRPlugin = function(pluginOptions) {
141
121
  }
142
122
  if (!isWeb) {
143
123
  chain.target("async-node");
144
- if (isDev()) {
124
+ if (isDev) {
145
125
  chain.plugin("UniverseEntryChunkTrackerPlugin").use(UniverseEntryChunkTrackerPlugin);
146
126
  }
147
127
  var userSSRConfig = pluginOptions.userConfig.ssr ? _type_of(pluginOptions.userConfig.ssr) === "object" ? pluginOptions.userConfig.ssr : {} : {};
@@ -150,7 +130,7 @@ var moduleFederationSSRPlugin = function(pluginOptions) {
150
130
  chain.output.publicPath("".concat(publicPath).concat(userSSRConfig.distOutputDir, "/"));
151
131
  }
152
132
  }
153
- if (isDev() && isWeb) {
133
+ if (isDev && isWeb) {
154
134
  chain.externals({
155
135
  "@module-federation/node/utils": "NOT_USED_IN_BROWSER"
156
136
  });
@@ -42,12 +42,8 @@ var skipByTarget = function(target) {
42
42
  }
43
43
  return false;
44
44
  };
45
- function isDev() {
46
- return process.env.NODE_ENV === "development";
47
- }
48
45
  export {
49
46
  getIPV4,
50
- isDev,
51
47
  isWebTarget,
52
48
  skipByTarget
53
49
  };
@@ -1,42 +1,6 @@
1
1
  var LOCALHOST = "localhost";
2
2
  var PLUGIN_IDENTIFIER = "[ Modern.js Module Federation ]";
3
- var DATA_FETCH_QUERY = "x-mf-data-fetch";
4
- var DATA_FETCH_ERROR_PREFIX = "caught the following error during dataFetch: ";
5
- var LOAD_REMOTE_ERROR_PREFIX = "caught the following error during loadRemote: ";
6
- var DOWNGRADE_KEY = "_mfSSRDowngrade";
7
- var DATA_FETCH_MAP_KEY = "__MF_DATA_FETCH_MAP__";
8
- var DATA_FETCH_FUNCTION = "_mfDataFetch";
9
- var FS_HREF = "_mfFSHref";
10
- var ERROR_TYPE = {
11
- DATA_FETCH: 1,
12
- LOAD_REMOTE: 2,
13
- UNKNOWN: 3
14
- };
15
- var WRAP_DATA_FETCH_ID_IDENTIFIER = "wrap_dfip_identifier";
16
- var MF_DATA_FETCH_TYPE;
17
- (function(MF_DATA_FETCH_TYPE2) {
18
- MF_DATA_FETCH_TYPE2[MF_DATA_FETCH_TYPE2["FETCH_SERVER"] = 1] = "FETCH_SERVER";
19
- MF_DATA_FETCH_TYPE2[MF_DATA_FETCH_TYPE2["FETCH_CLIENT"] = 2] = "FETCH_CLIENT";
20
- })(MF_DATA_FETCH_TYPE || (MF_DATA_FETCH_TYPE = {}));
21
- var MF_DATA_FETCH_STATUS;
22
- (function(MF_DATA_FETCH_STATUS2) {
23
- MF_DATA_FETCH_STATUS2[MF_DATA_FETCH_STATUS2["LOADED"] = 1] = "LOADED";
24
- MF_DATA_FETCH_STATUS2[MF_DATA_FETCH_STATUS2["LOADING"] = 2] = "LOADING";
25
- MF_DATA_FETCH_STATUS2[MF_DATA_FETCH_STATUS2["AWAIT"] = 0] = "AWAIT";
26
- MF_DATA_FETCH_STATUS2[MF_DATA_FETCH_STATUS2["ERROR"] = 3] = "ERROR";
27
- })(MF_DATA_FETCH_STATUS || (MF_DATA_FETCH_STATUS = {}));
28
3
  export {
29
- DATA_FETCH_ERROR_PREFIX,
30
- DATA_FETCH_FUNCTION,
31
- DATA_FETCH_MAP_KEY,
32
- DATA_FETCH_QUERY,
33
- DOWNGRADE_KEY,
34
- ERROR_TYPE,
35
- FS_HREF,
36
- LOAD_REMOTE_ERROR_PREFIX,
37
4
  LOCALHOST,
38
- MF_DATA_FETCH_STATUS,
39
- MF_DATA_FETCH_TYPE,
40
- PLUGIN_IDENTIFIER,
41
- WRAP_DATA_FETCH_ID_IDENTIFIER
5
+ PLUGIN_IDENTIFIER
42
6
  };
@@ -0,0 +1,191 @@
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 _object_without_properties } from "@swc/helpers/_/_object_without_properties";
5
+ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
6
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
7
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
8
+ import React from "react";
9
+ import { getInstance } from "@module-federation/enhanced/runtime";
10
+ import { ErrorBoundary } from "react-error-boundary";
11
+ function getLoadedRemoteInfos(instance, id) {
12
+ var _ref = instance.remoteHandler.idToRemoteMap[id] || {}, name = _ref.name, expose = _ref.expose;
13
+ if (!name) {
14
+ return;
15
+ }
16
+ var module = instance.moduleCache.get(name);
17
+ if (!module) {
18
+ return;
19
+ }
20
+ var remoteSnapshot = instance.snapshotHandler.getGlobalRemoteInfo(module.remoteInfo).remoteSnapshot;
21
+ return _object_spread_props(_object_spread({}, module.remoteInfo), {
22
+ snapshot: remoteSnapshot,
23
+ expose
24
+ });
25
+ }
26
+ function getTargetModuleInfo(id) {
27
+ var instance = getInstance();
28
+ if (!instance) {
29
+ return;
30
+ }
31
+ var loadedRemoteInfo = getLoadedRemoteInfos(instance, id);
32
+ if (!loadedRemoteInfo) {
33
+ return;
34
+ }
35
+ var snapshot = loadedRemoteInfo.snapshot;
36
+ if (!snapshot) {
37
+ return;
38
+ }
39
+ var publicPath = "publicPath" in snapshot ? snapshot.publicPath : "getPublicPath" in snapshot ? new Function(snapshot.getPublicPath)() : "";
40
+ if (!publicPath) {
41
+ return;
42
+ }
43
+ var modules = "modules" in snapshot ? snapshot.modules : [];
44
+ var targetModule = modules.find(function(m) {
45
+ return m.modulePath === loadedRemoteInfo.expose;
46
+ });
47
+ if (!targetModule) {
48
+ return;
49
+ }
50
+ var remoteEntry = "remoteEntry" in snapshot ? snapshot.remoteEntry : "";
51
+ if (!remoteEntry) {
52
+ return;
53
+ }
54
+ return {
55
+ module: targetModule,
56
+ publicPath,
57
+ remoteEntry
58
+ };
59
+ }
60
+ function collectSSRAssets(options) {
61
+ var _ref = typeof options === "string" ? {
62
+ id: options
63
+ } : options, id = _ref.id, _ref_injectLink = _ref.injectLink, injectLink = _ref_injectLink === void 0 ? true : _ref_injectLink, _ref_injectScript = _ref.injectScript, injectScript = _ref_injectScript === void 0 ? true : _ref_injectScript;
64
+ var links = [];
65
+ var scripts = [];
66
+ var instance = getInstance();
67
+ if (!instance || !injectLink && !injectScript) {
68
+ return _to_consumable_array(scripts).concat(_to_consumable_array(links));
69
+ }
70
+ var moduleAndPublicPath = getTargetModuleInfo(id);
71
+ if (!moduleAndPublicPath) {
72
+ return _to_consumable_array(scripts).concat(_to_consumable_array(links));
73
+ }
74
+ var targetModule = moduleAndPublicPath.module, publicPath = moduleAndPublicPath.publicPath, remoteEntry = moduleAndPublicPath.remoteEntry;
75
+ if (injectLink) {
76
+ _to_consumable_array(targetModule.assets.css.sync).concat(_to_consumable_array(targetModule.assets.css.async)).forEach(function(file, index) {
77
+ links.push(/* @__PURE__ */ _jsx("link", {
78
+ href: "".concat(publicPath).concat(file),
79
+ rel: "stylesheet",
80
+ type: "text/css"
81
+ }, "".concat(file.split(".")[0], "_").concat(index)));
82
+ });
83
+ }
84
+ if (injectScript) {
85
+ scripts.push(/* @__PURE__ */ _jsx("script", {
86
+ async: true,
87
+ src: "".concat(publicPath).concat(remoteEntry),
88
+ crossOrigin: "anonymous"
89
+ }, remoteEntry.split(".")[0]));
90
+ _to_consumable_array(targetModule.assets.js.sync).forEach(function(file, index) {
91
+ scripts.push(/* @__PURE__ */ _jsx("script", {
92
+ async: true,
93
+ src: "".concat(publicPath).concat(file),
94
+ crossOrigin: "anonymous"
95
+ }, "".concat(file.split(".")[0], "_").concat(index)));
96
+ });
97
+ }
98
+ return _to_consumable_array(scripts).concat(_to_consumable_array(links));
99
+ }
100
+ function createRemoteSSRComponent(info) {
101
+ var exportName = (info === null || info === void 0 ? void 0 : info.export) || "default";
102
+ var LazyComponent = /* @__PURE__ */ React.lazy(/* @__PURE__ */ _async_to_generator(function() {
103
+ var m, moduleId, assets, Com, err, FallbackFunctionComponent, FallbackNode;
104
+ return _ts_generator(this, function(_state) {
105
+ switch (_state.label) {
106
+ case 0:
107
+ _state.trys.push([
108
+ 0,
109
+ 2,
110
+ ,
111
+ 3
112
+ ]);
113
+ return [
114
+ 4,
115
+ info.loader()
116
+ ];
117
+ case 1:
118
+ m = _state.sent();
119
+ if (!m) {
120
+ throw new Error("load remote failed");
121
+ }
122
+ moduleId = m && m[Symbol.for("mf_module_id")];
123
+ assets = collectSSRAssets({
124
+ id: moduleId
125
+ });
126
+ Com = m[exportName];
127
+ if (exportName in m && typeof Com === "function") {
128
+ return [
129
+ 2,
130
+ {
131
+ default: function(props) {
132
+ return /* @__PURE__ */ _jsxs(_Fragment, {
133
+ children: [
134
+ assets,
135
+ /* @__PURE__ */ _jsx(Com, _object_spread({}, props))
136
+ ]
137
+ });
138
+ }
139
+ }
140
+ ];
141
+ } else {
142
+ throw Error("Make sure that ".concat(moduleId, " has the correct export when export is ").concat(String(exportName)));
143
+ }
144
+ return [
145
+ 3,
146
+ 3
147
+ ];
148
+ case 2:
149
+ err = _state.sent();
150
+ if (!info.fallback) {
151
+ throw err;
152
+ }
153
+ FallbackFunctionComponent = info.fallback;
154
+ FallbackNode = /* @__PURE__ */ _jsx(FallbackFunctionComponent, {
155
+ error: err,
156
+ resetErrorBoundary: function() {
157
+ console.log('SSR mode not support "resetErrorBoundary" !');
158
+ }
159
+ });
160
+ return [
161
+ 2,
162
+ {
163
+ default: function() {
164
+ return FallbackNode;
165
+ }
166
+ }
167
+ ];
168
+ case 3:
169
+ return [
170
+ 2
171
+ ];
172
+ }
173
+ });
174
+ }));
175
+ return function(props) {
176
+ var key = props.key, args = _object_without_properties(props, [
177
+ "key"
178
+ ]);
179
+ return /* @__PURE__ */ _jsx(ErrorBoundary, {
180
+ FallbackComponent: info.fallback,
181
+ children: /* @__PURE__ */ _jsx(React.Suspense, {
182
+ fallback: info.loading,
183
+ children: /* @__PURE__ */ _jsx(LazyComponent, _object_spread({}, args))
184
+ })
185
+ });
186
+ };
187
+ }
188
+ export {
189
+ collectSSRAssets,
190
+ createRemoteSSRComponent
191
+ };
@@ -1,22 +1,12 @@
1
1
  export * from "@module-federation/enhanced/runtime";
2
- import { ERROR_TYPE } from "../constant";
3
2
  var kit = {
4
3
  get createRemoteSSRComponent() {
5
- return require("./createRemoteComponent").createRemoteSSRComponent;
6
- },
7
- get createRemoteComponent() {
8
- return require("./createRemoteComponent").createRemoteComponent;
4
+ return require("./createRemoteSSRComponent").createRemoteSSRComponent;
9
5
  },
10
6
  get collectSSRAssets() {
11
- return require("./createRemoteComponent").collectSSRAssets;
12
- },
13
- get wrapNoSSR() {
14
- return require("./wrapNoSSR").wrapNoSSR;
7
+ return require("./createRemoteSSRComponent").collectSSRAssets;
15
8
  }
16
9
  };
17
- import { setSSREnv } from "../ssr-runtime/injectDataFetchFunctionPlugin";
18
10
  export {
19
- ERROR_TYPE,
20
- kit,
21
- setSSREnv
11
+ kit
22
12
  };
@@ -0,0 +1,98 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
3
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
+ import fs from "fs-extra";
5
+ import { LRUCache } from "lru-cache";
6
+ var FileCache = /* @__PURE__ */ function() {
7
+ "use strict";
8
+ function FileCache2() {
9
+ _class_call_check(this, FileCache2);
10
+ this.cache = new LRUCache({
11
+ maxSize: 200 * 1024 * 1024
12
+ });
13
+ }
14
+ var _proto = FileCache2.prototype;
15
+ _proto.getFile = function getFile(filepath) {
16
+ var _this = this;
17
+ return _async_to_generator(function() {
18
+ var stat, currentModified, cachedEntry, content, newEntry, err;
19
+ return _ts_generator(this, function(_state) {
20
+ switch (_state.label) {
21
+ case 0:
22
+ return [
23
+ 4,
24
+ fs.pathExists(filepath)
25
+ ];
26
+ case 1:
27
+ if (!_state.sent()) {
28
+ return [
29
+ 2,
30
+ null
31
+ ];
32
+ }
33
+ _state.label = 2;
34
+ case 2:
35
+ _state.trys.push([
36
+ 2,
37
+ 5,
38
+ ,
39
+ 6
40
+ ]);
41
+ return [
42
+ 4,
43
+ fs.lstat(filepath)
44
+ ];
45
+ case 3:
46
+ stat = _state.sent();
47
+ currentModified = stat.mtimeMs;
48
+ cachedEntry = _this.cache.get(filepath);
49
+ if (cachedEntry && currentModified <= cachedEntry.lastModified) {
50
+ return [
51
+ 2,
52
+ {
53
+ content: cachedEntry.content,
54
+ lastModified: cachedEntry.lastModified
55
+ }
56
+ ];
57
+ }
58
+ return [
59
+ 4,
60
+ fs.readFile(filepath, "utf-8")
61
+ ];
62
+ case 4:
63
+ content = _state.sent();
64
+ newEntry = {
65
+ content,
66
+ lastModified: currentModified
67
+ };
68
+ _this.cache.set(filepath, newEntry, {
69
+ size: stat.size || content.length
70
+ });
71
+ return [
72
+ 2,
73
+ {
74
+ content,
75
+ lastModified: currentModified
76
+ }
77
+ ];
78
+ case 5:
79
+ err = _state.sent();
80
+ return [
81
+ 2,
82
+ null
83
+ ];
84
+ case 6:
85
+ return [
86
+ 2
87
+ ];
88
+ }
89
+ });
90
+ })();
91
+ };
92
+ return FileCache2;
93
+ }();
94
+ var fileCache = new FileCache();
95
+ export {
96
+ FileCache,
97
+ fileCache
98
+ };
@@ -0,0 +1,50 @@
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, vi, beforeAll } from "vitest";
4
+ import { FileCache } from "./fileCache";
5
+ beforeAll(function() {
6
+ vi.mock("fs-extra", function() {
7
+ return {
8
+ default: {
9
+ pathExists: function() {
10
+ return true;
11
+ },
12
+ lstat: function() {
13
+ return {
14
+ mtimeMs: Date.now(),
15
+ size: 4
16
+ };
17
+ },
18
+ readFile: function() {
19
+ return "test";
20
+ }
21
+ }
22
+ };
23
+ });
24
+ });
25
+ describe("modern serve static file cache", /* @__PURE__ */ _async_to_generator(function() {
26
+ return _ts_generator(this, function(_state) {
27
+ it("should cache file", /* @__PURE__ */ _async_to_generator(function() {
28
+ var cache, result;
29
+ return _ts_generator(this, function(_state2) {
30
+ switch (_state2.label) {
31
+ case 0:
32
+ cache = new FileCache();
33
+ return [
34
+ 4,
35
+ cache.getFile("test.txt")
36
+ ];
37
+ case 1:
38
+ result = _state2.sent();
39
+ expect(result === null || result === void 0 ? void 0 : result.content).toBe("test");
40
+ return [
41
+ 2
42
+ ];
43
+ }
44
+ });
45
+ }));
46
+ return [
47
+ 2
48
+ ];
49
+ });
50
+ }));
@@ -0,0 +1,36 @@
1
+ import { createStaticMiddleware } from "./staticMiddleware";
2
+ var staticServePlugin = function() {
3
+ return {
4
+ name: "@modern-js/module-federation/server",
5
+ setup: function(api) {
6
+ api.onPrepare(function() {
7
+ var _config_output, _config_server;
8
+ console.log(process.env.NODE_ENV);
9
+ if (process.env.NODE_ENV === "development") {
10
+ return;
11
+ }
12
+ var middlewares = api.getServerContext().middlewares;
13
+ var config = api.getServerConfig();
14
+ var assetPrefix = ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.assetPrefix) || "";
15
+ if (!((_config_server = config.server) === null || _config_server === void 0 ? void 0 : _config_server.ssr)) {
16
+ return;
17
+ }
18
+ var context = api.getServerContext();
19
+ var pwd = context.distDirectory;
20
+ var serverStaticMiddleware = createStaticMiddleware({
21
+ assetPrefix,
22
+ pwd
23
+ });
24
+ middlewares.push({
25
+ name: "module-federation-serve-manifest",
26
+ handler: serverStaticMiddleware
27
+ });
28
+ });
29
+ }
30
+ };
31
+ };
32
+ var server_default = staticServePlugin;
33
+ export {
34
+ server_default as default,
35
+ staticServePlugin
36
+ };
@@ -0,0 +1,81 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
+ import fs from "fs-extra";
4
+ import path from "node:path";
5
+ import { fileCache } from "./fileCache";
6
+ var bundlesAssetPrefix = "/bundles";
7
+ var removeHost = function(url) {
8
+ try {
9
+ var hasProtocol = url.includes("://");
10
+ var hasDomain = hasProtocol || url.startsWith("//");
11
+ var pathname = hasDomain ? new URL(hasProtocol ? url : "http:".concat(url)).pathname : url;
12
+ return pathname;
13
+ } catch (e) {
14
+ return url;
15
+ }
16
+ };
17
+ var createStaticMiddleware = function(options) {
18
+ var assetPrefix = options.assetPrefix, pwd = options.pwd;
19
+ return function() {
20
+ var _ref = _async_to_generator(function(c, next) {
21
+ var pathname, prefixWithoutHost, prefixWithBundle, pathnameWithoutPrefix, filepath, fileResult;
22
+ return _ts_generator(this, function(_state) {
23
+ switch (_state.label) {
24
+ case 0:
25
+ pathname = c.req.path;
26
+ if (path.extname(pathname) !== ".js") {
27
+ return [
28
+ 2,
29
+ next()
30
+ ];
31
+ }
32
+ prefixWithoutHost = removeHost(assetPrefix);
33
+ prefixWithBundle = path.join(prefixWithoutHost, bundlesAssetPrefix);
34
+ if (!pathname.startsWith(prefixWithBundle)) {
35
+ return [
36
+ 2,
37
+ next()
38
+ ];
39
+ }
40
+ pathnameWithoutPrefix = pathname.replace(prefixWithBundle, "");
41
+ filepath = path.join(pwd, bundlesAssetPrefix, pathnameWithoutPrefix);
42
+ return [
43
+ 4,
44
+ fs.pathExists(filepath)
45
+ ];
46
+ case 1:
47
+ if (!_state.sent()) {
48
+ return [
49
+ 2,
50
+ next()
51
+ ];
52
+ }
53
+ return [
54
+ 4,
55
+ fileCache.getFile(filepath)
56
+ ];
57
+ case 2:
58
+ fileResult = _state.sent();
59
+ if (!fileResult) {
60
+ return [
61
+ 2,
62
+ next()
63
+ ];
64
+ }
65
+ c.header("Content-Type", "application/javascript");
66
+ c.header("Content-Length", String(fileResult.content.length));
67
+ return [
68
+ 2,
69
+ c.body(fileResult.content, 200)
70
+ ];
71
+ }
72
+ });
73
+ });
74
+ return function(c, next) {
75
+ return _ref.apply(this, arguments);
76
+ };
77
+ }();
78
+ };
79
+ export {
80
+ createStaticMiddleware
81
+ };