rspack-plugin-mock 1.0.1 → 1.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/dist/chunk-CUT6urMc.cjs +30 -0
- package/dist/helper.cjs +124 -113
- package/dist/helper.d.cts +92 -104
- package/dist/helper.d.ts +92 -104
- package/dist/helper.js +120 -113
- package/dist/index.cjs +82 -99
- package/dist/index.d.cts +10 -22
- package/dist/index.d.ts +10 -22
- package/dist/index.js +74 -97
- package/dist/json5-loader.cjs +30 -15
- package/dist/logger-C0V8Cvvd.cjs +800 -0
- package/dist/logger-C48_LmdS.js +710 -0
- package/dist/mockWebsocket-DkVHpZCx.d.cts +85 -0
- package/dist/mockWebsocket-qLVAe-RI.d.ts +85 -0
- package/dist/resolvePluginOptions-Da5uqlBx.cjs +506 -0
- package/dist/resolvePluginOptions-DlkIkykz.js +476 -0
- package/dist/rsbuild.cjs +164 -190
- package/dist/rsbuild.d.cts +5 -13
- package/dist/rsbuild.d.ts +5 -13
- package/dist/rsbuild.js +161 -188
- package/dist/server.cjs +9 -18
- package/dist/server.d.cts +21 -25
- package/dist/server.d.ts +21 -25
- package/dist/server.js +3 -18
- package/dist/types-6lajtJPx.d.cts +572 -0
- package/dist/types-DPzh7nJq.d.ts +572 -0
- package/package.json +28 -28
- package/dist/chunk-HTVJXQRM.cjs +0 -906
- package/dist/chunk-HV5L72CY.js +0 -557
- package/dist/chunk-M7F5AAOF.cjs +0 -557
- package/dist/chunk-OGWV5ZGG.js +0 -906
- package/dist/mockWebsocket-DBgZBsdo.d.ts +0 -76
- package/dist/mockWebsocket-Ki_cShTv.d.cts +0 -76
- package/dist/types-Aw0AciTG.d.cts +0 -570
- package/dist/types-Aw0AciTG.d.ts +0 -570
package/dist/index.d.cts
CHANGED
|
@@ -1,25 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
import { c as MockServerPluginOptions } from './types-Aw0AciTG.cjs';
|
|
5
|
-
export { B as BodyParserOptions, E as ExtraRequest, F as FormidableFile, i as LogLevel, L as LogType, f as Method, M as MockHttpItem, d as MockMatchPriority, e as MockMatchSpecialPriority, b as MockOptions, g as MockRequest, h as MockResponse, a as MockWebsocketItem, R as ResponseBody, S as ServerBuildOption, W as WebSocketSetupContext } from './types-Aw0AciTG.cjs';
|
|
6
|
-
import 'node:http';
|
|
7
|
-
import 'node:http2';
|
|
8
|
-
import 'node:fs';
|
|
9
|
-
import 'node:events';
|
|
10
|
-
import 'co-body';
|
|
11
|
-
import 'cookies';
|
|
12
|
-
import 'cors';
|
|
13
|
-
import 'formidable';
|
|
14
|
-
import 'node:buffer';
|
|
15
|
-
import 'node:stream';
|
|
16
|
-
import 'ws';
|
|
1
|
+
import { BodyParserOptions, ExtraRequest, FormidableFile, LogLevel, LogType, Method, MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, MockOptions, MockRequest, MockResponse, MockServerPluginOptions, MockWebsocketItem, ResponseBody, ServerBuildOption, WebSocketSetupContext } from "./types-6lajtJPx.cjs";
|
|
2
|
+
import { Middleware, MiddlewareOptions, MockCompiler, MockCompilerOptions, MockSocketOptions, ResolvePluginOptions, Server, createMockCompiler, createMockMiddleware, mockWebSocket } from "./mockWebsocket-DkVHpZCx.cjs";
|
|
3
|
+
import { Compiler, RspackPluginInstance } from "@rspack/core";
|
|
17
4
|
|
|
5
|
+
//#region src/rspack.d.ts
|
|
18
6
|
declare class MockServerPlugin implements RspackPluginInstance {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
7
|
+
options: MockServerPluginOptions;
|
|
8
|
+
constructor(options?: MockServerPluginOptions);
|
|
9
|
+
apply(compiler: Compiler): void;
|
|
22
10
|
}
|
|
23
|
-
declare function resolvePluginOptions(compiler: Compiler, options
|
|
24
|
-
|
|
25
|
-
export { MockServerPlugin, MockServerPluginOptions, resolvePluginOptions };
|
|
11
|
+
declare function resolvePluginOptions(compiler: Compiler, options?: MockServerPluginOptions): ResolvePluginOptions;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { BodyParserOptions, ExtraRequest, FormidableFile, LogLevel, LogType, Method, Middleware, MiddlewareOptions, MockCompiler, MockCompilerOptions, MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, MockOptions, MockRequest, MockResponse, MockServerPlugin, MockServerPluginOptions, MockSocketOptions, MockWebsocketItem, ResponseBody, Server, ServerBuildOption, WebSocketSetupContext, createMockCompiler, createMockMiddleware, mockWebSocket, resolvePluginOptions };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,25 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
import { c as MockServerPluginOptions } from './types-Aw0AciTG.js';
|
|
5
|
-
export { B as BodyParserOptions, E as ExtraRequest, F as FormidableFile, i as LogLevel, L as LogType, f as Method, M as MockHttpItem, d as MockMatchPriority, e as MockMatchSpecialPriority, b as MockOptions, g as MockRequest, h as MockResponse, a as MockWebsocketItem, R as ResponseBody, S as ServerBuildOption, W as WebSocketSetupContext } from './types-Aw0AciTG.js';
|
|
6
|
-
import 'node:http';
|
|
7
|
-
import 'node:http2';
|
|
8
|
-
import 'node:fs';
|
|
9
|
-
import 'node:events';
|
|
10
|
-
import 'co-body';
|
|
11
|
-
import 'cookies';
|
|
12
|
-
import 'cors';
|
|
13
|
-
import 'formidable';
|
|
14
|
-
import 'node:buffer';
|
|
15
|
-
import 'node:stream';
|
|
16
|
-
import 'ws';
|
|
1
|
+
import { BodyParserOptions, ExtraRequest, FormidableFile, LogLevel, LogType, Method, MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, MockOptions, MockRequest, MockResponse, MockServerPluginOptions, MockWebsocketItem, ResponseBody, ServerBuildOption, WebSocketSetupContext } from "./types-DPzh7nJq.js";
|
|
2
|
+
import { Middleware, MiddlewareOptions, MockCompiler, MockCompilerOptions, MockSocketOptions, ResolvePluginOptions, Server, createMockCompiler, createMockMiddleware, mockWebSocket } from "./mockWebsocket-qLVAe-RI.js";
|
|
3
|
+
import { Compiler, RspackPluginInstance } from "@rspack/core";
|
|
17
4
|
|
|
5
|
+
//#region src/rspack.d.ts
|
|
18
6
|
declare class MockServerPlugin implements RspackPluginInstance {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
7
|
+
options: MockServerPluginOptions;
|
|
8
|
+
constructor(options?: MockServerPluginOptions);
|
|
9
|
+
apply(compiler: Compiler): void;
|
|
22
10
|
}
|
|
23
|
-
declare function resolvePluginOptions(compiler: Compiler, options
|
|
24
|
-
|
|
25
|
-
export { MockServerPlugin, MockServerPluginOptions, resolvePluginOptions };
|
|
11
|
+
declare function resolvePluginOptions(compiler: Compiler, options?: MockServerPluginOptions): ResolvePluginOptions;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { BodyParserOptions, ExtraRequest, FormidableFile, LogLevel, LogType, Method, Middleware, MiddlewareOptions, MockCompiler, MockCompilerOptions, MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, MockOptions, MockRequest, MockResponse, MockServerPlugin, MockServerPluginOptions, MockSocketOptions, MockWebsocketItem, ResponseBody, Server, ServerBuildOption, WebSocketSetupContext, createMockCompiler, createMockMiddleware, mockWebSocket, resolvePluginOptions };
|
package/dist/index.js
CHANGED
|
@@ -1,103 +1,80 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
createMockCompiler,
|
|
5
|
-
createMockMiddleware,
|
|
6
|
-
resolvePluginOptions
|
|
7
|
-
} from "./chunk-HV5L72CY.js";
|
|
8
|
-
import {
|
|
9
|
-
mockWebSocket,
|
|
10
|
-
rewriteRequest,
|
|
11
|
-
waitingFor
|
|
12
|
-
} from "./chunk-OGWV5ZGG.js";
|
|
13
|
-
|
|
14
|
-
// src/rspack.ts
|
|
1
|
+
import { mockWebSocket, rewriteRequest, waitingFor } from "./logger-C48_LmdS.js";
|
|
2
|
+
import { MockCompiler, buildMockServer, createMockCompiler, createMockMiddleware, resolvePluginOptions } from "./resolvePluginOptions-DlkIkykz.js";
|
|
3
|
+
import { isString, toArray } from "@pengzhanbo/utils";
|
|
15
4
|
import path from "node:path";
|
|
16
5
|
import process from "node:process";
|
|
17
|
-
import { isString, toArray } from "@pengzhanbo/utils";
|
|
18
6
|
import rspack from "@rspack/core";
|
|
19
|
-
|
|
7
|
+
|
|
8
|
+
//#region src/rspack.ts
|
|
9
|
+
const PLUGIN_NAME = "rspack-plugin-mock";
|
|
20
10
|
var MockServerPlugin = class {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
compilerOptions.output.path || path.resolve(process.cwd(), "dist")
|
|
72
|
-
));
|
|
73
|
-
}
|
|
74
|
-
}
|
|
11
|
+
constructor(options = {}) {
|
|
12
|
+
this.options = options;
|
|
13
|
+
}
|
|
14
|
+
apply(compiler) {
|
|
15
|
+
const compilerOptions = compiler.options;
|
|
16
|
+
const options = resolvePluginOptions$1(compiler, this.options);
|
|
17
|
+
if (process.env.NODE_ENV !== "production") {
|
|
18
|
+
const mockCompiler = createMockCompiler(options);
|
|
19
|
+
const mockMiddleware = createMockMiddleware(mockCompiler, options);
|
|
20
|
+
const setupMiddlewares = compilerOptions.devServer?.setupMiddlewares;
|
|
21
|
+
const waitServerForMockWebSocket = waitingFor((server) => {
|
|
22
|
+
mockWebSocket(mockCompiler, server, options);
|
|
23
|
+
});
|
|
24
|
+
compilerOptions.devServer = {
|
|
25
|
+
...compilerOptions.devServer,
|
|
26
|
+
setupMiddlewares: (middlewares, devServer) => {
|
|
27
|
+
middlewares = setupMiddlewares?.(middlewares, devServer) || middlewares;
|
|
28
|
+
const reload = () => {
|
|
29
|
+
if (devServer.webSocketServer?.clients) devServer.sendMessage(devServer.webSocketServer.clients, "static-changed");
|
|
30
|
+
};
|
|
31
|
+
middlewares = mockMiddleware(middlewares, reload) || middlewares;
|
|
32
|
+
/**
|
|
33
|
+
* 在 @rspack/dev-server -> webpack-dev-server 中, setupMiddlewares 优先于 createServer
|
|
34
|
+
* 执行,需要等待 server 启动后再注入 mock websocket
|
|
35
|
+
*/
|
|
36
|
+
waitServerForMockWebSocket(() => devServer.server);
|
|
37
|
+
return middlewares;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
const wsPrefix = toArray(options.wsPrefix);
|
|
41
|
+
if (compilerOptions.devServer?.proxy?.length) {
|
|
42
|
+
const proxy = compilerOptions.devServer.proxy;
|
|
43
|
+
compilerOptions.devServer.proxy = proxy.filter((item) => {
|
|
44
|
+
if (typeof item !== "function" && item.ws === true && wsPrefix.length) return !toArray(item.context).filter(isString).some((context) => wsPrefix.includes(context));
|
|
45
|
+
return true;
|
|
46
|
+
}).map((item) => {
|
|
47
|
+
if (typeof item !== "function" && !item.ws) {
|
|
48
|
+
const onProxyReq = item.onProxyReq;
|
|
49
|
+
item.onProxyReq = (proxyReq, req, ...args) => {
|
|
50
|
+
onProxyReq?.(proxyReq, req, ...args);
|
|
51
|
+
rewriteRequest(proxyReq, req);
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
return item;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
compiler.hooks.watchRun.tap(PLUGIN_NAME, () => mockCompiler.run());
|
|
58
|
+
compiler.hooks.watchClose.tap(PLUGIN_NAME, () => mockCompiler.close());
|
|
59
|
+
} else if (options.build !== false) compiler.hooks.afterEmit.tap(PLUGIN_NAME, () => buildMockServer(options, compilerOptions.output.path || path.resolve(process.cwd(), "dist")));
|
|
60
|
+
}
|
|
75
61
|
};
|
|
76
|
-
function
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
context,
|
|
92
|
-
plugins: toArray(definePluginInstance),
|
|
93
|
-
proxies
|
|
94
|
-
});
|
|
62
|
+
function resolvePluginOptions$1(compiler, options = {}) {
|
|
63
|
+
const compilerOptions = compiler.options;
|
|
64
|
+
const alias = compilerOptions.resolve?.alias || {};
|
|
65
|
+
const context = compilerOptions.context;
|
|
66
|
+
const definePluginInstance = compilerOptions.plugins?.find((plugin) => plugin instanceof rspack.DefinePlugin);
|
|
67
|
+
const proxies = (compilerOptions.devServer?.proxy || []).flatMap((item) => {
|
|
68
|
+
if (typeof item !== "function" && item.context && !item.ws) return item.context;
|
|
69
|
+
return [];
|
|
70
|
+
});
|
|
71
|
+
return resolvePluginOptions(options, {
|
|
72
|
+
alias,
|
|
73
|
+
context,
|
|
74
|
+
plugins: toArray(definePluginInstance),
|
|
75
|
+
proxies
|
|
76
|
+
});
|
|
95
77
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
createMockCompiler,
|
|
100
|
-
createMockMiddleware,
|
|
101
|
-
mockWebSocket,
|
|
102
|
-
resolvePluginOptions2 as resolvePluginOptions
|
|
103
|
-
};
|
|
78
|
+
|
|
79
|
+
//#endregion
|
|
80
|
+
export { MockCompiler, MockServerPlugin, createMockCompiler, createMockMiddleware, mockWebSocket, resolvePluginOptions$1 as resolvePluginOptions };
|
package/dist/json5-loader.cjs
CHANGED
|
@@ -1,17 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
4
17
|
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
5
22
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
});
|
|
17
|
-
exports. default = require_json5_loader();
|
|
23
|
+
//#endregion
|
|
24
|
+
const json5 = __toESM(require("json5"));
|
|
25
|
+
|
|
26
|
+
//#region src/json5-loader.cts
|
|
27
|
+
module.exports = function(content) {
|
|
28
|
+
if (!content) return "export default {}";
|
|
29
|
+
return `export default ${JSON.stringify(json5.default.parse(content))}`;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
//#endregion
|