rspack-plugin-mock 2.0.0 → 2.1.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/README.md +406 -65
- package/README.zh-CN.md +406 -66
- package/dist/helper.d.ts +46 -8
- package/dist/helper.js +109 -14
- package/dist/{types-GT6M6WuI.d.ts → index-BMMGM-eD.d.ts} +537 -172
- package/dist/index.d.ts +9 -6
- package/dist/index.js +70 -67
- package/dist/json5-loader.mjs +2 -2
- package/dist/{ws-BcLCWVaK.js → logger-CzeuHKAL.js} +662 -102
- package/dist/{options-Bd0PSZeT.js → options-CI0LRpJZ.js} +48 -7
- package/dist/rsbuild.d.ts +4 -3
- package/dist/rsbuild.js +29 -41
- package/dist/{server-_doRwsZm.d.ts → server-pC78IYdH.d.ts} +48 -24
- package/dist/server.d.ts +2 -2
- package/dist/server.js +2 -2
- package/package.json +8 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
import { _ as
|
|
2
|
-
import { HeaderStream, MockData, SSEMessage, createDefineMock, createSSEStream, defineMock, defineMockData } from "./helper.js";
|
|
3
|
-
import { a as processMockData, c as
|
|
4
|
-
import { Compiler, RspackPluginInstance } from "@rspack/core";
|
|
1
|
+
import { A as ResponseBody, C as HandleFunction, D as MockResponse, E as MockRequest, M as CookiesOption, N as GetCookieOption, O as NextFunction, P as SetCookieOption, S as ExtraRequest, T as Method, _ as RecordedMeta, a as MockWebsocketItem, b as RecordedRes, c as MockHttpItem, d as LogType, f as MockMatchPriority, g as RecordOptions, h as ServerBuildOption, i as MockOptions, j as SimpleHandleFunction, k as NextHandleFunction, l as BodyParserOptions, m as MockServerPluginOptions, n as HttpProxyPlugin, o as WebSocketSetupContext, p as MockMatchSpecialPriority, r as PathFilter, s as MockErrorConfig, t as FormidableFile, u as LogLevel, v as RecordedReq, w as Headers, x as ResolvedRecordOptions, y as RecordedRequest } from "./index-BMMGM-eD.js";
|
|
2
|
+
import { DefineMockDataOptions, HeaderStream, MockData, SSEMessage, createDefineMock, createSSEStream, defineMock, defineMockData } from "./helper.js";
|
|
3
|
+
import { a as processMockData, c as MockCompiler, d as createLogger, f as logLevels, i as createMockMiddleware, l as ResolvePluginOptions, n as mockWebSocket, o as processRawData, r as CreateMockMiddlewareOptions, s as sortByValidator, t as MockSocketOptions, u as Logger } from "./server-pC78IYdH.js";
|
|
4
|
+
import { Compiler, DevServer, RspackPluginInstance } from "@rspack/core";
|
|
5
5
|
|
|
6
|
-
//#region src/rspack.d.ts
|
|
6
|
+
//#region src/core/rspack.d.ts
|
|
7
7
|
declare class MockServerPlugin implements RspackPluginInstance {
|
|
8
8
|
options: MockServerPluginOptions;
|
|
9
9
|
constructor(options?: MockServerPluginOptions);
|
|
10
10
|
apply(compiler: Compiler): void;
|
|
11
|
+
provideMiddleware(mockCompiler: MockCompiler, devServer: DevServer, options: ResolvePluginOptions): void;
|
|
12
|
+
provideProxyConfig(options: ResolvePluginOptions, devServer: DevServer): void;
|
|
13
|
+
resolvePluginOptions(compiler: Compiler, options?: MockServerPluginOptions): ResolvePluginOptions;
|
|
11
14
|
}
|
|
12
15
|
//#endregion
|
|
13
|
-
export {
|
|
16
|
+
export { BodyParserOptions, CookiesOption, CreateMockMiddlewareOptions, DefineMockDataOptions, ExtraRequest, type FormidableFile, GetCookieOption, HandleFunction, HeaderStream, Headers, HttpProxyPlugin, LogLevel, LogType, Logger, Method, MockData, MockErrorConfig, type MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, type MockOptions, type MockRequest, MockResponse, MockServerPlugin, type MockServerPluginOptions, MockSocketOptions, type MockWebsocketItem, NextFunction, NextHandleFunction, PathFilter, RecordOptions, RecordedMeta, RecordedReq, RecordedRequest, RecordedRes, ResolvedRecordOptions, ResponseBody, SSEMessage, ServerBuildOption, SetCookieOption, SimpleHandleFunction, WebSocketSetupContext, createDefineMock, createLogger, createMockMiddleware, createSSEStream, defineMock, defineMockData, logLevels, mockWebSocket, processMockData, processRawData, sortByValidator };
|
package/dist/index.js
CHANGED
|
@@ -1,84 +1,87 @@
|
|
|
1
|
+
import { a as rewriteRequest, c as processRawData, i as createMockMiddleware, l as sortByValidator, n as logLevels, o as Recorder, r as mockWebSocket, s as processMockData, t as createLogger, u as waitingFor } from "./logger-CzeuHKAL.js";
|
|
2
|
+
import { n as buildMockServer, r as createMockCompiler, t as resolvePluginOptions } from "./options-CI0LRpJZ.js";
|
|
1
3
|
import { createDefineMock, createSSEStream, defineMock, defineMockData } from "./helper.js";
|
|
2
|
-
import { a as processRawData, d as logLevels, i as processMockData, n as baseMiddleware, o as sortByValidator, r as rewriteRequest, s as waitingFor, t as mockWebSocket, u as createLogger } from "./ws-BcLCWVaK.js";
|
|
3
|
-
import { n as buildMockServer, r as createMockCompiler, t as resolvePluginOptions$1 } from "./options-Bd0PSZeT.js";
|
|
4
4
|
import "./server.js";
|
|
5
|
-
import { isFunction, isString, toArray, toTruthy } from "@pengzhanbo/utils";
|
|
6
5
|
import path from "node:path";
|
|
7
6
|
import process from "node:process";
|
|
7
|
+
import { isFunction, isString, toArray, toTruthy } from "@pengzhanbo/utils";
|
|
8
8
|
import rspack from "@rspack/core";
|
|
9
|
-
//#region src/rspack.ts
|
|
9
|
+
//#region src/core/rspack.ts
|
|
10
10
|
const PLUGIN_NAME = "rspack-plugin-mock";
|
|
11
11
|
var MockServerPlugin = class {
|
|
12
12
|
constructor(options = {}) {
|
|
13
13
|
this.options = options;
|
|
14
14
|
}
|
|
15
15
|
apply(compiler) {
|
|
16
|
+
if (this.options.enabled === false) return;
|
|
16
17
|
const compilerOptions = compiler.options;
|
|
17
|
-
const options = resolvePluginOptions(compiler, this.options);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
18
|
+
const options = this.resolvePluginOptions(compiler, this.options);
|
|
19
|
+
const isProd = process.env.NODE_ENV === "production";
|
|
20
|
+
if (isProd && options.build !== false) compiler.hooks.afterEmit.tap(PLUGIN_NAME, () => buildMockServer(options, compilerOptions.output.path || path.resolve(process.cwd(), "dist")));
|
|
21
|
+
if (isProd) return;
|
|
22
|
+
if (compilerOptions.devServer === false) {
|
|
23
|
+
options.logger.warn(`Not find devServer, ${PLUGIN_NAME} was disabled`);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const devServer = compilerOptions.devServer ||= {};
|
|
27
|
+
const mockCompiler = createMockCompiler(options);
|
|
28
|
+
this.provideMiddleware(mockCompiler, devServer, options);
|
|
29
|
+
this.provideProxyConfig(options, devServer);
|
|
30
|
+
compiler.hooks.watchRun.tap(PLUGIN_NAME, () => mockCompiler.run());
|
|
31
|
+
compiler.hooks.watchClose.tap(PLUGIN_NAME, () => mockCompiler.close());
|
|
32
|
+
}
|
|
33
|
+
provideMiddleware(mockCompiler, devServer, options) {
|
|
34
|
+
const setupMiddlewares = devServer.setupMiddlewares;
|
|
35
|
+
const waitServerForMockWebSocket = waitingFor((server) => {
|
|
36
|
+
mockWebSocket(mockCompiler, server, options);
|
|
37
|
+
});
|
|
38
|
+
devServer.setupMiddlewares = function(middlewares, devServer) {
|
|
39
|
+
middlewares = setupMiddlewares?.(middlewares, devServer) || middlewares;
|
|
40
|
+
middlewares.unshift(createMockMiddleware(mockCompiler, options));
|
|
41
|
+
if (options.reload) mockCompiler.on("update", () => {
|
|
42
|
+
if (devServer.webSocketServer?.clients) devServer.sendMessage(devServer.webSocketServer.clients, "static-changed");
|
|
27
43
|
});
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
return true;
|
|
50
|
-
}).map((item) => {
|
|
51
|
-
if (!isFunction(item) && !item.ws) {
|
|
52
|
-
const on = item.on ??= {};
|
|
53
|
-
const onProxyReq = on.proxyReq;
|
|
54
|
-
on.proxyReq = (proxyReq, req, ...args) => {
|
|
55
|
-
onProxyReq?.(proxyReq, req, ...args);
|
|
56
|
-
rewriteRequest(proxyReq, req);
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
return item;
|
|
60
|
-
});
|
|
44
|
+
/**
|
|
45
|
+
* 在 @rspack/dev-server 中, setupMiddlewares 优先于 createServer
|
|
46
|
+
* 执行,需要等待 server 启动后再注入 mock websocket
|
|
47
|
+
*/
|
|
48
|
+
waitServerForMockWebSocket(() => devServer.server);
|
|
49
|
+
return middlewares;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
provideProxyConfig(options, devServer) {
|
|
53
|
+
if (!devServer.proxy?.length) return;
|
|
54
|
+
const wsPrefix = toArray(options.wsPrefix);
|
|
55
|
+
let recorder = null;
|
|
56
|
+
if (options.record.enabled) recorder = new Recorder(options.record);
|
|
57
|
+
devServer.proxy = devServer.proxy.filter((item) => {
|
|
58
|
+
if (!isFunction(item) && item.ws === true && wsPrefix.length) return !toArray(item.pathFilter || item.context).filter(isString).some((context) => wsPrefix.includes(context));
|
|
59
|
+
return true;
|
|
60
|
+
}).map((item) => {
|
|
61
|
+
if (!isFunction(item) && !item.ws) {
|
|
62
|
+
const plugins = item.plugins ??= [];
|
|
63
|
+
plugins.push((proxyServer) => proxyServer.on("proxyReq", rewriteRequest));
|
|
64
|
+
if (options.record.enabled && recorder) plugins.push(recorder.getPlugin());
|
|
61
65
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
66
|
+
return item;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
resolvePluginOptions(compiler, options = {}) {
|
|
70
|
+
const compilerOptions = compiler.options;
|
|
71
|
+
const alias = compilerOptions.resolve?.alias || {};
|
|
72
|
+
const context = compilerOptions.context;
|
|
73
|
+
const definePluginInstance = compilerOptions.plugins?.find((plugin) => plugin instanceof rspack.DefinePlugin);
|
|
74
|
+
const proxies = ((compilerOptions.devServer || {}).proxy || []).flatMap((item) => {
|
|
75
|
+
if (!isFunction(item) && !item.ws) return item.pathFilter || item.context;
|
|
76
|
+
return [];
|
|
77
|
+
}).filter(toTruthy);
|
|
78
|
+
return resolvePluginOptions(options, {
|
|
79
|
+
alias,
|
|
80
|
+
context,
|
|
81
|
+
plugins: toArray(definePluginInstance),
|
|
82
|
+
proxies
|
|
83
|
+
});
|
|
65
84
|
}
|
|
66
85
|
};
|
|
67
|
-
function resolvePluginOptions(compiler, options = {}) {
|
|
68
|
-
const compilerOptions = compiler.options;
|
|
69
|
-
const alias = compilerOptions.resolve?.alias || {};
|
|
70
|
-
const context = compilerOptions.context;
|
|
71
|
-
const definePluginInstance = compilerOptions.plugins?.find((plugin) => plugin instanceof rspack.DefinePlugin);
|
|
72
|
-
const proxies = ((compilerOptions.devServer || {}).proxy || []).flatMap((item) => {
|
|
73
|
-
if (!isFunction(item) && !item.ws) return item.pathFilter || item.context;
|
|
74
|
-
return [];
|
|
75
|
-
}).filter(toTruthy);
|
|
76
|
-
return resolvePluginOptions$1(options, {
|
|
77
|
-
alias,
|
|
78
|
-
context,
|
|
79
|
-
plugins: toArray(definePluginInstance),
|
|
80
|
-
proxies
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
86
|
//#endregion
|
|
84
|
-
export { MockServerPlugin,
|
|
87
|
+
export { MockServerPlugin, createDefineMock, createLogger, createMockMiddleware, createSSEStream, defineMock, defineMockData, logLevels, mockWebSocket, processMockData, processRawData, sortByValidator };
|
package/dist/json5-loader.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import JSON5 from "json5";
|
|
2
2
|
//#region src/json5-loader.ts
|
|
3
|
-
function
|
|
3
|
+
function json5Loader(content) {
|
|
4
4
|
if (!content) return "export default {}";
|
|
5
5
|
return `export default ${JSON.stringify(JSON5.parse(content))}`;
|
|
6
6
|
}
|
|
7
7
|
//#endregion
|
|
8
|
-
export {
|
|
8
|
+
export { json5Loader as default };
|