rspack-plugin-mock 1.1.0 → 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 +22 -22
- 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/rsbuild.cjs
CHANGED
|
@@ -1,202 +1,176 @@
|
|
|
1
|
-
|
|
1
|
+
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
+
const require_logger = require('./logger-C0V8Cvvd.cjs');
|
|
3
|
+
const require_resolvePluginOptions = require('./resolvePluginOptions-Da5uqlBx.cjs');
|
|
4
|
+
const __pengzhanbo_utils = require_chunk.__toESM(require("@pengzhanbo/utils"));
|
|
5
|
+
const node_path = require_chunk.__toESM(require("node:path"));
|
|
6
|
+
const node_process = require_chunk.__toESM(require("node:process"));
|
|
7
|
+
const picocolors = require_chunk.__toESM(require("picocolors"));
|
|
8
|
+
const __rspack_core = require_chunk.__toESM(require("@rspack/core"));
|
|
9
|
+
const node_http = require_chunk.__toESM(require("node:http"));
|
|
10
|
+
const portfinder = require_chunk.__toESM(require("portfinder"));
|
|
2
11
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var _chunkM7F5AAOFcjs = require('./chunk-M7F5AAOF.cjs');
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
var _chunkHTVJXQRMcjs = require('./chunk-HTVJXQRM.cjs');
|
|
11
|
-
|
|
12
|
-
// src/rsbuild.ts
|
|
13
|
-
var _http = require('http');
|
|
14
|
-
var _path = require('path'); var _path2 = _interopRequireDefault(_path);
|
|
15
|
-
var _process = require('process'); var _process2 = _interopRequireDefault(_process);
|
|
16
|
-
var _utils = require('@pengzhanbo/utils');
|
|
17
|
-
var _core = require('@rspack/core'); var _core2 = _interopRequireDefault(_core);
|
|
18
|
-
var _picocolors = require('picocolors'); var _picocolors2 = _interopRequireDefault(_picocolors);
|
|
19
|
-
var _portfinder = require('portfinder');
|
|
12
|
+
//#region src/rsbuild.ts
|
|
20
13
|
function pluginMockServer(options = {}) {
|
|
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
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
});
|
|
80
|
-
} else {
|
|
81
|
-
mockCompiler.updateAlias(_optionalChain([compiler, 'access', _10 => _10.options, 'access', _11 => _11.resolve, 'optionalAccess', _12 => _12.alias]) || {});
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
api.onAfterStartDevServer(startMockServer);
|
|
85
|
-
api.onAfterStartProdServer(startMockServer);
|
|
86
|
-
api.onExit(close);
|
|
87
|
-
}
|
|
88
|
-
};
|
|
14
|
+
return {
|
|
15
|
+
name: "plugin-mock-server",
|
|
16
|
+
setup(api) {
|
|
17
|
+
const rsbuildConfig = api.getRsbuildConfig();
|
|
18
|
+
const resolvedOptions = require_resolvePluginOptions.resolvePluginOptions(options, {
|
|
19
|
+
proxies: resolveConfigProxies(rsbuildConfig),
|
|
20
|
+
alias: {},
|
|
21
|
+
context: api.context.rootPath,
|
|
22
|
+
plugins: [new __rspack_core.default.DefinePlugin(rsbuildConfig.source?.define || {})]
|
|
23
|
+
});
|
|
24
|
+
if (node_process.default.env.NODE_ENV === "production") {
|
|
25
|
+
if (resolvedOptions.build) api.onAfterBuild(async () => {
|
|
26
|
+
const config = api.getNormalizedConfig();
|
|
27
|
+
await require_resolvePluginOptions.buildMockServer(resolvedOptions, node_path.default.resolve(node_process.default.cwd(), config.output.distPath.root || "dist"));
|
|
28
|
+
});
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const mockCompiler = require_resolvePluginOptions.createMockCompiler(resolvedOptions);
|
|
32
|
+
api.modifyRsbuildConfig((config) => {
|
|
33
|
+
updateServerProxyConfigByHttpMock(config);
|
|
34
|
+
const mockMiddleware = require_resolvePluginOptions.createMockMiddleware(mockCompiler, resolvedOptions);
|
|
35
|
+
config.dev ??= {};
|
|
36
|
+
config.dev.setupMiddlewares ??= [];
|
|
37
|
+
config.dev.setupMiddlewares.push((middlewares, server$1) => {
|
|
38
|
+
mockMiddleware(middlewares, () => server$1.sockWrite("static-changed"));
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
let port = 3079;
|
|
42
|
+
const shouldMockWs = (0, __pengzhanbo_utils.toArray)(resolvedOptions.wsPrefix).length > 0;
|
|
43
|
+
if (shouldMockWs) api.modifyRsbuildConfig(async (config) => {
|
|
44
|
+
const defaultPort = (config.server?.port || port) + 1;
|
|
45
|
+
port = await (0, portfinder.getPortPromise)({ port: defaultPort });
|
|
46
|
+
updateServerProxyConfigByWSMock(config, options.wsPrefix || [], port);
|
|
47
|
+
});
|
|
48
|
+
let server;
|
|
49
|
+
function startMockServer() {
|
|
50
|
+
mockCompiler.run();
|
|
51
|
+
if (shouldMockWs) {
|
|
52
|
+
server = (0, node_http.createServer)();
|
|
53
|
+
require_logger.mockWebSocket(mockCompiler, server, resolvedOptions);
|
|
54
|
+
server.listen(port);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function close() {
|
|
58
|
+
mockCompiler.close();
|
|
59
|
+
server?.close();
|
|
60
|
+
}
|
|
61
|
+
api.onAfterCreateCompiler(({ compiler }) => {
|
|
62
|
+
if ("compilers" in compiler) compiler.compilers.forEach((compiler$1) => {
|
|
63
|
+
mockCompiler.updateAlias(compiler$1.options.resolve?.alias || {});
|
|
64
|
+
});
|
|
65
|
+
else mockCompiler.updateAlias(compiler.options.resolve?.alias || {});
|
|
66
|
+
});
|
|
67
|
+
api.onAfterStartDevServer(startMockServer);
|
|
68
|
+
api.onAfterStartProdServer(startMockServer);
|
|
69
|
+
api.onExit(close);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
89
72
|
}
|
|
90
73
|
function onProxyError(err, _req, res) {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
74
|
+
console.error(picocolors.default.red(err?.stack || err.message));
|
|
75
|
+
res.statusCode = 500;
|
|
76
|
+
res.end();
|
|
94
77
|
}
|
|
95
78
|
function updateServerProxyConfigByHttpMock(config) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
};
|
|
137
|
-
});
|
|
138
|
-
}
|
|
79
|
+
if (!config.server?.proxy) return;
|
|
80
|
+
if ((0, __pengzhanbo_utils.isArray)(config.server.proxy)) config.server.proxy = config.server.proxy.map((item) => {
|
|
81
|
+
if (typeof item !== "function" && !item.ws) {
|
|
82
|
+
const onProxyReq = item.onProxyReq;
|
|
83
|
+
const onError = item.onError;
|
|
84
|
+
return {
|
|
85
|
+
...item,
|
|
86
|
+
onError: onError || onProxyError,
|
|
87
|
+
onProxyReq: (proxyReq, req, ...args) => {
|
|
88
|
+
onProxyReq?.(proxyReq, req, ...args);
|
|
89
|
+
require_logger.rewriteRequest(proxyReq, req);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
return item;
|
|
94
|
+
});
|
|
95
|
+
else if ("target" in config.server.proxy) {
|
|
96
|
+
const onProxyReq = config.server.proxy.onProxyReq;
|
|
97
|
+
config.server.proxy.onProxyReq = (proxyReq, req, ...args) => {
|
|
98
|
+
onProxyReq?.(proxyReq, req, ...args);
|
|
99
|
+
require_logger.rewriteRequest(proxyReq, req);
|
|
100
|
+
};
|
|
101
|
+
config.server.proxy.onError ??= onProxyError;
|
|
102
|
+
} else if (config.server.proxy) {
|
|
103
|
+
const proxy = config.server.proxy;
|
|
104
|
+
Object.keys(proxy).forEach((key) => {
|
|
105
|
+
const target = proxy[key];
|
|
106
|
+
const options = typeof target === "string" ? { target } : target;
|
|
107
|
+
if (options.ws) return;
|
|
108
|
+
const { onProxyReq, onError,...rest } = options;
|
|
109
|
+
proxy[key] = {
|
|
110
|
+
...rest,
|
|
111
|
+
onProxyReq: (proxyReq, req, ...args) => {
|
|
112
|
+
onProxyReq?.(proxyReq, req, ...args);
|
|
113
|
+
require_logger.rewriteRequest(proxyReq, req);
|
|
114
|
+
},
|
|
115
|
+
onError: onError || onProxyError
|
|
116
|
+
};
|
|
117
|
+
});
|
|
118
|
+
}
|
|
139
119
|
}
|
|
140
120
|
function updateServerProxyConfigByWSMock(config, wsPrefix, port) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
121
|
+
config.server ??= {};
|
|
122
|
+
const proxy = config.server.proxy ??= {};
|
|
123
|
+
const wsTarget = `ws://localhost:${port}`;
|
|
124
|
+
const prefix = (0, __pengzhanbo_utils.toArray)(wsPrefix);
|
|
125
|
+
const has = (context) => typeof context === "string" && prefix.includes(context);
|
|
126
|
+
const used = /* @__PURE__ */ new Set();
|
|
127
|
+
function updateProxy(item) {
|
|
128
|
+
if ((0, __pengzhanbo_utils.isArray)(item.context)) item.context = item.context.filter(has);
|
|
129
|
+
else if (has(item.context)) {
|
|
130
|
+
used.add(item.context);
|
|
131
|
+
item.target = wsTarget;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if ((0, __pengzhanbo_utils.isArray)(proxy)) {
|
|
135
|
+
for (const item of proxy) if (typeof item !== "function" && item.context && item.ws) updateProxy(item);
|
|
136
|
+
prefix.filter((context) => !used.has(context)).forEach((context) => proxy.push({
|
|
137
|
+
context,
|
|
138
|
+
target: wsTarget
|
|
139
|
+
}));
|
|
140
|
+
} else if ("target" in proxy) {
|
|
141
|
+
if (proxy.ws) {
|
|
142
|
+
updateProxy(proxy);
|
|
143
|
+
const list = config.server.proxy = [proxy];
|
|
144
|
+
prefix.filter((context) => !used.has(context)).forEach((context) => list.push({
|
|
145
|
+
context,
|
|
146
|
+
target: wsTarget
|
|
147
|
+
}));
|
|
148
|
+
}
|
|
149
|
+
} else {
|
|
150
|
+
Object.entries(proxy).forEach(([, opt]) => {
|
|
151
|
+
if (typeof opt !== "string" && opt.ws) updateProxy(opt);
|
|
152
|
+
});
|
|
153
|
+
prefix.filter((context) => !used.has(context)).forEach((context) => {
|
|
154
|
+
proxy[context] = {
|
|
155
|
+
target: wsTarget,
|
|
156
|
+
ws: true
|
|
157
|
+
};
|
|
158
|
+
});
|
|
159
|
+
}
|
|
178
160
|
}
|
|
179
161
|
function resolveConfigProxies(config) {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
proxies.push(..._utils.toArray.call(void 0, proxy.context));
|
|
192
|
-
} else {
|
|
193
|
-
Object.entries(proxy).forEach(([context, opt]) => {
|
|
194
|
-
if (typeof opt === "string" || !opt.ws)
|
|
195
|
-
proxies.push(context);
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
return proxies;
|
|
162
|
+
config.server ??= {};
|
|
163
|
+
const proxy = config.server.proxy ??= {};
|
|
164
|
+
const proxies = [];
|
|
165
|
+
if ((0, __pengzhanbo_utils.isArray)(proxy)) {
|
|
166
|
+
for (const item of proxy) if (typeof item !== "function" && item.context && !item.ws) proxies.push(...(0, __pengzhanbo_utils.toArray)(item.context));
|
|
167
|
+
} else if ("target" in proxy) {
|
|
168
|
+
if (!proxy.ws) proxies.push(...(0, __pengzhanbo_utils.toArray)(proxy.context));
|
|
169
|
+
} else Object.entries(proxy).forEach(([context, opt]) => {
|
|
170
|
+
if (typeof opt === "string" || !opt.ws) proxies.push(context);
|
|
171
|
+
});
|
|
172
|
+
return proxies;
|
|
199
173
|
}
|
|
200
174
|
|
|
201
|
-
|
|
202
|
-
exports.pluginMockServer = pluginMockServer;
|
|
175
|
+
//#endregion
|
|
176
|
+
exports.pluginMockServer = pluginMockServer;
|
package/dist/rsbuild.d.cts
CHANGED
|
@@ -1,15 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
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';
|
|
4
|
-
import 'co-body';
|
|
5
|
-
import 'cookies';
|
|
6
|
-
import 'cors';
|
|
7
|
-
import 'formidable';
|
|
8
|
-
import 'node:buffer';
|
|
9
|
-
import 'node:http';
|
|
10
|
-
import 'node:stream';
|
|
11
|
-
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 { RsbuildPlugin } from "@rsbuild/core";
|
|
12
3
|
|
|
4
|
+
//#region src/rsbuild.d.ts
|
|
13
5
|
declare function pluginMockServer(options?: MockServerPluginOptions): RsbuildPlugin;
|
|
14
|
-
|
|
15
|
-
export { MockServerPluginOptions, pluginMockServer };
|
|
6
|
+
//#endregion
|
|
7
|
+
export { BodyParserOptions, ExtraRequest, FormidableFile, LogLevel, LogType, Method, MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, MockOptions, MockRequest, MockResponse, MockServerPluginOptions, MockWebsocketItem, ResponseBody, ServerBuildOption, WebSocketSetupContext, pluginMockServer };
|
package/dist/rsbuild.d.ts
CHANGED
|
@@ -1,15 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
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';
|
|
4
|
-
import 'co-body';
|
|
5
|
-
import 'cookies';
|
|
6
|
-
import 'cors';
|
|
7
|
-
import 'formidable';
|
|
8
|
-
import 'node:buffer';
|
|
9
|
-
import 'node:http';
|
|
10
|
-
import 'node:stream';
|
|
11
|
-
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 { RsbuildPlugin } from "@rsbuild/core";
|
|
12
3
|
|
|
4
|
+
//#region src/rsbuild.d.ts
|
|
13
5
|
declare function pluginMockServer(options?: MockServerPluginOptions): RsbuildPlugin;
|
|
14
|
-
|
|
15
|
-
export { MockServerPluginOptions, pluginMockServer };
|
|
6
|
+
//#endregion
|
|
7
|
+
export { BodyParserOptions, ExtraRequest, FormidableFile, LogLevel, LogType, Method, MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, MockOptions, MockRequest, MockResponse, MockServerPluginOptions, MockWebsocketItem, ResponseBody, ServerBuildOption, WebSocketSetupContext, pluginMockServer };
|