@module-federation/modern-js 0.14.1 → 0.14.3

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 (98) hide show
  1. package/dist/LICENSE +21 -0
  2. package/dist/cjs/cli/configPlugin.js +377 -0
  3. package/dist/cjs/cli/configPlugin.spec.js +112 -0
  4. package/dist/cjs/cli/constant.js +28 -0
  5. package/dist/cjs/cli/index.js +93 -0
  6. package/dist/cjs/cli/logger.js +27 -0
  7. package/dist/cjs/cli/manifest.js +75 -0
  8. package/dist/cjs/cli/mfRuntimePlugins/inject-node-fetch.js +44 -0
  9. package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +82 -0
  10. package/dist/cjs/cli/mfRuntimePlugins/shared-strategy.js +43 -0
  11. package/dist/cjs/cli/ssrPlugin.js +217 -0
  12. package/dist/cjs/cli/utils.js +85 -0
  13. package/dist/cjs/constant.js +31 -0
  14. package/dist/cjs/interfaces/bundler.js +16 -0
  15. package/dist/cjs/runtime/createRemoteSSRComponent.js +201 -0
  16. package/dist/cjs/runtime/index.js +38 -0
  17. package/dist/cjs/server/fileCache.js +84 -0
  18. package/dist/cjs/server/fileCache.spec.js +28 -0
  19. package/dist/cjs/server/index.js +58 -0
  20. package/dist/cjs/server/staticMiddleware.js +77 -0
  21. package/dist/cjs/server/staticMiddleware.spec.js +185 -0
  22. package/dist/cjs/ssr-runtime/SSRLiveReload.js +43 -0
  23. package/dist/cjs/ssr-runtime/index.js +22 -0
  24. package/dist/cjs/ssr-runtime/plugin.js +68 -0
  25. package/dist/cjs/types/index.js +16 -0
  26. package/dist/esm/cli/configPlugin.js +384 -0
  27. package/dist/esm/cli/configPlugin.spec.js +108 -0
  28. package/dist/esm/cli/constant.js +4 -0
  29. package/dist/esm/cli/index.js +88 -0
  30. package/dist/esm/cli/logger.js +7 -0
  31. package/dist/esm/cli/manifest.js +41 -0
  32. package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js +16 -0
  33. package/dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.js +74 -0
  34. package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +25 -0
  35. package/dist/esm/cli/ssrPlugin.js +199 -0
  36. package/dist/esm/cli/utils.js +49 -0
  37. package/dist/esm/constant.js +6 -0
  38. package/dist/esm/interfaces/bundler.js +0 -0
  39. package/dist/esm/runtime/createRemoteSSRComponent.js +191 -0
  40. package/dist/esm/runtime/index.js +12 -0
  41. package/dist/esm/server/fileCache.js +98 -0
  42. package/dist/esm/server/fileCache.spec.js +50 -0
  43. package/dist/esm/server/index.js +36 -0
  44. package/dist/esm/server/staticMiddleware.js +81 -0
  45. package/dist/esm/server/staticMiddleware.spec.js +328 -0
  46. package/dist/esm/ssr-runtime/SSRLiveReload.js +26 -0
  47. package/dist/esm/ssr-runtime/index.js +1 -0
  48. package/dist/esm/ssr-runtime/plugin.js +70 -0
  49. package/dist/esm/types/index.js +0 -0
  50. package/dist/esm-node/cli/configPlugin.js +336 -0
  51. package/dist/esm-node/cli/configPlugin.spec.js +89 -0
  52. package/dist/esm-node/cli/constant.js +4 -0
  53. package/dist/esm-node/cli/index.js +68 -0
  54. package/dist/esm-node/cli/logger.js +7 -0
  55. package/dist/esm-node/cli/manifest.js +41 -0
  56. package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.js +14 -0
  57. package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +62 -0
  58. package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +23 -0
  59. package/dist/esm-node/cli/ssrPlugin.js +181 -0
  60. package/dist/esm-node/cli/utils.js +49 -0
  61. package/dist/esm-node/constant.js +6 -0
  62. package/dist/esm-node/interfaces/bundler.js +0 -0
  63. package/dist/esm-node/runtime/createRemoteSSRComponent.js +166 -0
  64. package/dist/esm-node/runtime/index.js +12 -0
  65. package/dist/esm-node/server/fileCache.js +49 -0
  66. package/dist/esm-node/server/fileCache.spec.js +27 -0
  67. package/dist/esm-node/server/index.js +34 -0
  68. package/dist/esm-node/server/staticMiddleware.js +43 -0
  69. package/dist/esm-node/server/staticMiddleware.spec.js +162 -0
  70. package/dist/esm-node/ssr-runtime/SSRLiveReload.js +19 -0
  71. package/dist/esm-node/ssr-runtime/index.js +1 -0
  72. package/dist/esm-node/ssr-runtime/plugin.js +34 -0
  73. package/dist/esm-node/types/index.js +0 -0
  74. package/dist/types/cli/configPlugin.d.ts +21 -0
  75. package/dist/types/cli/configPlugin.spec.d.ts +1 -0
  76. package/dist/types/cli/constant.d.ts +1 -0
  77. package/dist/types/cli/index.d.ts +5 -0
  78. package/dist/types/cli/logger.d.ts +2 -0
  79. package/dist/types/cli/manifest.d.ts +2 -0
  80. package/dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts +3 -0
  81. package/dist/types/cli/mfRuntimePlugins/resolve-entry-ipv4.d.ts +3 -0
  82. package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +3 -0
  83. package/dist/types/cli/ssrPlugin.d.ts +6 -0
  84. package/dist/types/cli/utils.d.ts +5 -0
  85. package/dist/types/constant.d.ts +2 -0
  86. package/dist/types/interfaces/bundler.d.ts +18 -0
  87. package/dist/types/runtime/createRemoteSSRComponent.d.ts +18 -0
  88. package/dist/types/runtime/index.d.ts +5 -0
  89. package/dist/types/server/fileCache.d.ts +14 -0
  90. package/dist/types/server/fileCache.spec.d.ts +1 -0
  91. package/dist/types/server/index.d.ts +4 -0
  92. package/dist/types/server/staticMiddleware.d.ts +6 -0
  93. package/dist/types/server/staticMiddleware.spec.d.ts +1 -0
  94. package/dist/types/ssr-runtime/SSRLiveReload.d.ts +1 -0
  95. package/dist/types/ssr-runtime/index.d.ts +1 -0
  96. package/dist/types/ssr-runtime/plugin.d.ts +2 -0
  97. package/dist/types/types/index.d.ts +22 -0
  98. package/package.json +8 -8
@@ -0,0 +1,181 @@
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 { updateStatsAndManifest } from "./manifest";
7
+ import { isDev } from "./constant";
8
+ import logger from "./logger";
9
+ import { isWebTarget, skipByTarget } from "./utils";
10
+ function setEnv() {
11
+ process.env["MF_DISABLE_EMIT_STATS"] = "true";
12
+ process.env["MF_SSR_PRJ"] = "true";
13
+ }
14
+ const CHAIN_MF_PLUGIN_ID = "plugin-module-federation-server";
15
+ const mfSSRRsbuildPlugin = (pluginOptions) => {
16
+ return {
17
+ name: "@modern-js/plugin-mf-post-config",
18
+ pre: [
19
+ "@modern-js/builder-plugin-ssr"
20
+ ],
21
+ setup(api) {
22
+ if (pluginOptions.csrConfig.getPublicPath) {
23
+ return;
24
+ }
25
+ let csrOutputPath = "";
26
+ let ssrOutputPath = "";
27
+ let ssrEnv = "";
28
+ api.modifyEnvironmentConfig((config, { name }) => {
29
+ const target = config.output.target;
30
+ if (skipByTarget(target)) {
31
+ return config;
32
+ }
33
+ if (isWebTarget(target)) {
34
+ csrOutputPath = config.output.distPath.root;
35
+ } else {
36
+ ssrOutputPath = config.output.distPath.root;
37
+ ssrEnv = name;
38
+ }
39
+ return config;
40
+ });
41
+ const modifySSRPublicPath = (config, utils) => {
42
+ if (ssrEnv !== utils.environment.name) {
43
+ return config;
44
+ }
45
+ const userSSRConfig = pluginOptions.userConfig.ssr ? typeof pluginOptions.userConfig.ssr === "object" ? pluginOptions.userConfig.ssr : {} : {};
46
+ if (userSSRConfig.distOutputDir) {
47
+ return;
48
+ }
49
+ config.output.publicPath = `${config.output.publicPath}${path.relative(csrOutputPath, ssrOutputPath)}/`;
50
+ return config;
51
+ };
52
+ api.modifyWebpackConfig((config, utils) => {
53
+ modifySSRPublicPath(config, utils);
54
+ return config;
55
+ });
56
+ api.modifyRspackConfig((config, utils) => {
57
+ modifySSRPublicPath(config, utils);
58
+ return config;
59
+ });
60
+ }
61
+ };
62
+ };
63
+ const moduleFederationSSRPlugin = (pluginOptions) => ({
64
+ name: "@modern-js/plugin-module-federation-ssr",
65
+ pre: [
66
+ "@modern-js/plugin-module-federation-config",
67
+ "@modern-js/plugin-module-federation"
68
+ ],
69
+ setup: async (api) => {
70
+ var _pluginOptions_userConfig, _modernjsConfig_server;
71
+ const modernjsConfig = api.getConfig();
72
+ var _pluginOptions_userConfig_ssr;
73
+ 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);
74
+ if (!enableSSR) {
75
+ return;
76
+ }
77
+ setEnv();
78
+ api._internalRuntimePlugins(({ entrypoint, plugins }) => {
79
+ if (!isDev) {
80
+ return {
81
+ entrypoint,
82
+ plugins
83
+ };
84
+ }
85
+ plugins.push({
86
+ name: "mfSSR",
87
+ path: "@module-federation/modern-js/ssr-runtime",
88
+ config: {}
89
+ });
90
+ return {
91
+ entrypoint,
92
+ plugins
93
+ };
94
+ });
95
+ api.modifyBundlerChain((chain) => {
96
+ const target = chain.get("target");
97
+ if (skipByTarget(target)) {
98
+ return;
99
+ }
100
+ const bundlerType = api.getAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
101
+ const MFPlugin = bundlerType === "webpack" ? ModuleFederationPlugin : RspackModuleFederationPlugin;
102
+ const isWeb = isWebTarget(target);
103
+ if (!isWeb) {
104
+ if (!chain.plugins.has(CHAIN_MF_PLUGIN_ID)) {
105
+ chain.plugin(CHAIN_MF_PLUGIN_ID).use(MFPlugin, [
106
+ pluginOptions.ssrConfig
107
+ ]).init((Plugin, args) => {
108
+ pluginOptions.nodePlugin = new Plugin(args[0]);
109
+ return pluginOptions.nodePlugin;
110
+ });
111
+ }
112
+ }
113
+ if (!isWeb) {
114
+ chain.target("async-node");
115
+ if (isDev) {
116
+ chain.plugin("UniverseEntryChunkTrackerPlugin").use(UniverseEntryChunkTrackerPlugin);
117
+ }
118
+ const userSSRConfig = pluginOptions.userConfig.ssr ? typeof pluginOptions.userConfig.ssr === "object" ? pluginOptions.userConfig.ssr : {} : {};
119
+ const publicPath = chain.output.get("publicPath");
120
+ if (userSSRConfig.distOutputDir && publicPath) {
121
+ chain.output.publicPath(`${publicPath}${userSSRConfig.distOutputDir}/`);
122
+ }
123
+ }
124
+ if (isDev && isWeb) {
125
+ chain.externals({
126
+ "@module-federation/node/utils": "NOT_USED_IN_BROWSER"
127
+ });
128
+ }
129
+ });
130
+ api.config(() => {
131
+ return {
132
+ builderPlugins: [
133
+ mfSSRRsbuildPlugin(pluginOptions)
134
+ ],
135
+ tools: {
136
+ devServer: {
137
+ before: [
138
+ (req, res, next) => {
139
+ if (!enableSSR) {
140
+ next();
141
+ return;
142
+ }
143
+ try {
144
+ var _req_url, _req_url1;
145
+ 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"))) {
146
+ const filepath = path.join(process.cwd(), `dist${req.url}`);
147
+ fs.statSync(filepath);
148
+ res.setHeader("Access-Control-Allow-Origin", "*");
149
+ res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS");
150
+ res.setHeader("Access-Control-Allow-Headers", "*");
151
+ fs.createReadStream(filepath).pipe(res);
152
+ } else {
153
+ next();
154
+ }
155
+ } catch (err) {
156
+ logger.debug(err);
157
+ next();
158
+ }
159
+ }
160
+ ]
161
+ }
162
+ }
163
+ };
164
+ });
165
+ api.onAfterBuild(() => {
166
+ const { nodePlugin, browserPlugin, distOutputDir } = pluginOptions;
167
+ updateStatsAndManifest(nodePlugin, browserPlugin, distOutputDir);
168
+ });
169
+ api.onDevCompileDone(() => {
170
+ const { nodePlugin, browserPlugin, distOutputDir } = pluginOptions;
171
+ updateStatsAndManifest(nodePlugin, browserPlugin, distOutputDir);
172
+ });
173
+ }
174
+ });
175
+ var ssrPlugin_default = moduleFederationSSRPlugin;
176
+ export {
177
+ CHAIN_MF_PLUGIN_ID,
178
+ ssrPlugin_default as default,
179
+ moduleFederationSSRPlugin,
180
+ setEnv
181
+ };
@@ -0,0 +1,49 @@
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
+ export {
46
+ getIPV4,
47
+ isWebTarget,
48
+ skipByTarget
49
+ };
@@ -0,0 +1,6 @@
1
+ const LOCALHOST = "localhost";
2
+ const PLUGIN_IDENTIFIER = "[ Modern.js Module Federation ]";
3
+ export {
4
+ LOCALHOST,
5
+ PLUGIN_IDENTIFIER
6
+ };
File without changes
@@ -0,0 +1,166 @@
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import React from "react";
3
+ import { getInstance } from "@module-federation/enhanced/runtime";
4
+ import { ErrorBoundary } from "react-error-boundary";
5
+ function getLoadedRemoteInfos(instance, id) {
6
+ const { name, expose } = instance.remoteHandler.idToRemoteMap[id] || {};
7
+ if (!name) {
8
+ return;
9
+ }
10
+ const module = instance.moduleCache.get(name);
11
+ if (!module) {
12
+ return;
13
+ }
14
+ const { remoteSnapshot } = instance.snapshotHandler.getGlobalRemoteInfo(module.remoteInfo);
15
+ return {
16
+ ...module.remoteInfo,
17
+ snapshot: remoteSnapshot,
18
+ expose
19
+ };
20
+ }
21
+ function getTargetModuleInfo(id) {
22
+ const instance = getInstance();
23
+ if (!instance) {
24
+ return;
25
+ }
26
+ const loadedRemoteInfo = getLoadedRemoteInfos(instance, id);
27
+ if (!loadedRemoteInfo) {
28
+ return;
29
+ }
30
+ const snapshot = loadedRemoteInfo.snapshot;
31
+ if (!snapshot) {
32
+ return;
33
+ }
34
+ const publicPath = "publicPath" in snapshot ? snapshot.publicPath : "getPublicPath" in snapshot ? new Function(snapshot.getPublicPath)() : "";
35
+ if (!publicPath) {
36
+ return;
37
+ }
38
+ const modules = "modules" in snapshot ? snapshot.modules : [];
39
+ const targetModule = modules.find((m) => m.modulePath === loadedRemoteInfo.expose);
40
+ if (!targetModule) {
41
+ return;
42
+ }
43
+ const remoteEntry = "remoteEntry" in snapshot ? snapshot.remoteEntry : "";
44
+ if (!remoteEntry) {
45
+ return;
46
+ }
47
+ return {
48
+ module: targetModule,
49
+ publicPath,
50
+ remoteEntry
51
+ };
52
+ }
53
+ function collectSSRAssets(options) {
54
+ const { id, injectLink = true, injectScript = true } = typeof options === "string" ? {
55
+ id: options
56
+ } : options;
57
+ const links = [];
58
+ const scripts = [];
59
+ const instance = getInstance();
60
+ if (!instance || !injectLink && !injectScript) {
61
+ return [
62
+ ...scripts,
63
+ ...links
64
+ ];
65
+ }
66
+ const moduleAndPublicPath = getTargetModuleInfo(id);
67
+ if (!moduleAndPublicPath) {
68
+ return [
69
+ ...scripts,
70
+ ...links
71
+ ];
72
+ }
73
+ const { module: targetModule, publicPath, remoteEntry } = moduleAndPublicPath;
74
+ if (injectLink) {
75
+ [
76
+ ...targetModule.assets.css.sync,
77
+ ...targetModule.assets.css.async
78
+ ].forEach((file, index) => {
79
+ links.push(/* @__PURE__ */ _jsx("link", {
80
+ href: `${publicPath}${file}`,
81
+ rel: "stylesheet",
82
+ type: "text/css"
83
+ }, `${file.split(".")[0]}_${index}`));
84
+ });
85
+ }
86
+ if (injectScript) {
87
+ scripts.push(/* @__PURE__ */ _jsx("script", {
88
+ async: true,
89
+ src: `${publicPath}${remoteEntry}`,
90
+ crossOrigin: "anonymous"
91
+ }, remoteEntry.split(".")[0]));
92
+ [
93
+ ...targetModule.assets.js.sync
94
+ ].forEach((file, index) => {
95
+ scripts.push(/* @__PURE__ */ _jsx("script", {
96
+ async: true,
97
+ src: `${publicPath}${file}`,
98
+ crossOrigin: "anonymous"
99
+ }, `${file.split(".")[0]}_${index}`));
100
+ });
101
+ }
102
+ return [
103
+ ...scripts,
104
+ ...links
105
+ ];
106
+ }
107
+ function createRemoteSSRComponent(info) {
108
+ const exportName = (info === null || info === void 0 ? void 0 : info.export) || "default";
109
+ const LazyComponent = /* @__PURE__ */ React.lazy(async () => {
110
+ try {
111
+ const m = await info.loader();
112
+ if (!m) {
113
+ throw new Error("load remote failed");
114
+ }
115
+ const moduleId = m && m[Symbol.for("mf_module_id")];
116
+ const assets = collectSSRAssets({
117
+ id: moduleId
118
+ });
119
+ const Com = m[exportName];
120
+ if (exportName in m && typeof Com === "function") {
121
+ return {
122
+ default: (props) => /* @__PURE__ */ _jsxs(_Fragment, {
123
+ children: [
124
+ assets,
125
+ /* @__PURE__ */ _jsx(Com, {
126
+ ...props
127
+ })
128
+ ]
129
+ })
130
+ };
131
+ } else {
132
+ throw Error(`Make sure that ${moduleId} has the correct export when export is ${String(exportName)}`);
133
+ }
134
+ } catch (err) {
135
+ if (!info.fallback) {
136
+ throw err;
137
+ }
138
+ const FallbackFunctionComponent = info.fallback;
139
+ const FallbackNode = /* @__PURE__ */ _jsx(FallbackFunctionComponent, {
140
+ error: err,
141
+ resetErrorBoundary: () => {
142
+ console.log('SSR mode not support "resetErrorBoundary" !');
143
+ }
144
+ });
145
+ return {
146
+ default: () => FallbackNode
147
+ };
148
+ }
149
+ });
150
+ return (props) => {
151
+ const { key, ...args } = props;
152
+ return /* @__PURE__ */ _jsx(ErrorBoundary, {
153
+ FallbackComponent: info.fallback,
154
+ children: /* @__PURE__ */ _jsx(React.Suspense, {
155
+ fallback: info.loading,
156
+ children: /* @__PURE__ */ _jsx(LazyComponent, {
157
+ ...args
158
+ })
159
+ })
160
+ });
161
+ };
162
+ }
163
+ export {
164
+ collectSSRAssets,
165
+ createRemoteSSRComponent
166
+ };
@@ -0,0 +1,12 @@
1
+ export * from "@module-federation/enhanced/runtime";
2
+ const kit = {
3
+ get createRemoteSSRComponent() {
4
+ return require("./createRemoteSSRComponent").createRemoteSSRComponent;
5
+ },
6
+ get collectSSRAssets() {
7
+ return require("./createRemoteSSRComponent").collectSSRAssets;
8
+ }
9
+ };
10
+ export {
11
+ kit
12
+ };
@@ -0,0 +1,49 @@
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
+ };
@@ -0,0 +1,27 @@
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
+ });
@@ -0,0 +1,34 @@
1
+ import { 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
+ console.log(process.env.NODE_ENV);
8
+ if (process.env.NODE_ENV === "development") {
9
+ return;
10
+ }
11
+ const { middlewares } = api.getServerContext();
12
+ const config = api.getServerConfig();
13
+ const assetPrefix = ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.assetPrefix) || "";
14
+ if (!((_config_server = config.server) === null || _config_server === void 0 ? void 0 : _config_server.ssr)) {
15
+ return;
16
+ }
17
+ const context = api.getServerContext();
18
+ const pwd = context.distDirectory;
19
+ const serverStaticMiddleware = createStaticMiddleware({
20
+ assetPrefix,
21
+ pwd
22
+ });
23
+ middlewares.push({
24
+ name: "module-federation-serve-manifest",
25
+ handler: serverStaticMiddleware
26
+ });
27
+ });
28
+ }
29
+ });
30
+ var server_default = staticServePlugin;
31
+ export {
32
+ server_default as default,
33
+ staticServePlugin
34
+ };
@@ -0,0 +1,43 @@
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
+ export {
42
+ createStaticMiddleware
43
+ };