rspack-plugin-mock 0.1.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 +221 -19
- package/README.zh-CN.md +888 -0
- package/dist/{chunk-G53QRHGV.cjs → chunk-I54ZNZWL.cjs} +942 -607
- package/dist/chunk-OYBMX3GQ.cjs +97 -0
- package/dist/chunk-P5FOCSCE.js +97 -0
- package/dist/{chunk-5MGZAMDI.js → chunk-YSJVV4SH.js} +1104 -769
- package/dist/helper.d.cts +2 -2
- package/dist/helper.d.ts +2 -2
- package/dist/index.cjs +17 -3
- package/dist/index.d.cts +71 -4
- package/dist/index.d.ts +71 -4
- package/dist/index.js +21 -7
- package/dist/rsbuild.cjs +172 -93
- package/dist/rsbuild.d.cts +1 -1
- package/dist/rsbuild.d.ts +1 -1
- package/dist/rsbuild.js +173 -94
- package/dist/rspack-BB-Jtq4f.d.cts +32 -0
- package/dist/rspack-h3uerEgg.d.ts +32 -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 +11 -9
- package/dist/chunk-4BGDHRTO.cjs +0 -68
- 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,200 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
buildMockServer,
|
|
3
|
+
createMockCompiler,
|
|
4
|
+
createMockMiddleware,
|
|
5
|
+
mockWebSocket,
|
|
6
|
+
resolvePluginOptions,
|
|
3
7
|
rewriteRequest
|
|
4
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-YSJVV4SH.js";
|
|
5
9
|
|
|
6
10
|
// src/rsbuild.ts
|
|
7
11
|
import process from "process";
|
|
12
|
+
import { createServer } from "http";
|
|
13
|
+
import path from "path";
|
|
8
14
|
import { isArray, toArray } from "@pengzhanbo/utils";
|
|
9
15
|
import rspack from "@rspack/core";
|
|
16
|
+
import color from "picocolors";
|
|
17
|
+
import { getPortPromise } from "portfinder";
|
|
10
18
|
function pluginMockServer(options = {}) {
|
|
11
19
|
return {
|
|
12
20
|
name: "plugin-mock-server",
|
|
13
21
|
setup(api) {
|
|
14
|
-
|
|
15
|
-
|
|
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
|
+
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 || {})]
|
|
65
28
|
});
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
-
}
|
|
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
|
+
);
|
|
85
37
|
});
|
|
86
38
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
compilerRun = run;
|
|
94
|
-
compilerClose = close;
|
|
95
|
-
compilerUpdateAlias = updateAlias;
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const mockCompiler = createMockCompiler(resolvedOptions);
|
|
42
|
+
api.modifyRsbuildConfig((config) => {
|
|
43
|
+
updateServerProxyConfigByHttpMock(config);
|
|
44
|
+
const mockMiddleware = createMockMiddleware(mockCompiler, resolvedOptions);
|
|
96
45
|
config.dev ??= {};
|
|
97
46
|
config.dev.setupMiddlewares ??= [];
|
|
98
|
-
config.dev.setupMiddlewares.push((middlewares) => {
|
|
99
|
-
mockMiddleware(middlewares);
|
|
47
|
+
config.dev.setupMiddlewares.push((middlewares, server2) => {
|
|
48
|
+
mockMiddleware(middlewares, () => server2.sockWrite("static-changed"));
|
|
100
49
|
});
|
|
101
50
|
});
|
|
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
|
+
}
|
|
60
|
+
let server;
|
|
61
|
+
function startMockServer() {
|
|
62
|
+
mockCompiler.run();
|
|
63
|
+
if (shouldMockWs) {
|
|
64
|
+
server = createServer();
|
|
65
|
+
mockWebSocket(mockCompiler, server, resolvedOptions);
|
|
66
|
+
server.listen(port);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function close() {
|
|
70
|
+
mockCompiler.close();
|
|
71
|
+
server?.close();
|
|
72
|
+
}
|
|
102
73
|
api.onAfterCreateCompiler(({ compiler }) => {
|
|
103
|
-
if (
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
compilerUpdateAlias?.(compiler.options.resolve?.alias || {});
|
|
110
|
-
}
|
|
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 || {});
|
|
111
80
|
}
|
|
112
81
|
});
|
|
113
|
-
api.onAfterStartDevServer(
|
|
114
|
-
api.onAfterStartProdServer(
|
|
115
|
-
api.onExit(
|
|
82
|
+
api.onAfterStartDevServer(startMockServer);
|
|
83
|
+
api.onAfterStartProdServer(startMockServer);
|
|
84
|
+
api.onExit(close);
|
|
116
85
|
}
|
|
117
86
|
};
|
|
118
87
|
}
|
|
88
|
+
function updateServerProxyConfigByHttpMock(config) {
|
|
89
|
+
if (!config.server?.proxy)
|
|
90
|
+
return;
|
|
91
|
+
const onProxyError = (err, _req, res) => {
|
|
92
|
+
console.error(color.red(err?.stack || err.message));
|
|
93
|
+
res.statusCode = 500;
|
|
94
|
+
res.end();
|
|
95
|
+
};
|
|
96
|
+
if (isArray(config.server.proxy)) {
|
|
97
|
+
config.server.proxy = config.server.proxy.map((item) => {
|
|
98
|
+
if (typeof item !== "function" && !item.ws) {
|
|
99
|
+
const onProxyReq = item.onProxyReq;
|
|
100
|
+
const onError = item.onError;
|
|
101
|
+
return {
|
|
102
|
+
...item,
|
|
103
|
+
onError: onError || onProxyError,
|
|
104
|
+
onProxyReq: (proxyReq, req, ...args) => {
|
|
105
|
+
onProxyReq?.(proxyReq, req, ...args);
|
|
106
|
+
rewriteRequest(proxyReq, req);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
return item;
|
|
111
|
+
});
|
|
112
|
+
} else if ("target" in config.server.proxy) {
|
|
113
|
+
const onProxyReq = config.server.proxy.onProxyReq;
|
|
114
|
+
config.server.proxy.onProxyReq = (proxyReq, req, ...args) => {
|
|
115
|
+
onProxyReq?.(proxyReq, req, ...args);
|
|
116
|
+
rewriteRequest(proxyReq, req);
|
|
117
|
+
};
|
|
118
|
+
config.server.proxy.onError ??= onProxyError;
|
|
119
|
+
} else if (config.server.proxy) {
|
|
120
|
+
const proxy = config.server.proxy;
|
|
121
|
+
Object.keys(proxy).forEach((key) => {
|
|
122
|
+
const target = proxy[key];
|
|
123
|
+
const options = typeof target === "string" ? { target } : target;
|
|
124
|
+
if (options.ws)
|
|
125
|
+
return;
|
|
126
|
+
const { onProxyReq, onError, ...rest } = options;
|
|
127
|
+
proxy[key] = {
|
|
128
|
+
...rest,
|
|
129
|
+
onProxyReq: (proxyReq, req, ...args) => {
|
|
130
|
+
onProxyReq?.(proxyReq, req, ...args);
|
|
131
|
+
rewriteRequest(proxyReq, req);
|
|
132
|
+
},
|
|
133
|
+
onError: onError || onProxyError
|
|
134
|
+
};
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function updateServerProxyConfigByWSMock(config, wsPrefix, port) {
|
|
139
|
+
config.server ??= {};
|
|
140
|
+
const proxy = config.server.proxy ??= {};
|
|
141
|
+
const wsTarget = `ws://localhost:${port}`;
|
|
142
|
+
const prefix = toArray(wsPrefix);
|
|
143
|
+
const has = (context) => typeof context === "string" && prefix.includes(context);
|
|
144
|
+
const used = /* @__PURE__ */ new Set();
|
|
145
|
+
function updateProxy(item) {
|
|
146
|
+
if (isArray(item.context)) {
|
|
147
|
+
item.context = item.context.filter(has);
|
|
148
|
+
} else if (has(item.context)) {
|
|
149
|
+
used.add(item.context);
|
|
150
|
+
item.target = wsTarget;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (isArray(proxy)) {
|
|
154
|
+
for (const item of proxy) {
|
|
155
|
+
if (typeof item !== "function" && item.context && item.ws) {
|
|
156
|
+
updateProxy(item);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
prefix.filter((context) => !used.has(context)).forEach((context) => proxy.push({ context, target: wsTarget }));
|
|
160
|
+
} else if ("target" in proxy) {
|
|
161
|
+
if (proxy.ws) {
|
|
162
|
+
updateProxy(proxy);
|
|
163
|
+
const list = config.server.proxy = [proxy];
|
|
164
|
+
prefix.filter((context) => !used.has(context)).forEach((context) => list.push({ context, target: wsTarget }));
|
|
165
|
+
}
|
|
166
|
+
} else {
|
|
167
|
+
Object.entries(proxy).forEach(([, opt]) => {
|
|
168
|
+
if (typeof opt !== "string" && opt.ws) {
|
|
169
|
+
updateProxy(opt);
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
prefix.filter((context) => !used.has(context)).forEach((context) => {
|
|
173
|
+
proxy[context] = { target: wsTarget, ws: true };
|
|
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
|
+
}
|
|
196
|
+
return proxies;
|
|
197
|
+
}
|
|
119
198
|
export {
|
|
120
199
|
pluginMockServer
|
|
121
200
|
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { RspackPluginInstance, Compiler } from '@rspack/core';
|
|
2
|
+
import { L as LogLevel, M as MockServerPluginOptions, S as ServerBuildOption } 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
|
+
declare const logLevels: Record<LogLevel, number>;
|
|
11
|
+
declare function createLogger(prefix: string, defaultLevel?: LogLevel): Logger;
|
|
12
|
+
|
|
13
|
+
interface ResolvedCompilerOptions {
|
|
14
|
+
alias: Record<string, false | string | (string | false)[]>;
|
|
15
|
+
proxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
16
|
+
wsProxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
17
|
+
plugins: RspackPluginInstance[];
|
|
18
|
+
context?: string;
|
|
19
|
+
}
|
|
20
|
+
type ResolvePluginOptions = Required<Omit<MockServerPluginOptions, 'build'>> & ResolvedCompilerOptions & {
|
|
21
|
+
logger: Logger;
|
|
22
|
+
build: false | ServerBuildOption;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
declare class MockServerPlugin implements RspackPluginInstance {
|
|
26
|
+
options: MockServerPluginOptions;
|
|
27
|
+
constructor(options?: MockServerPluginOptions);
|
|
28
|
+
apply(compiler: Compiler): void;
|
|
29
|
+
}
|
|
30
|
+
declare function resolvePluginOptions(compiler: Compiler, options: MockServerPluginOptions): ResolvePluginOptions;
|
|
31
|
+
|
|
32
|
+
export { type Logger as L, MockServerPlugin as M, type ResolvePluginOptions as R, createLogger as c, logLevels as l, resolvePluginOptions as r };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { RspackPluginInstance, Compiler } from '@rspack/core';
|
|
2
|
+
import { L as LogLevel, M as MockServerPluginOptions, S as ServerBuildOption } 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
|
+
declare const logLevels: Record<LogLevel, number>;
|
|
11
|
+
declare function createLogger(prefix: string, defaultLevel?: LogLevel): Logger;
|
|
12
|
+
|
|
13
|
+
interface ResolvedCompilerOptions {
|
|
14
|
+
alias: Record<string, false | string | (string | false)[]>;
|
|
15
|
+
proxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
16
|
+
wsProxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
17
|
+
plugins: RspackPluginInstance[];
|
|
18
|
+
context?: string;
|
|
19
|
+
}
|
|
20
|
+
type ResolvePluginOptions = Required<Omit<MockServerPluginOptions, 'build'>> & ResolvedCompilerOptions & {
|
|
21
|
+
logger: Logger;
|
|
22
|
+
build: false | ServerBuildOption;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
declare class MockServerPlugin implements RspackPluginInstance {
|
|
26
|
+
options: MockServerPluginOptions;
|
|
27
|
+
constructor(options?: MockServerPluginOptions);
|
|
28
|
+
apply(compiler: Compiler): void;
|
|
29
|
+
}
|
|
30
|
+
declare function resolvePluginOptions(compiler: Compiler, options: MockServerPluginOptions): ResolvePluginOptions;
|
|
31
|
+
|
|
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
|
-
import './types-
|
|
3
|
-
export { M as MockServerPlugin, r as
|
|
2
|
+
import './types-C770q3L0.cjs';
|
|
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
|
-
import './types-
|
|
3
|
-
export { M as MockServerPlugin, r as
|
|
2
|
+
import './types-C770q3L0.js';
|
|
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
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MockServerPlugin,
|
|
3
|
-
|
|
4
|
-
} from "./chunk-
|
|
5
|
-
import "./chunk-
|
|
3
|
+
resolvePluginOptions
|
|
4
|
+
} from "./chunk-P5FOCSCE.js";
|
|
5
|
+
import "./chunk-YSJVV4SH.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,13 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rspack-plugin-mock",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.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",
|
|
8
8
|
"keywords": [
|
|
9
9
|
"mock",
|
|
10
10
|
"mock server",
|
|
11
|
+
"rspack",
|
|
12
|
+
"rsbuild",
|
|
11
13
|
"rspack-plugin",
|
|
12
14
|
"rsbuild-plugin"
|
|
13
15
|
],
|
|
@@ -92,12 +94,13 @@
|
|
|
92
94
|
"mime-types": "^2.1.35",
|
|
93
95
|
"path-to-regexp": "^7.1.0",
|
|
94
96
|
"picocolors": "^1.0.1",
|
|
97
|
+
"portfinder": "^1.0.32",
|
|
95
98
|
"ws": "^8.18.0"
|
|
96
99
|
},
|
|
97
100
|
"devDependencies": {
|
|
98
101
|
"@pengzhanbo/eslint-config": "^1.12.0",
|
|
99
|
-
"@rsbuild/core": "1.0.1-beta.
|
|
100
|
-
"@rspack/core": "1.0.0-beta.
|
|
102
|
+
"@rsbuild/core": "1.0.1-beta.9",
|
|
103
|
+
"@rspack/core": "1.0.0-beta.2",
|
|
101
104
|
"@types/co-body": "^6.1.3",
|
|
102
105
|
"@types/cookies": "^0.9.0",
|
|
103
106
|
"@types/cors": "^2.8.17",
|
|
@@ -105,15 +108,14 @@
|
|
|
105
108
|
"@types/formidable": "2.0.6",
|
|
106
109
|
"@types/is-core-module": "^2.2.2",
|
|
107
110
|
"@types/mime-types": "^2.1.4",
|
|
108
|
-
"@types/node": "^22.0
|
|
111
|
+
"@types/node": "^22.1.0",
|
|
109
112
|
"@types/ws": "^8.5.12",
|
|
110
|
-
"bumpp": "^9.4.
|
|
113
|
+
"bumpp": "^9.4.2",
|
|
111
114
|
"conventional-changelog-cli": "^5.0.0",
|
|
112
|
-
"esbuild": "^0.23.0",
|
|
113
115
|
"eslint": "^9.8.0",
|
|
114
|
-
"husky": "^9.1.
|
|
115
|
-
"lint-staged": "^15.2.
|
|
116
|
-
"tsup": "^8.2.
|
|
116
|
+
"husky": "^9.1.4",
|
|
117
|
+
"lint-staged": "^15.2.8",
|
|
118
|
+
"tsup": "^8.2.4",
|
|
117
119
|
"typescript": "^5.5.4"
|
|
118
120
|
},
|
|
119
121
|
"lint-staged": {
|
package/dist/chunk-4BGDHRTO.cjs
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var _chunkG53QRHGVcjs = require('./chunk-G53QRHGV.cjs');
|
|
5
|
-
|
|
6
|
-
// src/rspack.ts
|
|
7
|
-
var _process = require('process'); var _process2 = _interopRequireDefault(_process);
|
|
8
|
-
var _core = require('@rspack/core'); var _core2 = _interopRequireDefault(_core);
|
|
9
|
-
var _utils = require('@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 (_process2.default.env.NODE_ENV !== "production") {
|
|
18
|
-
const { mockMiddleware, run, close } = _chunkG53QRHGVcjs.createManuallyMockMiddleware.call(void 0,
|
|
19
|
-
resolveMiddleOptions(compiler),
|
|
20
|
-
this.options
|
|
21
|
-
);
|
|
22
|
-
const setupMiddlewares = _optionalChain([compilerOptions, 'access', _ => _.devServer, 'optionalAccess', _2 => _2.setupMiddlewares]);
|
|
23
|
-
compilerOptions.devServer = {
|
|
24
|
-
...compilerOptions.devServer,
|
|
25
|
-
setupMiddlewares: (middlewares, devServer) => {
|
|
26
|
-
middlewares = _optionalChain([setupMiddlewares, 'optionalCall', _3 => _3(middlewares, devServer)]) || middlewares;
|
|
27
|
-
middlewares = mockMiddleware(middlewares) || middlewares;
|
|
28
|
-
return middlewares;
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
const proxy = _optionalChain([compilerOptions, 'access', _4 => _4.devServer, 'optionalAccess', _5 => _5.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
|
-
_optionalChain([onProxyReq, 'optionalCall', _6 => _6(proxyReq, req, ...args)]);
|
|
38
|
-
_chunkG53QRHGVcjs.rewriteRequest.call(void 0, 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 = _optionalChain([compilerOptions, 'access', _7 => _7.resolve, 'optionalAccess', _8 => _8.alias]) || {};
|
|
52
|
-
const context = compilerOptions.context;
|
|
53
|
-
const definePluginInstance = _optionalChain([compilerOptions, 'access', _9 => _9.plugins, 'optionalAccess', _10 => _10.find, 'call', _11 => _11(
|
|
54
|
-
(plugin) => plugin instanceof _core2.default.DefinePlugin
|
|
55
|
-
)]);
|
|
56
|
-
const proxies = (_optionalChain([compilerOptions, 'access', _12 => _12.devServer, 'optionalAccess', _13 => _13.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: _utils.toArray.call(void 0, definePluginInstance), proxies };
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
exports.MockServerPlugin = MockServerPlugin; exports.resolveMiddleOptions = resolveMiddleOptions;
|
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
|
-
};
|