rspack-plugin-mock 0.2.0 → 0.3.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 +110 -15
- package/README.zh-CN.md +888 -0
- package/dist/{chunk-2S4KCTKW.cjs → chunk-I54ZNZWL.cjs} +294 -117
- package/dist/{chunk-T4AI3L6R.cjs → chunk-OYBMX3GQ.cjs} +15 -8
- package/dist/{chunk-P6DRAHHI.js → chunk-P5FOCSCE.js} +9 -2
- package/dist/{chunk-UJAKORAH.js → chunk-YSJVV4SH.js} +291 -114
- package/dist/index.cjs +17 -3
- package/dist/index.d.cts +28 -9
- package/dist/index.d.ts +28 -9
- package/dist/index.js +18 -4
- package/dist/rsbuild.cjs +85 -64
- package/dist/rsbuild.js +76 -55
- package/dist/{rspack-E_yBzR5v.d.ts → rspack-BB-Jtq4f.d.cts} +6 -3
- package/dist/{rspack-BcdszmoI.d.cts → rspack-h3uerEgg.d.ts} +6 -3
- package/dist/rspack.cjs +3 -3
- package/dist/rspack.d.cts +1 -1
- package/dist/rspack.d.ts +1 -1
- package/dist/rspack.js +2 -2
- package/package.json +10 -9
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { R as ResolvePluginOptions } from './rspack-
|
|
2
|
-
export { M as MockServerPlugin, r as resolvePluginOptions } from './rspack-
|
|
1
|
+
import { L as Logger, R as ResolvePluginOptions } from './rspack-h3uerEgg.js';
|
|
2
|
+
export { M as MockServerPlugin, c as createLogger, l as logLevels, r as resolvePluginOptions } from './rspack-h3uerEgg.js';
|
|
3
3
|
export { createDefineMock, defineMock } from './helper.js';
|
|
4
|
-
import * as rspackCore from '@rspack/core';
|
|
5
4
|
import { Compiler, RspackPluginInstance, RspackOptionsNormalized } from '@rspack/core';
|
|
6
5
|
import EventEmitter from 'node:events';
|
|
7
6
|
import { FSWatcher } from 'node:fs';
|
|
8
|
-
import { a as MockOptions } from './types-C770q3L0.js';
|
|
7
|
+
import { a as MockOptions, M as MockServerPluginOptions, b as MockHttpItem, c as MockWebsocketItem } from './types-C770q3L0.js';
|
|
8
|
+
import { Server as Server$1 } from 'node:http';
|
|
9
|
+
import { Http2SecureServer } from 'node:http2';
|
|
9
10
|
import 'node:buffer';
|
|
10
|
-
import 'node:http';
|
|
11
11
|
import 'node:stream';
|
|
12
12
|
import 'cookies';
|
|
13
13
|
import 'cors';
|
|
@@ -21,6 +21,7 @@ interface MockCompilerOptions {
|
|
|
21
21
|
cwd?: string;
|
|
22
22
|
include: string | string[];
|
|
23
23
|
exclude: string | string[];
|
|
24
|
+
logger: Logger;
|
|
24
25
|
}
|
|
25
26
|
declare function createMockCompiler(options: MockCompilerOptions): MockCompiler;
|
|
26
27
|
declare class MockCompiler extends EventEmitter {
|
|
@@ -29,7 +30,6 @@ declare class MockCompiler extends EventEmitter {
|
|
|
29
30
|
mockWatcher: FSWatcher;
|
|
30
31
|
moduleType: 'cjs' | 'esm';
|
|
31
32
|
entryFile: string;
|
|
32
|
-
outputFile: string;
|
|
33
33
|
private _mockData;
|
|
34
34
|
private fileFilter;
|
|
35
35
|
private watchInfo?;
|
|
@@ -42,8 +42,6 @@ declare class MockCompiler extends EventEmitter {
|
|
|
42
42
|
updateMockEntry(): Promise<void>;
|
|
43
43
|
getMockFiles(): Promise<string[]>;
|
|
44
44
|
watchMockFiles(): void;
|
|
45
|
-
resolveEntryFile(fileList: string[]): Promise<void>;
|
|
46
|
-
createCompiler(callback: (e: Error | null, stats?: rspackCore.Stats) => void): void;
|
|
47
45
|
}
|
|
48
46
|
|
|
49
47
|
interface MiddlewareOptions {
|
|
@@ -57,4 +55,25 @@ type SetupMiddlewaresFn = NonNullable<NonNullable<RspackOptionsNormalized['devSe
|
|
|
57
55
|
type Middleware = SetupMiddlewaresFn extends (middlewares: (infer T)[], devServer: any) => void ? T : never;
|
|
58
56
|
type Server = SetupMiddlewaresFn extends (middlewares: any, devServer: infer T) => void ? T : never;
|
|
59
57
|
|
|
60
|
-
|
|
58
|
+
interface BaseMiddlewareOptions {
|
|
59
|
+
formidableOptions: MockServerPluginOptions['formidableOptions'];
|
|
60
|
+
cookiesOptions: MockServerPluginOptions['cookiesOptions'];
|
|
61
|
+
bodyParserOptions: MockServerPluginOptions['bodyParserOptions'];
|
|
62
|
+
proxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
63
|
+
logger: Logger;
|
|
64
|
+
priority: MockServerPluginOptions['priority'];
|
|
65
|
+
}
|
|
66
|
+
declare function baseMiddleware(compiler: MockCompiler, { formidableOptions, bodyParserOptions, proxies, cookiesOptions, logger, priority, }: BaseMiddlewareOptions): Middleware;
|
|
67
|
+
|
|
68
|
+
interface MockSocketOptions {
|
|
69
|
+
wsProxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
70
|
+
cookiesOptions: MockServerPluginOptions['cookiesOptions'];
|
|
71
|
+
logger: Logger;
|
|
72
|
+
}
|
|
73
|
+
declare function mockWebSocket(compiler: MockCompiler, httpServer: Server$1 | Http2SecureServer, { wsProxies: proxies, cookiesOptions, logger, }: MockSocketOptions): void;
|
|
74
|
+
|
|
75
|
+
declare function transformRawData(rawData: (readonly [any, string])[]): (MockHttpItem | MockWebsocketItem | MockOptions)[];
|
|
76
|
+
declare function transformMockData(mockList: (MockHttpItem | MockWebsocketItem | MockOptions)[]): Record<string, MockOptions>;
|
|
77
|
+
declare function sortByValidator(mocks: MockOptions): (MockHttpItem | MockWebsocketItem)[];
|
|
78
|
+
|
|
79
|
+
export { type BaseMiddlewareOptions, Logger, type Middleware, type MiddlewareOptions, type MockSocketOptions, type Server, baseMiddleware, createMockCompiler, createMockMiddleware, mockWebSocket, sortByValidator, transformMockData, transformRawData };
|
package/dist/index.js
CHANGED
|
@@ -1,20 +1,34 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MockServerPlugin,
|
|
3
3
|
resolvePluginOptions
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-P5FOCSCE.js";
|
|
5
5
|
import {
|
|
6
6
|
createDefineMock,
|
|
7
7
|
defineMock
|
|
8
8
|
} from "./chunk-HTOWPFQ7.js";
|
|
9
9
|
import {
|
|
10
|
+
baseMiddleware,
|
|
11
|
+
createLogger,
|
|
10
12
|
createMockCompiler,
|
|
11
|
-
createMockMiddleware
|
|
12
|
-
|
|
13
|
+
createMockMiddleware,
|
|
14
|
+
logLevels,
|
|
15
|
+
mockWebSocket,
|
|
16
|
+
sortByValidator,
|
|
17
|
+
transformMockData,
|
|
18
|
+
transformRawData
|
|
19
|
+
} from "./chunk-YSJVV4SH.js";
|
|
13
20
|
export {
|
|
14
21
|
MockServerPlugin,
|
|
22
|
+
baseMiddleware,
|
|
15
23
|
createDefineMock,
|
|
24
|
+
createLogger,
|
|
16
25
|
createMockCompiler,
|
|
17
26
|
createMockMiddleware,
|
|
18
27
|
defineMock,
|
|
19
|
-
|
|
28
|
+
logLevels,
|
|
29
|
+
mockWebSocket,
|
|
30
|
+
resolvePluginOptions,
|
|
31
|
+
sortByValidator,
|
|
32
|
+
transformMockData,
|
|
33
|
+
transformRawData
|
|
20
34
|
};
|
package/dist/rsbuild.cjs
CHANGED
|
@@ -4,11 +4,13 @@
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
|
|
8
|
+
var _chunkI54ZNZWLcjs = require('./chunk-I54ZNZWL.cjs');
|
|
8
9
|
|
|
9
10
|
// src/rsbuild.ts
|
|
10
11
|
var _process = require('process'); var _process2 = _interopRequireDefault(_process);
|
|
11
12
|
var _http = require('http');
|
|
13
|
+
var _path = require('path'); var _path2 = _interopRequireDefault(_path);
|
|
12
14
|
var _utils = require('@pengzhanbo/utils');
|
|
13
15
|
var _core = require('@rspack/core'); var _core2 = _interopRequireDefault(_core);
|
|
14
16
|
var _picocolors = require('picocolors'); var _picocolors2 = _interopRequireDefault(_picocolors);
|
|
@@ -17,65 +19,77 @@ function pluginMockServer(options = {}) {
|
|
|
17
19
|
return {
|
|
18
20
|
name: "plugin-mock-server",
|
|
19
21
|
setup(api) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
22
|
+
const rsbuildConfig = api.getRsbuildConfig();
|
|
23
|
+
const resolvedOptions = _chunkI54ZNZWLcjs.resolvePluginOptions.call(void 0, options, {
|
|
24
|
+
proxies: resolveConfigProxies(rsbuildConfig),
|
|
25
|
+
alias: {},
|
|
26
|
+
context: api.context.rootPath,
|
|
27
|
+
plugins: [new _core2.default.DefinePlugin(_optionalChain([rsbuildConfig, 'access', _ => _.source, 'optionalAccess', _2 => _2.define]) || {})]
|
|
26
28
|
});
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
if (_process2.default.env.NODE_ENV === "production") {
|
|
30
|
+
if (resolvedOptions.build) {
|
|
31
|
+
api.onAfterBuild(async () => {
|
|
32
|
+
const config = api.getNormalizedConfig();
|
|
33
|
+
await _chunkI54ZNZWLcjs.buildMockServer.call(void 0,
|
|
34
|
+
resolvedOptions,
|
|
35
|
+
_path2.default.resolve(_process2.default.cwd(), config.output.distPath.root || "dist")
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const mockCompiler = _chunkI54ZNZWLcjs.createMockCompiler.call(void 0, resolvedOptions);
|
|
30
42
|
api.modifyRsbuildConfig((config) => {
|
|
31
|
-
config
|
|
32
|
-
|
|
33
|
-
proxies: resolveConfigProxies(config, options.wsPrefix || [], port),
|
|
34
|
-
alias: {},
|
|
35
|
-
context: api.context.rootPath,
|
|
36
|
-
plugins: [new _core2.default.DefinePlugin(_optionalChain([config, 'access', _3 => _3.source, 'optionalAccess', _4 => _4.define]) || {})]
|
|
37
|
-
});
|
|
38
|
-
mockCompiler = _chunk2S4KCTKWcjs.createMockCompiler.call(void 0, resolvedOptions);
|
|
39
|
-
const mockMiddleware = _chunk2S4KCTKWcjs.createMockMiddleware.call(void 0, mockCompiler, resolvedOptions);
|
|
43
|
+
updateServerProxyConfigByHttpMock(config);
|
|
44
|
+
const mockMiddleware = _chunkI54ZNZWLcjs.createMockMiddleware.call(void 0, mockCompiler, resolvedOptions);
|
|
40
45
|
config.dev ??= {};
|
|
41
46
|
config.dev.setupMiddlewares ??= [];
|
|
42
47
|
config.dev.setupMiddlewares.push((middlewares, server2) => {
|
|
43
48
|
mockMiddleware(middlewares, () => server2.sockWrite("static-changed"));
|
|
44
49
|
});
|
|
45
50
|
});
|
|
51
|
+
let port = 3079;
|
|
52
|
+
const shouldMockWs = _utils.toArray.call(void 0, resolvedOptions.wsPrefix).length > 0;
|
|
53
|
+
if (shouldMockWs) {
|
|
54
|
+
api.modifyRsbuildConfig(async (config) => {
|
|
55
|
+
const defaultPort = (_optionalChain([config, 'access', _3 => _3.server, 'optionalAccess', _4 => _4.port]) || port) + 1;
|
|
56
|
+
port = await _portfinder.getPortPromise.call(void 0, { port: defaultPort });
|
|
57
|
+
updateServerProxyConfigByWSMock(config, options.wsPrefix || [], port);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
let server;
|
|
61
|
+
function startMockServer() {
|
|
62
|
+
mockCompiler.run();
|
|
63
|
+
if (shouldMockWs) {
|
|
64
|
+
server = _http.createServer.call(void 0, );
|
|
65
|
+
_chunkI54ZNZWLcjs.mockWebSocket.call(void 0, mockCompiler, server, resolvedOptions);
|
|
66
|
+
server.listen(port);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function close() {
|
|
70
|
+
mockCompiler.close();
|
|
71
|
+
_optionalChain([server, 'optionalAccess', _5 => _5.close, 'call', _6 => _6()]);
|
|
72
|
+
}
|
|
46
73
|
api.onAfterCreateCompiler(({ compiler }) => {
|
|
47
74
|
if ("compilers" in compiler) {
|
|
48
75
|
compiler.compilers.forEach((compiler2) => {
|
|
49
|
-
|
|
76
|
+
mockCompiler.updateAlias(_optionalChain([compiler2, 'access', _7 => _7.options, 'access', _8 => _8.resolve, 'optionalAccess', _9 => _9.alias]) || {});
|
|
50
77
|
});
|
|
51
78
|
} else {
|
|
52
|
-
|
|
79
|
+
mockCompiler.updateAlias(_optionalChain([compiler, 'access', _10 => _10.options, 'access', _11 => _11.resolve, 'optionalAccess', _12 => _12.alias]) || {});
|
|
53
80
|
}
|
|
54
81
|
});
|
|
55
|
-
let server;
|
|
56
|
-
function startMockServer() {
|
|
57
|
-
if (!mockCompiler)
|
|
58
|
-
return;
|
|
59
|
-
mockCompiler.run();
|
|
60
|
-
server = _http.createServer.call(void 0, );
|
|
61
|
-
_chunk2S4KCTKWcjs.mockWebSocket.call(void 0, mockCompiler, server, resolvedOptions);
|
|
62
|
-
server.listen(port);
|
|
63
|
-
}
|
|
64
|
-
function close() {
|
|
65
|
-
_optionalChain([mockCompiler, 'optionalAccess', _15 => _15.close, 'call', _16 => _16()]);
|
|
66
|
-
_optionalChain([server, 'optionalAccess', _17 => _17.close, 'call', _18 => _18()]);
|
|
67
|
-
}
|
|
68
82
|
api.onAfterStartDevServer(startMockServer);
|
|
69
83
|
api.onAfterStartProdServer(startMockServer);
|
|
70
84
|
api.onExit(close);
|
|
71
85
|
}
|
|
72
86
|
};
|
|
73
87
|
}
|
|
74
|
-
function
|
|
75
|
-
if (!_optionalChain([config, 'access',
|
|
88
|
+
function updateServerProxyConfigByHttpMock(config) {
|
|
89
|
+
if (!_optionalChain([config, 'access', _13 => _13.server, 'optionalAccess', _14 => _14.proxy]))
|
|
76
90
|
return;
|
|
77
91
|
const onProxyError = (err, _req, res) => {
|
|
78
|
-
console.error(_picocolors2.default.red(_optionalChain([err, 'optionalAccess',
|
|
92
|
+
console.error(_picocolors2.default.red(_optionalChain([err, 'optionalAccess', _15 => _15.stack]) || err.message));
|
|
79
93
|
res.statusCode = 500;
|
|
80
94
|
res.end();
|
|
81
95
|
};
|
|
@@ -88,8 +102,8 @@ function updateServerProxyConfig(config) {
|
|
|
88
102
|
...item,
|
|
89
103
|
onError: onError || onProxyError,
|
|
90
104
|
onProxyReq: (proxyReq, req, ...args) => {
|
|
91
|
-
_optionalChain([onProxyReq, 'optionalCall',
|
|
92
|
-
|
|
105
|
+
_optionalChain([onProxyReq, 'optionalCall', _16 => _16(proxyReq, req, ...args)]);
|
|
106
|
+
_chunkI54ZNZWLcjs.rewriteRequest.call(void 0, proxyReq, req);
|
|
93
107
|
}
|
|
94
108
|
};
|
|
95
109
|
}
|
|
@@ -98,8 +112,8 @@ function updateServerProxyConfig(config) {
|
|
|
98
112
|
} else if ("target" in config.server.proxy) {
|
|
99
113
|
const onProxyReq = config.server.proxy.onProxyReq;
|
|
100
114
|
config.server.proxy.onProxyReq = (proxyReq, req, ...args) => {
|
|
101
|
-
_optionalChain([onProxyReq, 'optionalCall',
|
|
102
|
-
|
|
115
|
+
_optionalChain([onProxyReq, 'optionalCall', _17 => _17(proxyReq, req, ...args)]);
|
|
116
|
+
_chunkI54ZNZWLcjs.rewriteRequest.call(void 0, proxyReq, req);
|
|
103
117
|
};
|
|
104
118
|
config.server.proxy.onError ??= onProxyError;
|
|
105
119
|
} else if (config.server.proxy) {
|
|
@@ -113,17 +127,17 @@ function updateServerProxyConfig(config) {
|
|
|
113
127
|
proxy[key] = {
|
|
114
128
|
...rest,
|
|
115
129
|
onProxyReq: (proxyReq, req, ...args) => {
|
|
116
|
-
_optionalChain([onProxyReq, 'optionalCall',
|
|
117
|
-
|
|
130
|
+
_optionalChain([onProxyReq, 'optionalCall', _18 => _18(proxyReq, req, ...args)]);
|
|
131
|
+
_chunkI54ZNZWLcjs.rewriteRequest.call(void 0, proxyReq, req);
|
|
118
132
|
},
|
|
119
133
|
onError: onError || onProxyError
|
|
120
134
|
};
|
|
121
135
|
});
|
|
122
136
|
}
|
|
123
137
|
}
|
|
124
|
-
function
|
|
138
|
+
function updateServerProxyConfigByWSMock(config, wsPrefix, port) {
|
|
139
|
+
config.server ??= {};
|
|
125
140
|
const proxy = config.server.proxy ??= {};
|
|
126
|
-
const proxies = [];
|
|
127
141
|
const wsTarget = `ws://localhost:${port}`;
|
|
128
142
|
const prefix = _utils.toArray.call(void 0, wsPrefix);
|
|
129
143
|
const has = (context) => typeof context === "string" && prefix.includes(context);
|
|
@@ -138,32 +152,19 @@ function resolveConfigProxies(config, wsPrefix, port) {
|
|
|
138
152
|
}
|
|
139
153
|
if (_utils.isArray.call(void 0, proxy)) {
|
|
140
154
|
for (const item of proxy) {
|
|
141
|
-
if (typeof item !== "function" && item.context) {
|
|
142
|
-
|
|
143
|
-
proxies.push(..._utils.toArray.call(void 0, item.context));
|
|
144
|
-
} else {
|
|
145
|
-
updateProxy(item);
|
|
146
|
-
}
|
|
155
|
+
if (typeof item !== "function" && item.context && item.ws) {
|
|
156
|
+
updateProxy(item);
|
|
147
157
|
}
|
|
148
158
|
}
|
|
149
|
-
prefix.filter((context) => !used.has(context)).forEach((context) => {
|
|
150
|
-
proxy.push({ context, target: wsTarget });
|
|
151
|
-
});
|
|
159
|
+
prefix.filter((context) => !used.has(context)).forEach((context) => proxy.push({ context, target: wsTarget }));
|
|
152
160
|
} else if ("target" in proxy) {
|
|
153
|
-
if (
|
|
154
|
-
proxies.push(..._utils.toArray.call(void 0, proxy.context));
|
|
155
|
-
} else {
|
|
161
|
+
if (proxy.ws) {
|
|
156
162
|
updateProxy(proxy);
|
|
157
163
|
const list = config.server.proxy = [proxy];
|
|
158
|
-
prefix.filter((context) => !used.has(context)).forEach((context) => {
|
|
159
|
-
list.push({ context, target: wsTarget });
|
|
160
|
-
});
|
|
164
|
+
prefix.filter((context) => !used.has(context)).forEach((context) => list.push({ context, target: wsTarget }));
|
|
161
165
|
}
|
|
162
166
|
} else {
|
|
163
|
-
Object.entries(proxy).forEach(([
|
|
164
|
-
if (typeof opt === "string" || !opt.ws) {
|
|
165
|
-
proxies.push(context);
|
|
166
|
-
}
|
|
167
|
+
Object.entries(proxy).forEach(([, opt]) => {
|
|
167
168
|
if (typeof opt !== "string" && opt.ws) {
|
|
168
169
|
updateProxy(opt);
|
|
169
170
|
}
|
|
@@ -172,6 +173,26 @@ function resolveConfigProxies(config, wsPrefix, port) {
|
|
|
172
173
|
proxy[context] = { target: wsTarget, ws: true };
|
|
173
174
|
});
|
|
174
175
|
}
|
|
176
|
+
}
|
|
177
|
+
function resolveConfigProxies(config) {
|
|
178
|
+
config.server ??= {};
|
|
179
|
+
const proxy = config.server.proxy ??= {};
|
|
180
|
+
const proxies = [];
|
|
181
|
+
if (_utils.isArray.call(void 0, proxy)) {
|
|
182
|
+
for (const item of proxy) {
|
|
183
|
+
if (typeof item !== "function" && item.context && !item.ws) {
|
|
184
|
+
proxies.push(..._utils.toArray.call(void 0, item.context));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
} else if ("target" in proxy) {
|
|
188
|
+
if (!proxy.ws)
|
|
189
|
+
proxies.push(..._utils.toArray.call(void 0, proxy.context));
|
|
190
|
+
} else {
|
|
191
|
+
Object.entries(proxy).forEach(([context, opt]) => {
|
|
192
|
+
if (typeof opt === "string" || !opt.ws)
|
|
193
|
+
proxies.push(context);
|
|
194
|
+
});
|
|
195
|
+
}
|
|
175
196
|
return proxies;
|
|
176
197
|
}
|
|
177
198
|
|
package/dist/rsbuild.js
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
|
+
buildMockServer,
|
|
2
3
|
createMockCompiler,
|
|
3
4
|
createMockMiddleware,
|
|
4
5
|
mockWebSocket,
|
|
5
6
|
resolvePluginOptions,
|
|
6
7
|
rewriteRequest
|
|
7
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-YSJVV4SH.js";
|
|
8
9
|
|
|
9
10
|
// src/rsbuild.ts
|
|
10
11
|
import process from "process";
|
|
11
12
|
import { createServer } from "http";
|
|
13
|
+
import path from "path";
|
|
12
14
|
import { isArray, toArray } from "@pengzhanbo/utils";
|
|
13
15
|
import rspack from "@rspack/core";
|
|
14
16
|
import color from "picocolors";
|
|
@@ -17,25 +19,28 @@ function pluginMockServer(options = {}) {
|
|
|
17
19
|
return {
|
|
18
20
|
name: "plugin-mock-server",
|
|
19
21
|
setup(api) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
22
|
+
const rsbuildConfig = api.getRsbuildConfig();
|
|
23
|
+
const resolvedOptions = resolvePluginOptions(options, {
|
|
24
|
+
proxies: resolveConfigProxies(rsbuildConfig),
|
|
25
|
+
alias: {},
|
|
26
|
+
context: api.context.rootPath,
|
|
27
|
+
plugins: [new rspack.DefinePlugin(rsbuildConfig.source?.define || {})]
|
|
26
28
|
});
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
if (process.env.NODE_ENV === "production") {
|
|
30
|
+
if (resolvedOptions.build) {
|
|
31
|
+
api.onAfterBuild(async () => {
|
|
32
|
+
const config = api.getNormalizedConfig();
|
|
33
|
+
await buildMockServer(
|
|
34
|
+
resolvedOptions,
|
|
35
|
+
path.resolve(process.cwd(), config.output.distPath.root || "dist")
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const mockCompiler = createMockCompiler(resolvedOptions);
|
|
30
42
|
api.modifyRsbuildConfig((config) => {
|
|
31
|
-
config
|
|
32
|
-
resolvedOptions = resolvePluginOptions(options, {
|
|
33
|
-
proxies: resolveConfigProxies(config, options.wsPrefix || [], port),
|
|
34
|
-
alias: {},
|
|
35
|
-
context: api.context.rootPath,
|
|
36
|
-
plugins: [new rspack.DefinePlugin(config.source?.define || {})]
|
|
37
|
-
});
|
|
38
|
-
mockCompiler = createMockCompiler(resolvedOptions);
|
|
43
|
+
updateServerProxyConfigByHttpMock(config);
|
|
39
44
|
const mockMiddleware = createMockMiddleware(mockCompiler, resolvedOptions);
|
|
40
45
|
config.dev ??= {};
|
|
41
46
|
config.dev.setupMiddlewares ??= [];
|
|
@@ -43,35 +48,44 @@ function pluginMockServer(options = {}) {
|
|
|
43
48
|
mockMiddleware(middlewares, () => server2.sockWrite("static-changed"));
|
|
44
49
|
});
|
|
45
50
|
});
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
}
|
|
51
|
+
let port = 3079;
|
|
52
|
+
const shouldMockWs = toArray(resolvedOptions.wsPrefix).length > 0;
|
|
53
|
+
if (shouldMockWs) {
|
|
54
|
+
api.modifyRsbuildConfig(async (config) => {
|
|
55
|
+
const defaultPort = (config.server?.port || port) + 1;
|
|
56
|
+
port = await getPortPromise({ port: defaultPort });
|
|
57
|
+
updateServerProxyConfigByWSMock(config, options.wsPrefix || [], port);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
55
60
|
let server;
|
|
56
61
|
function startMockServer() {
|
|
57
|
-
if (!mockCompiler)
|
|
58
|
-
return;
|
|
59
62
|
mockCompiler.run();
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
+
if (shouldMockWs) {
|
|
64
|
+
server = createServer();
|
|
65
|
+
mockWebSocket(mockCompiler, server, resolvedOptions);
|
|
66
|
+
server.listen(port);
|
|
67
|
+
}
|
|
63
68
|
}
|
|
64
69
|
function close() {
|
|
65
|
-
mockCompiler
|
|
70
|
+
mockCompiler.close();
|
|
66
71
|
server?.close();
|
|
67
72
|
}
|
|
73
|
+
api.onAfterCreateCompiler(({ compiler }) => {
|
|
74
|
+
if ("compilers" in compiler) {
|
|
75
|
+
compiler.compilers.forEach((compiler2) => {
|
|
76
|
+
mockCompiler.updateAlias(compiler2.options.resolve?.alias || {});
|
|
77
|
+
});
|
|
78
|
+
} else {
|
|
79
|
+
mockCompiler.updateAlias(compiler.options.resolve?.alias || {});
|
|
80
|
+
}
|
|
81
|
+
});
|
|
68
82
|
api.onAfterStartDevServer(startMockServer);
|
|
69
83
|
api.onAfterStartProdServer(startMockServer);
|
|
70
84
|
api.onExit(close);
|
|
71
85
|
}
|
|
72
86
|
};
|
|
73
87
|
}
|
|
74
|
-
function
|
|
88
|
+
function updateServerProxyConfigByHttpMock(config) {
|
|
75
89
|
if (!config.server?.proxy)
|
|
76
90
|
return;
|
|
77
91
|
const onProxyError = (err, _req, res) => {
|
|
@@ -121,9 +135,9 @@ function updateServerProxyConfig(config) {
|
|
|
121
135
|
});
|
|
122
136
|
}
|
|
123
137
|
}
|
|
124
|
-
function
|
|
138
|
+
function updateServerProxyConfigByWSMock(config, wsPrefix, port) {
|
|
139
|
+
config.server ??= {};
|
|
125
140
|
const proxy = config.server.proxy ??= {};
|
|
126
|
-
const proxies = [];
|
|
127
141
|
const wsTarget = `ws://localhost:${port}`;
|
|
128
142
|
const prefix = toArray(wsPrefix);
|
|
129
143
|
const has = (context) => typeof context === "string" && prefix.includes(context);
|
|
@@ -138,32 +152,19 @@ function resolveConfigProxies(config, wsPrefix, port) {
|
|
|
138
152
|
}
|
|
139
153
|
if (isArray(proxy)) {
|
|
140
154
|
for (const item of proxy) {
|
|
141
|
-
if (typeof item !== "function" && item.context) {
|
|
142
|
-
|
|
143
|
-
proxies.push(...toArray(item.context));
|
|
144
|
-
} else {
|
|
145
|
-
updateProxy(item);
|
|
146
|
-
}
|
|
155
|
+
if (typeof item !== "function" && item.context && item.ws) {
|
|
156
|
+
updateProxy(item);
|
|
147
157
|
}
|
|
148
158
|
}
|
|
149
|
-
prefix.filter((context) => !used.has(context)).forEach((context) => {
|
|
150
|
-
proxy.push({ context, target: wsTarget });
|
|
151
|
-
});
|
|
159
|
+
prefix.filter((context) => !used.has(context)).forEach((context) => proxy.push({ context, target: wsTarget }));
|
|
152
160
|
} else if ("target" in proxy) {
|
|
153
|
-
if (
|
|
154
|
-
proxies.push(...toArray(proxy.context));
|
|
155
|
-
} else {
|
|
161
|
+
if (proxy.ws) {
|
|
156
162
|
updateProxy(proxy);
|
|
157
163
|
const list = config.server.proxy = [proxy];
|
|
158
|
-
prefix.filter((context) => !used.has(context)).forEach((context) => {
|
|
159
|
-
list.push({ context, target: wsTarget });
|
|
160
|
-
});
|
|
164
|
+
prefix.filter((context) => !used.has(context)).forEach((context) => list.push({ context, target: wsTarget }));
|
|
161
165
|
}
|
|
162
166
|
} else {
|
|
163
|
-
Object.entries(proxy).forEach(([
|
|
164
|
-
if (typeof opt === "string" || !opt.ws) {
|
|
165
|
-
proxies.push(context);
|
|
166
|
-
}
|
|
167
|
+
Object.entries(proxy).forEach(([, opt]) => {
|
|
167
168
|
if (typeof opt !== "string" && opt.ws) {
|
|
168
169
|
updateProxy(opt);
|
|
169
170
|
}
|
|
@@ -172,6 +173,26 @@ function resolveConfigProxies(config, wsPrefix, port) {
|
|
|
172
173
|
proxy[context] = { target: wsTarget, ws: true };
|
|
173
174
|
});
|
|
174
175
|
}
|
|
176
|
+
}
|
|
177
|
+
function resolveConfigProxies(config) {
|
|
178
|
+
config.server ??= {};
|
|
179
|
+
const proxy = config.server.proxy ??= {};
|
|
180
|
+
const proxies = [];
|
|
181
|
+
if (isArray(proxy)) {
|
|
182
|
+
for (const item of proxy) {
|
|
183
|
+
if (typeof item !== "function" && item.context && !item.ws) {
|
|
184
|
+
proxies.push(...toArray(item.context));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
} else if ("target" in proxy) {
|
|
188
|
+
if (!proxy.ws)
|
|
189
|
+
proxies.push(...toArray(proxy.context));
|
|
190
|
+
} else {
|
|
191
|
+
Object.entries(proxy).forEach(([context, opt]) => {
|
|
192
|
+
if (typeof opt === "string" || !opt.ws)
|
|
193
|
+
proxies.push(context);
|
|
194
|
+
});
|
|
195
|
+
}
|
|
175
196
|
return proxies;
|
|
176
197
|
}
|
|
177
198
|
export {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { RspackPluginInstance, Compiler } from '@rspack/core';
|
|
2
|
-
import { L as LogLevel, M as MockServerPluginOptions } from './types-C770q3L0.
|
|
2
|
+
import { L as LogLevel, M as MockServerPluginOptions, S as ServerBuildOption } from './types-C770q3L0.cjs';
|
|
3
3
|
|
|
4
4
|
interface Logger {
|
|
5
5
|
debug: (msg: string, level?: boolean | LogLevel) => void;
|
|
@@ -7,6 +7,8 @@ interface Logger {
|
|
|
7
7
|
warn: (msg: string, level?: boolean | LogLevel) => void;
|
|
8
8
|
error: (msg: string, level?: boolean | LogLevel) => void;
|
|
9
9
|
}
|
|
10
|
+
declare const logLevels: Record<LogLevel, number>;
|
|
11
|
+
declare function createLogger(prefix: string, defaultLevel?: LogLevel): Logger;
|
|
10
12
|
|
|
11
13
|
interface ResolvedCompilerOptions {
|
|
12
14
|
alias: Record<string, false | string | (string | false)[]>;
|
|
@@ -15,8 +17,9 @@ interface ResolvedCompilerOptions {
|
|
|
15
17
|
plugins: RspackPluginInstance[];
|
|
16
18
|
context?: string;
|
|
17
19
|
}
|
|
18
|
-
type ResolvePluginOptions = Required<MockServerPluginOptions
|
|
20
|
+
type ResolvePluginOptions = Required<Omit<MockServerPluginOptions, 'build'>> & ResolvedCompilerOptions & {
|
|
19
21
|
logger: Logger;
|
|
22
|
+
build: false | ServerBuildOption;
|
|
20
23
|
};
|
|
21
24
|
|
|
22
25
|
declare class MockServerPlugin implements RspackPluginInstance {
|
|
@@ -26,4 +29,4 @@ declare class MockServerPlugin implements RspackPluginInstance {
|
|
|
26
29
|
}
|
|
27
30
|
declare function resolvePluginOptions(compiler: Compiler, options: MockServerPluginOptions): ResolvePluginOptions;
|
|
28
31
|
|
|
29
|
-
export { MockServerPlugin as M, type ResolvePluginOptions as R, resolvePluginOptions as r };
|
|
32
|
+
export { type Logger as L, MockServerPlugin as M, type ResolvePluginOptions as R, createLogger as c, logLevels as l, resolvePluginOptions as r };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { RspackPluginInstance, Compiler } from '@rspack/core';
|
|
2
|
-
import { L as LogLevel, M as MockServerPluginOptions } from './types-C770q3L0.
|
|
2
|
+
import { L as LogLevel, M as MockServerPluginOptions, S as ServerBuildOption } from './types-C770q3L0.js';
|
|
3
3
|
|
|
4
4
|
interface Logger {
|
|
5
5
|
debug: (msg: string, level?: boolean | LogLevel) => void;
|
|
@@ -7,6 +7,8 @@ interface Logger {
|
|
|
7
7
|
warn: (msg: string, level?: boolean | LogLevel) => void;
|
|
8
8
|
error: (msg: string, level?: boolean | LogLevel) => void;
|
|
9
9
|
}
|
|
10
|
+
declare const logLevels: Record<LogLevel, number>;
|
|
11
|
+
declare function createLogger(prefix: string, defaultLevel?: LogLevel): Logger;
|
|
10
12
|
|
|
11
13
|
interface ResolvedCompilerOptions {
|
|
12
14
|
alias: Record<string, false | string | (string | false)[]>;
|
|
@@ -15,8 +17,9 @@ interface ResolvedCompilerOptions {
|
|
|
15
17
|
plugins: RspackPluginInstance[];
|
|
16
18
|
context?: string;
|
|
17
19
|
}
|
|
18
|
-
type ResolvePluginOptions = Required<MockServerPluginOptions
|
|
20
|
+
type ResolvePluginOptions = Required<Omit<MockServerPluginOptions, 'build'>> & ResolvedCompilerOptions & {
|
|
19
21
|
logger: Logger;
|
|
22
|
+
build: false | ServerBuildOption;
|
|
20
23
|
};
|
|
21
24
|
|
|
22
25
|
declare class MockServerPlugin implements RspackPluginInstance {
|
|
@@ -26,4 +29,4 @@ declare class MockServerPlugin implements RspackPluginInstance {
|
|
|
26
29
|
}
|
|
27
30
|
declare function resolvePluginOptions(compiler: Compiler, options: MockServerPluginOptions): ResolvePluginOptions;
|
|
28
31
|
|
|
29
|
-
export { MockServerPlugin as M, type ResolvePluginOptions as R, resolvePluginOptions as r };
|
|
32
|
+
export { type Logger as L, MockServerPlugin as M, type ResolvePluginOptions as R, createLogger as c, logLevels as l, resolvePluginOptions as r };
|
package/dist/rspack.cjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
var
|
|
5
|
-
require('./chunk-
|
|
4
|
+
var _chunkOYBMX3GQcjs = require('./chunk-OYBMX3GQ.cjs');
|
|
5
|
+
require('./chunk-I54ZNZWL.cjs');
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
exports.MockServerPlugin =
|
|
9
|
+
exports.MockServerPlugin = _chunkOYBMX3GQcjs.MockServerPlugin; exports.resolvePluginOptions = _chunkOYBMX3GQcjs.resolvePluginOptions;
|
package/dist/rspack.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import '@rspack/core';
|
|
2
2
|
import './types-C770q3L0.cjs';
|
|
3
|
-
export { M as MockServerPlugin, r as resolvePluginOptions } from './rspack-
|
|
3
|
+
export { M as MockServerPlugin, r as resolvePluginOptions } from './rspack-BB-Jtq4f.cjs';
|
|
4
4
|
import 'node:buffer';
|
|
5
5
|
import 'node:http';
|
|
6
6
|
import 'node:stream';
|
package/dist/rspack.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import '@rspack/core';
|
|
2
2
|
import './types-C770q3L0.js';
|
|
3
|
-
export { M as MockServerPlugin, r as resolvePluginOptions } from './rspack-
|
|
3
|
+
export { M as MockServerPlugin, r as resolvePluginOptions } from './rspack-h3uerEgg.js';
|
|
4
4
|
import 'node:buffer';
|
|
5
5
|
import 'node:http';
|
|
6
6
|
import 'node:stream';
|
package/dist/rspack.js
CHANGED