rspack-plugin-mock
Version:
inject api mock server to development server
81 lines (80 loc) • 3.63 kB
JavaScript
import { a as rewriteRequest, i as createMockMiddleware, o as Recorder, r as mockWebSocket } from "./logger-CzeuHKAL.js";
import { n as buildMockServer, r as createMockCompiler, t as resolvePluginOptions } from "./options-CI0LRpJZ.js";
import { createDefineMock, createSSEStream, defineMock, defineMockData } from "./helper.js";
import path from "node:path";
import process from "node:process";
import { isArray, objectEntries, toArray } from "@pengzhanbo/utils";
import rspack from "@rspack/core";
//#region src/core/rsbuild.ts
function pluginMockServer(options = {}) {
return {
name: "plugin-mock-server",
setup(api) {
if (options.enabled === false) return;
const rsbuildConfig = api.getRsbuildConfig();
const resolvedOptions = resolvePluginOptions(options, {
proxies: resolveConfigProxies(rsbuildConfig),
alias: {},
context: api.context.rootPath,
plugins: [new rspack.DefinePlugin(rsbuildConfig.source?.define || {})]
});
if (api.context.action === "build") {
if (resolvedOptions.build) api.onAfterBuild(async () => {
const config = api.getNormalizedConfig();
await buildMockServer(resolvedOptions, path.resolve(process.cwd(), config.output.distPath.root || "dist"));
});
return;
}
const mockCompiler = createMockCompiler(resolvedOptions);
api.onAfterCreateCompiler(({ compiler }) => {
if ("compilers" in compiler) compiler.compilers.forEach((compiler) => {
mockCompiler.updateAlias(compiler.options.resolve?.alias || {});
});
else mockCompiler.updateAlias(compiler.options.resolve?.alias || {});
});
let recorder = null;
if (resolvedOptions.record.enabled) recorder = new Recorder(resolvedOptions.record);
function wrapProxyOptions(options) {
const plugins = options.plugins ??= [];
plugins.push((proxyServer) => proxyServer.on("proxyReq", rewriteRequest));
if (resolvedOptions.record.enabled && recorder) plugins.push(recorder.getPlugin());
return options;
}
api.modifyRsbuildConfig((config) => {
if (!config.server?.proxy) return;
if (isArray(config.server.proxy)) config.server.proxy = config.server.proxy.map((item) => item.ws ? item : wrapProxyOptions(item));
else if (config.server.proxy) {
const proxy = config.server.proxy;
Object.keys(proxy).forEach((key) => {
const target = proxy[key];
const options = typeof target === "string" ? { target } : target;
if (options.ws) return;
proxy[key] = wrapProxyOptions(options);
});
}
});
function initMockServer(server) {
server.middlewares.use(createMockMiddleware(mockCompiler, resolvedOptions));
if (resolvedOptions.reload && api.context.action === "dev") mockCompiler.on("update", () => server.sockWrite("static-changed"));
if (toArray(resolvedOptions.wsPrefix).length > 0) mockWebSocket(mockCompiler, server.httpServer, resolvedOptions);
mockCompiler.run();
}
api.onBeforeStartDevServer(({ server }) => initMockServer(server));
api.onBeforeStartPreviewServer(({ server }) => initMockServer(server));
api.onExit(() => mockCompiler.close());
}
};
}
function resolveConfigProxies(config) {
config.server ??= {};
const proxy = config.server.proxy ??= {};
const proxies = [];
if (isArray(proxy)) {
for (const item of proxy) if (item.pathFilter && !item.ws) proxies.push(...toArray(item.pathFilter));
} else objectEntries(proxy).forEach(([pathFilter, opt]) => {
if (typeof opt === "string" || !opt.ws) proxies.push(pathFilter);
});
return proxies;
}
//#endregion
export { createDefineMock, createSSEStream, defineMock, defineMockData, pluginMockServer };