rspack-plugin-mock 2.0.0 → 2.2.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 +124 -87
- package/dist/helper.js +45 -71
- package/dist/index-BjZK24gW.d.ts +1065 -0
- package/dist/index.d.ts +9 -7
- package/dist/index.js +78 -68
- package/dist/json5-loader.mjs +2 -2
- package/dist/{ws-BcLCWVaK.js → logger-DTZcb53u.js} +519 -432
- package/dist/{options-Bd0PSZeT.js → options-C3NAM6gv.js} +59 -27
- package/dist/rsbuild.d.ts +4 -4
- package/dist/rsbuild.js +30 -42
- package/dist/server-DurHqymI.d.ts +97 -0
- package/dist/server.d.ts +2 -2
- package/dist/server.js +2 -2
- package/package.json +38 -51
- package/dist/server-_doRwsZm.d.ts +0 -89
- package/dist/types-GT6M6WuI.d.ts +0 -701
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
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";
|
|
5
|
-
|
|
6
|
-
//#region src/rspack.d.ts
|
|
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-BjZK24gW.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-DurHqymI.js";
|
|
4
|
+
import { Compiler, DevServer, RspackPluginInstance } from "@rspack/core";
|
|
5
|
+
//#region src/core/rspack.d.ts
|
|
7
6
|
declare class MockServerPlugin implements RspackPluginInstance {
|
|
8
7
|
options: MockServerPluginOptions;
|
|
9
8
|
constructor(options?: MockServerPluginOptions);
|
|
10
9
|
apply(compiler: Compiler): void;
|
|
10
|
+
provideMiddleware(mockCompiler: MockCompiler, devServer: DevServer, options: ResolvePluginOptions): void;
|
|
11
|
+
provideProxyConfig(options: ResolvePluginOptions, devServer: DevServer): void;
|
|
12
|
+
resolvePluginOptions(compiler: Compiler, options?: MockServerPluginOptions): ResolvePluginOptions;
|
|
11
13
|
}
|
|
12
14
|
//#endregion
|
|
13
|
-
export {
|
|
15
|
+
export { type BodyParserOptions, type CookiesOption, CreateMockMiddlewareOptions, DefineMockDataOptions, type ExtraRequest, type FormidableFile, type GetCookieOption, type HandleFunction, HeaderStream, type Headers, type HttpProxyPlugin, type LogLevel, type LogType, Logger, type Method, MockData, type MockErrorConfig, type MockHttpItem, type MockMatchPriority, type MockMatchSpecialPriority, type MockOptions, type MockRequest, type MockResponse, MockServerPlugin, type MockServerPluginOptions, MockSocketOptions, type MockWebsocketItem, type NextFunction, type NextHandleFunction, type PathFilter, type RecordOptions, type RecordedMeta, type RecordedReq, type RecordedRequest, type RecordedRes, type ResolvedRecordOptions, type ResponseBody, SSEMessage, type ServerBuildOption, type SetCookieOption, type SimpleHandleFunction, type WebSocketSetupContext, createDefineMock, createLogger, createMockMiddleware, createSSEStream, defineMock, defineMockData, logLevels, mockWebSocket, processMockData, processRawData, sortByValidator };
|
package/dist/index.js
CHANGED
|
@@ -1,84 +1,94 @@
|
|
|
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 } from "./logger-DTZcb53u.js";
|
|
2
|
+
import { n as buildMockServer, r as createMockCompiler, t as resolvePluginOptions } from "./options-C3NAM6gv.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
|
-
|
|
9
|
+
|
|
10
|
+
function waitingFor(onSuccess, maxRetry = 5) {
|
|
11
|
+
return function wait(getter, retry = 0) {
|
|
12
|
+
const value = getter();
|
|
13
|
+
if (value) onSuccess(value);
|
|
14
|
+
else if (retry < maxRetry) setTimeout(() => wait(getter, retry + 1), 100);
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
|
|
10
19
|
const PLUGIN_NAME = "rspack-plugin-mock";
|
|
11
20
|
var MockServerPlugin = class {
|
|
21
|
+
options;
|
|
12
22
|
constructor(options = {}) {
|
|
13
23
|
this.options = options;
|
|
14
24
|
}
|
|
15
25
|
apply(compiler) {
|
|
26
|
+
if (this.options.enabled === false) return;
|
|
16
27
|
const compilerOptions = compiler.options;
|
|
17
|
-
const options = resolvePluginOptions(compiler, this.options);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
28
|
+
const options = this.resolvePluginOptions(compiler, this.options);
|
|
29
|
+
const isProd = process.env.NODE_ENV === "production";
|
|
30
|
+
if (isProd && options.build !== false) compiler.hooks.afterEmit.tap(PLUGIN_NAME, () => buildMockServer(options, compilerOptions.output.path || path.resolve(process.cwd(), "dist")));
|
|
31
|
+
if (isProd) return;
|
|
32
|
+
if (compilerOptions.devServer === false) {
|
|
33
|
+
options.logger.warn(`Not find devServer, ${PLUGIN_NAME} was disabled`);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const devServer = compilerOptions.devServer ||= {};
|
|
37
|
+
const mockCompiler = createMockCompiler(options);
|
|
38
|
+
this.provideMiddleware(mockCompiler, devServer, options);
|
|
39
|
+
this.provideProxyConfig(options, devServer);
|
|
40
|
+
compiler.hooks.watchRun.tap(PLUGIN_NAME, () => mockCompiler.run());
|
|
41
|
+
compiler.hooks.watchClose.tap(PLUGIN_NAME, () => mockCompiler.close());
|
|
42
|
+
}
|
|
43
|
+
provideMiddleware(mockCompiler, devServer, options) {
|
|
44
|
+
const setupMiddlewares = devServer.setupMiddlewares;
|
|
45
|
+
const waitServerForMockWebSocket = waitingFor((server) => {
|
|
46
|
+
mockWebSocket(mockCompiler, server, options);
|
|
47
|
+
});
|
|
48
|
+
devServer.setupMiddlewares = function(middlewares, devServer) {
|
|
49
|
+
middlewares = setupMiddlewares?.(middlewares, devServer) || middlewares;
|
|
50
|
+
middlewares.unshift(createMockMiddleware(mockCompiler, options));
|
|
51
|
+
if (options.reload) mockCompiler.on("update", () => {
|
|
52
|
+
if (devServer.webSocketServer?.clients) devServer.sendMessage(devServer.webSocketServer.clients, "static-changed");
|
|
27
53
|
});
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const proxy = compilerOptions.devServer.proxy;
|
|
47
|
-
compilerOptions.devServer.proxy = proxy.filter((item) => {
|
|
48
|
-
if (!isFunction(item) && item.ws === true && wsPrefix.length) return !toArray(item.pathFilter || item.context).filter(isString).some((context) => wsPrefix.includes(context));
|
|
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
|
-
});
|
|
54
|
+
|
|
55
|
+
waitServerForMockWebSocket(() => devServer.server);
|
|
56
|
+
return middlewares;
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
provideProxyConfig(options, devServer) {
|
|
60
|
+
if (!devServer.proxy?.length) return;
|
|
61
|
+
const wsPrefix = toArray(options.wsPrefix);
|
|
62
|
+
let recorder = null;
|
|
63
|
+
if (options.record.enabled) recorder = new Recorder(options.record);
|
|
64
|
+
devServer.proxy = devServer.proxy.filter((item) => {
|
|
65
|
+
if (!isFunction(item) && item.ws === true && wsPrefix.length) return !toArray(item.pathFilter || item.context).filter(isString).some((context) => wsPrefix.includes(context));
|
|
66
|
+
return true;
|
|
67
|
+
}).map((item) => {
|
|
68
|
+
if (!isFunction(item) && !item.ws) {
|
|
69
|
+
const plugins = item.plugins ??= [];
|
|
70
|
+
plugins.push((proxyServer) => proxyServer.on("proxyReq", rewriteRequest));
|
|
71
|
+
if (options.record.enabled && recorder) plugins.push(recorder.getPlugin());
|
|
61
72
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
73
|
+
return item;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
resolvePluginOptions(compiler, options = {}) {
|
|
77
|
+
const compilerOptions = compiler.options;
|
|
78
|
+
const alias = compilerOptions.resolve?.alias || {};
|
|
79
|
+
const context = compilerOptions.context;
|
|
80
|
+
const definePluginInstance = compilerOptions.plugins?.find((plugin) => plugin instanceof rspack.DefinePlugin);
|
|
81
|
+
const proxies = ((compilerOptions.devServer || {}).proxy || []).flatMap((item) => {
|
|
82
|
+
if (!isFunction(item) && !item.ws) return item.pathFilter || item.context;
|
|
83
|
+
return [];
|
|
84
|
+
}).filter(toTruthy);
|
|
85
|
+
return resolvePluginOptions(options, {
|
|
86
|
+
alias,
|
|
87
|
+
context,
|
|
88
|
+
plugins: toArray(definePluginInstance),
|
|
89
|
+
proxies
|
|
90
|
+
});
|
|
65
91
|
}
|
|
66
92
|
};
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
//#endregion
|
|
84
|
-
export { MockServerPlugin, baseMiddleware, createDefineMock, createLogger, createSSEStream, defineMock, defineMockData, logLevels, mockWebSocket, processMockData, processRawData, sortByValidator };
|
|
93
|
+
|
|
94
|
+
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 };
|