@module-federation/modern-js 0.21.5 → 0.22.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 (113) hide show
  1. package/dist/cjs/cli/configPlugin.js +301 -357
  2. package/dist/cjs/cli/index.js +103 -93
  3. package/dist/cjs/cli/mfRuntimePlugins/inject-node-fetch.js +51 -41
  4. package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +73 -75
  5. package/dist/cjs/cli/mfRuntimePlugins/shared-strategy.js +50 -40
  6. package/dist/cjs/cli/server/data-fetch-server-plugin.js +55 -44
  7. package/dist/cjs/cli/ssrPlugin.js +266 -298
  8. package/dist/cjs/cli/utils.js +84 -81
  9. package/dist/cjs/constant.js +37 -28
  10. package/dist/cjs/interfaces/bundler.js +17 -15
  11. package/dist/cjs/logger.js +37 -25
  12. package/dist/cjs/react/index.js +55 -19
  13. package/dist/cjs/react/plugin.js +62 -37
  14. package/dist/cjs/react/v18.js +55 -19
  15. package/dist/cjs/react/v19.js +55 -19
  16. package/dist/cjs/runtime/index.js +55 -19
  17. package/dist/cjs/server/fileCache.js +76 -76
  18. package/dist/cjs/server/index.js +65 -57
  19. package/dist/cjs/server/staticMiddleware.js +84 -84
  20. package/dist/cjs/ssr-runtime/SSRLiveReload.js +40 -34
  21. package/dist/cjs/ssr-runtime/devPlugin.js +63 -67
  22. package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +44 -36
  23. package/dist/cjs/types/index.js +17 -15
  24. package/dist/esm/cli/configPlugin.mjs +305 -0
  25. package/dist/esm/cli/index.mjs +83 -0
  26. package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.mjs +12 -0
  27. package/dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.mjs +58 -0
  28. package/dist/esm/cli/mfRuntimePlugins/shared-strategy.mjs +21 -0
  29. package/dist/esm/cli/server/data-fetch-server-plugin.mjs +17 -0
  30. package/dist/esm/cli/ssrPlugin.mjs +254 -0
  31. package/dist/esm/cli/utils.mjs +40 -0
  32. package/dist/esm/constant.mjs +3 -0
  33. package/dist/esm/logger.mjs +5 -0
  34. package/dist/esm/react/plugin.mjs +3 -0
  35. package/dist/esm/server/fileCache.mjs +90 -0
  36. package/dist/esm/server/index.mjs +36 -0
  37. package/dist/esm/server/staticMiddleware.mjs +87 -0
  38. package/dist/esm/ssr-runtime/SSRLiveReload.mjs +22 -0
  39. package/dist/esm/ssr-runtime/devPlugin.mjs +70 -0
  40. package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.mjs +34 -0
  41. package/dist/esm-node/cli/configPlugin.mjs +263 -0
  42. package/dist/esm-node/cli/index.mjs +67 -0
  43. package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.mjs +10 -0
  44. package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.mjs +46 -0
  45. package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.mjs +19 -0
  46. package/dist/esm-node/cli/server/data-fetch-server-plugin.mjs +15 -0
  47. package/dist/esm-node/cli/ssrPlugin.mjs +228 -0
  48. package/dist/esm-node/cli/utils.mjs +40 -0
  49. package/dist/esm-node/constant.mjs +3 -0
  50. package/dist/esm-node/logger.mjs +5 -0
  51. package/dist/esm-node/react/plugin.mjs +3 -0
  52. package/dist/esm-node/server/fileCache.mjs +37 -0
  53. package/dist/esm-node/server/index.mjs +34 -0
  54. package/dist/esm-node/server/staticMiddleware.mjs +42 -0
  55. package/dist/esm-node/ssr-runtime/SSRLiveReload.mjs +15 -0
  56. package/dist/esm-node/ssr-runtime/devPlugin.mjs +33 -0
  57. package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.mjs +14 -0
  58. package/package.json +32 -31
  59. package/dist/cjs/cli/configPlugin.spec.js +0 -112
  60. package/dist/cjs/server/fileCache.spec.js +0 -28
  61. package/dist/cjs/server/staticMiddleware.spec.js +0 -185
  62. package/dist/esm/cli/configPlugin.js +0 -395
  63. package/dist/esm/cli/configPlugin.spec.js +0 -108
  64. package/dist/esm/cli/index.js +0 -92
  65. package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js +0 -16
  66. package/dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.js +0 -74
  67. package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +0 -25
  68. package/dist/esm/cli/server/data-fetch-server-plugin.js +0 -20
  69. package/dist/esm/cli/ssrPlugin.js +0 -305
  70. package/dist/esm/cli/utils.js +0 -53
  71. package/dist/esm/constant.js +0 -6
  72. package/dist/esm/logger.js +0 -7
  73. package/dist/esm/react/plugin.js +0 -5
  74. package/dist/esm/server/fileCache.js +0 -98
  75. package/dist/esm/server/fileCache.spec.js +0 -50
  76. package/dist/esm/server/index.js +0 -41
  77. package/dist/esm/server/staticMiddleware.js +0 -104
  78. package/dist/esm/server/staticMiddleware.spec.js +0 -328
  79. package/dist/esm/ssr-runtime/SSRLiveReload.js +0 -26
  80. package/dist/esm/ssr-runtime/devPlugin.js +0 -73
  81. package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -34
  82. package/dist/esm-node/cli/configPlugin.js +0 -347
  83. package/dist/esm-node/cli/configPlugin.spec.js +0 -89
  84. package/dist/esm-node/cli/index.js +0 -72
  85. package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.js +0 -14
  86. package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +0 -62
  87. package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +0 -23
  88. package/dist/esm-node/cli/server/data-fetch-server-plugin.js +0 -18
  89. package/dist/esm-node/cli/ssrPlugin.js +0 -280
  90. package/dist/esm-node/cli/utils.js +0 -53
  91. package/dist/esm-node/constant.js +0 -6
  92. package/dist/esm-node/logger.js +0 -7
  93. package/dist/esm-node/react/plugin.js +0 -5
  94. package/dist/esm-node/server/fileCache.js +0 -49
  95. package/dist/esm-node/server/fileCache.spec.js +0 -27
  96. package/dist/esm-node/server/index.js +0 -39
  97. package/dist/esm-node/server/staticMiddleware.js +0 -55
  98. package/dist/esm-node/server/staticMiddleware.spec.js +0 -162
  99. package/dist/esm-node/ssr-runtime/SSRLiveReload.js +0 -19
  100. package/dist/esm-node/ssr-runtime/devPlugin.js +0 -37
  101. package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -16
  102. /package/dist/esm/interfaces/{bundler.js → bundler.mjs} +0 -0
  103. /package/dist/esm/react/{index.js → index.mjs} +0 -0
  104. /package/dist/esm/react/{v18.js → v18.mjs} +0 -0
  105. /package/dist/esm/react/{v19.js → v19.mjs} +0 -0
  106. /package/dist/esm/runtime/{index.js → index.mjs} +0 -0
  107. /package/dist/esm/types/{index.js → index.mjs} +0 -0
  108. /package/dist/esm-node/interfaces/{bundler.js → bundler.mjs} +0 -0
  109. /package/dist/esm-node/react/{index.js → index.mjs} +0 -0
  110. /package/dist/esm-node/react/{v18.js → v18.mjs} +0 -0
  111. /package/dist/esm-node/react/{v19.js → v19.mjs} +0 -0
  112. /package/dist/esm-node/runtime/{index.js → index.mjs} +0 -0
  113. /package/dist/esm-node/types/{index.js → index.mjs} +0 -0
@@ -1,280 +0,0 @@
1
- import path from "path";
2
- import fs from "fs-extra";
3
- import { ModuleFederationPlugin } from "@module-federation/enhanced/webpack";
4
- import { ModuleFederationPlugin as RspackModuleFederationPlugin } from "@module-federation/enhanced/rspack";
5
- import UniverseEntryChunkTrackerPlugin from "@module-federation/node/universe-entry-chunk-tracker-plugin";
6
- import logger from "../logger";
7
- import { isDev } from "./utils";
8
- import { updateStatsAndManifest } from "@module-federation/rsbuild-plugin/utils";
9
- import { ManifestFileName, StatsFileName, simpleJoinRemoteEntry } from "@module-federation/sdk";
10
- import { isWebTarget, skipByTarget } from "./utils";
11
- function setEnv() {
12
- process.env["MF_SSR_PRJ"] = "true";
13
- }
14
- const CHAIN_MF_PLUGIN_ID = "plugin-module-federation-server";
15
- function getManifestAssetFileNames(manifestOption) {
16
- if (!manifestOption) {
17
- return {
18
- statsFileName: StatsFileName,
19
- manifestFileName: ManifestFileName
20
- };
21
- }
22
- const JSON_EXT = ".json";
23
- const filePath = typeof manifestOption === "boolean" ? "" : manifestOption.filePath || "";
24
- const baseFileName = typeof manifestOption === "boolean" ? "" : manifestOption.fileName || "";
25
- const ensureExt = (name) => name.endsWith(JSON_EXT) ? name : `${name}${JSON_EXT}`;
26
- const withSuffix = (name, suffix) => name.replace(JSON_EXT, `${suffix}${JSON_EXT}`);
27
- const manifestFileName = baseFileName ? ensureExt(baseFileName) : ManifestFileName;
28
- const statsFileName = baseFileName ? withSuffix(manifestFileName, "-stats") : StatsFileName;
29
- return {
30
- statsFileName: simpleJoinRemoteEntry(filePath, statsFileName),
31
- manifestFileName: simpleJoinRemoteEntry(filePath, manifestFileName)
32
- };
33
- }
34
- const mfSSRRsbuildPlugin = (pluginOptions) => {
35
- return {
36
- name: "@modern-js/plugin-mf-post-config",
37
- pre: [
38
- "@modern-js/builder-plugin-ssr"
39
- ],
40
- setup(api) {
41
- var _pluginOptions_csrConfig, _pluginOptions_assetFileNames, _pluginOptions_ssrConfig;
42
- if (pluginOptions.csrConfig.getPublicPath) {
43
- return;
44
- }
45
- let csrOutputPath = "";
46
- let ssrOutputPath = "";
47
- let ssrEnv = "";
48
- let csrEnv = "";
49
- const browserAssetFileNames = pluginOptions.assetFileNames.browser || getManifestAssetFileNames((_pluginOptions_csrConfig = pluginOptions.csrConfig) === null || _pluginOptions_csrConfig === void 0 ? void 0 : _pluginOptions_csrConfig.manifest);
50
- const nodeAssetFileNames = ((_pluginOptions_assetFileNames = pluginOptions.assetFileNames) === null || _pluginOptions_assetFileNames === void 0 ? void 0 : _pluginOptions_assetFileNames.node) || getManifestAssetFileNames((_pluginOptions_ssrConfig = pluginOptions.ssrConfig) === null || _pluginOptions_ssrConfig === void 0 ? void 0 : _pluginOptions_ssrConfig.manifest);
51
- const collectAssets = (assets, fileNames, tag) => {
52
- const statsAsset = assets[fileNames.statsFileName];
53
- const manifestAsset = assets[fileNames.manifestFileName];
54
- if (!statsAsset || !manifestAsset) {
55
- return void 0;
56
- }
57
- try {
58
- const statsRaw = statsAsset.source();
59
- const manifestRaw = manifestAsset.source();
60
- const statsContent = typeof statsRaw === "string" ? statsRaw : statsRaw.toString();
61
- const manifestContent = typeof manifestRaw === "string" ? manifestRaw : manifestRaw.toString();
62
- return {
63
- stats: {
64
- data: JSON.parse(statsContent),
65
- filename: fileNames.statsFileName
66
- },
67
- manifest: {
68
- data: JSON.parse(manifestContent),
69
- filename: fileNames.manifestFileName
70
- }
71
- };
72
- } catch (err) {
73
- const message = err instanceof Error ? err.message : String(err);
74
- logger.error(`Failed to parse ${tag} manifest assets: ${message}`);
75
- return void 0;
76
- }
77
- };
78
- api.modifyEnvironmentConfig((config, { name }) => {
79
- const target = config.output.target;
80
- if (skipByTarget(target)) {
81
- return config;
82
- }
83
- if (isWebTarget(target)) {
84
- csrOutputPath = config.output.distPath.root;
85
- csrEnv = name;
86
- } else {
87
- ssrOutputPath = config.output.distPath.root;
88
- ssrEnv = name;
89
- }
90
- return config;
91
- });
92
- const modifySSRPublicPath = (config, utils) => {
93
- if (ssrEnv !== utils.environment.name) {
94
- return config;
95
- }
96
- const userSSRConfig = pluginOptions.userConfig.ssr ? typeof pluginOptions.userConfig.ssr === "object" ? pluginOptions.userConfig.ssr : {} : {};
97
- if (userSSRConfig.distOutputDir) {
98
- return;
99
- }
100
- config.output.publicPath = `${config.output.publicPath}${path.relative(csrOutputPath, ssrOutputPath)}/`;
101
- return config;
102
- };
103
- api.modifyWebpackConfig((config, utils) => {
104
- modifySSRPublicPath(config, utils);
105
- return config;
106
- });
107
- api.modifyRspackConfig((config, utils) => {
108
- modifySSRPublicPath(config, utils);
109
- return config;
110
- });
111
- api.processAssets({
112
- stage: "report"
113
- }, ({ assets, environment: envContext }) => {
114
- var _pluginOptions_csrConfig2, _pluginOptions_ssrConfig2;
115
- const envName = envContext.name;
116
- if (((_pluginOptions_csrConfig2 = pluginOptions.csrConfig) === null || _pluginOptions_csrConfig2 === void 0 ? void 0 : _pluginOptions_csrConfig2.manifest) !== false && csrEnv && envName === csrEnv) {
117
- const browserAssets = collectAssets(assets, browserAssetFileNames, "browser");
118
- if (browserAssets) {
119
- pluginOptions.assetResources.browser = browserAssets;
120
- }
121
- }
122
- if (((_pluginOptions_ssrConfig2 = pluginOptions.ssrConfig) === null || _pluginOptions_ssrConfig2 === void 0 ? void 0 : _pluginOptions_ssrConfig2.manifest) !== false && ssrEnv && envName === ssrEnv) {
123
- const nodeAssets = collectAssets(assets, nodeAssetFileNames, "node");
124
- if (nodeAssets) {
125
- pluginOptions.assetResources.node = nodeAssets;
126
- }
127
- }
128
- });
129
- }
130
- };
131
- };
132
- const moduleFederationSSRPlugin = (pluginOptions) => ({
133
- name: "@modern-js/plugin-module-federation-ssr",
134
- pre: [
135
- "@modern-js/plugin-module-federation-config",
136
- "@modern-js/plugin-module-federation"
137
- ],
138
- setup: async (api) => {
139
- var _pluginOptions_userConfig, _modernjsConfig_server;
140
- const modernjsConfig = api.getConfig();
141
- var _pluginOptions_userConfig_ssr;
142
- const enableSSR = (_pluginOptions_userConfig_ssr = (_pluginOptions_userConfig = pluginOptions.userConfig) === null || _pluginOptions_userConfig === void 0 ? void 0 : _pluginOptions_userConfig.ssr) !== null && _pluginOptions_userConfig_ssr !== void 0 ? _pluginOptions_userConfig_ssr : Boolean(modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
143
- if (!enableSSR) {
144
- return;
145
- }
146
- setEnv();
147
- api._internalRuntimePlugins(({ entrypoint, plugins }) => {
148
- const { fetchServerQuery } = pluginOptions;
149
- plugins.push({
150
- name: "injectDataFetchFunction",
151
- path: "@module-federation/modern-js/ssr-inject-data-fetch-function-plugin",
152
- config: {
153
- fetchServerQuery
154
- }
155
- });
156
- if (!isDev()) {
157
- return {
158
- entrypoint,
159
- plugins
160
- };
161
- }
162
- plugins.push({
163
- name: "mfSSRDev",
164
- path: "@module-federation/modern-js/ssr-dev-plugin",
165
- config: {}
166
- });
167
- return {
168
- entrypoint,
169
- plugins
170
- };
171
- });
172
- if (pluginOptions.ssrConfig.remotes) {
173
- api._internalServerPlugins(({ plugins }) => {
174
- plugins.push({
175
- name: "@module-federation/modern-js/data-fetch-server-plugin",
176
- options: {}
177
- });
178
- return {
179
- plugins
180
- };
181
- });
182
- }
183
- api.modifyBundlerChain((chain) => {
184
- const target = chain.get("target");
185
- if (skipByTarget(target)) {
186
- return;
187
- }
188
- const bundlerType = api.getAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
189
- const MFPlugin = bundlerType === "webpack" ? ModuleFederationPlugin : RspackModuleFederationPlugin;
190
- const isWeb = isWebTarget(target);
191
- if (!isWeb) {
192
- if (!chain.plugins.has(CHAIN_MF_PLUGIN_ID)) {
193
- chain.plugin(CHAIN_MF_PLUGIN_ID).use(MFPlugin, [
194
- pluginOptions.ssrConfig
195
- ]).init((Plugin, args) => {
196
- pluginOptions.nodePlugin = new Plugin(args[0]);
197
- return pluginOptions.nodePlugin;
198
- });
199
- }
200
- }
201
- if (!isWeb) {
202
- chain.target("async-node");
203
- if (isDev()) {
204
- chain.plugin("UniverseEntryChunkTrackerPlugin").use(UniverseEntryChunkTrackerPlugin);
205
- }
206
- const userSSRConfig = pluginOptions.userConfig.ssr ? typeof pluginOptions.userConfig.ssr === "object" ? pluginOptions.userConfig.ssr : {} : {};
207
- const publicPath = chain.output.get("publicPath");
208
- if (userSSRConfig.distOutputDir && publicPath) {
209
- chain.output.publicPath(`${publicPath}${userSSRConfig.distOutputDir}/`);
210
- }
211
- }
212
- if (isDev() && isWeb) {
213
- chain.externals({
214
- "@module-federation/node/utils": "NOT_USED_IN_BROWSER"
215
- });
216
- }
217
- });
218
- api.config(() => {
219
- return {
220
- builderPlugins: [
221
- mfSSRRsbuildPlugin(pluginOptions)
222
- ],
223
- tools: {
224
- devServer: {
225
- before: [
226
- (req, res, next) => {
227
- if (!enableSSR) {
228
- next();
229
- return;
230
- }
231
- try {
232
- var _req_url, _req_url1;
233
- if (((_req_url = req.url) === null || _req_url === void 0 ? void 0 : _req_url.includes(".json")) && !((_req_url1 = req.url) === null || _req_url1 === void 0 ? void 0 : _req_url1.includes("hot-update"))) {
234
- const filepath = path.join(process.cwd(), `dist${req.url}`);
235
- fs.statSync(filepath);
236
- res.setHeader("Access-Control-Allow-Origin", "*");
237
- res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS");
238
- res.setHeader("Access-Control-Allow-Headers", "*");
239
- fs.createReadStream(filepath).pipe(res);
240
- } else {
241
- next();
242
- }
243
- } catch (err) {
244
- logger.debug(err);
245
- next();
246
- }
247
- }
248
- ]
249
- }
250
- }
251
- };
252
- });
253
- const writeMergedManifest = () => {
254
- const { distOutputDir, assetResources } = pluginOptions;
255
- const browserAssets = assetResources.browser;
256
- const nodeAssets = assetResources.node;
257
- if (!distOutputDir || !browserAssets || !nodeAssets) {
258
- return;
259
- }
260
- try {
261
- updateStatsAndManifest(nodeAssets, browserAssets, distOutputDir);
262
- } catch (err) {
263
- logger.error(err);
264
- }
265
- };
266
- api.onAfterBuild(() => {
267
- writeMergedManifest();
268
- });
269
- api.onDevCompileDone(() => {
270
- writeMergedManifest();
271
- });
272
- }
273
- });
274
- var ssrPlugin_default = moduleFederationSSRPlugin;
275
- export {
276
- CHAIN_MF_PLUGIN_ID,
277
- ssrPlugin_default as default,
278
- moduleFederationSSRPlugin,
279
- setEnv
280
- };
@@ -1,53 +0,0 @@
1
- import os from "os";
2
- const localIpv4 = "127.0.0.1";
3
- const getIpv4Interfaces = () => {
4
- try {
5
- const interfaces = os.networkInterfaces();
6
- const ipv4Interfaces = [];
7
- Object.values(interfaces).forEach((detail) => {
8
- detail === null || detail === void 0 ? void 0 : detail.forEach((detail2) => {
9
- const familyV4Value = typeof detail2.family === "string" ? "IPv4" : 4;
10
- if (detail2.family === familyV4Value && detail2.address !== localIpv4) {
11
- ipv4Interfaces.push(detail2);
12
- }
13
- });
14
- });
15
- return ipv4Interfaces;
16
- } catch (_err) {
17
- return [];
18
- }
19
- };
20
- const getIPV4 = () => {
21
- const ipv4Interfaces = getIpv4Interfaces();
22
- const ipv4Interface = ipv4Interfaces[0] || {
23
- address: localIpv4
24
- };
25
- return ipv4Interface.address;
26
- };
27
- const isWebTarget = (target) => {
28
- const WEB_TARGET = "web";
29
- if (Array.isArray(target)) {
30
- return target.includes(WEB_TARGET);
31
- } else if (typeof target === "string") {
32
- return target === WEB_TARGET;
33
- }
34
- return false;
35
- };
36
- const skipByTarget = (target) => {
37
- const IGNORE_TARGET = "webworker";
38
- if (Array.isArray(target)) {
39
- return target.includes(IGNORE_TARGET);
40
- } else if (typeof target === "string") {
41
- return target === IGNORE_TARGET;
42
- }
43
- return false;
44
- };
45
- function isDev() {
46
- return process.env.NODE_ENV === "development";
47
- }
48
- export {
49
- getIPV4,
50
- isDev,
51
- isWebTarget,
52
- skipByTarget
53
- };
@@ -1,6 +0,0 @@
1
- const LOCALHOST = "localhost";
2
- const PLUGIN_IDENTIFIER = "[ Modern.js Module Federation ]";
3
- export {
4
- LOCALHOST,
5
- PLUGIN_IDENTIFIER
6
- };
@@ -1,7 +0,0 @@
1
- import { createLogger } from "@module-federation/sdk";
2
- import { PLUGIN_IDENTIFIER } from "./constant";
3
- const logger = createLogger(PLUGIN_IDENTIFIER);
4
- var logger_default = logger;
5
- export {
6
- logger_default as default
7
- };
@@ -1,5 +0,0 @@
1
- export * from "@module-federation/bridge-react/plugin";
2
- import { default as default2 } from "@module-federation/bridge-react/plugin";
3
- export {
4
- default2 as default
5
- };
@@ -1,49 +0,0 @@
1
- import fs from "fs-extra";
2
- import { LRUCache } from "lru-cache";
3
- class FileCache {
4
- /**
5
- * Check if file exists and return file info
6
- * @param filepath Path to the file
7
- * @returns FileResult or null if file doesn't exist
8
- */
9
- async getFile(filepath) {
10
- if (!await fs.pathExists(filepath)) {
11
- return null;
12
- }
13
- try {
14
- const stat = await fs.lstat(filepath);
15
- const currentModified = stat.mtimeMs;
16
- const cachedEntry = this.cache.get(filepath);
17
- if (cachedEntry && currentModified <= cachedEntry.lastModified) {
18
- return {
19
- content: cachedEntry.content,
20
- lastModified: cachedEntry.lastModified
21
- };
22
- }
23
- const content = await fs.readFile(filepath, "utf-8");
24
- const newEntry = {
25
- content,
26
- lastModified: currentModified
27
- };
28
- this.cache.set(filepath, newEntry, {
29
- size: stat.size || content.length
30
- });
31
- return {
32
- content,
33
- lastModified: currentModified
34
- };
35
- } catch (err) {
36
- return null;
37
- }
38
- }
39
- constructor() {
40
- this.cache = new LRUCache({
41
- maxSize: 200 * 1024 * 1024
42
- });
43
- }
44
- }
45
- const fileCache = new FileCache();
46
- export {
47
- FileCache,
48
- fileCache
49
- };
@@ -1,27 +0,0 @@
1
- import { it, expect, describe, vi, beforeAll } from "vitest";
2
- import { FileCache } from "./fileCache";
3
- beforeAll(() => {
4
- vi.mock("fs-extra", () => ({
5
- default: {
6
- pathExists: () => {
7
- return true;
8
- },
9
- lstat: () => {
10
- return {
11
- mtimeMs: Date.now(),
12
- size: 4
13
- };
14
- },
15
- readFile: () => {
16
- return "test";
17
- }
18
- }
19
- }));
20
- });
21
- describe("modern serve static file cache", async () => {
22
- it("should cache file", async () => {
23
- const cache = new FileCache();
24
- const result = await cache.getFile("test.txt");
25
- expect(result === null || result === void 0 ? void 0 : result.content).toBe("test");
26
- });
27
- });
@@ -1,39 +0,0 @@
1
- import { createCorsMiddleware, createStaticMiddleware } from "./staticMiddleware";
2
- const staticServePlugin = () => ({
3
- name: "@modern-js/module-federation/server",
4
- setup: (api) => {
5
- api.onPrepare(() => {
6
- var _config_output, _config_server;
7
- if (process.env.NODE_ENV === "development") {
8
- return;
9
- }
10
- const { middlewares } = api.getServerContext();
11
- const config = api.getServerConfig();
12
- const assetPrefix = ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.assetPrefix) || "";
13
- if ((_config_server = config.server) === null || _config_server === void 0 ? void 0 : _config_server.ssr) {
14
- const context = api.getServerContext();
15
- const pwd = context.distDirectory;
16
- const serverStaticMiddleware = createStaticMiddleware({
17
- assetPrefix,
18
- pwd
19
- });
20
- middlewares.push({
21
- name: "module-federation-serve-manifest",
22
- handler: serverStaticMiddleware
23
- });
24
- }
25
- if (process.env.MODERN_MF_AUTO_CORS) {
26
- const corsMiddleware = createCorsMiddleware();
27
- middlewares.push({
28
- name: "module-federation-cors",
29
- handler: corsMiddleware
30
- });
31
- }
32
- });
33
- }
34
- });
35
- var server_default = staticServePlugin;
36
- export {
37
- server_default as default,
38
- staticServePlugin
39
- };
@@ -1,55 +0,0 @@
1
- import fs from "fs-extra";
2
- import path from "node:path";
3
- import { fileCache } from "./fileCache";
4
- const bundlesAssetPrefix = "/bundles";
5
- const removeHost = (url) => {
6
- try {
7
- const hasProtocol = url.includes("://");
8
- const hasDomain = hasProtocol || url.startsWith("//");
9
- const pathname = hasDomain ? new URL(hasProtocol ? url : `http:${url}`).pathname : url;
10
- return pathname;
11
- } catch (e) {
12
- return url;
13
- }
14
- };
15
- const createStaticMiddleware = (options) => {
16
- const { assetPrefix, pwd } = options;
17
- return async (c, next) => {
18
- const pathname = c.req.path;
19
- if (path.extname(pathname) !== ".js") {
20
- return next();
21
- }
22
- const prefixWithoutHost = removeHost(assetPrefix);
23
- const prefixWithBundle = path.join(prefixWithoutHost, bundlesAssetPrefix);
24
- if (!pathname.startsWith(prefixWithBundle)) {
25
- return next();
26
- }
27
- const pathnameWithoutPrefix = pathname.replace(prefixWithBundle, "");
28
- const filepath = path.join(pwd, bundlesAssetPrefix, pathnameWithoutPrefix);
29
- if (!await fs.pathExists(filepath)) {
30
- return next();
31
- }
32
- const fileResult = await fileCache.getFile(filepath);
33
- if (!fileResult) {
34
- return next();
35
- }
36
- c.header("Content-Type", "application/javascript");
37
- c.header("Content-Length", String(fileResult.content.length));
38
- return c.body(fileResult.content, 200);
39
- };
40
- };
41
- const createCorsMiddleware = () => {
42
- return async (c, next) => {
43
- const pathname = c.req.path;
44
- if (path.extname(pathname)) {
45
- c.header("Access-Control-Allow-Origin", "*");
46
- c.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS");
47
- c.header("Access-Control-Allow-Headers", "*");
48
- }
49
- return next();
50
- };
51
- };
52
- export {
53
- createCorsMiddleware,
54
- createStaticMiddleware
55
- };
@@ -1,162 +0,0 @@
1
- import { it, expect, describe, vi, beforeEach } from "vitest";
2
- import { createStaticMiddleware } from "./staticMiddleware";
3
- vi.mock("fs-extra", () => ({
4
- default: {
5
- pathExists: vi.fn()
6
- }
7
- }));
8
- vi.mock("./fileCache", () => ({
9
- fileCache: {
10
- getFile: vi.fn()
11
- }
12
- }));
13
- import fs from "fs-extra";
14
- import { fileCache } from "./fileCache";
15
- describe("staticMiddleware", () => {
16
- let middleware;
17
- let mockContext;
18
- let nextSpy;
19
- beforeEach(() => {
20
- vi.clearAllMocks();
21
- middleware = createStaticMiddleware({
22
- assetPrefix: "",
23
- pwd: "/test/path"
24
- });
25
- nextSpy = vi.fn();
26
- mockContext = {
27
- req: {
28
- path: ""
29
- },
30
- header: vi.fn(),
31
- body: vi.fn()
32
- };
33
- });
34
- describe("file extension filtering", () => {
35
- it("should call next() for non-js files", async () => {
36
- mockContext.req.path = "/bundles/test.css";
37
- await middleware(mockContext, nextSpy);
38
- expect(nextSpy).toHaveBeenCalledOnce();
39
- expect(mockContext.header).not.toHaveBeenCalled();
40
- expect(mockContext.body).not.toHaveBeenCalled();
41
- });
42
- it("should call next() for files without extension", async () => {
43
- mockContext.req.path = "/bundles/test";
44
- await middleware(mockContext, nextSpy);
45
- expect(nextSpy).toHaveBeenCalledOnce();
46
- expect(mockContext.header).not.toHaveBeenCalled();
47
- expect(mockContext.body).not.toHaveBeenCalled();
48
- });
49
- it("should process .js files", async () => {
50
- mockContext.req.path = "/bundles/test.js";
51
- fs.pathExists.mockResolvedValue(false);
52
- await middleware(mockContext, nextSpy);
53
- expect(fs.pathExists).toHaveBeenCalled();
54
- });
55
- });
56
- describe("asset prefix filtering", () => {
57
- it("should call next() for paths not starting with /bundles", async () => {
58
- mockContext.req.path = "/assets/test.js";
59
- await middleware(mockContext, nextSpy);
60
- expect(nextSpy).toHaveBeenCalledOnce();
61
- expect(fs.pathExists).not.toHaveBeenCalled();
62
- expect(mockContext.header).not.toHaveBeenCalled();
63
- expect(mockContext.body).not.toHaveBeenCalled();
64
- });
65
- it("should call next() for root path", async () => {
66
- mockContext.req.path = "/test.js";
67
- await middleware(mockContext, nextSpy);
68
- expect(nextSpy).toHaveBeenCalledOnce();
69
- expect(fs.pathExists).not.toHaveBeenCalled();
70
- });
71
- it("should process paths starting with /bundles", async () => {
72
- mockContext.req.path = "/bundles/test.js";
73
- fs.pathExists.mockResolvedValue(false);
74
- await middleware(mockContext, nextSpy);
75
- expect(fs.pathExists).toHaveBeenCalledWith("/test/path/bundles/test.js");
76
- });
77
- });
78
- describe("file existence check", () => {
79
- it("should call next() when file does not exist", async () => {
80
- mockContext.req.path = "/bundles/nonexistent.js";
81
- fs.pathExists.mockResolvedValue(false);
82
- await middleware(mockContext, nextSpy);
83
- expect(fs.pathExists).toHaveBeenCalledWith("/test/path/bundles/nonexistent.js");
84
- expect(nextSpy).toHaveBeenCalledOnce();
85
- expect(fileCache.getFile).not.toHaveBeenCalled();
86
- expect(mockContext.header).not.toHaveBeenCalled();
87
- expect(mockContext.body).not.toHaveBeenCalled();
88
- });
89
- it("should proceed to file cache when file exists", async () => {
90
- mockContext.req.path = "/bundles/existing.js";
91
- fs.pathExists.mockResolvedValue(true);
92
- fileCache.getFile.mockResolvedValue(null);
93
- await middleware(mockContext, nextSpy);
94
- expect(fs.pathExists).toHaveBeenCalledWith("/test/path/bundles/existing.js");
95
- expect(fileCache.getFile).toHaveBeenCalledWith("/test/path/bundles/existing.js");
96
- });
97
- });
98
- describe("successful file serving", () => {
99
- it("should serve file content with correct headers", async () => {
100
- const mockFileContent = 'console.log("test");';
101
- const mockFileResult = {
102
- content: mockFileContent,
103
- lastModified: Date.now()
104
- };
105
- mockContext.req.path = "/bundles/app.js";
106
- fs.pathExists.mockResolvedValue(true);
107
- fileCache.getFile.mockResolvedValue(mockFileResult);
108
- mockContext.body.mockReturnValue("response");
109
- const result = await middleware(mockContext, nextSpy);
110
- expect(fs.pathExists).toHaveBeenCalledWith("/test/path/bundles/app.js");
111
- expect(fileCache.getFile).toHaveBeenCalledWith("/test/path/bundles/app.js");
112
- expect(nextSpy).not.toHaveBeenCalled();
113
- expect(mockContext.header).toHaveBeenCalledWith("Content-Type", "application/javascript");
114
- expect(mockContext.header).toHaveBeenCalledWith("Content-Length", String(mockFileResult.content.length));
115
- expect(mockContext.body).toHaveBeenCalledWith(mockFileResult.content, 200);
116
- expect(result).toBe("response");
117
- });
118
- it("should handle empty file content", async () => {
119
- const mockFileResult = {
120
- content: "",
121
- lastModified: Date.now()
122
- };
123
- mockContext.req.path = "/bundles/empty.js";
124
- fs.pathExists.mockResolvedValue(true);
125
- fileCache.getFile.mockResolvedValue(mockFileResult);
126
- mockContext.body.mockReturnValue("empty-response");
127
- const result = await middleware(mockContext, nextSpy);
128
- expect(mockContext.header).toHaveBeenCalledWith("Content-Length", "0");
129
- expect(mockContext.body).toHaveBeenCalledWith(mockFileResult.content, 200);
130
- expect(result).toBe("empty-response");
131
- expect(nextSpy).not.toHaveBeenCalled();
132
- });
133
- });
134
- describe("asset prefix handling", () => {
135
- it("should handle custom asset prefix correctly", async () => {
136
- const customMiddleware = createStaticMiddleware({
137
- assetPrefix: "/custom-prefix",
138
- pwd: "/test/path"
139
- });
140
- mockContext.req.path = "/bundles/test.js";
141
- await customMiddleware(mockContext, nextSpy);
142
- expect(nextSpy).toHaveBeenCalledOnce();
143
- expect(mockContext.header).not.toHaveBeenCalled();
144
- expect(mockContext.body).not.toHaveBeenCalled();
145
- });
146
- it("should handle asset prefix removal correctly", async () => {
147
- const customMiddleware = createStaticMiddleware({
148
- assetPrefix: "/prefix",
149
- pwd: "/test/path"
150
- });
151
- const mockFileResult = {
152
- content: "test content",
153
- lastModified: Date.now()
154
- };
155
- mockContext.req.path = "/prefix/bundles/test.js";
156
- fs.pathExists.mockResolvedValue(true);
157
- fileCache.getFile.mockResolvedValue(mockFileResult);
158
- await customMiddleware(mockContext, nextSpy);
159
- expect(fs.pathExists).toHaveBeenCalledWith("/test/path/bundles/test.js");
160
- });
161
- });
162
- });