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
|
@@ -1,20 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { createRequire } from "node:module";
|
|
3
|
-
import { attemptAsync, isBoolean, isPlainObject, toArray, uniq } from "@pengzhanbo/utils";
|
|
1
|
+
import { c as processRawData, d as vfs, f as getPackageDepList, m as createMatcher, p as getPackageDeps, s as processMockData, t as createLogger, u as normalizePath } from "./logger-DTZcb53u.js";
|
|
2
|
+
import { createRequire, isBuiltin } from "node:module";
|
|
4
3
|
import path from "node:path";
|
|
5
4
|
import process from "node:process";
|
|
5
|
+
import { attemptAsync, isBoolean, isPlainObject, toArray, uniq } from "@pengzhanbo/utils";
|
|
6
6
|
import * as rspackCore from "@rspack/core";
|
|
7
7
|
import fs, { promises } from "node:fs";
|
|
8
8
|
import fsp from "node:fs/promises";
|
|
9
9
|
import ansis from "ansis";
|
|
10
10
|
import { glob } from "tinyglobby";
|
|
11
|
-
import isCore from "is-core-module";
|
|
12
11
|
import { loadPackageJSONSync } from "local-pkg";
|
|
13
12
|
import { pathToFileURL } from "node:url";
|
|
14
13
|
import { createHash } from "node:crypto";
|
|
15
14
|
import EventEmitter from "node:events";
|
|
16
15
|
import chokidar from "chokidar";
|
|
17
|
-
|
|
16
|
+
|
|
18
17
|
const require = createRequire(import.meta.url);
|
|
19
18
|
function createCompiler(options, callback) {
|
|
20
19
|
const rspackOptions = resolveRspackOptions(options);
|
|
@@ -38,7 +37,7 @@ function createCompiler(options, callback) {
|
|
|
38
37
|
const aliasList = Object.keys(options.alias || {}).map((key) => key.replace(/\$$/g, ""));
|
|
39
38
|
for (const { name } of modules) if (name?.startsWith("external")) {
|
|
40
39
|
const packageName = normalizePackageName(name);
|
|
41
|
-
if (!
|
|
40
|
+
if (!isBuiltin(packageName) && !aliasList.includes(packageName)) externals.push(normalizePackageName(name));
|
|
42
41
|
}
|
|
43
42
|
}
|
|
44
43
|
await callback({
|
|
@@ -136,8 +135,8 @@ function resolveRspackOptions({ cwd, isEsm = true, entryFile, plugins, alias, wa
|
|
|
136
135
|
] }
|
|
137
136
|
};
|
|
138
137
|
}
|
|
139
|
-
|
|
140
|
-
|
|
138
|
+
|
|
139
|
+
|
|
141
140
|
async function loadFromCode({ filepath, code, isESM, cwd }) {
|
|
142
141
|
filepath = path.resolve(cwd, filepath);
|
|
143
142
|
const ext = isESM ? ".mjs" : ".cjs";
|
|
@@ -154,12 +153,13 @@ async function importDefault(filepath) {
|
|
|
154
153
|
function getHash(str) {
|
|
155
154
|
return createHash("md5").update(str).digest("hex");
|
|
156
155
|
}
|
|
157
|
-
|
|
158
|
-
|
|
156
|
+
|
|
157
|
+
|
|
159
158
|
function createMockCompiler(options) {
|
|
160
159
|
return new MockCompiler(options);
|
|
161
160
|
}
|
|
162
161
|
var MockCompiler = class extends EventEmitter {
|
|
162
|
+
options;
|
|
163
163
|
cwd;
|
|
164
164
|
mockWatcher;
|
|
165
165
|
entryFile;
|
|
@@ -269,12 +269,12 @@ var MockCompiler = class extends EventEmitter {
|
|
|
269
269
|
});
|
|
270
270
|
}
|
|
271
271
|
};
|
|
272
|
-
|
|
273
|
-
|
|
272
|
+
|
|
273
|
+
|
|
274
274
|
var name = "rspack-plugin-mock";
|
|
275
|
-
var version = "2.
|
|
276
|
-
|
|
277
|
-
|
|
275
|
+
var version = "2.2.0";
|
|
276
|
+
|
|
277
|
+
|
|
278
278
|
function generatePackageJson(options, externals) {
|
|
279
279
|
const deps = getPackageDeps(options.context);
|
|
280
280
|
const exclude = [
|
|
@@ -297,8 +297,8 @@ function generatePackageJson(options, externals) {
|
|
|
297
297
|
});
|
|
298
298
|
return JSON.stringify(mockPkg, null, 2);
|
|
299
299
|
}
|
|
300
|
-
|
|
301
|
-
|
|
300
|
+
|
|
301
|
+
|
|
302
302
|
function generatorServerEntryCode({ proxies, wsPrefix, cookiesOptions, bodyParserOptions, priority, build }) {
|
|
303
303
|
const { serverPort, log } = build;
|
|
304
304
|
return `import { createServer } from 'node:http';
|
|
@@ -343,8 +343,8 @@ server.listen(${serverPort});
|
|
|
343
343
|
console.log('listen: http://localhost:${serverPort}');
|
|
344
344
|
`;
|
|
345
345
|
}
|
|
346
|
-
|
|
347
|
-
|
|
346
|
+
|
|
347
|
+
|
|
348
348
|
async function writeMockEntryFile(entryFile, files, cwd, dir) {
|
|
349
349
|
const importers = [];
|
|
350
350
|
const exporters = [];
|
|
@@ -359,9 +359,10 @@ async function writeMockEntryFile(entryFile, files, cwd, dir) {
|
|
|
359
359
|
if (!fs.existsSync(dirname)) await fsp.mkdir(dirname, { recursive: true });
|
|
360
360
|
await fsp.writeFile(entryFile, code, "utf8");
|
|
361
361
|
}
|
|
362
|
-
|
|
363
|
-
|
|
362
|
+
|
|
363
|
+
|
|
364
364
|
async function buildMockServer(options, outputDir) {
|
|
365
|
+
const buildOptions = options.build;
|
|
365
366
|
const entryFile = path.resolve(process.cwd(), "node_modules/.cache/mock-server/mock-server.ts");
|
|
366
367
|
const { pattern, ignore } = createMatcher(options.include, options.exclude);
|
|
367
368
|
await writeMockEntryFile(entryFile, await glob(pattern, {
|
|
@@ -389,6 +390,16 @@ async function buildMockServer(options, outputDir) {
|
|
|
389
390
|
source: generatePackageJson(options, externals)
|
|
390
391
|
}
|
|
391
392
|
];
|
|
393
|
+
if (options.record.enabled && buildOptions.includeRecord) {
|
|
394
|
+
const files = await glob(path.join(options.record.dir, "**/*.json"), {
|
|
395
|
+
cwd: options.cwd,
|
|
396
|
+
dot: true
|
|
397
|
+
});
|
|
398
|
+
for (const file of files) outputList.push({
|
|
399
|
+
filename: path.join(outputDir, file),
|
|
400
|
+
source: await fsp.readFile(path.join(options.cwd, file), "utf-8")
|
|
401
|
+
});
|
|
402
|
+
}
|
|
392
403
|
const dist = path.resolve(outputDir, options.build.dist);
|
|
393
404
|
options.logger.info(`${ansis.green("✓")} generate mock server in ${ansis.cyan(path.relative(process.cwd(), dist))}`);
|
|
394
405
|
if (!fs.existsSync(dist)) await fsp.mkdir(dist, { recursive: true });
|
|
@@ -399,9 +410,9 @@ async function buildMockServer(options, outputDir) {
|
|
|
399
410
|
options.logger.info(` ${ansis.green(filename)}${space}${ansis.bold.dim(`${sourceSize} kB`)}`);
|
|
400
411
|
}
|
|
401
412
|
}
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
function resolvePluginOptions({ prefix = [], wsPrefix = [], cwd, dir = "mock", include = ["**/*.mock.{js,ts,cjs,mjs,json,json5}"], exclude = [], reload = false, log = "info", cors = true, formidableOptions = {}, build = false, cookiesOptions = {}, bodyParserOptions = {}, priority = {} }, { alias, context, plugins, proxies: rawProxies }) {
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
function resolvePluginOptions({ prefix = [], wsPrefix = [], cwd, dir = "mock", include = ["**/*.mock.{js,ts,cjs,mjs,json,json5}"], exclude = [], reload = false, log = "info", cors = true, formidableOptions = {}, build = false, cookiesOptions = {}, bodyParserOptions = {}, priority = {}, activeScene = [], record = false, replay }, { alias, context, plugins, proxies: rawProxies }) {
|
|
405
416
|
const logger = createLogger("rspack:mock", isBoolean(log) ? log ? "info" : "error" : log);
|
|
406
417
|
const proxies = [...toArray(prefix), ...rawProxies];
|
|
407
418
|
const wsProxies = toArray(wsPrefix);
|
|
@@ -412,10 +423,13 @@ function resolvePluginOptions({ prefix = [], wsPrefix = [], cwd, dir = "mock", i
|
|
|
412
423
|
...corsOptions,
|
|
413
424
|
...cors
|
|
414
425
|
};
|
|
426
|
+
cwd = cwd || context || process.cwd();
|
|
427
|
+
const resolvedRecord = resolveRecordOptions(cwd, dir, record);
|
|
415
428
|
return {
|
|
429
|
+
enabled: true,
|
|
416
430
|
prefix,
|
|
417
431
|
wsPrefix,
|
|
418
|
-
cwd
|
|
432
|
+
cwd,
|
|
419
433
|
dir,
|
|
420
434
|
include,
|
|
421
435
|
exclude,
|
|
@@ -439,8 +453,26 @@ function resolvePluginOptions({ prefix = [], wsPrefix = [], cwd, dir = "mock", i
|
|
|
439
453
|
plugins,
|
|
440
454
|
proxies,
|
|
441
455
|
wsProxies,
|
|
442
|
-
logger
|
|
456
|
+
logger,
|
|
457
|
+
activeScene: toArray(activeScene),
|
|
458
|
+
record: resolvedRecord,
|
|
459
|
+
replay: replay ?? resolvedRecord.enabled ?? false
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
function resolveRecordOptions(cwd, dir, record) {
|
|
464
|
+
const recordOptions = typeof record === "boolean" ? { enabled: record } : record;
|
|
465
|
+
const expires = recordOptions?.expires ?? 0;
|
|
466
|
+
return {
|
|
467
|
+
enabled: recordOptions?.enabled ?? false,
|
|
468
|
+
cwd,
|
|
469
|
+
dir: path.join(dir, recordOptions?.dir || ".recordings"),
|
|
470
|
+
overwrite: recordOptions?.overwrite ?? true,
|
|
471
|
+
status: toArray(recordOptions?.status).map(Number),
|
|
472
|
+
expires: expires === 0 ? Number.MAX_SAFE_INTEGER : expires * 1e3,
|
|
473
|
+
gitignore: recordOptions?.gitignore ?? true,
|
|
474
|
+
filter: recordOptions?.filter || (() => true)
|
|
443
475
|
};
|
|
444
476
|
}
|
|
445
|
-
|
|
477
|
+
|
|
446
478
|
export { buildMockServer as n, createMockCompiler as r, resolvePluginOptions as t };
|
package/dist/rsbuild.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { _ as
|
|
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";
|
|
2
3
|
import { RsbuildPlugin } from "@rsbuild/core";
|
|
3
|
-
|
|
4
|
-
//#region src/rsbuild.d.ts
|
|
4
|
+
//#region src/core/rsbuild.d.ts
|
|
5
5
|
declare function pluginMockServer(options?: MockServerPluginOptions): RsbuildPlugin;
|
|
6
6
|
//#endregion
|
|
7
|
-
export { BodyParserOptions, ExtraRequest, FormidableFile, LogLevel, LogType, Method, MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, MockOptions, MockRequest, MockResponse, MockServerPluginOptions, MockWebsocketItem, ResponseBody, ServerBuildOption, WebSocketSetupContext, pluginMockServer };
|
|
7
|
+
export { type BodyParserOptions, type CookiesOption, DefineMockDataOptions, type ExtraRequest, type FormidableFile, type GetCookieOption, type HandleFunction, HeaderStream, type Headers, type HttpProxyPlugin, type LogLevel, type LogType, type Method, MockData, type MockErrorConfig, type MockHttpItem, type MockMatchPriority, type MockMatchSpecialPriority, type MockOptions, type MockRequest, type MockResponse, type MockServerPluginOptions, 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, createSSEStream, defineMock, defineMockData, pluginMockServer };
|
package/dist/rsbuild.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { n as buildMockServer, r as createMockCompiler, t as resolvePluginOptions } from "./options-
|
|
3
|
-
import {
|
|
1
|
+
import { a as rewriteRequest, i as createMockMiddleware, o as Recorder, r as mockWebSocket } from "./logger-DTZcb53u.js";
|
|
2
|
+
import { n as buildMockServer, r as createMockCompiler, t as resolvePluginOptions } from "./options-C3NAM6gv.js";
|
|
3
|
+
import { createDefineMock, createSSEStream, defineMock, defineMockData } from "./helper.js";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import process from "node:process";
|
|
6
|
+
import { isArray, objectEntries, toArray } from "@pengzhanbo/utils";
|
|
6
7
|
import rspack from "@rspack/core";
|
|
7
|
-
|
|
8
|
-
import { Socket } from "node:net";
|
|
9
|
-
//#region src/rsbuild.ts
|
|
8
|
+
|
|
10
9
|
function pluginMockServer(options = {}) {
|
|
11
10
|
return {
|
|
12
11
|
name: "plugin-mock-server",
|
|
13
12
|
setup(api) {
|
|
13
|
+
if (options.enabled === false) return;
|
|
14
14
|
const rsbuildConfig = api.getRsbuildConfig();
|
|
15
15
|
const resolvedOptions = resolvePluginOptions(options, {
|
|
16
16
|
proxies: resolveConfigProxies(rsbuildConfig),
|
|
@@ -32,9 +32,29 @@ function pluginMockServer(options = {}) {
|
|
|
32
32
|
});
|
|
33
33
|
else mockCompiler.updateAlias(compiler.options.resolve?.alias || {});
|
|
34
34
|
});
|
|
35
|
-
|
|
35
|
+
let recorder = null;
|
|
36
|
+
if (resolvedOptions.record.enabled) recorder = new Recorder(resolvedOptions.record);
|
|
37
|
+
function wrapProxyOptions(options) {
|
|
38
|
+
const plugins = options.plugins ??= [];
|
|
39
|
+
plugins.push((proxyServer) => proxyServer.on("proxyReq", rewriteRequest));
|
|
40
|
+
if (resolvedOptions.record.enabled && recorder) plugins.push(recorder.getPlugin());
|
|
41
|
+
return options;
|
|
42
|
+
}
|
|
43
|
+
api.modifyRsbuildConfig((config) => {
|
|
44
|
+
if (!config.server?.proxy) return;
|
|
45
|
+
if (isArray(config.server.proxy)) config.server.proxy = config.server.proxy.map((item) => item.ws ? item : wrapProxyOptions(item));
|
|
46
|
+
else if (config.server.proxy) {
|
|
47
|
+
const proxy = config.server.proxy;
|
|
48
|
+
Object.keys(proxy).forEach((key) => {
|
|
49
|
+
const target = proxy[key];
|
|
50
|
+
const options = typeof target === "string" ? { target } : target;
|
|
51
|
+
if (options.ws) return;
|
|
52
|
+
proxy[key] = wrapProxyOptions(options);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
});
|
|
36
56
|
function initMockServer(server) {
|
|
37
|
-
server.middlewares.use(
|
|
57
|
+
server.middlewares.use(createMockMiddleware(mockCompiler, resolvedOptions));
|
|
38
58
|
if (resolvedOptions.reload && api.context.action === "dev") mockCompiler.on("update", () => server.sockWrite("static-changed"));
|
|
39
59
|
if (toArray(resolvedOptions.wsPrefix).length > 0) mockWebSocket(mockCompiler, server.httpServer, resolvedOptions);
|
|
40
60
|
mockCompiler.run();
|
|
@@ -45,38 +65,6 @@ function pluginMockServer(options = {}) {
|
|
|
45
65
|
}
|
|
46
66
|
};
|
|
47
67
|
}
|
|
48
|
-
function onProxyError(err, _req, res) {
|
|
49
|
-
console.error(ansis.red(err?.stack || err.message));
|
|
50
|
-
if (!(res instanceof Socket)) res.statusCode = 500;
|
|
51
|
-
res.end();
|
|
52
|
-
}
|
|
53
|
-
function wrapProxyOptions(options) {
|
|
54
|
-
const { on, ...rest } = options;
|
|
55
|
-
return {
|
|
56
|
-
...rest,
|
|
57
|
-
on: {
|
|
58
|
-
...on,
|
|
59
|
-
proxyReq: (proxyReq, req, ...args) => {
|
|
60
|
-
on?.proxyReq?.(proxyReq, req, ...args);
|
|
61
|
-
rewriteRequest(proxyReq, req);
|
|
62
|
-
},
|
|
63
|
-
error: on?.error || onProxyError
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
function updateServerProxyConfigByHttpMock(config) {
|
|
68
|
-
if (!config.server?.proxy) return;
|
|
69
|
-
if (isArray(config.server.proxy)) config.server.proxy = config.server.proxy.map((item) => item.ws ? item : wrapProxyOptions(item));
|
|
70
|
-
else if (config.server.proxy) {
|
|
71
|
-
const proxy = config.server.proxy;
|
|
72
|
-
Object.keys(proxy).forEach((key) => {
|
|
73
|
-
const target = proxy[key];
|
|
74
|
-
const options = typeof target === "string" ? { target } : target;
|
|
75
|
-
if (options.ws) return;
|
|
76
|
-
proxy[key] = wrapProxyOptions(options);
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
68
|
function resolveConfigProxies(config) {
|
|
81
69
|
config.server ??= {};
|
|
82
70
|
const proxy = config.server.proxy ??= {};
|
|
@@ -88,5 +76,5 @@ function resolveConfigProxies(config) {
|
|
|
88
76
|
});
|
|
89
77
|
return proxies;
|
|
90
78
|
}
|
|
91
|
-
|
|
92
|
-
export { pluginMockServer };
|
|
79
|
+
|
|
80
|
+
export { createDefineMock, createSSEStream, defineMock, defineMockData, pluginMockServer };
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { a as MockWebsocketItem, c as MockHttpItem, h as ServerBuildOption, i as MockOptions, k as NextHandleFunction, m as MockServerPluginOptions, r as PathFilter, u as LogLevel, x as ResolvedRecordOptions } from "./index-BjZK24gW.js";
|
|
2
|
+
import { Arrayable } from "@pengzhanbo/utils";
|
|
3
|
+
import { Compiler, RspackPluginInstance } from "@rspack/core";
|
|
4
|
+
import { Matcher } from "picomatch";
|
|
5
|
+
import EventEmitter from "node:events";
|
|
6
|
+
import { FSWatcher } from "chokidar";
|
|
7
|
+
import { CorsOptions } from "cors";
|
|
8
|
+
import { Server } from "node:http";
|
|
9
|
+
import { Http2SecureServer } from "node:http2";
|
|
10
|
+
//#region src/core/logger.d.ts
|
|
11
|
+
interface Logger {
|
|
12
|
+
debug: (msg: string, level?: boolean | LogLevel) => void;
|
|
13
|
+
info: (msg: string, level?: boolean | LogLevel) => void;
|
|
14
|
+
warn: (msg: string, level?: boolean | LogLevel) => void;
|
|
15
|
+
error: (msg: string, level?: boolean | LogLevel) => void;
|
|
16
|
+
}
|
|
17
|
+
declare const logLevels: Record<LogLevel, number>;
|
|
18
|
+
declare function createLogger(prefix: string, defaultLevel?: LogLevel): Logger;
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/core/options.d.ts
|
|
21
|
+
interface ResolvedCompilerOptions {
|
|
22
|
+
alias: Record<string, Arrayable<false | string>>;
|
|
23
|
+
proxies: PathFilter[];
|
|
24
|
+
wsProxies: PathFilter[];
|
|
25
|
+
plugins: RspackPluginInstance[];
|
|
26
|
+
context?: string;
|
|
27
|
+
}
|
|
28
|
+
type ResolvePluginOptions = Required<Omit<MockServerPluginOptions, "build">> & ResolvedCompilerOptions & {
|
|
29
|
+
logger: Logger;
|
|
30
|
+
build: false | ServerBuildOption;
|
|
31
|
+
cors: false | CorsOptions;
|
|
32
|
+
record: ResolvedRecordOptions;
|
|
33
|
+
activeScene: string[];
|
|
34
|
+
};
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/compiler/mockCompiler.d.ts
|
|
37
|
+
declare class MockCompiler extends EventEmitter {
|
|
38
|
+
options: ResolvePluginOptions;
|
|
39
|
+
cwd: string;
|
|
40
|
+
mockWatcher: FSWatcher;
|
|
41
|
+
entryFile: string;
|
|
42
|
+
deps: string[];
|
|
43
|
+
isESM: boolean;
|
|
44
|
+
private _mockData;
|
|
45
|
+
private watchInfo?;
|
|
46
|
+
compiler?: Compiler | null;
|
|
47
|
+
constructor(options: ResolvePluginOptions);
|
|
48
|
+
get mockData(): Record<string, MockOptions>;
|
|
49
|
+
run(): Promise<void>;
|
|
50
|
+
close(): void;
|
|
51
|
+
updateAlias(alias: Record<string, false | string | (string | false)[]>): void;
|
|
52
|
+
updateMockEntry(): Promise<void>;
|
|
53
|
+
watchMockFiles(isMatch: Matcher): void;
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/compiler/processData.d.ts
|
|
57
|
+
declare function processRawData(rawData: (readonly [any, string])[]): (MockHttpItem | MockWebsocketItem | MockOptions)[];
|
|
58
|
+
declare function processMockData(mockList: (MockHttpItem | MockWebsocketItem | MockOptions)[]): Record<string, MockOptions>;
|
|
59
|
+
declare function sortByValidator(mocks: MockOptions): (MockHttpItem | MockWebsocketItem)[];
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region src/mockHttp/middleware.d.ts
|
|
62
|
+
interface CreateMockMiddlewareOptions extends Pick<ResolvePluginOptions, "formidableOptions" | "cookiesOptions" | "bodyParserOptions" | "priority" | "record" | "replay" | "activeScene"> {
|
|
63
|
+
proxies: PathFilter[];
|
|
64
|
+
logger: Logger;
|
|
65
|
+
cors: false | CorsOptions;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Create mock middleware
|
|
69
|
+
*
|
|
70
|
+
* 创建 Mock 中间件
|
|
71
|
+
*
|
|
72
|
+
* @param compiler - Compiler instance / 编译器实例
|
|
73
|
+
* @param options - Middleware options / 中间件配置项
|
|
74
|
+
* @param options.formidableOptions - Formidable options / Formidable 配置项
|
|
75
|
+
* @param options.bodyParserOptions - Body parser options / 请求体解析配置项
|
|
76
|
+
* @param options.proxies - Proxy paths / 代理路径
|
|
77
|
+
* @param options.cookiesOptions - Cookies options / Cookies 配置项
|
|
78
|
+
* @param options.logger - Logger instance / 日志实例
|
|
79
|
+
* @param options.priority - Path matching priority / 路径匹配优先级
|
|
80
|
+
* @param options.cors - CORS options / CORS 配置项
|
|
81
|
+
* @param options.record - Record options / 录制配置项
|
|
82
|
+
* @param options.replay - Replay options / 回放配置项
|
|
83
|
+
* @param options.activeScene - Active scene / 活动场景
|
|
84
|
+
*
|
|
85
|
+
* @returns Connect middleware function / Connect 中间件函数
|
|
86
|
+
*/
|
|
87
|
+
declare function createMockMiddleware(compiler: MockCompiler, { formidableOptions, bodyParserOptions, proxies, cookiesOptions, logger, priority, cors: corsOptions, record, replay, activeScene }: CreateMockMiddlewareOptions): NextHandleFunction;
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/mockWebsocket/server.d.ts
|
|
90
|
+
interface MockSocketOptions {
|
|
91
|
+
wsProxies: PathFilter[];
|
|
92
|
+
cookiesOptions: MockServerPluginOptions["cookiesOptions"];
|
|
93
|
+
logger: Logger;
|
|
94
|
+
}
|
|
95
|
+
declare function mockWebSocket(compiler: MockCompiler, httpServer: Server | Http2SecureServer | null, { wsProxies: proxies, cookiesOptions, logger }: MockSocketOptions): void;
|
|
96
|
+
//#endregion
|
|
97
|
+
export { processMockData as a, MockCompiler as c, createLogger as d, logLevels as f, createMockMiddleware as i, ResolvePluginOptions as l, mockWebSocket as n, processRawData as o, CreateMockMiddlewareOptions as r, sortByValidator as s, MockSocketOptions as t, Logger as u };
|
package/dist/server.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as processMockData,
|
|
2
|
-
export {
|
|
1
|
+
import { a as processMockData, d as createLogger, f as logLevels, i as createMockMiddleware, n as mockWebSocket, o as processRawData, r as CreateMockMiddlewareOptions, s as sortByValidator, t as MockSocketOptions, u as Logger } from "./server-DurHqymI.js";
|
|
2
|
+
export { CreateMockMiddlewareOptions, Logger, MockSocketOptions, createLogger, createMockMiddleware, logLevels, mockWebSocket, processMockData, processRawData, sortByValidator };
|
package/dist/server.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
1
|
+
import { c as processRawData, i as createMockMiddleware, l as sortByValidator, n as logLevels, r as mockWebSocket, s as processMockData, t as createLogger } from "./logger-DTZcb53u.js";
|
|
2
|
+
export { createLogger, createMockMiddleware, logLevels, mockWebSocket, processMockData, processRawData, sortByValidator };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rspack-plugin-mock",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.2.0",
|
|
5
5
|
"description": "inject api mock server to development server",
|
|
6
6
|
"author": "pengzhanbo <q942450674@outlook.com> (https://github.com/pengzhanbo)",
|
|
7
7
|
"license": "MIT",
|
|
@@ -22,33 +22,12 @@
|
|
|
22
22
|
"#json5-loader": "./dist/json5-loader.mjs"
|
|
23
23
|
},
|
|
24
24
|
"exports": {
|
|
25
|
-
".":
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
"./server": {
|
|
32
|
-
"import": {
|
|
33
|
-
"types": "./dist/server.d.ts",
|
|
34
|
-
"default": "./dist/server.js"
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
|
-
"./rsbuild": {
|
|
38
|
-
"import": {
|
|
39
|
-
"types": "./dist/rsbuild.d.ts",
|
|
40
|
-
"default": "./dist/rsbuild.js"
|
|
41
|
-
}
|
|
42
|
-
},
|
|
43
|
-
"./helper": {
|
|
44
|
-
"import": {
|
|
45
|
-
"types": "./dist/helper.d.ts",
|
|
46
|
-
"default": "./dist/helper.js"
|
|
47
|
-
}
|
|
48
|
-
},
|
|
25
|
+
".": "./dist/index.js",
|
|
26
|
+
"./helper": "./dist/helper.js",
|
|
27
|
+
"./rsbuild": "./dist/rsbuild.js",
|
|
28
|
+
"./server": "./dist/server.js",
|
|
49
29
|
"./package.json": "./package.json"
|
|
50
30
|
},
|
|
51
|
-
"main": "dist/index.js",
|
|
52
31
|
"types": "dist/index.d.ts",
|
|
53
32
|
"files": [
|
|
54
33
|
"dist"
|
|
@@ -59,7 +38,8 @@
|
|
|
59
38
|
},
|
|
60
39
|
"peerDependencies": {
|
|
61
40
|
"@rsbuild/core": ">=2.0.0",
|
|
62
|
-
"@rspack/core": ">=2.0.0"
|
|
41
|
+
"@rspack/core": ">=2.0.0",
|
|
42
|
+
"zstd-codec": ">=0.1.5"
|
|
63
43
|
},
|
|
64
44
|
"peerDependenciesMeta": {
|
|
65
45
|
"@rsbuild/core": {
|
|
@@ -67,49 +47,56 @@
|
|
|
67
47
|
},
|
|
68
48
|
"@rspack/core": {
|
|
69
49
|
"optional": true
|
|
50
|
+
},
|
|
51
|
+
"zstd-codec": {
|
|
52
|
+
"optional": true
|
|
70
53
|
}
|
|
71
54
|
},
|
|
72
55
|
"dependencies": {
|
|
73
|
-
"@pengzhanbo/utils": "^3.
|
|
74
|
-
"ansis": "^4.
|
|
56
|
+
"@pengzhanbo/utils": "^3.10.0",
|
|
57
|
+
"ansis": "^4.3.1",
|
|
75
58
|
"chokidar": "^5.0.0",
|
|
76
59
|
"co-body": "^6.2.0",
|
|
60
|
+
"cookies": "^0.9.1",
|
|
77
61
|
"cors": "^2.8.6",
|
|
78
|
-
"debug": "^4.4.3",
|
|
79
62
|
"formidable": "^3.5.4",
|
|
80
63
|
"http-status": "^2.1.0",
|
|
81
|
-
"is-core-module": "^2.16.1",
|
|
82
64
|
"json5": "^2.2.3",
|
|
83
|
-
"local-pkg": "^1.1
|
|
84
|
-
"memfs": "^4.
|
|
65
|
+
"local-pkg": "^1.2.1",
|
|
66
|
+
"memfs": "^4.68.0",
|
|
85
67
|
"mime-types": "^3.0.2",
|
|
86
68
|
"path-to-regexp": "^8.4.2",
|
|
87
|
-
"picomatch": "^4.0.
|
|
88
|
-
"tinyglobby": "^0.2.
|
|
89
|
-
"ws": "^8.
|
|
69
|
+
"picomatch": "^4.0.5",
|
|
70
|
+
"tinyglobby": "^0.2.17",
|
|
71
|
+
"ws": "^8.21.3"
|
|
90
72
|
},
|
|
91
73
|
"devDependencies": {
|
|
92
|
-
"@pengzhanbo/eslint-config": "^2.
|
|
93
|
-
"@rsbuild/core": "^2.
|
|
94
|
-
"@rspack/core": "^2.
|
|
74
|
+
"@pengzhanbo/eslint-config": "^2.8.5",
|
|
75
|
+
"@rsbuild/core": "^2.1.10",
|
|
76
|
+
"@rspack/core": "^2.1.8",
|
|
95
77
|
"@types/co-body": "^6.1.3",
|
|
78
|
+
"@types/cookies": "^0.9.2",
|
|
96
79
|
"@types/cors": "^2.8.19",
|
|
97
|
-
"@types/debug": "^4.1.13",
|
|
98
80
|
"@types/formidable": "^3.5.1",
|
|
99
|
-
"@types/is-core-module": "^2.2.2",
|
|
100
81
|
"@types/mime-types": "^3.0.1",
|
|
101
|
-
"@types/node": "^
|
|
82
|
+
"@types/node": "^26.2.0",
|
|
102
83
|
"@types/picomatch": "^4.0.3",
|
|
103
84
|
"@types/ws": "^8.18.1",
|
|
104
|
-
"bumpp": "^
|
|
105
|
-
"conventional-changelog
|
|
106
|
-
"
|
|
107
|
-
"
|
|
108
|
-
"
|
|
109
|
-
"
|
|
110
|
-
"
|
|
85
|
+
"bumpp": "^12.2.0",
|
|
86
|
+
"conventional-changelog": "^7.2.0",
|
|
87
|
+
"conventional-changelog-angular": "^8.3.1",
|
|
88
|
+
"eslint": "^10.8.1",
|
|
89
|
+
"js-tokens": "^10.0.0",
|
|
90
|
+
"nano-staged": "^1.0.2",
|
|
91
|
+
"simple-git-hooks": "^2.13.1",
|
|
92
|
+
"tsdown": "^0.22.14",
|
|
93
|
+
"typescript": "6.0.3",
|
|
94
|
+
"zstd-codec": "^0.1.5"
|
|
95
|
+
},
|
|
96
|
+
"simple-git-hooks": {
|
|
97
|
+
"pre-commit": "./node_modules/.bin/nano-staged --allow-empty"
|
|
111
98
|
},
|
|
112
|
-
"
|
|
99
|
+
"nano-staged": {
|
|
113
100
|
"*": "eslint --fix"
|
|
114
101
|
},
|
|
115
102
|
"publishConfig": {
|
|
@@ -121,7 +108,7 @@
|
|
|
121
108
|
"build": "tsdown",
|
|
122
109
|
"lint": "eslint .",
|
|
123
110
|
"release:publish": "pnpm -r publish",
|
|
124
|
-
"release:changelog": "conventional-changelog -p angular
|
|
111
|
+
"release:changelog": "conventional-changelog -p angular",
|
|
125
112
|
"release": "bumpp package.json --execute=\"pnpm release:changelog\" --commit --all --push --tag"
|
|
126
113
|
}
|
|
127
114
|
}
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { g as ServerBuildOption, i as LogLevel, m as MockWebsocketItem, p as MockServerPluginOptions, s as MockHttpItem, u as MockOptions } from "./types-GT6M6WuI.js";
|
|
2
|
-
import { Compiler, RspackPluginInstance } from "@rspack/core";
|
|
3
|
-
import { Matcher } from "picomatch";
|
|
4
|
-
import Debug from "debug";
|
|
5
|
-
import EventEmitter from "node:events";
|
|
6
|
-
import { FSWatcher } from "chokidar";
|
|
7
|
-
import { CorsOptions } from "cors";
|
|
8
|
-
import * as http$1 from "node:http";
|
|
9
|
-
import http, { Server } from "node:http";
|
|
10
|
-
import { Http2SecureServer } from "node:http2";
|
|
11
|
-
|
|
12
|
-
//#region src/utils/logger.d.ts
|
|
13
|
-
interface Logger {
|
|
14
|
-
debug: (msg: string, level?: boolean | LogLevel) => void;
|
|
15
|
-
info: (msg: string, level?: boolean | LogLevel) => void;
|
|
16
|
-
warn: (msg: string, level?: boolean | LogLevel) => void;
|
|
17
|
-
error: (msg: string, level?: boolean | LogLevel) => void;
|
|
18
|
-
}
|
|
19
|
-
declare const logLevels: Record<LogLevel, number>;
|
|
20
|
-
declare function createLogger(prefix: string, defaultLevel?: LogLevel): Logger;
|
|
21
|
-
//#endregion
|
|
22
|
-
//#region src/options.d.ts
|
|
23
|
-
interface ResolvedCompilerOptions {
|
|
24
|
-
alias: Record<string, false | string | (string | false)[]>;
|
|
25
|
-
proxies: (string | ((pathname: string, req: http.IncomingMessage) => boolean))[];
|
|
26
|
-
wsProxies: (string | ((pathname: string, req: http.IncomingMessage) => boolean))[];
|
|
27
|
-
plugins: RspackPluginInstance[];
|
|
28
|
-
context?: string;
|
|
29
|
-
cors: false | CorsOptions;
|
|
30
|
-
}
|
|
31
|
-
type ResolvePluginOptions = Required<Omit<MockServerPluginOptions, "build">> & ResolvedCompilerOptions & {
|
|
32
|
-
logger: Logger;
|
|
33
|
-
build: false | ServerBuildOption;
|
|
34
|
-
};
|
|
35
|
-
//#endregion
|
|
36
|
-
//#region src/compiler/processData.d.ts
|
|
37
|
-
declare function processRawData(rawData: (readonly [any, string])[]): (MockHttpItem | MockWebsocketItem | MockOptions)[];
|
|
38
|
-
declare function processMockData(mockList: (MockHttpItem | MockWebsocketItem | MockOptions)[]): Record<string, MockOptions>;
|
|
39
|
-
declare function sortByValidator(mocks: MockOptions): (MockHttpItem | MockWebsocketItem)[];
|
|
40
|
-
//#endregion
|
|
41
|
-
//#region src/compiler/mockCompiler.d.ts
|
|
42
|
-
declare class MockCompiler extends EventEmitter {
|
|
43
|
-
options: ResolvePluginOptions;
|
|
44
|
-
cwd: string;
|
|
45
|
-
mockWatcher!: FSWatcher;
|
|
46
|
-
entryFile!: string;
|
|
47
|
-
deps: string[];
|
|
48
|
-
isESM: boolean;
|
|
49
|
-
private _mockData;
|
|
50
|
-
private watchInfo?;
|
|
51
|
-
compiler?: Compiler | null;
|
|
52
|
-
constructor(options: ResolvePluginOptions);
|
|
53
|
-
get mockData(): Record<string, MockOptions>;
|
|
54
|
-
run(): Promise<void>;
|
|
55
|
-
close(): void;
|
|
56
|
-
updateAlias(alias: Record<string, false | string | (string | false)[]>): void;
|
|
57
|
-
updateMockEntry(): Promise<void>;
|
|
58
|
-
watchMockFiles(isMatch: Matcher): void;
|
|
59
|
-
}
|
|
60
|
-
//#endregion
|
|
61
|
-
//#region src/core/mockMiddleware.d.ts
|
|
62
|
-
interface BaseMiddlewareOptions extends Pick<MockServerPluginOptions, "formidableOptions" | "cookiesOptions" | "bodyParserOptions" | "priority"> {
|
|
63
|
-
proxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
64
|
-
logger: Logger;
|
|
65
|
-
cors: false | CorsOptions;
|
|
66
|
-
}
|
|
67
|
-
declare function baseMiddleware(compiler: MockCompiler, {
|
|
68
|
-
formidableOptions,
|
|
69
|
-
bodyParserOptions,
|
|
70
|
-
proxies,
|
|
71
|
-
cookiesOptions,
|
|
72
|
-
logger,
|
|
73
|
-
priority,
|
|
74
|
-
cors: corsOptions
|
|
75
|
-
}: BaseMiddlewareOptions): (req: http$1.IncomingMessage, res: http$1.ServerResponse, next: (err?: any) => void) => void;
|
|
76
|
-
//#endregion
|
|
77
|
-
//#region src/core/ws.d.ts
|
|
78
|
-
interface MockSocketOptions {
|
|
79
|
-
wsProxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
80
|
-
cookiesOptions: MockServerPluginOptions["cookiesOptions"];
|
|
81
|
-
logger: Logger;
|
|
82
|
-
}
|
|
83
|
-
declare function mockWebSocket(compiler: MockCompiler, httpServer: Server | Http2SecureServer | null, {
|
|
84
|
-
wsProxies: proxies,
|
|
85
|
-
cookiesOptions,
|
|
86
|
-
logger
|
|
87
|
-
}: MockSocketOptions): void;
|
|
88
|
-
//#endregion
|
|
89
|
-
export { processMockData as a, Logger as c, baseMiddleware as i, createLogger as l, mockWebSocket as n, processRawData as o, BaseMiddlewareOptions as r, sortByValidator as s, MockSocketOptions as t, logLevels as u };
|