@module-federation/modern-js 0.0.0-next-20250618045528 → 0.0.0-next-20250618090118
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/LICENSE +21 -0
- package/dist/cjs/cli/configPlugin.js +381 -0
- package/dist/cjs/cli/configPlugin.spec.js +114 -0
- package/dist/cjs/cli/index.js +95 -0
- package/dist/cjs/cli/mfRuntimePlugins/inject-node-fetch.js +44 -0
- package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +82 -0
- package/dist/cjs/cli/mfRuntimePlugins/shared-strategy.js +43 -0
- package/dist/cjs/cli/server/data-fetch-server-plugin.js +48 -0
- package/dist/cjs/cli/ssrPlugin.js +236 -0
- package/dist/cjs/cli/utils.js +90 -0
- package/dist/cjs/constant.js +31 -0
- package/dist/cjs/interfaces/bundler.js +16 -0
- package/dist/cjs/logger.js +27 -0
- package/dist/cjs/react/index.js +67 -0
- package/dist/cjs/runtime/index.js +22 -0
- package/dist/cjs/server/fileCache.js +84 -0
- package/dist/cjs/server/fileCache.spec.js +28 -0
- package/dist/cjs/server/index.js +58 -0
- package/dist/cjs/server/staticMiddleware.js +77 -0
- package/dist/cjs/server/staticMiddleware.spec.js +185 -0
- package/dist/cjs/ssr-runtime/SSRLiveReload.js +43 -0
- package/dist/cjs/ssr-runtime/devPlugin.js +71 -0
- package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +41 -0
- package/dist/cjs/types/index.js +16 -0
- package/dist/esm/cli/configPlugin.js +388 -0
- package/dist/esm/cli/configPlugin.spec.js +110 -0
- package/dist/esm/cli/index.js +90 -0
- package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js +16 -0
- package/dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.js +74 -0
- package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +25 -0
- package/dist/esm/cli/server/data-fetch-server-plugin.js +20 -0
- package/dist/esm/cli/ssrPlugin.js +219 -0
- package/dist/esm/cli/utils.js +53 -0
- package/dist/esm/constant.js +6 -0
- package/dist/esm/interfaces/bundler.js +0 -0
- package/dist/esm/logger.js +7 -0
- package/dist/esm/react/index.js +28 -0
- package/dist/esm/runtime/index.js +1 -0
- package/dist/esm/server/fileCache.js +98 -0
- package/dist/esm/server/fileCache.spec.js +50 -0
- package/dist/esm/server/index.js +36 -0
- package/dist/esm/server/staticMiddleware.js +81 -0
- package/dist/esm/server/staticMiddleware.spec.js +328 -0
- package/dist/esm/ssr-runtime/SSRLiveReload.js +26 -0
- package/dist/esm/ssr-runtime/devPlugin.js +73 -0
- package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js +35 -0
- package/dist/esm/types/index.js +0 -0
- package/dist/esm-node/cli/configPlugin.js +340 -0
- package/dist/esm-node/cli/configPlugin.spec.js +91 -0
- package/dist/esm-node/cli/index.js +70 -0
- package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.js +14 -0
- package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +62 -0
- package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +23 -0
- package/dist/esm-node/cli/server/data-fetch-server-plugin.js +18 -0
- package/dist/esm-node/cli/ssrPlugin.js +200 -0
- package/dist/esm-node/cli/utils.js +53 -0
- package/dist/esm-node/constant.js +6 -0
- package/dist/esm-node/interfaces/bundler.js +0 -0
- package/dist/esm-node/logger.js +7 -0
- package/dist/esm-node/react/index.js +29 -0
- package/dist/esm-node/runtime/index.js +1 -0
- package/dist/esm-node/server/fileCache.js +49 -0
- package/dist/esm-node/server/fileCache.spec.js +27 -0
- package/dist/esm-node/server/index.js +34 -0
- package/dist/esm-node/server/staticMiddleware.js +43 -0
- package/dist/esm-node/server/staticMiddleware.spec.js +162 -0
- package/dist/esm-node/ssr-runtime/SSRLiveReload.js +19 -0
- package/dist/esm-node/ssr-runtime/devPlugin.js +37 -0
- package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.js +17 -0
- package/dist/esm-node/types/index.js +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/resolve-entry-ipv4.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/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/index.d.ts +10 -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 +6 -0
- package/dist/types/server/staticMiddleware.spec.d.ts +1 -0
- package/dist/types/ssr-runtime/SSRLiveReload.d.ts +1 -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 +24 -0
- package/package.json +37 -12
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
var sharedStrategy = function() {
|
|
2
|
+
return {
|
|
3
|
+
name: "shared-strategy-plugin",
|
|
4
|
+
beforeInit: function beforeInit(args) {
|
|
5
|
+
var userOptions = args.userOptions;
|
|
6
|
+
var shared = userOptions.shared;
|
|
7
|
+
if (shared) {
|
|
8
|
+
Object.keys(shared).forEach(function(sharedKey) {
|
|
9
|
+
var sharedConfigs = shared[sharedKey];
|
|
10
|
+
var arraySharedConfigs = Array.isArray(sharedConfigs) ? sharedConfigs : [
|
|
11
|
+
sharedConfigs
|
|
12
|
+
];
|
|
13
|
+
arraySharedConfigs.forEach(function(s) {
|
|
14
|
+
s.strategy = "loaded-first";
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
return args;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
var shared_strategy_default = sharedStrategy;
|
|
23
|
+
export {
|
|
24
|
+
shared_strategy_default as default
|
|
25
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import dataFetchMiddleWare from "@module-federation/bridge-react/data-fetch-server-middleware";
|
|
2
|
+
var dataFetchServePlugin = function() {
|
|
3
|
+
return {
|
|
4
|
+
name: "mf-data-fetch-server-plugin",
|
|
5
|
+
setup: function(api) {
|
|
6
|
+
api.onPrepare(function() {
|
|
7
|
+
var middlewares = api.getServerContext().middlewares;
|
|
8
|
+
middlewares.push({
|
|
9
|
+
name: "module-federation-serve-manifest",
|
|
10
|
+
// @ts-ignore type error
|
|
11
|
+
handler: dataFetchMiddleWare
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
var data_fetch_server_plugin_default = dataFetchServePlugin;
|
|
18
|
+
export {
|
|
19
|
+
data_fetch_server_plugin_default as default
|
|
20
|
+
};
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _type_of } from "@swc/helpers/_/_type_of";
|
|
3
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import fs from "fs-extra";
|
|
6
|
+
import { ModuleFederationPlugin } from "@module-federation/enhanced/webpack";
|
|
7
|
+
import { ModuleFederationPlugin as RspackModuleFederationPlugin } from "@module-federation/enhanced/rspack";
|
|
8
|
+
import UniverseEntryChunkTrackerPlugin from "@module-federation/node/universe-entry-chunk-tracker-plugin";
|
|
9
|
+
import logger from "../logger";
|
|
10
|
+
import { isDev } from "./utils";
|
|
11
|
+
import { updateStatsAndManifest } from "@module-federation/rsbuild-plugin/manifest";
|
|
12
|
+
import { isWebTarget, skipByTarget } from "./utils";
|
|
13
|
+
function setEnv() {
|
|
14
|
+
process.env["MF_DISABLE_EMIT_STATS"] = "true";
|
|
15
|
+
process.env["MF_SSR_PRJ"] = "true";
|
|
16
|
+
}
|
|
17
|
+
var CHAIN_MF_PLUGIN_ID = "plugin-module-federation-server";
|
|
18
|
+
var mfSSRRsbuildPlugin = function(pluginOptions) {
|
|
19
|
+
return {
|
|
20
|
+
name: "@modern-js/plugin-mf-post-config",
|
|
21
|
+
pre: [
|
|
22
|
+
"@modern-js/builder-plugin-ssr"
|
|
23
|
+
],
|
|
24
|
+
setup: function setup(api) {
|
|
25
|
+
if (pluginOptions.csrConfig.getPublicPath) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
var csrOutputPath = "";
|
|
29
|
+
var ssrOutputPath = "";
|
|
30
|
+
var ssrEnv = "";
|
|
31
|
+
api.modifyEnvironmentConfig(function(config, param) {
|
|
32
|
+
var name = param.name;
|
|
33
|
+
var target = config.output.target;
|
|
34
|
+
if (skipByTarget(target)) {
|
|
35
|
+
return config;
|
|
36
|
+
}
|
|
37
|
+
if (isWebTarget(target)) {
|
|
38
|
+
csrOutputPath = config.output.distPath.root;
|
|
39
|
+
} else {
|
|
40
|
+
ssrOutputPath = config.output.distPath.root;
|
|
41
|
+
ssrEnv = name;
|
|
42
|
+
}
|
|
43
|
+
return config;
|
|
44
|
+
});
|
|
45
|
+
var modifySSRPublicPath = function(config, utils) {
|
|
46
|
+
if (ssrEnv !== utils.environment.name) {
|
|
47
|
+
return config;
|
|
48
|
+
}
|
|
49
|
+
var userSSRConfig = pluginOptions.userConfig.ssr ? _type_of(pluginOptions.userConfig.ssr) === "object" ? pluginOptions.userConfig.ssr : {} : {};
|
|
50
|
+
if (userSSRConfig.distOutputDir) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
config.output.publicPath = "".concat(config.output.publicPath).concat(path.relative(csrOutputPath, ssrOutputPath), "/");
|
|
54
|
+
return config;
|
|
55
|
+
};
|
|
56
|
+
api.modifyWebpackConfig(function(config, utils) {
|
|
57
|
+
modifySSRPublicPath(config, utils);
|
|
58
|
+
return config;
|
|
59
|
+
});
|
|
60
|
+
api.modifyRspackConfig(function(config, utils) {
|
|
61
|
+
modifySSRPublicPath(config, utils);
|
|
62
|
+
return config;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
var moduleFederationSSRPlugin = function(pluginOptions) {
|
|
68
|
+
return {
|
|
69
|
+
name: "@modern-js/plugin-module-federation-ssr",
|
|
70
|
+
pre: [
|
|
71
|
+
"@modern-js/plugin-module-federation-config",
|
|
72
|
+
"@modern-js/plugin-module-federation"
|
|
73
|
+
],
|
|
74
|
+
setup: function() {
|
|
75
|
+
var _ref = _async_to_generator(function(api) {
|
|
76
|
+
var _pluginOptions_userConfig, _modernjsConfig_server, modernjsConfig, _pluginOptions_userConfig_ssr, enableSSR;
|
|
77
|
+
return _ts_generator(this, function(_state) {
|
|
78
|
+
modernjsConfig = api.getConfig();
|
|
79
|
+
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);
|
|
80
|
+
if (!enableSSR) {
|
|
81
|
+
return [
|
|
82
|
+
2
|
|
83
|
+
];
|
|
84
|
+
}
|
|
85
|
+
setEnv();
|
|
86
|
+
api._internalRuntimePlugins(function(param) {
|
|
87
|
+
var entrypoint = param.entrypoint, plugins = param.plugins;
|
|
88
|
+
var fetchServerQuery = pluginOptions.fetchServerQuery;
|
|
89
|
+
plugins.push({
|
|
90
|
+
name: "injectDataFetchFunction",
|
|
91
|
+
path: "@module-federation/modern-js/ssr-inject-data-fetch-function-plugin",
|
|
92
|
+
config: {
|
|
93
|
+
fetchServerQuery
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
if (!isDev()) {
|
|
97
|
+
return {
|
|
98
|
+
entrypoint,
|
|
99
|
+
plugins
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
plugins.push({
|
|
103
|
+
name: "mfSSRDev",
|
|
104
|
+
path: "@module-federation/modern-js/ssr-dev-plugin",
|
|
105
|
+
config: {}
|
|
106
|
+
});
|
|
107
|
+
return {
|
|
108
|
+
entrypoint,
|
|
109
|
+
plugins
|
|
110
|
+
};
|
|
111
|
+
});
|
|
112
|
+
if (pluginOptions.ssrConfig.remotes) {
|
|
113
|
+
api._internalServerPlugins(function(param) {
|
|
114
|
+
var plugins = param.plugins;
|
|
115
|
+
plugins.push({
|
|
116
|
+
name: "@module-federation/modern-js/data-fetch-server-plugin",
|
|
117
|
+
options: {}
|
|
118
|
+
});
|
|
119
|
+
return {
|
|
120
|
+
plugins
|
|
121
|
+
};
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
api.modifyBundlerChain(function(chain) {
|
|
125
|
+
var target = chain.get("target");
|
|
126
|
+
if (skipByTarget(target)) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
var bundlerType = api.getAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
|
|
130
|
+
var MFPlugin = bundlerType === "webpack" ? ModuleFederationPlugin : RspackModuleFederationPlugin;
|
|
131
|
+
var isWeb = isWebTarget(target);
|
|
132
|
+
if (!isWeb) {
|
|
133
|
+
if (!chain.plugins.has(CHAIN_MF_PLUGIN_ID)) {
|
|
134
|
+
chain.plugin(CHAIN_MF_PLUGIN_ID).use(MFPlugin, [
|
|
135
|
+
pluginOptions.ssrConfig
|
|
136
|
+
]).init(function(Plugin, args) {
|
|
137
|
+
pluginOptions.nodePlugin = new Plugin(args[0]);
|
|
138
|
+
return pluginOptions.nodePlugin;
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (!isWeb) {
|
|
143
|
+
chain.target("async-node");
|
|
144
|
+
if (isDev()) {
|
|
145
|
+
chain.plugin("UniverseEntryChunkTrackerPlugin").use(UniverseEntryChunkTrackerPlugin);
|
|
146
|
+
}
|
|
147
|
+
var userSSRConfig = pluginOptions.userConfig.ssr ? _type_of(pluginOptions.userConfig.ssr) === "object" ? pluginOptions.userConfig.ssr : {} : {};
|
|
148
|
+
var publicPath = chain.output.get("publicPath");
|
|
149
|
+
if (userSSRConfig.distOutputDir && publicPath) {
|
|
150
|
+
chain.output.publicPath("".concat(publicPath).concat(userSSRConfig.distOutputDir, "/"));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (isDev() && isWeb) {
|
|
154
|
+
chain.externals({
|
|
155
|
+
"@module-federation/node/utils": "NOT_USED_IN_BROWSER"
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
api.config(function() {
|
|
160
|
+
return {
|
|
161
|
+
builderPlugins: [
|
|
162
|
+
mfSSRRsbuildPlugin(pluginOptions)
|
|
163
|
+
],
|
|
164
|
+
tools: {
|
|
165
|
+
devServer: {
|
|
166
|
+
before: [
|
|
167
|
+
function(req, res, next) {
|
|
168
|
+
if (!enableSSR) {
|
|
169
|
+
next();
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
try {
|
|
173
|
+
var _req_url, _req_url1;
|
|
174
|
+
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"))) {
|
|
175
|
+
var filepath = path.join(process.cwd(), "dist".concat(req.url));
|
|
176
|
+
fs.statSync(filepath);
|
|
177
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
178
|
+
res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS");
|
|
179
|
+
res.setHeader("Access-Control-Allow-Headers", "*");
|
|
180
|
+
fs.createReadStream(filepath).pipe(res);
|
|
181
|
+
} else {
|
|
182
|
+
next();
|
|
183
|
+
}
|
|
184
|
+
} catch (err) {
|
|
185
|
+
logger.debug(err);
|
|
186
|
+
next();
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
]
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
});
|
|
194
|
+
api.onAfterBuild(function() {
|
|
195
|
+
var nodePlugin = pluginOptions.nodePlugin, browserPlugin = pluginOptions.browserPlugin, distOutputDir = pluginOptions.distOutputDir;
|
|
196
|
+
updateStatsAndManifest(nodePlugin, browserPlugin, distOutputDir);
|
|
197
|
+
});
|
|
198
|
+
api.onDevCompileDone(function() {
|
|
199
|
+
var nodePlugin = pluginOptions.nodePlugin, browserPlugin = pluginOptions.browserPlugin, distOutputDir = pluginOptions.distOutputDir;
|
|
200
|
+
updateStatsAndManifest(nodePlugin, browserPlugin, distOutputDir);
|
|
201
|
+
});
|
|
202
|
+
return [
|
|
203
|
+
2
|
|
204
|
+
];
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
return function(api) {
|
|
208
|
+
return _ref.apply(this, arguments);
|
|
209
|
+
};
|
|
210
|
+
}()
|
|
211
|
+
};
|
|
212
|
+
};
|
|
213
|
+
var ssrPlugin_default = moduleFederationSSRPlugin;
|
|
214
|
+
export {
|
|
215
|
+
CHAIN_MF_PLUGIN_ID,
|
|
216
|
+
ssrPlugin_default as default,
|
|
217
|
+
moduleFederationSSRPlugin,
|
|
218
|
+
setEnv
|
|
219
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import os from "os";
|
|
2
|
+
var localIpv4 = "127.0.0.1";
|
|
3
|
+
var getIpv4Interfaces = function() {
|
|
4
|
+
try {
|
|
5
|
+
var interfaces = os.networkInterfaces();
|
|
6
|
+
var ipv4Interfaces = [];
|
|
7
|
+
Object.values(interfaces).forEach(function(detail) {
|
|
8
|
+
detail === null || detail === void 0 ? void 0 : detail.forEach(function(detail2) {
|
|
9
|
+
var 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
|
+
var getIPV4 = function() {
|
|
21
|
+
var ipv4Interfaces = getIpv4Interfaces();
|
|
22
|
+
var ipv4Interface = ipv4Interfaces[0] || {
|
|
23
|
+
address: localIpv4
|
|
24
|
+
};
|
|
25
|
+
return ipv4Interface.address;
|
|
26
|
+
};
|
|
27
|
+
var isWebTarget = function(target) {
|
|
28
|
+
var 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
|
+
var skipByTarget = function(target) {
|
|
37
|
+
var IGNORE_TARGET = "webworker";
|
|
38
|
+
if (Array.isArray(target)) {
|
|
39
|
+
return target.includes(IGNORE_TARGET);
|
|
40
|
+
} else if (typeof target === "string") {
|
|
41
|
+
return target === IGNORE_TARGET;
|
|
42
|
+
}
|
|
43
|
+
return false;
|
|
44
|
+
};
|
|
45
|
+
function isDev() {
|
|
46
|
+
return process.env.NODE_ENV === "development";
|
|
47
|
+
}
|
|
48
|
+
export {
|
|
49
|
+
getIPV4,
|
|
50
|
+
isDev,
|
|
51
|
+
isWebTarget,
|
|
52
|
+
skipByTarget
|
|
53
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
2
|
+
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
3
|
+
import * as mfRuntime from "@module-federation/enhanced/runtime";
|
|
4
|
+
import { ERROR_TYPE, createLazyComponent as rawCreateLazyComponent, collectSSRAssets as rawCollectSsrAssets, injectDataFetch } from "@module-federation/bridge-react";
|
|
5
|
+
var createLazyComponent = function(options) {
|
|
6
|
+
return rawCreateLazyComponent(_object_spread_props(_object_spread({}, options), {
|
|
7
|
+
runtime: mfRuntime
|
|
8
|
+
}));
|
|
9
|
+
};
|
|
10
|
+
var collectSSRAssets = function(options) {
|
|
11
|
+
return rawCollectSsrAssets(_object_spread_props(_object_spread({}, options), {
|
|
12
|
+
runtime: mfRuntime
|
|
13
|
+
}));
|
|
14
|
+
};
|
|
15
|
+
function wrapNoSSR(createLazyComponentFn) {
|
|
16
|
+
return function(options) {
|
|
17
|
+
return createLazyComponentFn(_object_spread_props(_object_spread({}, options), {
|
|
18
|
+
noSSR: true
|
|
19
|
+
}));
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export {
|
|
23
|
+
ERROR_TYPE,
|
|
24
|
+
collectSSRAssets,
|
|
25
|
+
createLazyComponent,
|
|
26
|
+
injectDataFetch,
|
|
27
|
+
wrapNoSSR
|
|
28
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@module-federation/enhanced/runtime";
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
|
|
3
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
4
|
+
import fs from "fs-extra";
|
|
5
|
+
import { LRUCache } from "lru-cache";
|
|
6
|
+
var FileCache = /* @__PURE__ */ function() {
|
|
7
|
+
"use strict";
|
|
8
|
+
function FileCache2() {
|
|
9
|
+
_class_call_check(this, FileCache2);
|
|
10
|
+
this.cache = new LRUCache({
|
|
11
|
+
maxSize: 200 * 1024 * 1024
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
var _proto = FileCache2.prototype;
|
|
15
|
+
_proto.getFile = function getFile(filepath) {
|
|
16
|
+
var _this = this;
|
|
17
|
+
return _async_to_generator(function() {
|
|
18
|
+
var stat, currentModified, cachedEntry, content, newEntry, err;
|
|
19
|
+
return _ts_generator(this, function(_state) {
|
|
20
|
+
switch (_state.label) {
|
|
21
|
+
case 0:
|
|
22
|
+
return [
|
|
23
|
+
4,
|
|
24
|
+
fs.pathExists(filepath)
|
|
25
|
+
];
|
|
26
|
+
case 1:
|
|
27
|
+
if (!_state.sent()) {
|
|
28
|
+
return [
|
|
29
|
+
2,
|
|
30
|
+
null
|
|
31
|
+
];
|
|
32
|
+
}
|
|
33
|
+
_state.label = 2;
|
|
34
|
+
case 2:
|
|
35
|
+
_state.trys.push([
|
|
36
|
+
2,
|
|
37
|
+
5,
|
|
38
|
+
,
|
|
39
|
+
6
|
|
40
|
+
]);
|
|
41
|
+
return [
|
|
42
|
+
4,
|
|
43
|
+
fs.lstat(filepath)
|
|
44
|
+
];
|
|
45
|
+
case 3:
|
|
46
|
+
stat = _state.sent();
|
|
47
|
+
currentModified = stat.mtimeMs;
|
|
48
|
+
cachedEntry = _this.cache.get(filepath);
|
|
49
|
+
if (cachedEntry && currentModified <= cachedEntry.lastModified) {
|
|
50
|
+
return [
|
|
51
|
+
2,
|
|
52
|
+
{
|
|
53
|
+
content: cachedEntry.content,
|
|
54
|
+
lastModified: cachedEntry.lastModified
|
|
55
|
+
}
|
|
56
|
+
];
|
|
57
|
+
}
|
|
58
|
+
return [
|
|
59
|
+
4,
|
|
60
|
+
fs.readFile(filepath, "utf-8")
|
|
61
|
+
];
|
|
62
|
+
case 4:
|
|
63
|
+
content = _state.sent();
|
|
64
|
+
newEntry = {
|
|
65
|
+
content,
|
|
66
|
+
lastModified: currentModified
|
|
67
|
+
};
|
|
68
|
+
_this.cache.set(filepath, newEntry, {
|
|
69
|
+
size: stat.size || content.length
|
|
70
|
+
});
|
|
71
|
+
return [
|
|
72
|
+
2,
|
|
73
|
+
{
|
|
74
|
+
content,
|
|
75
|
+
lastModified: currentModified
|
|
76
|
+
}
|
|
77
|
+
];
|
|
78
|
+
case 5:
|
|
79
|
+
err = _state.sent();
|
|
80
|
+
return [
|
|
81
|
+
2,
|
|
82
|
+
null
|
|
83
|
+
];
|
|
84
|
+
case 6:
|
|
85
|
+
return [
|
|
86
|
+
2
|
|
87
|
+
];
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
})();
|
|
91
|
+
};
|
|
92
|
+
return FileCache2;
|
|
93
|
+
}();
|
|
94
|
+
var fileCache = new FileCache();
|
|
95
|
+
export {
|
|
96
|
+
FileCache,
|
|
97
|
+
fileCache
|
|
98
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
3
|
+
import { it, expect, describe, vi, beforeAll } from "vitest";
|
|
4
|
+
import { FileCache } from "./fileCache";
|
|
5
|
+
beforeAll(function() {
|
|
6
|
+
vi.mock("fs-extra", function() {
|
|
7
|
+
return {
|
|
8
|
+
default: {
|
|
9
|
+
pathExists: function() {
|
|
10
|
+
return true;
|
|
11
|
+
},
|
|
12
|
+
lstat: function() {
|
|
13
|
+
return {
|
|
14
|
+
mtimeMs: Date.now(),
|
|
15
|
+
size: 4
|
|
16
|
+
};
|
|
17
|
+
},
|
|
18
|
+
readFile: function() {
|
|
19
|
+
return "test";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
describe("modern serve static file cache", /* @__PURE__ */ _async_to_generator(function() {
|
|
26
|
+
return _ts_generator(this, function(_state) {
|
|
27
|
+
it("should cache file", /* @__PURE__ */ _async_to_generator(function() {
|
|
28
|
+
var cache, result;
|
|
29
|
+
return _ts_generator(this, function(_state2) {
|
|
30
|
+
switch (_state2.label) {
|
|
31
|
+
case 0:
|
|
32
|
+
cache = new FileCache();
|
|
33
|
+
return [
|
|
34
|
+
4,
|
|
35
|
+
cache.getFile("test.txt")
|
|
36
|
+
];
|
|
37
|
+
case 1:
|
|
38
|
+
result = _state2.sent();
|
|
39
|
+
expect(result === null || result === void 0 ? void 0 : result.content).toBe("test");
|
|
40
|
+
return [
|
|
41
|
+
2
|
|
42
|
+
];
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}));
|
|
46
|
+
return [
|
|
47
|
+
2
|
|
48
|
+
];
|
|
49
|
+
});
|
|
50
|
+
}));
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { createStaticMiddleware } from "./staticMiddleware";
|
|
2
|
+
var staticServePlugin = function() {
|
|
3
|
+
return {
|
|
4
|
+
name: "@modern-js/module-federation/server",
|
|
5
|
+
setup: function(api) {
|
|
6
|
+
api.onPrepare(function() {
|
|
7
|
+
var _config_output, _config_server;
|
|
8
|
+
console.log(process.env.NODE_ENV);
|
|
9
|
+
if (process.env.NODE_ENV === "development") {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
var middlewares = api.getServerContext().middlewares;
|
|
13
|
+
var config = api.getServerConfig();
|
|
14
|
+
var assetPrefix = ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.assetPrefix) || "";
|
|
15
|
+
if (!((_config_server = config.server) === null || _config_server === void 0 ? void 0 : _config_server.ssr)) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
var context = api.getServerContext();
|
|
19
|
+
var pwd = context.distDirectory;
|
|
20
|
+
var serverStaticMiddleware = createStaticMiddleware({
|
|
21
|
+
assetPrefix,
|
|
22
|
+
pwd
|
|
23
|
+
});
|
|
24
|
+
middlewares.push({
|
|
25
|
+
name: "module-federation-serve-manifest",
|
|
26
|
+
handler: serverStaticMiddleware
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
var server_default = staticServePlugin;
|
|
33
|
+
export {
|
|
34
|
+
server_default as default,
|
|
35
|
+
staticServePlugin
|
|
36
|
+
};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
3
|
+
import fs from "fs-extra";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { fileCache } from "./fileCache";
|
|
6
|
+
var bundlesAssetPrefix = "/bundles";
|
|
7
|
+
var removeHost = function(url) {
|
|
8
|
+
try {
|
|
9
|
+
var hasProtocol = url.includes("://");
|
|
10
|
+
var hasDomain = hasProtocol || url.startsWith("//");
|
|
11
|
+
var pathname = hasDomain ? new URL(hasProtocol ? url : "http:".concat(url)).pathname : url;
|
|
12
|
+
return pathname;
|
|
13
|
+
} catch (e) {
|
|
14
|
+
return url;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var createStaticMiddleware = function(options) {
|
|
18
|
+
var assetPrefix = options.assetPrefix, pwd = options.pwd;
|
|
19
|
+
return function() {
|
|
20
|
+
var _ref = _async_to_generator(function(c, next) {
|
|
21
|
+
var pathname, prefixWithoutHost, prefixWithBundle, pathnameWithoutPrefix, filepath, fileResult;
|
|
22
|
+
return _ts_generator(this, function(_state) {
|
|
23
|
+
switch (_state.label) {
|
|
24
|
+
case 0:
|
|
25
|
+
pathname = c.req.path;
|
|
26
|
+
if (path.extname(pathname) !== ".js") {
|
|
27
|
+
return [
|
|
28
|
+
2,
|
|
29
|
+
next()
|
|
30
|
+
];
|
|
31
|
+
}
|
|
32
|
+
prefixWithoutHost = removeHost(assetPrefix);
|
|
33
|
+
prefixWithBundle = path.join(prefixWithoutHost, bundlesAssetPrefix);
|
|
34
|
+
if (!pathname.startsWith(prefixWithBundle)) {
|
|
35
|
+
return [
|
|
36
|
+
2,
|
|
37
|
+
next()
|
|
38
|
+
];
|
|
39
|
+
}
|
|
40
|
+
pathnameWithoutPrefix = pathname.replace(prefixWithBundle, "");
|
|
41
|
+
filepath = path.join(pwd, bundlesAssetPrefix, pathnameWithoutPrefix);
|
|
42
|
+
return [
|
|
43
|
+
4,
|
|
44
|
+
fs.pathExists(filepath)
|
|
45
|
+
];
|
|
46
|
+
case 1:
|
|
47
|
+
if (!_state.sent()) {
|
|
48
|
+
return [
|
|
49
|
+
2,
|
|
50
|
+
next()
|
|
51
|
+
];
|
|
52
|
+
}
|
|
53
|
+
return [
|
|
54
|
+
4,
|
|
55
|
+
fileCache.getFile(filepath)
|
|
56
|
+
];
|
|
57
|
+
case 2:
|
|
58
|
+
fileResult = _state.sent();
|
|
59
|
+
if (!fileResult) {
|
|
60
|
+
return [
|
|
61
|
+
2,
|
|
62
|
+
next()
|
|
63
|
+
];
|
|
64
|
+
}
|
|
65
|
+
c.header("Content-Type", "application/javascript");
|
|
66
|
+
c.header("Content-Length", String(fileResult.content.length));
|
|
67
|
+
return [
|
|
68
|
+
2,
|
|
69
|
+
c.body(fileResult.content, 200)
|
|
70
|
+
];
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
return function(c, next) {
|
|
75
|
+
return _ref.apply(this, arguments);
|
|
76
|
+
};
|
|
77
|
+
}();
|
|
78
|
+
};
|
|
79
|
+
export {
|
|
80
|
+
createStaticMiddleware
|
|
81
|
+
};
|