@module-federation/modern-js 0.0.0-next-20250926024003 → 0.0.0-perf-devtools-20260106124142

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 (115) hide show
  1. package/dist/cjs/cli/configPlugin.js +301 -363
  2. package/dist/cjs/cli/index.js +103 -91
  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 +270 -222
  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 +65 -0
  14. package/dist/cjs/react/v18.js +58 -0
  15. package/dist/cjs/react/v19.js +58 -0
  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/react/v18.mjs +1 -0
  36. package/dist/esm/react/v19.mjs +1 -0
  37. package/dist/esm/server/fileCache.mjs +90 -0
  38. package/dist/esm/server/index.mjs +36 -0
  39. package/dist/esm/server/staticMiddleware.mjs +87 -0
  40. package/dist/esm/ssr-runtime/SSRLiveReload.mjs +22 -0
  41. package/dist/esm/ssr-runtime/devPlugin.mjs +70 -0
  42. package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.mjs +34 -0
  43. package/dist/esm-node/cli/configPlugin.mjs +263 -0
  44. package/dist/esm-node/cli/index.mjs +67 -0
  45. package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.mjs +10 -0
  46. package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.mjs +46 -0
  47. package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.mjs +19 -0
  48. package/dist/esm-node/cli/server/data-fetch-server-plugin.mjs +15 -0
  49. package/dist/esm-node/cli/ssrPlugin.mjs +228 -0
  50. package/dist/esm-node/cli/utils.mjs +40 -0
  51. package/dist/esm-node/constant.mjs +3 -0
  52. package/dist/esm-node/logger.mjs +5 -0
  53. package/dist/esm-node/react/plugin.mjs +3 -0
  54. package/dist/esm-node/react/v18.mjs +1 -0
  55. package/dist/esm-node/react/v19.mjs +1 -0
  56. package/dist/esm-node/server/fileCache.mjs +37 -0
  57. package/dist/esm-node/server/index.mjs +34 -0
  58. package/dist/esm-node/server/staticMiddleware.mjs +42 -0
  59. package/dist/esm-node/ssr-runtime/SSRLiveReload.mjs +15 -0
  60. package/dist/esm-node/ssr-runtime/devPlugin.mjs +33 -0
  61. package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.mjs +14 -0
  62. package/dist/types/react/plugin.d.ts +2 -0
  63. package/dist/types/react/v18.d.ts +1 -0
  64. package/dist/types/react/v19.d.ts +1 -0
  65. package/dist/types/types/index.d.ts +13 -0
  66. package/package.json +59 -29
  67. package/dist/cjs/cli/configPlugin.spec.js +0 -112
  68. package/dist/cjs/server/fileCache.spec.js +0 -28
  69. package/dist/cjs/server/staticMiddleware.spec.js +0 -185
  70. package/dist/esm/cli/configPlugin.js +0 -401
  71. package/dist/esm/cli/configPlugin.spec.js +0 -108
  72. package/dist/esm/cli/index.js +0 -90
  73. package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js +0 -16
  74. package/dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.js +0 -74
  75. package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +0 -25
  76. package/dist/esm/cli/server/data-fetch-server-plugin.js +0 -20
  77. package/dist/esm/cli/ssrPlugin.js +0 -219
  78. package/dist/esm/cli/utils.js +0 -53
  79. package/dist/esm/constant.js +0 -6
  80. package/dist/esm/logger.js +0 -7
  81. package/dist/esm/server/fileCache.js +0 -98
  82. package/dist/esm/server/fileCache.spec.js +0 -50
  83. package/dist/esm/server/index.js +0 -41
  84. package/dist/esm/server/staticMiddleware.js +0 -104
  85. package/dist/esm/server/staticMiddleware.spec.js +0 -328
  86. package/dist/esm/ssr-runtime/SSRLiveReload.js +0 -26
  87. package/dist/esm/ssr-runtime/devPlugin.js +0 -73
  88. package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -34
  89. package/dist/esm-node/cli/configPlugin.js +0 -353
  90. package/dist/esm-node/cli/configPlugin.spec.js +0 -89
  91. package/dist/esm-node/cli/index.js +0 -70
  92. package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.js +0 -14
  93. package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +0 -62
  94. package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +0 -23
  95. package/dist/esm-node/cli/server/data-fetch-server-plugin.js +0 -18
  96. package/dist/esm-node/cli/ssrPlugin.js +0 -200
  97. package/dist/esm-node/cli/utils.js +0 -53
  98. package/dist/esm-node/constant.js +0 -6
  99. package/dist/esm-node/logger.js +0 -7
  100. package/dist/esm-node/server/fileCache.js +0 -49
  101. package/dist/esm-node/server/fileCache.spec.js +0 -27
  102. package/dist/esm-node/server/index.js +0 -39
  103. package/dist/esm-node/server/staticMiddleware.js +0 -55
  104. package/dist/esm-node/server/staticMiddleware.spec.js +0 -162
  105. package/dist/esm-node/ssr-runtime/SSRLiveReload.js +0 -19
  106. package/dist/esm-node/ssr-runtime/devPlugin.js +0 -37
  107. package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -16
  108. /package/dist/esm/interfaces/{bundler.js → bundler.mjs} +0 -0
  109. /package/dist/esm/react/{index.js → index.mjs} +0 -0
  110. /package/dist/esm/runtime/{index.js → index.mjs} +0 -0
  111. /package/dist/esm/types/{index.js → index.mjs} +0 -0
  112. /package/dist/esm-node/interfaces/{bundler.js → bundler.mjs} +0 -0
  113. /package/dist/esm-node/react/{index.js → index.mjs} +0 -0
  114. /package/dist/esm-node/runtime/{index.js → index.mjs} +0 -0
  115. /package/dist/esm-node/types/{index.js → index.mjs} +0 -0
@@ -0,0 +1,228 @@
1
+ import path from "path";
2
+ import fs_extra from "fs-extra";
3
+ import { ModuleFederationPlugin } from "@module-federation/enhanced/webpack";
4
+ import { ModuleFederationPlugin as rspack_ModuleFederationPlugin } from "@module-federation/enhanced/rspack";
5
+ import universe_entry_chunk_tracker_plugin from "@module-federation/node/universe-entry-chunk-tracker-plugin";
6
+ import logger from "../logger.mjs";
7
+ import { isDev, isWebTarget, skipByTarget } from "./utils.mjs";
8
+ import { updateStatsAndManifest } from "@module-federation/rsbuild-plugin/utils";
9
+ import { ManifestFileName, StatsFileName, simpleJoinRemoteEntry } from "@module-federation/sdk";
10
+ function setEnv() {
11
+ process.env['MF_SSR_PRJ'] = 'true';
12
+ }
13
+ const CHAIN_MF_PLUGIN_ID = 'plugin-module-federation-server';
14
+ function getManifestAssetFileNames(manifestOption) {
15
+ if (!manifestOption) return {
16
+ statsFileName: StatsFileName,
17
+ manifestFileName: ManifestFileName
18
+ };
19
+ const JSON_EXT = '.json';
20
+ const filePath = 'boolean' == typeof manifestOption ? '' : manifestOption.filePath || '';
21
+ const baseFileName = 'boolean' == typeof manifestOption ? '' : manifestOption.fileName || '';
22
+ const ensureExt = (name)=>name.endsWith(JSON_EXT) ? name : `${name}${JSON_EXT}`;
23
+ const withSuffix = (name, suffix)=>name.replace(JSON_EXT, `${suffix}${JSON_EXT}`);
24
+ const manifestFileName = baseFileName ? ensureExt(baseFileName) : ManifestFileName;
25
+ const statsFileName = baseFileName ? withSuffix(manifestFileName, '-stats') : StatsFileName;
26
+ return {
27
+ statsFileName: simpleJoinRemoteEntry(filePath, statsFileName),
28
+ manifestFileName: simpleJoinRemoteEntry(filePath, manifestFileName)
29
+ };
30
+ }
31
+ const mfSSRRsbuildPlugin = (pluginOptions)=>({
32
+ name: '@modern-js/plugin-mf-post-config',
33
+ pre: [
34
+ '@modern-js/builder-plugin-ssr'
35
+ ],
36
+ setup (api) {
37
+ var _pluginOptions_csrConfig, _pluginOptions_assetFileNames, _pluginOptions_ssrConfig;
38
+ if (pluginOptions.csrConfig.getPublicPath) return;
39
+ let csrOutputPath = '';
40
+ let ssrOutputPath = '';
41
+ let ssrEnv = '';
42
+ let csrEnv = '';
43
+ const browserAssetFileNames = pluginOptions.assetFileNames.browser || getManifestAssetFileNames(null == (_pluginOptions_csrConfig = pluginOptions.csrConfig) ? void 0 : _pluginOptions_csrConfig.manifest);
44
+ const nodeAssetFileNames = (null == (_pluginOptions_assetFileNames = pluginOptions.assetFileNames) ? void 0 : _pluginOptions_assetFileNames.node) || getManifestAssetFileNames(null == (_pluginOptions_ssrConfig = pluginOptions.ssrConfig) ? void 0 : _pluginOptions_ssrConfig.manifest);
45
+ const collectAssets = (assets, fileNames, tag)=>{
46
+ const statsAsset = assets[fileNames.statsFileName];
47
+ const manifestAsset = assets[fileNames.manifestFileName];
48
+ if (!statsAsset || !manifestAsset) return;
49
+ try {
50
+ const statsRaw = statsAsset.source();
51
+ const manifestRaw = manifestAsset.source();
52
+ const statsContent = 'string' == typeof statsRaw ? statsRaw : statsRaw.toString();
53
+ const manifestContent = 'string' == typeof manifestRaw ? manifestRaw : manifestRaw.toString();
54
+ return {
55
+ stats: {
56
+ data: JSON.parse(statsContent),
57
+ filename: fileNames.statsFileName
58
+ },
59
+ manifest: {
60
+ data: JSON.parse(manifestContent),
61
+ filename: fileNames.manifestFileName
62
+ }
63
+ };
64
+ } catch (err) {
65
+ const message = err instanceof Error ? err.message : String(err);
66
+ logger.error(`Failed to parse ${tag} manifest assets: ${message}`);
67
+ return;
68
+ }
69
+ };
70
+ api.modifyEnvironmentConfig((config, { name })=>{
71
+ const target = config.output.target;
72
+ if (skipByTarget(target)) return config;
73
+ if (isWebTarget(target)) {
74
+ csrOutputPath = config.output.distPath.root;
75
+ csrEnv = name;
76
+ } else {
77
+ ssrOutputPath = config.output.distPath.root;
78
+ ssrEnv = name;
79
+ }
80
+ return config;
81
+ });
82
+ const modifySSRPublicPath = (config, utils)=>{
83
+ if (ssrEnv !== utils.environment.name) return config;
84
+ const userSSRConfig = pluginOptions.userConfig.ssr ? 'object' == typeof pluginOptions.userConfig.ssr ? pluginOptions.userConfig.ssr : {} : {};
85
+ if (!userSSRConfig.distOutputDir) return;
86
+ config.output.publicPath = `${config.output.publicPath}${path.relative(csrOutputPath, ssrOutputPath)}/`;
87
+ return config;
88
+ };
89
+ api.modifyWebpackConfig((config, utils)=>{
90
+ modifySSRPublicPath(config, utils);
91
+ return config;
92
+ });
93
+ api.modifyRspackConfig((config, utils)=>{
94
+ modifySSRPublicPath(config, utils);
95
+ return config;
96
+ });
97
+ api.processAssets({
98
+ stage: 'report'
99
+ }, ({ assets, environment: envContext })=>{
100
+ var _pluginOptions_csrConfig, _pluginOptions_ssrConfig;
101
+ const envName = envContext.name;
102
+ if ((null == (_pluginOptions_csrConfig = pluginOptions.csrConfig) ? void 0 : _pluginOptions_csrConfig.manifest) !== false && csrEnv && envName === csrEnv) {
103
+ const browserAssets = collectAssets(assets, browserAssetFileNames, 'browser');
104
+ if (browserAssets) pluginOptions.assetResources.browser = browserAssets;
105
+ }
106
+ if ((null == (_pluginOptions_ssrConfig = pluginOptions.ssrConfig) ? void 0 : _pluginOptions_ssrConfig.manifest) !== false && ssrEnv && envName === ssrEnv) {
107
+ const nodeAssets = collectAssets(assets, nodeAssetFileNames, 'node');
108
+ if (nodeAssets) pluginOptions.assetResources.node = nodeAssets;
109
+ }
110
+ });
111
+ }
112
+ });
113
+ const moduleFederationSSRPlugin = (pluginOptions)=>({
114
+ name: '@modern-js/plugin-module-federation-ssr',
115
+ pre: [
116
+ '@modern-js/plugin-module-federation-config',
117
+ '@modern-js/plugin-module-federation'
118
+ ],
119
+ setup: async (api)=>{
120
+ var _pluginOptions_userConfig, _modernjsConfig_server;
121
+ const modernjsConfig = api.getConfig();
122
+ var _ref;
123
+ const enableSSR = null != (_ref = null == (_pluginOptions_userConfig = pluginOptions.userConfig) ? void 0 : _pluginOptions_userConfig.ssr) ? _ref : Boolean(null == modernjsConfig ? void 0 : null == (_modernjsConfig_server = modernjsConfig.server) ? void 0 : _modernjsConfig_server.ssr);
124
+ if (!enableSSR) return;
125
+ setEnv();
126
+ api._internalRuntimePlugins(({ entrypoint, plugins })=>{
127
+ const { fetchServerQuery } = pluginOptions;
128
+ plugins.push({
129
+ name: 'injectDataFetchFunction',
130
+ path: '@module-federation/modern-js/ssr-inject-data-fetch-function-plugin',
131
+ config: {
132
+ fetchServerQuery
133
+ }
134
+ });
135
+ if (!isDev()) return {
136
+ entrypoint,
137
+ plugins
138
+ };
139
+ plugins.push({
140
+ name: 'mfSSRDev',
141
+ path: '@module-federation/modern-js/ssr-dev-plugin',
142
+ config: {}
143
+ });
144
+ return {
145
+ entrypoint,
146
+ plugins
147
+ };
148
+ });
149
+ if (pluginOptions.ssrConfig.remotes) api._internalServerPlugins(({ plugins })=>{
150
+ plugins.push({
151
+ name: '@module-federation/modern-js/data-fetch-server-plugin',
152
+ options: {}
153
+ });
154
+ return {
155
+ plugins
156
+ };
157
+ });
158
+ api.modifyBundlerChain((chain)=>{
159
+ const target = chain.get('target');
160
+ if (skipByTarget(target)) return;
161
+ const bundlerType = 'rspack' === api.getAppContext().bundlerType ? 'rspack' : 'webpack';
162
+ const MFPlugin = 'webpack' === bundlerType ? ModuleFederationPlugin : rspack_ModuleFederationPlugin;
163
+ const isWeb = isWebTarget(target);
164
+ if (!isWeb) {
165
+ if (!chain.plugins.has(CHAIN_MF_PLUGIN_ID)) chain.plugin(CHAIN_MF_PLUGIN_ID).use(MFPlugin, [
166
+ pluginOptions.ssrConfig
167
+ ]).init((Plugin, args)=>{
168
+ pluginOptions.nodePlugin = new Plugin(args[0]);
169
+ return pluginOptions.nodePlugin;
170
+ });
171
+ }
172
+ if (!isWeb) {
173
+ chain.target('async-node');
174
+ if (isDev()) chain.plugin('UniverseEntryChunkTrackerPlugin').use(universe_entry_chunk_tracker_plugin);
175
+ }
176
+ if (isDev() && isWeb) chain.externals({
177
+ '@module-federation/node/utils': 'NOT_USED_IN_BROWSER'
178
+ });
179
+ });
180
+ api.config(()=>({
181
+ builderPlugins: [
182
+ mfSSRRsbuildPlugin(pluginOptions)
183
+ ],
184
+ tools: {
185
+ devServer: {
186
+ before: [
187
+ (req, res, next)=>{
188
+ if (!enableSSR) return void next();
189
+ try {
190
+ var _req_url, _req_url1;
191
+ if ((null == (_req_url = req.url) ? void 0 : _req_url.includes('.json')) && !(null == (_req_url1 = req.url) ? void 0 : _req_url1.includes('hot-update'))) {
192
+ const filepath = path.join(process.cwd(), `dist${req.url}`);
193
+ fs_extra.statSync(filepath);
194
+ res.setHeader('Access-Control-Allow-Origin', '*');
195
+ res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
196
+ res.setHeader('Access-Control-Allow-Headers', '*');
197
+ fs_extra.createReadStream(filepath).pipe(res);
198
+ } else next();
199
+ } catch (err) {
200
+ logger.debug(err);
201
+ next();
202
+ }
203
+ }
204
+ ]
205
+ }
206
+ }
207
+ }));
208
+ const writeMergedManifest = ()=>{
209
+ const { distOutputDir, assetResources } = pluginOptions;
210
+ const browserAssets = assetResources.browser;
211
+ const nodeAssets = assetResources.node;
212
+ if (!distOutputDir || !browserAssets || !nodeAssets) return;
213
+ try {
214
+ updateStatsAndManifest(nodeAssets, browserAssets, distOutputDir);
215
+ } catch (err) {
216
+ logger.error(err);
217
+ }
218
+ };
219
+ api.onAfterBuild(()=>{
220
+ writeMergedManifest();
221
+ });
222
+ api.onDevCompileDone(()=>{
223
+ writeMergedManifest();
224
+ });
225
+ }
226
+ });
227
+ const ssrPlugin = moduleFederationSSRPlugin;
228
+ export { CHAIN_MF_PLUGIN_ID, ssrPlugin as default, moduleFederationSSRPlugin, setEnv };
@@ -0,0 +1,40 @@
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
+ null == detail || detail.forEach((detail)=>{
9
+ const familyV4Value = 'string' == typeof detail.family ? 'IPv4' : 4;
10
+ if (detail.family === familyV4Value && detail.address !== localIpv4) ipv4Interfaces.push(detail);
11
+ });
12
+ });
13
+ return ipv4Interfaces;
14
+ } catch (_err) {
15
+ return [];
16
+ }
17
+ };
18
+ const getIPV4 = ()=>{
19
+ const ipv4Interfaces = getIpv4Interfaces();
20
+ const ipv4Interface = ipv4Interfaces[0] || {
21
+ address: localIpv4
22
+ };
23
+ return ipv4Interface.address;
24
+ };
25
+ const isWebTarget = (target)=>{
26
+ const WEB_TARGET = 'web';
27
+ if (Array.isArray(target)) return target.includes(WEB_TARGET);
28
+ if ('string' == typeof target) return target === WEB_TARGET;
29
+ return false;
30
+ };
31
+ const skipByTarget = (target)=>{
32
+ const IGNORE_TARGET = 'webworker';
33
+ if (Array.isArray(target)) return target.includes(IGNORE_TARGET);
34
+ if ('string' == typeof target) return target === IGNORE_TARGET;
35
+ return false;
36
+ };
37
+ function isDev() {
38
+ return 'development' === process.env.NODE_ENV;
39
+ }
40
+ export { getIPV4, isDev, isWebTarget, skipByTarget };
@@ -0,0 +1,3 @@
1
+ const LOCALHOST = 'localhost';
2
+ const PLUGIN_IDENTIFIER = '[ Modern.js Module Federation ]';
3
+ export { LOCALHOST, PLUGIN_IDENTIFIER };
@@ -0,0 +1,5 @@
1
+ import { createLogger } from "@module-federation/sdk";
2
+ import { PLUGIN_IDENTIFIER } from "./constant.mjs";
3
+ const logger = createLogger(PLUGIN_IDENTIFIER);
4
+ const src_logger = logger;
5
+ export { src_logger as default };
@@ -0,0 +1,3 @@
1
+ import bridge_react_plugin from "@module-federation/bridge-react/plugin";
2
+ export * from "@module-federation/bridge-react/plugin";
3
+ export { bridge_react_plugin as default };
@@ -0,0 +1 @@
1
+ export * from "@module-federation/bridge-react/v18";
@@ -0,0 +1 @@
1
+ export * from "@module-federation/bridge-react/v19";
@@ -0,0 +1,37 @@
1
+ import fs_extra from "fs-extra";
2
+ import { LRUCache } from "lru-cache";
3
+ class FileCache {
4
+ async getFile(filepath) {
5
+ if (!await fs_extra.pathExists(filepath)) return null;
6
+ try {
7
+ const stat = await fs_extra.lstat(filepath);
8
+ const currentModified = stat.mtimeMs;
9
+ const cachedEntry = this.cache.get(filepath);
10
+ if (cachedEntry && currentModified <= cachedEntry.lastModified) return {
11
+ content: cachedEntry.content,
12
+ lastModified: cachedEntry.lastModified
13
+ };
14
+ const content = await fs_extra.readFile(filepath, 'utf-8');
15
+ const newEntry = {
16
+ content,
17
+ lastModified: currentModified
18
+ };
19
+ this.cache.set(filepath, newEntry, {
20
+ size: stat.size || content.length
21
+ });
22
+ return {
23
+ content,
24
+ lastModified: currentModified
25
+ };
26
+ } catch (err) {
27
+ return null;
28
+ }
29
+ }
30
+ constructor(){
31
+ this.cache = new LRUCache({
32
+ maxSize: 209715200
33
+ });
34
+ }
35
+ }
36
+ const fileCache = new FileCache();
37
+ export { FileCache, fileCache };
@@ -0,0 +1,34 @@
1
+ import { createCorsMiddleware, createStaticMiddleware } from "./staticMiddleware.mjs";
2
+ const staticServePlugin = ()=>({
3
+ name: '@modern-js/module-federation/server',
4
+ setup: (api)=>{
5
+ api.onPrepare(()=>{
6
+ var _config_output, _config_server;
7
+ if ('development' === process.env.NODE_ENV) return;
8
+ const { middlewares } = api.getServerContext();
9
+ const config = api.getServerConfig();
10
+ const assetPrefix = (null == (_config_output = config.output) ? void 0 : _config_output.assetPrefix) || '';
11
+ if (null == (_config_server = config.server) ? void 0 : _config_server.ssr) {
12
+ const context = api.getServerContext();
13
+ const pwd = context.distDirectory;
14
+ const serverStaticMiddleware = createStaticMiddleware({
15
+ assetPrefix,
16
+ pwd
17
+ });
18
+ middlewares.push({
19
+ name: 'module-federation-serve-manifest',
20
+ handler: serverStaticMiddleware
21
+ });
22
+ }
23
+ if (process.env.MODERN_MF_AUTO_CORS) {
24
+ const corsMiddleware = createCorsMiddleware();
25
+ middlewares.push({
26
+ name: 'module-federation-cors',
27
+ handler: corsMiddleware
28
+ });
29
+ }
30
+ });
31
+ }
32
+ });
33
+ const server = staticServePlugin;
34
+ export { server as default, staticServePlugin };
@@ -0,0 +1,42 @@
1
+ import fs_extra from "fs-extra";
2
+ import path from "path";
3
+ import { fileCache } from "./fileCache.mjs";
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 ('.js' !== path.extname(pathname)) return next();
20
+ const prefixWithoutHost = removeHost(assetPrefix);
21
+ const prefixWithBundle = path.join(prefixWithoutHost, bundlesAssetPrefix);
22
+ if (!pathname.startsWith(prefixWithBundle)) return next();
23
+ const pathnameWithoutPrefix = pathname.replace(prefixWithBundle, '');
24
+ const filepath = path.join(pwd, bundlesAssetPrefix, pathnameWithoutPrefix);
25
+ if (!await fs_extra.pathExists(filepath)) return next();
26
+ const fileResult = await fileCache.getFile(filepath);
27
+ if (!fileResult) return next();
28
+ c.header('Content-Type', "application/javascript");
29
+ c.header('Content-Length', String(fileResult.content.length));
30
+ return c.body(fileResult.content, 200);
31
+ };
32
+ };
33
+ const createCorsMiddleware = ()=>async (c, next)=>{
34
+ const pathname = c.req.path;
35
+ if (path.extname(pathname)) {
36
+ c.header('Access-Control-Allow-Origin', '*');
37
+ c.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
38
+ c.header('Access-Control-Allow-Headers', '*');
39
+ }
40
+ return next();
41
+ };
42
+ export { createCorsMiddleware, createStaticMiddleware };
@@ -0,0 +1,15 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ function SSRLiveReload() {
3
+ if ('development' !== process.env.NODE_ENV) return null;
4
+ return /*#__PURE__*/ jsx("script", {
5
+ suppressHydrationWarning: true,
6
+ dangerouslySetInnerHTML: {
7
+ __html: String.raw`
8
+ if(${globalThis.shouldUpdate}){
9
+ location.reload();
10
+ }
11
+ `
12
+ }
13
+ });
14
+ }
15
+ export { SSRLiveReload };
@@ -0,0 +1,33 @@
1
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
2
+ import { SSRLiveReload } from "./SSRLiveReload.mjs";
3
+ import { flushDataFetch } from "@module-federation/bridge-react/lazy-utils";
4
+ const mfSSRDevPlugin = ()=>({
5
+ name: '@module-federation/modern-js',
6
+ setup: (api)=>{
7
+ api.onBeforeRender(async ()=>{
8
+ if ("u" > typeof window) return;
9
+ globalThis.shouldUpdate = false;
10
+ const nodeUtils = await import("@module-federation/node/utils");
11
+ const shouldUpdate = await nodeUtils.revalidate();
12
+ console.log('shouldUpdate: ', shouldUpdate);
13
+ if (shouldUpdate) {
14
+ console.log('should RELOAD', shouldUpdate);
15
+ await nodeUtils.flushChunks();
16
+ flushDataFetch();
17
+ globalThis.shouldUpdate = true;
18
+ }
19
+ });
20
+ api.wrapRoot((App)=>{
21
+ const AppWrapper = (props)=>/*#__PURE__*/ jsxs(Fragment, {
22
+ children: [
23
+ /*#__PURE__*/ jsx(SSRLiveReload, {}),
24
+ /*#__PURE__*/ jsx(App, {
25
+ ...props
26
+ })
27
+ ]
28
+ });
29
+ return AppWrapper;
30
+ });
31
+ }
32
+ });
33
+ export { mfSSRDevPlugin };
@@ -0,0 +1,14 @@
1
+ import { callDataFetch } from "@module-federation/bridge-react/data-fetch-utils";
2
+ import { setSSREnv } from "@module-federation/bridge-react/lazy-utils";
3
+ const injectDataFetchFunctionPlugin = ({ fetchServerQuery })=>({
4
+ name: '@module-federation/inject-data-fetch-function-plugin',
5
+ setup: (api)=>{
6
+ api.onBeforeRender(async ()=>{
7
+ setSSREnv({
8
+ fetchServerQuery
9
+ });
10
+ await callDataFetch();
11
+ });
12
+ }
13
+ });
14
+ export { injectDataFetchFunctionPlugin };
@@ -0,0 +1,2 @@
1
+ export * from '@module-federation/bridge-react/plugin';
2
+ export { default } from '@module-federation/bridge-react/plugin';
@@ -0,0 +1 @@
1
+ export * from '@module-federation/bridge-react/v18';
@@ -0,0 +1 @@
1
+ export * from '@module-federation/bridge-react/v19';
@@ -1,4 +1,5 @@
1
1
  import { moduleFederationPlugin } from '@module-federation/sdk';
2
+ import type { StatsAssetResource } from '@module-federation/rsbuild-plugin/utils';
2
3
  export interface PluginOptions {
3
4
  config?: moduleFederationPlugin.ModuleFederationPluginOptions;
4
5
  configPath?: string;
@@ -8,6 +9,10 @@ export interface PluginOptions {
8
9
  remoteIpStrategy?: 'ipv4' | 'inherit';
9
10
  fetchServerQuery?: Record<string, unknown>;
10
11
  }
12
+ export type AssetFileNames = {
13
+ statsFileName: string;
14
+ manifestFileName: string;
15
+ };
11
16
  export interface InternalModernPluginOptions {
12
17
  csrConfig?: moduleFederationPlugin.ModuleFederationPluginOptions;
13
18
  ssrConfig?: moduleFederationPlugin.ModuleFederationPluginOptions;
@@ -15,6 +20,14 @@ export interface InternalModernPluginOptions {
15
20
  originPluginOptions: PluginOptions;
16
21
  browserPlugin?: BundlerPlugin;
17
22
  nodePlugin?: BundlerPlugin;
23
+ assetFileNames: {
24
+ node?: AssetFileNames;
25
+ browser?: AssetFileNames;
26
+ };
27
+ assetResources: {
28
+ browser?: StatsAssetResource;
29
+ node?: StatsAssetResource;
30
+ };
18
31
  remoteIpStrategy?: 'ipv4' | 'inherit';
19
32
  userConfig?: PluginOptions;
20
33
  fetchServerQuery?: Record<string, unknown>;