rspack-plugin-mock 0.1.0 → 0.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 +117 -10
- package/dist/{chunk-G53QRHGV.cjs → chunk-2S4KCTKW.cjs} +1096 -938
- package/dist/chunk-P6DRAHHI.js +90 -0
- package/dist/{chunk-4BGDHRTO.cjs → chunk-T4AI3L6R.cjs} +41 -19
- package/dist/{chunk-5MGZAMDI.js → chunk-UJAKORAH.js} +1100 -942
- package/dist/helper.d.cts +2 -2
- package/dist/helper.d.ts +2 -2
- package/dist/index.cjs +3 -3
- package/dist/index.d.cts +51 -3
- package/dist/index.d.ts +51 -3
- package/dist/index.js +6 -6
- package/dist/rsbuild.cjs +149 -91
- package/dist/rsbuild.d.cts +1 -1
- package/dist/rsbuild.d.ts +1 -1
- package/dist/rsbuild.js +150 -92
- package/dist/rspack-BcdszmoI.d.cts +29 -0
- package/dist/rspack-E_yBzR5v.d.ts +29 -0
- package/dist/rspack.cjs +3 -3
- package/dist/rspack.d.cts +2 -2
- package/dist/rspack.d.ts +2 -2
- package/dist/rspack.js +4 -4
- package/dist/{types-DhT3pRJ3.d.cts → types-C770q3L0.d.cts} +4 -1
- package/dist/{types-DhT3pRJ3.d.ts → types-C770q3L0.d.ts} +4 -1
- package/package.json +2 -1
- package/dist/chunk-B56QNVSS.js +0 -68
- package/dist/rspack-BI-Ifj4a.d.cts +0 -28
- package/dist/rspack-Db7drzDm.d.ts +0 -28
package/dist/rsbuild.js
CHANGED
|
@@ -1,121 +1,179 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
createMockCompiler,
|
|
3
|
+
createMockMiddleware,
|
|
4
|
+
mockWebSocket,
|
|
5
|
+
resolvePluginOptions,
|
|
3
6
|
rewriteRequest
|
|
4
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-UJAKORAH.js";
|
|
5
8
|
|
|
6
9
|
// src/rsbuild.ts
|
|
7
10
|
import process from "process";
|
|
11
|
+
import { createServer } from "http";
|
|
8
12
|
import { isArray, toArray } from "@pengzhanbo/utils";
|
|
9
13
|
import rspack from "@rspack/core";
|
|
14
|
+
import color from "picocolors";
|
|
15
|
+
import { getPortPromise } from "portfinder";
|
|
10
16
|
function pluginMockServer(options = {}) {
|
|
11
17
|
return {
|
|
12
18
|
name: "plugin-mock-server",
|
|
13
19
|
setup(api) {
|
|
14
20
|
if (process.env.NODE_ENV === "production")
|
|
15
21
|
return;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
console.error(err?.stack || err);
|
|
21
|
-
res.statusCode = 500;
|
|
22
|
-
res.end();
|
|
23
|
-
};
|
|
24
|
-
if (isArray(config.server.proxy)) {
|
|
25
|
-
config.server.proxy = config.server.proxy.map((item) => {
|
|
26
|
-
if (typeof item !== "function" && !item.ws) {
|
|
27
|
-
const onProxyReq = item.onProxyReq;
|
|
28
|
-
const onError = item.onError;
|
|
29
|
-
return {
|
|
30
|
-
...item,
|
|
31
|
-
onError: onError || onProxyError,
|
|
32
|
-
onProxyReq: (proxyReq, req, ...args) => {
|
|
33
|
-
onProxyReq?.(proxyReq, req, ...args);
|
|
34
|
-
rewriteRequest(proxyReq, req);
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
return item;
|
|
39
|
-
});
|
|
40
|
-
} else if ("target" in config.server.proxy) {
|
|
41
|
-
const onProxyReq = config.server.proxy.onProxyReq;
|
|
42
|
-
config.server.proxy.onProxyReq = (proxyReq, req, ...args) => {
|
|
43
|
-
onProxyReq?.(proxyReq, req, ...args);
|
|
44
|
-
rewriteRequest(proxyReq, req);
|
|
45
|
-
};
|
|
46
|
-
config.server.proxy.onError ??= onProxyError;
|
|
47
|
-
} else if (config.server.proxy) {
|
|
48
|
-
const proxy = config.server.proxy;
|
|
49
|
-
Object.keys(proxy).forEach((key) => {
|
|
50
|
-
const target = proxy[key];
|
|
51
|
-
const options2 = typeof target === "string" ? { target } : target;
|
|
52
|
-
if (options2.ws)
|
|
53
|
-
return;
|
|
54
|
-
const { onProxyReq, onError, ...rest } = options2;
|
|
55
|
-
proxy[key] = {
|
|
56
|
-
...rest,
|
|
57
|
-
onProxyReq: (proxyReq, req, ...args) => {
|
|
58
|
-
onProxyReq?.(proxyReq, req, ...args);
|
|
59
|
-
rewriteRequest(proxyReq, req);
|
|
60
|
-
},
|
|
61
|
-
onError: onError || onProxyError
|
|
62
|
-
};
|
|
63
|
-
});
|
|
64
|
-
}
|
|
22
|
+
let port = 3079;
|
|
23
|
+
api.modifyRsbuildConfig(async (config) => {
|
|
24
|
+
const defaultPort = (config.server?.port || port) + 1;
|
|
25
|
+
port = await getPortPromise({ port: defaultPort });
|
|
65
26
|
});
|
|
66
|
-
let
|
|
67
|
-
let
|
|
68
|
-
|
|
27
|
+
let mockCompiler = null;
|
|
28
|
+
let resolvedOptions;
|
|
29
|
+
api.modifyRsbuildConfig(updateServerProxyConfig);
|
|
69
30
|
api.modifyRsbuildConfig((config) => {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
for (const item of proxy) {
|
|
74
|
-
if (typeof item !== "function" && !item.ws && item.context) {
|
|
75
|
-
proxies.push(...toArray(item.context));
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
} else if ("target" in proxy) {
|
|
79
|
-
proxies.push(...toArray(proxy.context));
|
|
80
|
-
} else {
|
|
81
|
-
Object.entries(proxy).forEach(([context, opt]) => {
|
|
82
|
-
if (typeof opt === "string" || !opt.ws) {
|
|
83
|
-
proxies.push(context);
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
const { mockMiddleware, run, close, updateAlias } = createManuallyMockMiddleware({
|
|
88
|
-
plugins: [new rspack.DefinePlugin(config.source?.define || {})],
|
|
31
|
+
config.server ??= {};
|
|
32
|
+
resolvedOptions = resolvePluginOptions(options, {
|
|
33
|
+
proxies: resolveConfigProxies(config, options.wsPrefix || [], port),
|
|
89
34
|
alias: {},
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
compilerUpdateAlias = updateAlias;
|
|
35
|
+
context: api.context.rootPath,
|
|
36
|
+
plugins: [new rspack.DefinePlugin(config.source?.define || {})]
|
|
37
|
+
});
|
|
38
|
+
mockCompiler = createMockCompiler(resolvedOptions);
|
|
39
|
+
const mockMiddleware = createMockMiddleware(mockCompiler, resolvedOptions);
|
|
96
40
|
config.dev ??= {};
|
|
97
41
|
config.dev.setupMiddlewares ??= [];
|
|
98
|
-
config.dev.setupMiddlewares.push((middlewares) => {
|
|
99
|
-
mockMiddleware(middlewares);
|
|
42
|
+
config.dev.setupMiddlewares.push((middlewares, server2) => {
|
|
43
|
+
mockMiddleware(middlewares, () => server2.sockWrite("static-changed"));
|
|
100
44
|
});
|
|
101
45
|
});
|
|
102
46
|
api.onAfterCreateCompiler(({ compiler }) => {
|
|
103
|
-
if (
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
compilerUpdateAlias?.(compiler.options.resolve?.alias || {});
|
|
110
|
-
}
|
|
47
|
+
if ("compilers" in compiler) {
|
|
48
|
+
compiler.compilers.forEach((compiler2) => {
|
|
49
|
+
mockCompiler?.updateAlias(compiler2.options.resolve?.alias || {});
|
|
50
|
+
});
|
|
51
|
+
} else {
|
|
52
|
+
mockCompiler?.updateAlias(compiler.options.resolve?.alias || {});
|
|
111
53
|
}
|
|
112
54
|
});
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
55
|
+
let server;
|
|
56
|
+
function startMockServer() {
|
|
57
|
+
if (!mockCompiler)
|
|
58
|
+
return;
|
|
59
|
+
mockCompiler.run();
|
|
60
|
+
server = createServer();
|
|
61
|
+
mockWebSocket(mockCompiler, server, resolvedOptions);
|
|
62
|
+
server.listen(port);
|
|
63
|
+
}
|
|
64
|
+
function close() {
|
|
65
|
+
mockCompiler?.close();
|
|
66
|
+
server?.close();
|
|
67
|
+
}
|
|
68
|
+
api.onAfterStartDevServer(startMockServer);
|
|
69
|
+
api.onAfterStartProdServer(startMockServer);
|
|
70
|
+
api.onExit(close);
|
|
116
71
|
}
|
|
117
72
|
};
|
|
118
73
|
}
|
|
74
|
+
function updateServerProxyConfig(config) {
|
|
75
|
+
if (!config.server?.proxy)
|
|
76
|
+
return;
|
|
77
|
+
const onProxyError = (err, _req, res) => {
|
|
78
|
+
console.error(color.red(err?.stack || err.message));
|
|
79
|
+
res.statusCode = 500;
|
|
80
|
+
res.end();
|
|
81
|
+
};
|
|
82
|
+
if (isArray(config.server.proxy)) {
|
|
83
|
+
config.server.proxy = config.server.proxy.map((item) => {
|
|
84
|
+
if (typeof item !== "function" && !item.ws) {
|
|
85
|
+
const onProxyReq = item.onProxyReq;
|
|
86
|
+
const onError = item.onError;
|
|
87
|
+
return {
|
|
88
|
+
...item,
|
|
89
|
+
onError: onError || onProxyError,
|
|
90
|
+
onProxyReq: (proxyReq, req, ...args) => {
|
|
91
|
+
onProxyReq?.(proxyReq, req, ...args);
|
|
92
|
+
rewriteRequest(proxyReq, req);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
return item;
|
|
97
|
+
});
|
|
98
|
+
} else if ("target" in config.server.proxy) {
|
|
99
|
+
const onProxyReq = config.server.proxy.onProxyReq;
|
|
100
|
+
config.server.proxy.onProxyReq = (proxyReq, req, ...args) => {
|
|
101
|
+
onProxyReq?.(proxyReq, req, ...args);
|
|
102
|
+
rewriteRequest(proxyReq, req);
|
|
103
|
+
};
|
|
104
|
+
config.server.proxy.onError ??= onProxyError;
|
|
105
|
+
} else if (config.server.proxy) {
|
|
106
|
+
const proxy = config.server.proxy;
|
|
107
|
+
Object.keys(proxy).forEach((key) => {
|
|
108
|
+
const target = proxy[key];
|
|
109
|
+
const options = typeof target === "string" ? { target } : target;
|
|
110
|
+
if (options.ws)
|
|
111
|
+
return;
|
|
112
|
+
const { onProxyReq, onError, ...rest } = options;
|
|
113
|
+
proxy[key] = {
|
|
114
|
+
...rest,
|
|
115
|
+
onProxyReq: (proxyReq, req, ...args) => {
|
|
116
|
+
onProxyReq?.(proxyReq, req, ...args);
|
|
117
|
+
rewriteRequest(proxyReq, req);
|
|
118
|
+
},
|
|
119
|
+
onError: onError || onProxyError
|
|
120
|
+
};
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
function resolveConfigProxies(config, wsPrefix, port) {
|
|
125
|
+
const proxy = config.server.proxy ??= {};
|
|
126
|
+
const proxies = [];
|
|
127
|
+
const wsTarget = `ws://localhost:${port}`;
|
|
128
|
+
const prefix = toArray(wsPrefix);
|
|
129
|
+
const has = (context) => typeof context === "string" && prefix.includes(context);
|
|
130
|
+
const used = /* @__PURE__ */ new Set();
|
|
131
|
+
function updateProxy(item) {
|
|
132
|
+
if (isArray(item.context)) {
|
|
133
|
+
item.context = item.context.filter(has);
|
|
134
|
+
} else if (has(item.context)) {
|
|
135
|
+
used.add(item.context);
|
|
136
|
+
item.target = wsTarget;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (isArray(proxy)) {
|
|
140
|
+
for (const item of proxy) {
|
|
141
|
+
if (typeof item !== "function" && item.context) {
|
|
142
|
+
if (!item.ws) {
|
|
143
|
+
proxies.push(...toArray(item.context));
|
|
144
|
+
} else {
|
|
145
|
+
updateProxy(item);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
prefix.filter((context) => !used.has(context)).forEach((context) => {
|
|
150
|
+
proxy.push({ context, target: wsTarget });
|
|
151
|
+
});
|
|
152
|
+
} else if ("target" in proxy) {
|
|
153
|
+
if (!proxy.ws) {
|
|
154
|
+
proxies.push(...toArray(proxy.context));
|
|
155
|
+
} else {
|
|
156
|
+
updateProxy(proxy);
|
|
157
|
+
const list = config.server.proxy = [proxy];
|
|
158
|
+
prefix.filter((context) => !used.has(context)).forEach((context) => {
|
|
159
|
+
list.push({ context, target: wsTarget });
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
} else {
|
|
163
|
+
Object.entries(proxy).forEach(([context, opt]) => {
|
|
164
|
+
if (typeof opt === "string" || !opt.ws) {
|
|
165
|
+
proxies.push(context);
|
|
166
|
+
}
|
|
167
|
+
if (typeof opt !== "string" && opt.ws) {
|
|
168
|
+
updateProxy(opt);
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
prefix.filter((context) => !used.has(context)).forEach((context) => {
|
|
172
|
+
proxy[context] = { target: wsTarget, ws: true };
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
return proxies;
|
|
176
|
+
}
|
|
119
177
|
export {
|
|
120
178
|
pluginMockServer
|
|
121
179
|
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { RspackPluginInstance, Compiler } from '@rspack/core';
|
|
2
|
+
import { L as LogLevel, M as MockServerPluginOptions } from './types-C770q3L0.cjs';
|
|
3
|
+
|
|
4
|
+
interface Logger {
|
|
5
|
+
debug: (msg: string, level?: boolean | LogLevel) => void;
|
|
6
|
+
info: (msg: string, level?: boolean | LogLevel) => void;
|
|
7
|
+
warn: (msg: string, level?: boolean | LogLevel) => void;
|
|
8
|
+
error: (msg: string, level?: boolean | LogLevel) => void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface ResolvedCompilerOptions {
|
|
12
|
+
alias: Record<string, false | string | (string | false)[]>;
|
|
13
|
+
proxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
14
|
+
wsProxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
15
|
+
plugins: RspackPluginInstance[];
|
|
16
|
+
context?: string;
|
|
17
|
+
}
|
|
18
|
+
type ResolvePluginOptions = Required<MockServerPluginOptions> & ResolvedCompilerOptions & {
|
|
19
|
+
logger: Logger;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
declare class MockServerPlugin implements RspackPluginInstance {
|
|
23
|
+
options: MockServerPluginOptions;
|
|
24
|
+
constructor(options?: MockServerPluginOptions);
|
|
25
|
+
apply(compiler: Compiler): void;
|
|
26
|
+
}
|
|
27
|
+
declare function resolvePluginOptions(compiler: Compiler, options: MockServerPluginOptions): ResolvePluginOptions;
|
|
28
|
+
|
|
29
|
+
export { MockServerPlugin as M, type ResolvePluginOptions as R, resolvePluginOptions as r };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { RspackPluginInstance, Compiler } from '@rspack/core';
|
|
2
|
+
import { L as LogLevel, M as MockServerPluginOptions } from './types-C770q3L0.js';
|
|
3
|
+
|
|
4
|
+
interface Logger {
|
|
5
|
+
debug: (msg: string, level?: boolean | LogLevel) => void;
|
|
6
|
+
info: (msg: string, level?: boolean | LogLevel) => void;
|
|
7
|
+
warn: (msg: string, level?: boolean | LogLevel) => void;
|
|
8
|
+
error: (msg: string, level?: boolean | LogLevel) => void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface ResolvedCompilerOptions {
|
|
12
|
+
alias: Record<string, false | string | (string | false)[]>;
|
|
13
|
+
proxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
14
|
+
wsProxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
15
|
+
plugins: RspackPluginInstance[];
|
|
16
|
+
context?: string;
|
|
17
|
+
}
|
|
18
|
+
type ResolvePluginOptions = Required<MockServerPluginOptions> & ResolvedCompilerOptions & {
|
|
19
|
+
logger: Logger;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
declare class MockServerPlugin implements RspackPluginInstance {
|
|
23
|
+
options: MockServerPluginOptions;
|
|
24
|
+
constructor(options?: MockServerPluginOptions);
|
|
25
|
+
apply(compiler: Compiler): void;
|
|
26
|
+
}
|
|
27
|
+
declare function resolvePluginOptions(compiler: Compiler, options: MockServerPluginOptions): ResolvePluginOptions;
|
|
28
|
+
|
|
29
|
+
export { MockServerPlugin as M, type ResolvePluginOptions as R, 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 _chunkT4AI3L6Rcjs = require('./chunk-T4AI3L6R.cjs');
|
|
5
|
+
require('./chunk-2S4KCTKW.cjs');
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
exports.MockServerPlugin =
|
|
9
|
+
exports.MockServerPlugin = _chunkT4AI3L6Rcjs.MockServerPlugin; exports.resolvePluginOptions = _chunkT4AI3L6Rcjs.resolvePluginOptions;
|
package/dist/rspack.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import '@rspack/core';
|
|
2
|
-
import './types-
|
|
3
|
-
export { M as MockServerPlugin, r as
|
|
2
|
+
import './types-C770q3L0.cjs';
|
|
3
|
+
export { M as MockServerPlugin, r as resolvePluginOptions } from './rspack-BcdszmoI.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
|
-
import './types-
|
|
3
|
-
export { M as MockServerPlugin, r as
|
|
2
|
+
import './types-C770q3L0.js';
|
|
3
|
+
export { M as MockServerPlugin, r as resolvePluginOptions } from './rspack-E_yBzR5v.js';
|
|
4
4
|
import 'node:buffer';
|
|
5
5
|
import 'node:http';
|
|
6
6
|
import 'node:stream';
|
package/dist/rspack.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MockServerPlugin,
|
|
3
|
-
|
|
4
|
-
} from "./chunk-
|
|
5
|
-
import "./chunk-
|
|
3
|
+
resolvePluginOptions
|
|
4
|
+
} from "./chunk-P6DRAHHI.js";
|
|
5
|
+
import "./chunk-UJAKORAH.js";
|
|
6
6
|
export {
|
|
7
7
|
MockServerPlugin,
|
|
8
|
-
|
|
8
|
+
resolvePluginOptions
|
|
9
9
|
};
|
|
@@ -36,6 +36,7 @@ interface MockServerPluginOptions {
|
|
|
36
36
|
* @default []
|
|
37
37
|
* @example ['/socket.io']
|
|
38
38
|
*/
|
|
39
|
+
wsPrefix?: string | string[];
|
|
39
40
|
/**
|
|
40
41
|
* Configure the matching context for `include` and `exclude`.
|
|
41
42
|
*
|
|
@@ -76,6 +77,7 @@ interface MockServerPluginOptions {
|
|
|
76
77
|
* 当你希望每次修改mock文件都刷新页面时,可以打开此选项。
|
|
77
78
|
* @default false
|
|
78
79
|
*/
|
|
80
|
+
reload?: boolean;
|
|
79
81
|
/**
|
|
80
82
|
* Configure to `cors`
|
|
81
83
|
*
|
|
@@ -108,6 +110,7 @@ interface MockServerPluginOptions {
|
|
|
108
110
|
* 当需要构建一个小型mock服务时,可配置此项
|
|
109
111
|
* @default false
|
|
110
112
|
*/
|
|
113
|
+
build?: boolean | ServerBuildOption;
|
|
111
114
|
/**
|
|
112
115
|
* Priority sorting for path matching rules is valid only for rules containing dynamic parameters.
|
|
113
116
|
* In most cases, the default sorting rules can meet the needs.
|
|
@@ -564,4 +567,4 @@ type FormidableFile = formidable.File | formidable.File[];
|
|
|
564
567
|
type LogType = 'info' | 'warn' | 'error' | 'debug';
|
|
565
568
|
type LogLevel = LogType | 'silent';
|
|
566
569
|
|
|
567
|
-
export type { BodyParserOptions as B, ExtraRequest as E, FormidableFile as F,
|
|
570
|
+
export type { BodyParserOptions as B, ExtraRequest as E, FormidableFile as F, LogLevel as L, MockServerPluginOptions as M, ResponseBody as R, ServerBuildOption as S, WebSocketSetupContext as W, MockOptions as a, MockHttpItem as b, MockWebsocketItem as c, MockMatchPriority as d, MockMatchSpecialPriority as e, Method as f, MockRequest as g, MockResponse as h, LogType as i };
|
|
@@ -36,6 +36,7 @@ interface MockServerPluginOptions {
|
|
|
36
36
|
* @default []
|
|
37
37
|
* @example ['/socket.io']
|
|
38
38
|
*/
|
|
39
|
+
wsPrefix?: string | string[];
|
|
39
40
|
/**
|
|
40
41
|
* Configure the matching context for `include` and `exclude`.
|
|
41
42
|
*
|
|
@@ -76,6 +77,7 @@ interface MockServerPluginOptions {
|
|
|
76
77
|
* 当你希望每次修改mock文件都刷新页面时,可以打开此选项。
|
|
77
78
|
* @default false
|
|
78
79
|
*/
|
|
80
|
+
reload?: boolean;
|
|
79
81
|
/**
|
|
80
82
|
* Configure to `cors`
|
|
81
83
|
*
|
|
@@ -108,6 +110,7 @@ interface MockServerPluginOptions {
|
|
|
108
110
|
* 当需要构建一个小型mock服务时,可配置此项
|
|
109
111
|
* @default false
|
|
110
112
|
*/
|
|
113
|
+
build?: boolean | ServerBuildOption;
|
|
111
114
|
/**
|
|
112
115
|
* Priority sorting for path matching rules is valid only for rules containing dynamic parameters.
|
|
113
116
|
* In most cases, the default sorting rules can meet the needs.
|
|
@@ -564,4 +567,4 @@ type FormidableFile = formidable.File | formidable.File[];
|
|
|
564
567
|
type LogType = 'info' | 'warn' | 'error' | 'debug';
|
|
565
568
|
type LogLevel = LogType | 'silent';
|
|
566
569
|
|
|
567
|
-
export type { BodyParserOptions as B, ExtraRequest as E, FormidableFile as F,
|
|
570
|
+
export type { BodyParserOptions as B, ExtraRequest as E, FormidableFile as F, LogLevel as L, MockServerPluginOptions as M, ResponseBody as R, ServerBuildOption as S, WebSocketSetupContext as W, MockOptions as a, MockHttpItem as b, MockWebsocketItem as c, MockMatchPriority as d, MockMatchSpecialPriority as e, Method as f, MockRequest as g, MockResponse as h, LogType as i };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rspack-plugin-mock",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.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",
|
|
@@ -92,6 +92,7 @@
|
|
|
92
92
|
"mime-types": "^2.1.35",
|
|
93
93
|
"path-to-regexp": "^7.1.0",
|
|
94
94
|
"picocolors": "^1.0.1",
|
|
95
|
+
"portfinder": "^1.0.32",
|
|
95
96
|
"ws": "^8.18.0"
|
|
96
97
|
},
|
|
97
98
|
"devDependencies": {
|
package/dist/chunk-B56QNVSS.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createManuallyMockMiddleware,
|
|
3
|
-
rewriteRequest
|
|
4
|
-
} from "./chunk-5MGZAMDI.js";
|
|
5
|
-
|
|
6
|
-
// src/rspack.ts
|
|
7
|
-
import process from "process";
|
|
8
|
-
import rspack from "@rspack/core";
|
|
9
|
-
import { toArray } from "@pengzhanbo/utils";
|
|
10
|
-
var PLUGIN_NAME = "rspack-plugin-mock";
|
|
11
|
-
var MockServerPlugin = class {
|
|
12
|
-
constructor(options = {}) {
|
|
13
|
-
this.options = options;
|
|
14
|
-
}
|
|
15
|
-
apply(compiler) {
|
|
16
|
-
const compilerOptions = compiler.options;
|
|
17
|
-
if (process.env.NODE_ENV !== "production") {
|
|
18
|
-
const { mockMiddleware, run, close } = createManuallyMockMiddleware(
|
|
19
|
-
resolveMiddleOptions(compiler),
|
|
20
|
-
this.options
|
|
21
|
-
);
|
|
22
|
-
const setupMiddlewares = compilerOptions.devServer?.setupMiddlewares;
|
|
23
|
-
compilerOptions.devServer = {
|
|
24
|
-
...compilerOptions.devServer,
|
|
25
|
-
setupMiddlewares: (middlewares, devServer) => {
|
|
26
|
-
middlewares = setupMiddlewares?.(middlewares, devServer) || middlewares;
|
|
27
|
-
middlewares = mockMiddleware(middlewares) || middlewares;
|
|
28
|
-
return middlewares;
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
const proxy = compilerOptions.devServer?.proxy || [];
|
|
32
|
-
if (proxy.length) {
|
|
33
|
-
compilerOptions.devServer.proxy = proxy.map((item) => {
|
|
34
|
-
if (typeof item !== "function" && !item.ws) {
|
|
35
|
-
const onProxyReq = item.onProxyReq;
|
|
36
|
-
item.onProxyReq = (proxyReq, req, ...args) => {
|
|
37
|
-
onProxyReq?.(proxyReq, req, ...args);
|
|
38
|
-
rewriteRequest(proxyReq, req);
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
return item;
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
compiler.hooks.watchRun.tap(PLUGIN_NAME, () => run());
|
|
45
|
-
compiler.hooks.watchClose.tap(PLUGIN_NAME, () => close());
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
function resolveMiddleOptions(compiler) {
|
|
50
|
-
const compilerOptions = compiler.options;
|
|
51
|
-
const alias = compilerOptions.resolve?.alias || {};
|
|
52
|
-
const context = compilerOptions.context;
|
|
53
|
-
const definePluginInstance = compilerOptions.plugins?.find(
|
|
54
|
-
(plugin) => plugin instanceof rspack.DefinePlugin
|
|
55
|
-
);
|
|
56
|
-
const proxies = (compilerOptions.devServer?.proxy || []).map((item) => {
|
|
57
|
-
if (typeof item !== "function" && !item.ws && item.context) {
|
|
58
|
-
return item.context;
|
|
59
|
-
}
|
|
60
|
-
return [];
|
|
61
|
-
}).flat();
|
|
62
|
-
return { alias, context, plugins: toArray(definePluginInstance), proxies };
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export {
|
|
66
|
-
MockServerPlugin,
|
|
67
|
-
resolveMiddleOptions
|
|
68
|
-
};
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { RspackPluginInstance, RspackOptionsNormalized, Compiler } from '@rspack/core';
|
|
2
|
-
import { M as MockServerPluginOptions } from './types-DhT3pRJ3.cjs';
|
|
3
|
-
|
|
4
|
-
interface MiddlewareOptions {
|
|
5
|
-
alias: Record<string, false | string | (string | false)[]>;
|
|
6
|
-
proxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
7
|
-
context?: string;
|
|
8
|
-
plugins: RspackPluginInstance[];
|
|
9
|
-
}
|
|
10
|
-
declare function createManuallyMockMiddleware({ alias, proxies, context, plugins }: MiddlewareOptions, pluginOptions: MockServerPluginOptions): {
|
|
11
|
-
mockMiddleware: (middlewares: Middleware[]) => Middleware[];
|
|
12
|
-
run: () => Promise<void>;
|
|
13
|
-
close: () => void;
|
|
14
|
-
updateAlias: (alias: Record<string, false | string | (string | false)[]>) => void;
|
|
15
|
-
};
|
|
16
|
-
declare function createMockMiddleware(middlewareOptions: MiddlewareOptions, pluginOptions: MockServerPluginOptions): (middlewares: Middleware[]) => Middleware[];
|
|
17
|
-
type SetupMiddlewaresFn = NonNullable<NonNullable<RspackOptionsNormalized['devServer']>['setupMiddlewares']>;
|
|
18
|
-
type Middleware = SetupMiddlewaresFn extends (middlewares: (infer T)[], devServer: any) => void ? T : never;
|
|
19
|
-
type Server = SetupMiddlewaresFn extends (middlewares: any, devServer: infer T) => void ? T : never;
|
|
20
|
-
|
|
21
|
-
declare class MockServerPlugin implements RspackPluginInstance {
|
|
22
|
-
options: MockServerPluginOptions;
|
|
23
|
-
constructor(options?: MockServerPluginOptions);
|
|
24
|
-
apply(compiler: Compiler): void;
|
|
25
|
-
}
|
|
26
|
-
declare function resolveMiddleOptions(compiler: Compiler): MiddlewareOptions;
|
|
27
|
-
|
|
28
|
-
export { MockServerPlugin as M, type Server as S, type MiddlewareOptions as a, createMockMiddleware as b, createManuallyMockMiddleware as c, type Middleware as d, resolveMiddleOptions as r };
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { RspackPluginInstance, RspackOptionsNormalized, Compiler } from '@rspack/core';
|
|
2
|
-
import { M as MockServerPluginOptions } from './types-DhT3pRJ3.js';
|
|
3
|
-
|
|
4
|
-
interface MiddlewareOptions {
|
|
5
|
-
alias: Record<string, false | string | (string | false)[]>;
|
|
6
|
-
proxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
7
|
-
context?: string;
|
|
8
|
-
plugins: RspackPluginInstance[];
|
|
9
|
-
}
|
|
10
|
-
declare function createManuallyMockMiddleware({ alias, proxies, context, plugins }: MiddlewareOptions, pluginOptions: MockServerPluginOptions): {
|
|
11
|
-
mockMiddleware: (middlewares: Middleware[]) => Middleware[];
|
|
12
|
-
run: () => Promise<void>;
|
|
13
|
-
close: () => void;
|
|
14
|
-
updateAlias: (alias: Record<string, false | string | (string | false)[]>) => void;
|
|
15
|
-
};
|
|
16
|
-
declare function createMockMiddleware(middlewareOptions: MiddlewareOptions, pluginOptions: MockServerPluginOptions): (middlewares: Middleware[]) => Middleware[];
|
|
17
|
-
type SetupMiddlewaresFn = NonNullable<NonNullable<RspackOptionsNormalized['devServer']>['setupMiddlewares']>;
|
|
18
|
-
type Middleware = SetupMiddlewaresFn extends (middlewares: (infer T)[], devServer: any) => void ? T : never;
|
|
19
|
-
type Server = SetupMiddlewaresFn extends (middlewares: any, devServer: infer T) => void ? T : never;
|
|
20
|
-
|
|
21
|
-
declare class MockServerPlugin implements RspackPluginInstance {
|
|
22
|
-
options: MockServerPluginOptions;
|
|
23
|
-
constructor(options?: MockServerPluginOptions);
|
|
24
|
-
apply(compiler: Compiler): void;
|
|
25
|
-
}
|
|
26
|
-
declare function resolveMiddleOptions(compiler: Compiler): MiddlewareOptions;
|
|
27
|
-
|
|
28
|
-
export { MockServerPlugin as M, type Server as S, type MiddlewareOptions as a, createMockMiddleware as b, createManuallyMockMiddleware as c, type Middleware as d, resolveMiddleOptions as r };
|