@module-federation/modern-js 0.21.6 → 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.
- package/dist/cjs/cli/configPlugin.js +301 -357
- package/dist/cjs/cli/index.js +103 -93
- package/dist/cjs/cli/mfRuntimePlugins/inject-node-fetch.js +51 -41
- package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +73 -75
- package/dist/cjs/cli/mfRuntimePlugins/shared-strategy.js +50 -40
- package/dist/cjs/cli/server/data-fetch-server-plugin.js +55 -44
- package/dist/cjs/cli/ssrPlugin.js +266 -298
- package/dist/cjs/cli/utils.js +84 -81
- package/dist/cjs/constant.js +37 -28
- package/dist/cjs/interfaces/bundler.js +17 -15
- package/dist/cjs/logger.js +37 -25
- package/dist/cjs/react/index.js +55 -19
- package/dist/cjs/react/plugin.js +62 -37
- package/dist/cjs/react/v18.js +55 -19
- package/dist/cjs/react/v19.js +55 -19
- package/dist/cjs/runtime/index.js +55 -19
- package/dist/cjs/server/fileCache.js +76 -76
- package/dist/cjs/server/index.js +65 -57
- package/dist/cjs/server/staticMiddleware.js +84 -84
- package/dist/cjs/ssr-runtime/SSRLiveReload.js +40 -34
- package/dist/cjs/ssr-runtime/devPlugin.js +63 -67
- package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +44 -36
- package/dist/cjs/types/index.js +17 -15
- package/dist/esm/cli/configPlugin.mjs +305 -0
- package/dist/esm/cli/index.mjs +83 -0
- package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.mjs +12 -0
- package/dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.mjs +58 -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 +254 -0
- package/dist/esm/cli/utils.mjs +40 -0
- package/dist/esm/constant.mjs +3 -0
- package/dist/esm/logger.mjs +5 -0
- package/dist/esm/react/plugin.mjs +3 -0
- package/dist/esm/server/fileCache.mjs +90 -0
- package/dist/esm/server/index.mjs +36 -0
- package/dist/esm/server/staticMiddleware.mjs +87 -0
- package/dist/esm/ssr-runtime/SSRLiveReload.mjs +22 -0
- package/dist/esm/ssr-runtime/devPlugin.mjs +70 -0
- package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.mjs +34 -0
- package/dist/esm-node/cli/configPlugin.mjs +263 -0
- package/dist/esm-node/cli/index.mjs +67 -0
- package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.mjs +10 -0
- package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.mjs +46 -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 +228 -0
- package/dist/esm-node/cli/utils.mjs +40 -0
- package/dist/esm-node/constant.mjs +3 -0
- package/dist/esm-node/logger.mjs +5 -0
- package/dist/esm-node/react/plugin.mjs +3 -0
- package/dist/esm-node/server/fileCache.mjs +37 -0
- package/dist/esm-node/server/index.mjs +34 -0
- package/dist/esm-node/server/staticMiddleware.mjs +42 -0
- package/dist/esm-node/ssr-runtime/SSRLiveReload.mjs +15 -0
- package/dist/esm-node/ssr-runtime/devPlugin.mjs +33 -0
- package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.mjs +14 -0
- package/package.json +32 -31
- package/dist/cjs/cli/configPlugin.spec.js +0 -112
- package/dist/cjs/server/fileCache.spec.js +0 -28
- package/dist/cjs/server/staticMiddleware.spec.js +0 -185
- package/dist/esm/cli/configPlugin.js +0 -395
- package/dist/esm/cli/configPlugin.spec.js +0 -108
- package/dist/esm/cli/index.js +0 -92
- package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js +0 -16
- package/dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.js +0 -74
- package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +0 -25
- package/dist/esm/cli/server/data-fetch-server-plugin.js +0 -20
- package/dist/esm/cli/ssrPlugin.js +0 -305
- package/dist/esm/cli/utils.js +0 -53
- package/dist/esm/constant.js +0 -6
- package/dist/esm/logger.js +0 -7
- package/dist/esm/react/plugin.js +0 -5
- package/dist/esm/server/fileCache.js +0 -98
- package/dist/esm/server/fileCache.spec.js +0 -50
- package/dist/esm/server/index.js +0 -41
- package/dist/esm/server/staticMiddleware.js +0 -104
- package/dist/esm/server/staticMiddleware.spec.js +0 -328
- package/dist/esm/ssr-runtime/SSRLiveReload.js +0 -26
- package/dist/esm/ssr-runtime/devPlugin.js +0 -73
- package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -34
- package/dist/esm-node/cli/configPlugin.js +0 -347
- package/dist/esm-node/cli/configPlugin.spec.js +0 -89
- package/dist/esm-node/cli/index.js +0 -72
- package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.js +0 -14
- package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +0 -62
- package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +0 -23
- package/dist/esm-node/cli/server/data-fetch-server-plugin.js +0 -18
- package/dist/esm-node/cli/ssrPlugin.js +0 -280
- package/dist/esm-node/cli/utils.js +0 -53
- package/dist/esm-node/constant.js +0 -6
- package/dist/esm-node/logger.js +0 -7
- package/dist/esm-node/react/plugin.js +0 -5
- package/dist/esm-node/server/fileCache.js +0 -49
- package/dist/esm-node/server/fileCache.spec.js +0 -27
- package/dist/esm-node/server/index.js +0 -39
- package/dist/esm-node/server/staticMiddleware.js +0 -55
- package/dist/esm-node/server/staticMiddleware.spec.js +0 -162
- package/dist/esm-node/ssr-runtime/SSRLiveReload.js +0 -19
- package/dist/esm-node/ssr-runtime/devPlugin.js +0 -37
- package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -16
- /package/dist/esm/interfaces/{bundler.js → bundler.mjs} +0 -0
- /package/dist/esm/react/{index.js → index.mjs} +0 -0
- /package/dist/esm/react/{v18.js → v18.mjs} +0 -0
- /package/dist/esm/react/{v19.js → v19.mjs} +0 -0
- /package/dist/esm/runtime/{index.js → index.mjs} +0 -0
- /package/dist/esm/types/{index.js → index.mjs} +0 -0
- /package/dist/esm-node/interfaces/{bundler.js → bundler.mjs} +0 -0
- /package/dist/esm-node/react/{index.js → index.mjs} +0 -0
- /package/dist/esm-node/react/{v18.js → v18.mjs} +0 -0
- /package/dist/esm-node/react/{v19.js → v19.mjs} +0 -0
- /package/dist/esm-node/runtime/{index.js → index.mjs} +0 -0
- /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,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 };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@module-federation/modern-js",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.22.0",
|
|
4
4
|
"files": [
|
|
5
5
|
"dist/",
|
|
6
6
|
"types.d.ts",
|
|
@@ -17,60 +17,60 @@
|
|
|
17
17
|
"exports": {
|
|
18
18
|
".": {
|
|
19
19
|
"types": "./dist/types/cli/index.d.ts",
|
|
20
|
-
"import": "./dist/esm/cli/index.
|
|
20
|
+
"import": "./dist/esm/cli/index.mjs",
|
|
21
21
|
"require": "./dist/cjs/cli/index.js"
|
|
22
22
|
},
|
|
23
23
|
"./runtime": {
|
|
24
24
|
"types": "./dist/types/runtime/index.d.ts",
|
|
25
|
-
"default": "./dist/esm/runtime/index.
|
|
25
|
+
"default": "./dist/esm/runtime/index.mjs"
|
|
26
26
|
},
|
|
27
27
|
"./react": {
|
|
28
28
|
"types": "./dist/types/react/index.d.ts",
|
|
29
|
-
"default": "./dist/esm/react/index.
|
|
29
|
+
"default": "./dist/esm/react/index.mjs"
|
|
30
30
|
},
|
|
31
31
|
"./react-v18": {
|
|
32
32
|
"types": "./dist/types/react/v18.d.ts",
|
|
33
|
-
"default": "./dist/esm/react/v18.
|
|
33
|
+
"default": "./dist/esm/react/v18.mjs"
|
|
34
34
|
},
|
|
35
35
|
"./react-v19": {
|
|
36
36
|
"types": "./dist/types/react/v19.d.ts",
|
|
37
|
-
"default": "./dist/esm/react/v19.
|
|
37
|
+
"default": "./dist/esm/react/v19.mjs"
|
|
38
38
|
},
|
|
39
39
|
"./react-plugin": {
|
|
40
40
|
"types": "./dist/types/react/plugin.d.ts",
|
|
41
|
-
"default": "./dist/esm/react/plugin.
|
|
41
|
+
"default": "./dist/esm/react/plugin.mjs"
|
|
42
42
|
},
|
|
43
43
|
"./ssr-dev-plugin": {
|
|
44
44
|
"types": "./dist/types/ssr-runtime/devPlugin.d.ts",
|
|
45
|
-
"default": "./dist/esm/ssr-runtime/devPlugin.
|
|
45
|
+
"default": "./dist/esm/ssr-runtime/devPlugin.mjs"
|
|
46
46
|
},
|
|
47
47
|
"./ssr-inject-data-fetch-function-plugin": {
|
|
48
48
|
"types": "./dist/types/ssr-runtime/injectDataFetchFunctionPlugin.d.ts",
|
|
49
|
-
"default": "./dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.
|
|
49
|
+
"default": "./dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.mjs"
|
|
50
50
|
},
|
|
51
51
|
"./config-plugin": {
|
|
52
52
|
"types": "./dist/types/cli/configPlugin.d.ts",
|
|
53
|
-
"import": "./dist/esm/cli/configPlugin.
|
|
53
|
+
"import": "./dist/esm/cli/configPlugin.mjs",
|
|
54
54
|
"require": "./dist/cjs/cli/configPlugin.js"
|
|
55
55
|
},
|
|
56
56
|
"./ssr-plugin": {
|
|
57
57
|
"types": "./dist/types/cli/ssrPlugin.d.ts",
|
|
58
|
-
"import": "./dist/esm/cli/ssrPlugin.
|
|
58
|
+
"import": "./dist/esm/cli/ssrPlugin.mjs",
|
|
59
59
|
"require": "./dist/cjs/cli/ssrPlugin.js"
|
|
60
60
|
},
|
|
61
61
|
"./shared-strategy": {
|
|
62
62
|
"types": "./dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts",
|
|
63
|
-
"import": "./dist/esm/cli/mfRuntimePlugins/shared-strategy.
|
|
63
|
+
"import": "./dist/esm/cli/mfRuntimePlugins/shared-strategy.mjs",
|
|
64
64
|
"require": "./dist/cjs/cli/mfRuntimePlugins/shared-strategy.js"
|
|
65
65
|
},
|
|
66
66
|
"./resolve-entry-ipv4": {
|
|
67
67
|
"types": "./dist/types/cli/mfRuntimePlugins/resolve-entry-ipv4.d.ts",
|
|
68
|
-
"import": "./dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.
|
|
68
|
+
"import": "./dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.mjs",
|
|
69
69
|
"require": "./dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js"
|
|
70
70
|
},
|
|
71
71
|
"./inject-node-fetch": {
|
|
72
72
|
"types": "./dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts",
|
|
73
|
-
"import": "./dist/esm/cli/mfRuntimePlugins/inject-node-fetch.
|
|
73
|
+
"import": "./dist/esm/cli/mfRuntimePlugins/inject-node-fetch.mjs",
|
|
74
74
|
"require": "./dist/cjs/cli/mfRuntimePlugins/inject-node-fetch.js"
|
|
75
75
|
},
|
|
76
76
|
"./data-fetch-server-plugin": {
|
|
@@ -133,32 +133,33 @@
|
|
|
133
133
|
"author": "hanric <hanric.zhang@gmail.com>",
|
|
134
134
|
"license": "MIT",
|
|
135
135
|
"dependencies": {
|
|
136
|
-
"@modern-js/utils": "2.
|
|
137
|
-
"@modern-js/node-bundle-require": "2.
|
|
136
|
+
"@modern-js/utils": "2.69.3",
|
|
137
|
+
"@modern-js/node-bundle-require": "2.69.3",
|
|
138
138
|
"fs-extra": "11.3.0",
|
|
139
139
|
"lru-cache": "10.4.3",
|
|
140
140
|
"@swc/helpers": "^0.5.17",
|
|
141
141
|
"node-fetch": "~3.3.0",
|
|
142
142
|
"react-error-boundary": "4.1.2",
|
|
143
|
-
"@module-federation/rsbuild-plugin": "0.
|
|
144
|
-
"@module-federation/
|
|
145
|
-
"@module-federation/runtime": "0.
|
|
146
|
-
"@module-federation/
|
|
147
|
-
"@module-federation/
|
|
148
|
-
"@module-federation/
|
|
149
|
-
"@module-federation/
|
|
143
|
+
"@module-federation/rsbuild-plugin": "0.22.0",
|
|
144
|
+
"@module-federation/enhanced": "0.22.0",
|
|
145
|
+
"@module-federation/runtime": "0.22.0",
|
|
146
|
+
"@module-federation/node": "2.7.26",
|
|
147
|
+
"@module-federation/sdk": "0.22.0",
|
|
148
|
+
"@module-federation/bridge-react": "0.22.0",
|
|
149
|
+
"@module-federation/cli": "0.22.0"
|
|
150
150
|
},
|
|
151
151
|
"devDependencies": {
|
|
152
|
-
"@
|
|
152
|
+
"@rsbuild/plugin-react": "1.4.2",
|
|
153
|
+
"@rslib/core": "0.18.5",
|
|
153
154
|
"@rsbuild/core": "1.3.21",
|
|
154
|
-
"@modern-js/app-tools": "2.
|
|
155
|
-
"@modern-js/server-runtime": "2.
|
|
156
|
-
"@modern-js/module-tools": "2.
|
|
157
|
-
"@modern-js/runtime": "2.
|
|
158
|
-
"@modern-js/tsconfig": "2.
|
|
155
|
+
"@modern-js/app-tools": "2.69.3",
|
|
156
|
+
"@modern-js/server-runtime": "2.69.3",
|
|
157
|
+
"@modern-js/module-tools": "2.69.3",
|
|
158
|
+
"@modern-js/runtime": "2.69.3",
|
|
159
|
+
"@modern-js/tsconfig": "2.69.3",
|
|
159
160
|
"@types/react": "^18.3.11",
|
|
160
161
|
"@types/react-dom": "^18.3.0",
|
|
161
|
-
"@module-federation/manifest": "0.
|
|
162
|
+
"@module-federation/manifest": "0.22.0"
|
|
162
163
|
},
|
|
163
164
|
"peerDependencies": {
|
|
164
165
|
"react": ">=17",
|
|
@@ -183,6 +184,6 @@
|
|
|
183
184
|
}
|
|
184
185
|
},
|
|
185
186
|
"scripts": {
|
|
186
|
-
"build": "
|
|
187
|
+
"build": "rslib build"
|
|
187
188
|
}
|
|
188
189
|
}
|