@module-federation/modern-js 0.0.0-chore-bump-node-22-20260710161714
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.
- package/LICENSE +21 -0
- package/README.md +5 -0
- package/dist/cjs/cli/configPlugin.js +346 -0
- package/dist/cjs/cli/index.js +115 -0
- package/dist/cjs/cli/mfRuntimePlugins/inject-node-fetch.js +54 -0
- package/dist/cjs/cli/mfRuntimePlugins/shared-strategy.js +53 -0
- package/dist/cjs/cli/server/data-fetch-server-plugin.js +59 -0
- package/dist/cjs/cli/ssrPlugin.js +314 -0
- package/dist/cjs/cli/utils.js +93 -0
- package/dist/cjs/constant.js +40 -0
- package/dist/cjs/interfaces/bundler.js +18 -0
- package/dist/cjs/logger.js +39 -0
- package/dist/cjs/react/data-fetch.js +58 -0
- package/dist/cjs/react/index.js +58 -0
- package/dist/cjs/react/plugin.js +65 -0
- package/dist/cjs/react/v18.js +58 -0
- package/dist/cjs/react/v19.js +58 -0
- package/dist/cjs/runtime/index.js +58 -0
- package/dist/cjs/server/fileCache.js +82 -0
- package/dist/cjs/server/index.js +71 -0
- package/dist/cjs/server/staticMiddleware.js +97 -0
- package/dist/cjs/ssr-runtime/SSRLiveReload.js +50 -0
- package/dist/cjs/ssr-runtime/devPlugin.js +67 -0
- package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +47 -0
- package/dist/cjs/types/index.js +18 -0
- package/dist/esm/cli/configPlugin.mjs +321 -0
- package/dist/esm/cli/index.mjs +91 -0
- package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.mjs +12 -0
- package/dist/esm/cli/mfRuntimePlugins/shared-strategy.mjs +21 -0
- package/dist/esm/cli/server/data-fetch-server-plugin.mjs +17 -0
- package/dist/esm/cli/ssrPlugin.mjs +297 -0
- package/dist/esm/cli/utils.mjs +40 -0
- package/dist/esm/constant.mjs +3 -0
- package/dist/esm/interfaces/bundler.mjs +0 -0
- package/dist/esm/logger.mjs +5 -0
- package/dist/esm/react/data-fetch.mjs +1 -0
- package/dist/esm/react/index.mjs +1 -0
- package/dist/esm/react/plugin.mjs +3 -0
- package/dist/esm/react/v18.mjs +1 -0
- package/dist/esm/react/v19.mjs +1 -0
- package/dist/esm/runtime/index.mjs +1 -0
- package/dist/esm/server/fileCache.mjs +125 -0
- package/dist/esm/server/index.mjs +36 -0
- package/dist/esm/server/staticMiddleware.mjs +122 -0
- package/dist/esm/ssr-runtime/SSRLiveReload.mjs +23 -0
- package/dist/esm/ssr-runtime/devPlugin.mjs +70 -0
- package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.mjs +33 -0
- package/dist/esm/types/index.mjs +0 -0
- package/dist/esm-node/cli/configPlugin.mjs +276 -0
- package/dist/esm-node/cli/index.mjs +75 -0
- package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.mjs +10 -0
- package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.mjs +19 -0
- package/dist/esm-node/cli/server/data-fetch-server-plugin.mjs +15 -0
- package/dist/esm-node/cli/ssrPlugin.mjs +258 -0
- package/dist/esm-node/cli/utils.mjs +40 -0
- package/dist/esm-node/constant.mjs +3 -0
- package/dist/esm-node/interfaces/bundler.mjs +0 -0
- package/dist/esm-node/logger.mjs +5 -0
- package/dist/esm-node/react/data-fetch.mjs +1 -0
- package/dist/esm-node/react/index.mjs +1 -0
- package/dist/esm-node/react/plugin.mjs +3 -0
- package/dist/esm-node/react/v18.mjs +1 -0
- package/dist/esm-node/react/v19.mjs +1 -0
- package/dist/esm-node/runtime/index.mjs +1 -0
- package/dist/esm-node/server/fileCache.mjs +45 -0
- package/dist/esm-node/server/index.mjs +34 -0
- package/dist/esm-node/server/staticMiddleware.mjs +50 -0
- package/dist/esm-node/ssr-runtime/SSRLiveReload.mjs +16 -0
- package/dist/esm-node/ssr-runtime/devPlugin.mjs +33 -0
- package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.mjs +13 -0
- package/dist/esm-node/types/index.mjs +0 -0
- package/dist/types/cli/configPlugin.d.ts +21 -0
- package/dist/types/cli/configPlugin.spec.d.ts +1 -0
- package/dist/types/cli/index.d.ts +6 -0
- package/dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts +3 -0
- package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +3 -0
- package/dist/types/cli/server/data-fetch-server-plugin.d.ts +3 -0
- package/dist/types/cli/ssrPlugin.d.ts +6 -0
- package/dist/types/cli/ssrPlugin.spec.d.ts +1 -0
- package/dist/types/cli/utils.d.ts +6 -0
- package/dist/types/constant.d.ts +2 -0
- package/dist/types/interfaces/bundler.d.ts +18 -0
- package/dist/types/logger.d.ts +2 -0
- package/dist/types/react/data-fetch.d.ts +1 -0
- package/dist/types/react/index.d.ts +1 -0
- package/dist/types/react/plugin.d.ts +2 -0
- package/dist/types/react/v18.d.ts +1 -0
- package/dist/types/react/v19.d.ts +1 -0
- package/dist/types/runtime/index.d.ts +1 -0
- package/dist/types/server/fileCache.d.ts +14 -0
- package/dist/types/server/fileCache.spec.d.ts +1 -0
- package/dist/types/server/index.d.ts +4 -0
- package/dist/types/server/staticMiddleware.d.ts +7 -0
- package/dist/types/server/staticMiddleware.spec.d.ts +1 -0
- package/dist/types/ssr-runtime/SSRLiveReload.d.ts +2 -0
- package/dist/types/ssr-runtime/devPlugin.d.ts +2 -0
- package/dist/types/ssr-runtime/injectDataFetchFunctionPlugin.d.ts +4 -0
- package/dist/types/types/index.d.ts +35 -0
- package/package.json +192 -0
- package/types.d.ts +6 -0
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import { ModuleFederationPlugin, TreeShakingSharedPlugin as enhanced_TreeShakingSharedPlugin } from "@module-federation/enhanced";
|
|
4
|
+
import { ModuleFederationPlugin as rspack_ModuleFederationPlugin, TreeShakingSharedPlugin as rspack_TreeShakingSharedPlugin } 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 STATIC_ASSET_MODULE_TYPES = [
|
|
32
|
+
'asset',
|
|
33
|
+
'asset/resource'
|
|
34
|
+
];
|
|
35
|
+
const preserveStaticAssetPublicPath = (config, publicPath)=>{
|
|
36
|
+
var _config, _moduleConfig;
|
|
37
|
+
if ('string' != typeof publicPath || '' === publicPath || 'auto' === publicPath) return;
|
|
38
|
+
(_config = config).module || (_config.module = {});
|
|
39
|
+
const moduleConfig = config.module;
|
|
40
|
+
(_moduleConfig = moduleConfig).generator || (_moduleConfig.generator = {});
|
|
41
|
+
for (const moduleType of STATIC_ASSET_MODULE_TYPES){
|
|
42
|
+
var _moduleConfig_generator, _moduleType, _moduleConfig_generator_moduleType;
|
|
43
|
+
(_moduleConfig_generator = moduleConfig.generator)[_moduleType = moduleType] || (_moduleConfig_generator[_moduleType] = {});
|
|
44
|
+
null != (_moduleConfig_generator_moduleType = moduleConfig.generator[moduleType]).publicPath || (_moduleConfig_generator_moduleType.publicPath = publicPath);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
const mfSSRRsbuildPlugin = (pluginOptions)=>({
|
|
48
|
+
name: '@modern-js/plugin-mf-post-config',
|
|
49
|
+
pre: [
|
|
50
|
+
'@modern-js/builder-plugin-ssr'
|
|
51
|
+
],
|
|
52
|
+
setup (api) {
|
|
53
|
+
var _pluginOptions_csrConfig, _pluginOptions_assetFileNames, _pluginOptions_ssrConfig;
|
|
54
|
+
if (pluginOptions.csrConfig.getPublicPath) return;
|
|
55
|
+
let csrOutputPath = '';
|
|
56
|
+
let ssrOutputPath = '';
|
|
57
|
+
let ssrEnv = '';
|
|
58
|
+
let csrEnv = '';
|
|
59
|
+
const browserAssetFileNames = pluginOptions.assetFileNames.browser || getManifestAssetFileNames(null == (_pluginOptions_csrConfig = pluginOptions.csrConfig) ? void 0 : _pluginOptions_csrConfig.manifest);
|
|
60
|
+
const nodeAssetFileNames = (null == (_pluginOptions_assetFileNames = pluginOptions.assetFileNames) ? void 0 : _pluginOptions_assetFileNames.node) || getManifestAssetFileNames(null == (_pluginOptions_ssrConfig = pluginOptions.ssrConfig) ? void 0 : _pluginOptions_ssrConfig.manifest);
|
|
61
|
+
const collectAssets = (assets, fileNames, tag)=>{
|
|
62
|
+
const statsAsset = assets[fileNames.statsFileName];
|
|
63
|
+
const manifestAsset = assets[fileNames.manifestFileName];
|
|
64
|
+
if (!statsAsset || !manifestAsset) return;
|
|
65
|
+
try {
|
|
66
|
+
const statsRaw = statsAsset.source();
|
|
67
|
+
const manifestRaw = manifestAsset.source();
|
|
68
|
+
const statsContent = 'string' == typeof statsRaw ? statsRaw : statsRaw.toString();
|
|
69
|
+
const manifestContent = 'string' == typeof manifestRaw ? manifestRaw : manifestRaw.toString();
|
|
70
|
+
return {
|
|
71
|
+
stats: {
|
|
72
|
+
data: JSON.parse(statsContent),
|
|
73
|
+
filename: fileNames.statsFileName
|
|
74
|
+
},
|
|
75
|
+
manifest: {
|
|
76
|
+
data: JSON.parse(manifestContent),
|
|
77
|
+
filename: fileNames.manifestFileName
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
} catch (err) {
|
|
81
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
82
|
+
logger.error(`Failed to parse ${tag} manifest assets: ${message}`);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
api.modifyEnvironmentConfig((config, { name })=>{
|
|
87
|
+
const target = config.output.target;
|
|
88
|
+
if (skipByTarget(target)) return config;
|
|
89
|
+
if (isWebTarget(target)) {
|
|
90
|
+
csrOutputPath = config.output.distPath.root;
|
|
91
|
+
csrEnv = name;
|
|
92
|
+
} else {
|
|
93
|
+
ssrOutputPath = config.output.distPath.root;
|
|
94
|
+
ssrEnv = name;
|
|
95
|
+
}
|
|
96
|
+
return config;
|
|
97
|
+
});
|
|
98
|
+
const modifySSRPublicPath = (config, utils)=>{
|
|
99
|
+
if (ssrEnv !== utils.environment.name) return config;
|
|
100
|
+
const userSSRConfig = pluginOptions.userConfig.ssr ? 'object' == typeof pluginOptions.userConfig.ssr ? pluginOptions.userConfig.ssr : {} : {};
|
|
101
|
+
if (!userSSRConfig.distOutputDir) return;
|
|
102
|
+
const publicPath = config.output.publicPath;
|
|
103
|
+
preserveStaticAssetPublicPath(config, publicPath);
|
|
104
|
+
config.output.publicPath = `${publicPath}${path.relative(csrOutputPath, ssrOutputPath)}/`;
|
|
105
|
+
return config;
|
|
106
|
+
};
|
|
107
|
+
api.modifyWebpackConfig((config, utils)=>{
|
|
108
|
+
modifySSRPublicPath(config, utils);
|
|
109
|
+
return config;
|
|
110
|
+
});
|
|
111
|
+
api.modifyRspackConfig((config, utils)=>{
|
|
112
|
+
modifySSRPublicPath(config, utils);
|
|
113
|
+
return config;
|
|
114
|
+
});
|
|
115
|
+
api.processAssets({
|
|
116
|
+
stage: 'report'
|
|
117
|
+
}, ({ assets, environment: envContext })=>{
|
|
118
|
+
var _pluginOptions_csrConfig, _pluginOptions_ssrConfig;
|
|
119
|
+
const envName = envContext.name;
|
|
120
|
+
if ((null == (_pluginOptions_csrConfig = pluginOptions.csrConfig) ? void 0 : _pluginOptions_csrConfig.manifest) !== false && csrEnv && envName === csrEnv) {
|
|
121
|
+
const browserAssets = collectAssets(assets, browserAssetFileNames, 'browser');
|
|
122
|
+
if (browserAssets) pluginOptions.assetResources.browser = browserAssets;
|
|
123
|
+
}
|
|
124
|
+
if ((null == (_pluginOptions_ssrConfig = pluginOptions.ssrConfig) ? void 0 : _pluginOptions_ssrConfig.manifest) !== false && ssrEnv && envName === ssrEnv) {
|
|
125
|
+
const nodeAssets = collectAssets(assets, nodeAssetFileNames, 'node');
|
|
126
|
+
if (nodeAssets) pluginOptions.assetResources.node = nodeAssets;
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
const moduleFederationSSRPlugin = (pluginOptions)=>({
|
|
132
|
+
name: '@modern-js/plugin-module-federation-ssr',
|
|
133
|
+
pre: [
|
|
134
|
+
'@modern-js/plugin-module-federation-config',
|
|
135
|
+
'@modern-js/plugin-module-federation'
|
|
136
|
+
],
|
|
137
|
+
setup: async (api)=>{
|
|
138
|
+
var _ref;
|
|
139
|
+
var _pluginOptions_userConfig, _modernjsConfig_server;
|
|
140
|
+
const modernjsConfig = api.getConfig();
|
|
141
|
+
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);
|
|
142
|
+
const { secondarySharedTreeShaking } = pluginOptions;
|
|
143
|
+
if (!enableSSR) return;
|
|
144
|
+
setEnv();
|
|
145
|
+
api._internalRuntimePlugins(({ entrypoint, plugins })=>{
|
|
146
|
+
if (secondarySharedTreeShaking) return {
|
|
147
|
+
entrypoint,
|
|
148
|
+
plugins
|
|
149
|
+
};
|
|
150
|
+
const { fetchServerQuery } = pluginOptions;
|
|
151
|
+
plugins.push({
|
|
152
|
+
name: 'injectDataFetchFunction',
|
|
153
|
+
path: '@module-federation/modern-js/ssr-inject-data-fetch-function-plugin',
|
|
154
|
+
config: {
|
|
155
|
+
fetchServerQuery
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
if (!isDev()) return {
|
|
159
|
+
entrypoint,
|
|
160
|
+
plugins
|
|
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) api._internalServerPlugins(({ plugins })=>{
|
|
173
|
+
plugins.push({
|
|
174
|
+
name: '@module-federation/modern-js/data-fetch-server-plugin',
|
|
175
|
+
options: {}
|
|
176
|
+
});
|
|
177
|
+
return {
|
|
178
|
+
plugins
|
|
179
|
+
};
|
|
180
|
+
});
|
|
181
|
+
api.modifyBundlerChain((chain)=>{
|
|
182
|
+
const target = chain.get('target');
|
|
183
|
+
if (skipByTarget(target)) return;
|
|
184
|
+
const bundlerType = 'rspack' === api.getAppContext().bundlerType ? 'rspack' : 'webpack';
|
|
185
|
+
const MFPlugin = 'webpack' === bundlerType ? ModuleFederationPlugin : rspack_ModuleFederationPlugin;
|
|
186
|
+
const TreeShakingSharedPlugin = 'webpack' === bundlerType ? enhanced_TreeShakingSharedPlugin : rspack_TreeShakingSharedPlugin;
|
|
187
|
+
const isWeb = isWebTarget(target);
|
|
188
|
+
if (!isWeb) {
|
|
189
|
+
if (!chain.plugins.has(CHAIN_MF_PLUGIN_ID)) if (secondarySharedTreeShaking) chain.plugin(CHAIN_MF_PLUGIN_ID).use(TreeShakingSharedPlugin, [
|
|
190
|
+
{
|
|
191
|
+
mfConfig: pluginOptions.ssrConfig,
|
|
192
|
+
secondary: true
|
|
193
|
+
}
|
|
194
|
+
]);
|
|
195
|
+
else chain.plugin(CHAIN_MF_PLUGIN_ID).use(MFPlugin, [
|
|
196
|
+
pluginOptions.ssrConfig
|
|
197
|
+
]).init((Plugin, args)=>{
|
|
198
|
+
pluginOptions.nodePlugin = new Plugin(args[0]);
|
|
199
|
+
return pluginOptions.nodePlugin;
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
if (!isWeb && !secondarySharedTreeShaking) {
|
|
203
|
+
chain.target('async-node');
|
|
204
|
+
if (isDev()) chain.plugin('UniverseEntryChunkTrackerPlugin').use(universe_entry_chunk_tracker_plugin);
|
|
205
|
+
}
|
|
206
|
+
if (isDev() && isWeb) chain.externals({
|
|
207
|
+
'@module-federation/node/utils': 'NOT_USED_IN_BROWSER'
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
api.config(()=>({
|
|
211
|
+
builderPlugins: [
|
|
212
|
+
mfSSRRsbuildPlugin(pluginOptions)
|
|
213
|
+
],
|
|
214
|
+
tools: {
|
|
215
|
+
devServer: {
|
|
216
|
+
before: [
|
|
217
|
+
(req, res, next)=>{
|
|
218
|
+
if (!enableSSR) return void next();
|
|
219
|
+
try {
|
|
220
|
+
var _req_url, _req_url1;
|
|
221
|
+
if ((null == (_req_url = req.url) ? void 0 : _req_url.includes('.json')) && !(null == (_req_url1 = req.url) ? void 0 : _req_url1.includes('hot-update'))) {
|
|
222
|
+
const filepath = path.join(process.cwd(), `dist${req.url}`);
|
|
223
|
+
fs.statSync(filepath);
|
|
224
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
225
|
+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
|
|
226
|
+
res.setHeader('Access-Control-Allow-Headers', '*');
|
|
227
|
+
fs.createReadStream(filepath).pipe(res);
|
|
228
|
+
} else next();
|
|
229
|
+
} catch (err) {
|
|
230
|
+
logger.debug(err);
|
|
231
|
+
next();
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
]
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}));
|
|
238
|
+
const writeMergedManifest = ()=>{
|
|
239
|
+
const { distOutputDir, assetResources } = pluginOptions;
|
|
240
|
+
const browserAssets = assetResources.browser;
|
|
241
|
+
const nodeAssets = assetResources.node;
|
|
242
|
+
if (!distOutputDir || !browserAssets || !nodeAssets) return;
|
|
243
|
+
try {
|
|
244
|
+
updateStatsAndManifest(nodeAssets, browserAssets, distOutputDir);
|
|
245
|
+
} catch (err) {
|
|
246
|
+
logger.error(err);
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
api.onAfterBuild(()=>{
|
|
250
|
+
writeMergedManifest();
|
|
251
|
+
});
|
|
252
|
+
api.onDevCompileDone(()=>{
|
|
253
|
+
writeMergedManifest();
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
const ssrPlugin = moduleFederationSSRPlugin;
|
|
258
|
+
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 };
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@module-federation/bridge-react/data-fetch";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@module-federation/bridge-react";
|
|
@@ -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 @@
|
|
|
1
|
+
export * from "@module-federation/enhanced/runtime";
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { access, lstat, readFile } from "fs/promises";
|
|
2
|
+
import { LRUCache } from "lru-cache";
|
|
3
|
+
const pathExists = async (filepath)=>{
|
|
4
|
+
try {
|
|
5
|
+
await access(filepath);
|
|
6
|
+
return true;
|
|
7
|
+
} catch {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
class FileCache {
|
|
12
|
+
async getFile(filepath) {
|
|
13
|
+
if (!await pathExists(filepath)) return null;
|
|
14
|
+
try {
|
|
15
|
+
const stat = await lstat(filepath);
|
|
16
|
+
const currentModified = stat.mtimeMs;
|
|
17
|
+
const cachedEntry = this.cache.get(filepath);
|
|
18
|
+
if (cachedEntry && currentModified <= cachedEntry.lastModified) return {
|
|
19
|
+
content: cachedEntry.content,
|
|
20
|
+
lastModified: cachedEntry.lastModified
|
|
21
|
+
};
|
|
22
|
+
const content = await readFile(filepath, 'utf-8');
|
|
23
|
+
const newEntry = {
|
|
24
|
+
content,
|
|
25
|
+
lastModified: currentModified
|
|
26
|
+
};
|
|
27
|
+
this.cache.set(filepath, newEntry, {
|
|
28
|
+
size: stat.size || content.length
|
|
29
|
+
});
|
|
30
|
+
return {
|
|
31
|
+
content,
|
|
32
|
+
lastModified: currentModified
|
|
33
|
+
};
|
|
34
|
+
} catch (err) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
constructor(){
|
|
39
|
+
this.cache = new LRUCache({
|
|
40
|
+
maxSize: 209715200
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const fileCache = new FileCache();
|
|
45
|
+
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,50 @@
|
|
|
1
|
+
import { access } from "fs/promises";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { fileCache } from "./fileCache.mjs";
|
|
4
|
+
const pathExists = async (filepath)=>{
|
|
5
|
+
try {
|
|
6
|
+
await access(filepath);
|
|
7
|
+
return true;
|
|
8
|
+
} catch {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
const bundlesAssetPrefix = '/bundles';
|
|
13
|
+
const removeHost = (url)=>{
|
|
14
|
+
try {
|
|
15
|
+
const hasProtocol = url.includes('://');
|
|
16
|
+
const hasDomain = hasProtocol || url.startsWith('//');
|
|
17
|
+
const pathname = hasDomain ? new URL(hasProtocol ? url : `http:${url}`).pathname : url;
|
|
18
|
+
return pathname;
|
|
19
|
+
} catch (e) {
|
|
20
|
+
return url;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
const createStaticMiddleware = (options)=>{
|
|
24
|
+
const { assetPrefix, pwd } = options;
|
|
25
|
+
return async (c, next)=>{
|
|
26
|
+
const pathname = c.req.path;
|
|
27
|
+
if ('.js' !== path.extname(pathname)) return next();
|
|
28
|
+
const prefixWithoutHost = removeHost(assetPrefix);
|
|
29
|
+
const prefixWithBundle = path.join(prefixWithoutHost, bundlesAssetPrefix);
|
|
30
|
+
if (!pathname.startsWith(prefixWithBundle)) return next();
|
|
31
|
+
const pathnameWithoutPrefix = pathname.replace(prefixWithBundle, '');
|
|
32
|
+
const filepath = path.join(pwd, bundlesAssetPrefix, pathnameWithoutPrefix);
|
|
33
|
+
if (!await pathExists(filepath)) return next();
|
|
34
|
+
const fileResult = await fileCache.getFile(filepath);
|
|
35
|
+
if (!fileResult) return next();
|
|
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 = ()=>async (c, next)=>{
|
|
42
|
+
const pathname = c.req.path;
|
|
43
|
+
if (path.extname(pathname)) {
|
|
44
|
+
c.header('Access-Control-Allow-Origin', '*');
|
|
45
|
+
c.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
|
|
46
|
+
c.header('Access-Control-Allow-Headers', '*');
|
|
47
|
+
}
|
|
48
|
+
return next();
|
|
49
|
+
};
|
|
50
|
+
export { createCorsMiddleware, createStaticMiddleware };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import "react";
|
|
3
|
+
function SSRLiveReload() {
|
|
4
|
+
if ('development' !== process.env.NODE_ENV) return null;
|
|
5
|
+
return /*#__PURE__*/ jsx("script", {
|
|
6
|
+
suppressHydrationWarning: true,
|
|
7
|
+
dangerouslySetInnerHTML: {
|
|
8
|
+
__html: String.raw`
|
|
9
|
+
if(${globalThis.shouldUpdate}){
|
|
10
|
+
location.reload();
|
|
11
|
+
}
|
|
12
|
+
`
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
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/data-fetch";
|
|
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,13 @@
|
|
|
1
|
+
import { callDataFetch, setSSREnv } from "@module-federation/bridge-react/data-fetch";
|
|
2
|
+
const injectDataFetchFunctionPlugin = ({ fetchServerQuery })=>({
|
|
3
|
+
name: '@module-federation/inject-data-fetch-function-plugin',
|
|
4
|
+
setup: (api)=>{
|
|
5
|
+
api.onBeforeRender(async ()=>{
|
|
6
|
+
setSSREnv({
|
|
7
|
+
fetchServerQuery
|
|
8
|
+
});
|
|
9
|
+
await callDataFetch();
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
export { injectDataFetchFunctionPlugin };
|
|
File without changes
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { isWebTarget, skipByTarget } from './utils';
|
|
2
|
+
import { moduleFederationPlugin } from '@module-federation/sdk';
|
|
3
|
+
import { PluginOptions } from '../types';
|
|
4
|
+
import type { InternalModernPluginOptions } from '../types';
|
|
5
|
+
import type { AppTools, webpack, UserConfig, Rspack, CliPluginFuture } from '@modern-js/app-tools';
|
|
6
|
+
import type { BundlerChainConfig } from '../interfaces/bundler';
|
|
7
|
+
export type ConfigType<T> = T extends 'webpack' ? webpack.Configuration : T extends 'rspack' ? Rspack.Configuration : never;
|
|
8
|
+
export declare function setEnv(enableSSR: boolean): void;
|
|
9
|
+
export declare const getMFConfig: (userConfig: PluginOptions) => Promise<moduleFederationPlugin.ModuleFederationPluginOptions>;
|
|
10
|
+
export declare const patchMFConfig: (mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions, isServer: boolean) => moduleFederationPlugin.ModuleFederationPluginOptions;
|
|
11
|
+
export declare function addMyTypes2Ignored(chain: BundlerChainConfig, mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions): void;
|
|
12
|
+
export declare function patchBundlerConfig(options: {
|
|
13
|
+
chain: BundlerChainConfig;
|
|
14
|
+
isServer: boolean;
|
|
15
|
+
modernjsConfig: UserConfig<AppTools>;
|
|
16
|
+
mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions;
|
|
17
|
+
enableSSR: boolean;
|
|
18
|
+
}): void;
|
|
19
|
+
export declare const moduleFederationConfigPlugin: (userConfig: InternalModernPluginOptions) => CliPluginFuture<AppTools>;
|
|
20
|
+
export default moduleFederationConfigPlugin;
|
|
21
|
+
export { isWebTarget, skipByTarget };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { CliPluginFuture, AppTools } from '@modern-js/app-tools';
|
|
2
|
+
import type { PluginOptions } from '../types';
|
|
3
|
+
export declare const moduleFederationPlugin: (userConfig?: PluginOptions) => CliPluginFuture<AppTools>;
|
|
4
|
+
export default moduleFederationPlugin;
|
|
5
|
+
export { createModuleFederationConfig } from '@module-federation/enhanced';
|
|
6
|
+
export type { ModuleFederationRuntimePlugin } from '@module-federation/enhanced/runtime';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { CliPluginFuture, AppTools } from '@modern-js/app-tools';
|
|
2
|
+
import type { InternalModernPluginOptions } from '../types';
|
|
3
|
+
export declare function setEnv(): void;
|
|
4
|
+
export declare const CHAIN_MF_PLUGIN_ID = "plugin-module-federation-server";
|
|
5
|
+
export declare const moduleFederationSSRPlugin: (pluginOptions: Required<InternalModernPluginOptions>) => CliPluginFuture<AppTools>;
|
|
6
|
+
export default moduleFederationSSRPlugin;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { webpack, Rspack } from '@modern-js/app-tools';
|
|
2
|
+
export type ConfigType<T> = T extends 'webpack' ? webpack.Configuration : T extends 'rspack' ? Rspack.Configuration : never;
|
|
3
|
+
export declare const getIPV4: () => string;
|
|
4
|
+
export declare const isWebTarget: (target: string[] | string) => boolean;
|
|
5
|
+
export declare const skipByTarget: (target: string[] | string) => boolean;
|
|
6
|
+
export declare function isDev(): boolean;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { AppTools, Bundler, UserConfig } from '@modern-js/app-tools';
|
|
2
|
+
type AppToolsUserConfig<T extends Bundler> = AppTools<T>['userConfig']['tools'];
|
|
3
|
+
type ExcludeUndefined<T> = T extends undefined ? never : T;
|
|
4
|
+
type ExtractObjectType<T> = T extends (...args: any[]) => any ? never : T;
|
|
5
|
+
type OmitArrayConfiguration<T> = T extends Array<any> ? (T extends (infer U)[] ? U : T) : ExtractObjectType<T>;
|
|
6
|
+
type WebpackConfigs = ExcludeUndefined<AppToolsUserConfig<'webpack'>> extends {
|
|
7
|
+
webpack?: infer U;
|
|
8
|
+
} ? U : never;
|
|
9
|
+
type ObjectWebpack = ExtractObjectType<OmitArrayConfiguration<WebpackConfigs>>;
|
|
10
|
+
type RspackConfigs = ExcludeUndefined<AppToolsUserConfig<'rspack'>> extends {
|
|
11
|
+
rspack?: infer U;
|
|
12
|
+
} ? U : never;
|
|
13
|
+
type ObjectRspack = ExtractObjectType<OmitArrayConfiguration<RspackConfigs>>;
|
|
14
|
+
type BundlerChain = ExcludeUndefined<ExcludeUndefined<UserConfig<AppTools>['tools']>['bundlerChain']>;
|
|
15
|
+
type BundlerChainFunc = Extract<BundlerChain, (chain: any, utils: any) => any>;
|
|
16
|
+
export type BundlerChainConfig = Parameters<BundlerChainFunc>[0];
|
|
17
|
+
export type BundlerConfig<T extends Bundler> = T extends 'rspack' ? ObjectRspack : ObjectWebpack;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@module-federation/bridge-react/data-fetch';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@module-federation/bridge-react';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@module-federation/bridge-react/v18';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@module-federation/bridge-react/v19';
|