rspack-plugin-mock 0.1.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/LICENSE +21 -0
- package/README.md +690 -0
- package/dist/chunk-4BGDHRTO.cjs +68 -0
- package/dist/chunk-5MGZAMDI.js +1089 -0
- package/dist/chunk-B56QNVSS.js +68 -0
- package/dist/chunk-G53QRHGV.cjs +1089 -0
- package/dist/chunk-HTOWPFQ7.js +20 -0
- package/dist/chunk-KE2LVHUP.cjs +20 -0
- package/dist/helper.cjs +8 -0
- package/dist/helper.d.cts +72 -0
- package/dist/helper.d.ts +72 -0
- package/dist/helper.js +8 -0
- package/dist/index.cjs +20 -0
- package/dist/index.d.cts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +20 -0
- package/dist/json5-loader.cjs +11 -0
- package/dist/json5-loader.d.cts +5 -0
- package/dist/json5-loader.d.ts +5 -0
- package/dist/json5-loader.js +11 -0
- package/dist/rsbuild.cjs +121 -0
- package/dist/rsbuild.d.cts +14 -0
- package/dist/rsbuild.d.ts +14 -0
- package/dist/rsbuild.js +121 -0
- package/dist/rspack-BI-Ifj4a.d.cts +28 -0
- package/dist/rspack-Db7drzDm.d.ts +28 -0
- package/dist/rspack.cjs +9 -0
- package/dist/rspack.d.cts +11 -0
- package/dist/rspack.d.ts +11 -0
- package/dist/rspack.js +9 -0
- package/dist/types-DhT3pRJ3.d.cts +567 -0
- package/dist/types-DhT3pRJ3.d.ts +567 -0
- package/package.json +129 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// src/core/defineMock.ts
|
|
2
|
+
import { isArray } from "@pengzhanbo/utils";
|
|
3
|
+
function defineMock(config) {
|
|
4
|
+
return config;
|
|
5
|
+
}
|
|
6
|
+
function createDefineMock(transformer) {
|
|
7
|
+
const define = (config) => {
|
|
8
|
+
if (isArray(config))
|
|
9
|
+
config = config.map((item) => transformer(item) || item);
|
|
10
|
+
else
|
|
11
|
+
config = transformer(config) || config;
|
|
12
|
+
return config;
|
|
13
|
+
};
|
|
14
|
+
return define;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
defineMock,
|
|
19
|
+
createDefineMock
|
|
20
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/core/defineMock.ts
|
|
2
|
+
var _utils = require('@pengzhanbo/utils');
|
|
3
|
+
function defineMock(config) {
|
|
4
|
+
return config;
|
|
5
|
+
}
|
|
6
|
+
function createDefineMock(transformer) {
|
|
7
|
+
const define = (config) => {
|
|
8
|
+
if (_utils.isArray.call(void 0, config))
|
|
9
|
+
config = config.map((item) => transformer(item) || item);
|
|
10
|
+
else
|
|
11
|
+
config = transformer(config) || config;
|
|
12
|
+
return config;
|
|
13
|
+
};
|
|
14
|
+
return define;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
exports.defineMock = defineMock; exports.createDefineMock = createDefineMock;
|
package/dist/helper.cjs
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { a as MockHttpItem, b as MockWebsocketItem, c as MockOptions } from './types-DhT3pRJ3.cjs';
|
|
2
|
+
export { B as BodyParserOptions, E as ExtraRequest, F as FormidableFile, i as LogLevel, L as LogType, f as Method, d as MockMatchPriority, e as MockMatchSpecialPriority, g as MockRequest, h as MockResponse, M as MockServerPluginOptions, R as ResponseBody, S as ServerBuildOption, W as WebSocketSetupContext } from './types-DhT3pRJ3.cjs';
|
|
3
|
+
import 'node:buffer';
|
|
4
|
+
import 'node:http';
|
|
5
|
+
import 'node:stream';
|
|
6
|
+
import 'cookies';
|
|
7
|
+
import 'cors';
|
|
8
|
+
import 'formidable';
|
|
9
|
+
import 'ws';
|
|
10
|
+
import 'co-body';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* mock config Type helper
|
|
14
|
+
*
|
|
15
|
+
* mock配置 类型帮助函数
|
|
16
|
+
* @param config see config docs:
|
|
17
|
+
* {@link https://vite-plugin-mock-dev-server.netlify.app/en/guide/mock-config en-US DOC} |
|
|
18
|
+
* {@link https://vite-plugin-mock-dev-server.netlify.app/guide/mock-config zh-CN DOC}
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* Mock Http Request
|
|
22
|
+
* ```ts
|
|
23
|
+
* export default defineMock({
|
|
24
|
+
* url: '/api/example',
|
|
25
|
+
* method: ['GET', 'POST'],
|
|
26
|
+
* body: { a: 1 },
|
|
27
|
+
* })
|
|
28
|
+
* ```
|
|
29
|
+
* ```ts
|
|
30
|
+
* export default defineMock({
|
|
31
|
+
* url: '/api/example',
|
|
32
|
+
* method: 'GET',
|
|
33
|
+
* body: ({ query }) => ({ a: 1, b: query.b }),
|
|
34
|
+
* })
|
|
35
|
+
* ```
|
|
36
|
+
* @example
|
|
37
|
+
* Mock WebSocket
|
|
38
|
+
* ```ts
|
|
39
|
+
* export default defineMock({
|
|
40
|
+
* url: '/socket.io',
|
|
41
|
+
* ws: true,
|
|
42
|
+
* setup(wss) {
|
|
43
|
+
* wss.on('connection', (ws) => {
|
|
44
|
+
* ws.on('message', (rawData) => console.log(rawData))
|
|
45
|
+
* ws.send('data')
|
|
46
|
+
* })
|
|
47
|
+
* },
|
|
48
|
+
* })
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
declare function defineMock(config: MockHttpItem): MockHttpItem;
|
|
52
|
+
declare function defineMock(config: MockWebsocketItem): MockWebsocketItem;
|
|
53
|
+
declare function defineMock(config: MockOptions): MockOptions;
|
|
54
|
+
/**
|
|
55
|
+
* Return a custom defineMock function to support preprocessing of mock config.
|
|
56
|
+
*
|
|
57
|
+
* 返回一个自定义的 defineMock 函数,用于支持对 mock config 的预处理。
|
|
58
|
+
* @param transformer preprocessing function
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* const definePostMock = createDefineMock((mock) => {
|
|
62
|
+
* mock.url = '/api/post/' + mock.url
|
|
63
|
+
* })
|
|
64
|
+
* export default definePostMock({
|
|
65
|
+
* url: 'list',
|
|
66
|
+
* body: [{ title: '1' }, { title: '2' }],
|
|
67
|
+
* })
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
declare function createDefineMock(transformer: (mock: MockHttpItem | MockWebsocketItem) => MockHttpItem | MockWebsocketItem | void): typeof defineMock;
|
|
71
|
+
|
|
72
|
+
export { MockHttpItem, MockOptions, MockWebsocketItem, createDefineMock, defineMock };
|
package/dist/helper.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { a as MockHttpItem, b as MockWebsocketItem, c as MockOptions } from './types-DhT3pRJ3.js';
|
|
2
|
+
export { B as BodyParserOptions, E as ExtraRequest, F as FormidableFile, i as LogLevel, L as LogType, f as Method, d as MockMatchPriority, e as MockMatchSpecialPriority, g as MockRequest, h as MockResponse, M as MockServerPluginOptions, R as ResponseBody, S as ServerBuildOption, W as WebSocketSetupContext } from './types-DhT3pRJ3.js';
|
|
3
|
+
import 'node:buffer';
|
|
4
|
+
import 'node:http';
|
|
5
|
+
import 'node:stream';
|
|
6
|
+
import 'cookies';
|
|
7
|
+
import 'cors';
|
|
8
|
+
import 'formidable';
|
|
9
|
+
import 'ws';
|
|
10
|
+
import 'co-body';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* mock config Type helper
|
|
14
|
+
*
|
|
15
|
+
* mock配置 类型帮助函数
|
|
16
|
+
* @param config see config docs:
|
|
17
|
+
* {@link https://vite-plugin-mock-dev-server.netlify.app/en/guide/mock-config en-US DOC} |
|
|
18
|
+
* {@link https://vite-plugin-mock-dev-server.netlify.app/guide/mock-config zh-CN DOC}
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* Mock Http Request
|
|
22
|
+
* ```ts
|
|
23
|
+
* export default defineMock({
|
|
24
|
+
* url: '/api/example',
|
|
25
|
+
* method: ['GET', 'POST'],
|
|
26
|
+
* body: { a: 1 },
|
|
27
|
+
* })
|
|
28
|
+
* ```
|
|
29
|
+
* ```ts
|
|
30
|
+
* export default defineMock({
|
|
31
|
+
* url: '/api/example',
|
|
32
|
+
* method: 'GET',
|
|
33
|
+
* body: ({ query }) => ({ a: 1, b: query.b }),
|
|
34
|
+
* })
|
|
35
|
+
* ```
|
|
36
|
+
* @example
|
|
37
|
+
* Mock WebSocket
|
|
38
|
+
* ```ts
|
|
39
|
+
* export default defineMock({
|
|
40
|
+
* url: '/socket.io',
|
|
41
|
+
* ws: true,
|
|
42
|
+
* setup(wss) {
|
|
43
|
+
* wss.on('connection', (ws) => {
|
|
44
|
+
* ws.on('message', (rawData) => console.log(rawData))
|
|
45
|
+
* ws.send('data')
|
|
46
|
+
* })
|
|
47
|
+
* },
|
|
48
|
+
* })
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
declare function defineMock(config: MockHttpItem): MockHttpItem;
|
|
52
|
+
declare function defineMock(config: MockWebsocketItem): MockWebsocketItem;
|
|
53
|
+
declare function defineMock(config: MockOptions): MockOptions;
|
|
54
|
+
/**
|
|
55
|
+
* Return a custom defineMock function to support preprocessing of mock config.
|
|
56
|
+
*
|
|
57
|
+
* 返回一个自定义的 defineMock 函数,用于支持对 mock config 的预处理。
|
|
58
|
+
* @param transformer preprocessing function
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* const definePostMock = createDefineMock((mock) => {
|
|
62
|
+
* mock.url = '/api/post/' + mock.url
|
|
63
|
+
* })
|
|
64
|
+
* export default definePostMock({
|
|
65
|
+
* url: 'list',
|
|
66
|
+
* body: [{ title: '1' }, { title: '2' }],
|
|
67
|
+
* })
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
declare function createDefineMock(transformer: (mock: MockHttpItem | MockWebsocketItem) => MockHttpItem | MockWebsocketItem | void): typeof defineMock;
|
|
71
|
+
|
|
72
|
+
export { MockHttpItem, MockOptions, MockWebsocketItem, createDefineMock, defineMock };
|
package/dist/helper.js
ADDED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunk4BGDHRTOcjs = require('./chunk-4BGDHRTO.cjs');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
var _chunkKE2LVHUPcjs = require('./chunk-KE2LVHUP.cjs');
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
var _chunkG53QRHGVcjs = require('./chunk-G53QRHGV.cjs');
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
exports.MockServerPlugin = _chunk4BGDHRTOcjs.MockServerPlugin; exports.createDefineMock = _chunkKE2LVHUPcjs.createDefineMock; exports.createManuallyMockMiddleware = _chunkG53QRHGVcjs.createManuallyMockMiddleware; exports.createMockMiddleware = _chunkG53QRHGVcjs.createMockMiddleware; exports.defineMock = _chunkKE2LVHUPcjs.defineMock; exports.resolveMiddleOptions = _chunk4BGDHRTOcjs.resolveMiddleOptions;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { d as Middleware, a as MiddlewareOptions, M as MockServerPlugin, S as Server, c as createManuallyMockMiddleware, b as createMockMiddleware, r as resolveMiddleOptions } from './rspack-BI-Ifj4a.cjs';
|
|
2
|
+
export { createDefineMock, defineMock } from './helper.cjs';
|
|
3
|
+
import '@rspack/core';
|
|
4
|
+
import './types-DhT3pRJ3.cjs';
|
|
5
|
+
import 'node:buffer';
|
|
6
|
+
import 'node:http';
|
|
7
|
+
import 'node:stream';
|
|
8
|
+
import 'cookies';
|
|
9
|
+
import 'cors';
|
|
10
|
+
import 'formidable';
|
|
11
|
+
import 'ws';
|
|
12
|
+
import 'co-body';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { d as Middleware, a as MiddlewareOptions, M as MockServerPlugin, S as Server, c as createManuallyMockMiddleware, b as createMockMiddleware, r as resolveMiddleOptions } from './rspack-Db7drzDm.js';
|
|
2
|
+
export { createDefineMock, defineMock } from './helper.js';
|
|
3
|
+
import '@rspack/core';
|
|
4
|
+
import './types-DhT3pRJ3.js';
|
|
5
|
+
import 'node:buffer';
|
|
6
|
+
import 'node:http';
|
|
7
|
+
import 'node:stream';
|
|
8
|
+
import 'cookies';
|
|
9
|
+
import 'cors';
|
|
10
|
+
import 'formidable';
|
|
11
|
+
import 'ws';
|
|
12
|
+
import 'co-body';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MockServerPlugin,
|
|
3
|
+
resolveMiddleOptions
|
|
4
|
+
} from "./chunk-B56QNVSS.js";
|
|
5
|
+
import {
|
|
6
|
+
createDefineMock,
|
|
7
|
+
defineMock
|
|
8
|
+
} from "./chunk-HTOWPFQ7.js";
|
|
9
|
+
import {
|
|
10
|
+
createManuallyMockMiddleware,
|
|
11
|
+
createMockMiddleware
|
|
12
|
+
} from "./chunk-5MGZAMDI.js";
|
|
13
|
+
export {
|
|
14
|
+
MockServerPlugin,
|
|
15
|
+
createDefineMock,
|
|
16
|
+
createManuallyMockMiddleware,
|
|
17
|
+
createMockMiddleware,
|
|
18
|
+
defineMock,
|
|
19
|
+
resolveMiddleOptions
|
|
20
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }// src/json5-loader.ts
|
|
2
|
+
var _json5 = require('json5'); var _json52 = _interopRequireDefault(_json5);
|
|
3
|
+
var json5Loader = function(content) {
|
|
4
|
+
if (!content)
|
|
5
|
+
return "export default {}";
|
|
6
|
+
return `export default ${JSON.stringify(_json52.default.parse(content))}`;
|
|
7
|
+
};
|
|
8
|
+
var json5_loader_default = json5Loader;
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
exports.default = json5_loader_default;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// src/json5-loader.ts
|
|
2
|
+
import JSON5 from "json5";
|
|
3
|
+
var json5Loader = function(content) {
|
|
4
|
+
if (!content)
|
|
5
|
+
return "export default {}";
|
|
6
|
+
return `export default ${JSON.stringify(JSON5.parse(content))}`;
|
|
7
|
+
};
|
|
8
|
+
var json5_loader_default = json5Loader;
|
|
9
|
+
export {
|
|
10
|
+
json5_loader_default as default
|
|
11
|
+
};
|
package/dist/rsbuild.cjs
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
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/rsbuild.ts
|
|
7
|
+
var _process = require('process'); var _process2 = _interopRequireDefault(_process);
|
|
8
|
+
var _utils = require('@pengzhanbo/utils');
|
|
9
|
+
var _core = require('@rspack/core'); var _core2 = _interopRequireDefault(_core);
|
|
10
|
+
function pluginMockServer(options = {}) {
|
|
11
|
+
return {
|
|
12
|
+
name: "plugin-mock-server",
|
|
13
|
+
setup(api) {
|
|
14
|
+
if (_process2.default.env.NODE_ENV === "production")
|
|
15
|
+
return;
|
|
16
|
+
api.modifyRsbuildConfig((config) => {
|
|
17
|
+
if (!_optionalChain([config, 'access', _ => _.server, 'optionalAccess', _2 => _2.proxy]))
|
|
18
|
+
return;
|
|
19
|
+
const onProxyError = (err, req, res) => {
|
|
20
|
+
console.error(_optionalChain([err, 'optionalAccess', _3 => _3.stack]) || err);
|
|
21
|
+
res.statusCode = 500;
|
|
22
|
+
res.end();
|
|
23
|
+
};
|
|
24
|
+
if (_utils.isArray.call(void 0, 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
|
+
_optionalChain([onProxyReq, 'optionalCall', _4 => _4(proxyReq, req, ...args)]);
|
|
34
|
+
_chunkG53QRHGVcjs.rewriteRequest.call(void 0, 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
|
+
_optionalChain([onProxyReq, 'optionalCall', _5 => _5(proxyReq, req, ...args)]);
|
|
44
|
+
_chunkG53QRHGVcjs.rewriteRequest.call(void 0, 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
|
+
_optionalChain([onProxyReq, 'optionalCall', _6 => _6(proxyReq, req, ...args)]);
|
|
59
|
+
_chunkG53QRHGVcjs.rewriteRequest.call(void 0, proxyReq, req);
|
|
60
|
+
},
|
|
61
|
+
onError: onError || onProxyError
|
|
62
|
+
};
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
let compilerRun;
|
|
67
|
+
let compilerClose;
|
|
68
|
+
let compilerUpdateAlias;
|
|
69
|
+
api.modifyRsbuildConfig((config) => {
|
|
70
|
+
const proxy = _optionalChain([config, 'access', _7 => _7.server, 'optionalAccess', _8 => _8.proxy]) || [];
|
|
71
|
+
const proxies = [];
|
|
72
|
+
if (_utils.isArray.call(void 0, proxy)) {
|
|
73
|
+
for (const item of proxy) {
|
|
74
|
+
if (typeof item !== "function" && !item.ws && item.context) {
|
|
75
|
+
proxies.push(..._utils.toArray.call(void 0, item.context));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
} else if ("target" in proxy) {
|
|
79
|
+
proxies.push(..._utils.toArray.call(void 0, 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 } = _chunkG53QRHGVcjs.createManuallyMockMiddleware.call(void 0, {
|
|
88
|
+
plugins: [new _core2.default.DefinePlugin(_optionalChain([config, 'access', _9 => _9.source, 'optionalAccess', _10 => _10.define]) || {})],
|
|
89
|
+
alias: {},
|
|
90
|
+
proxies,
|
|
91
|
+
context: api.context.rootPath
|
|
92
|
+
}, options);
|
|
93
|
+
compilerRun = run;
|
|
94
|
+
compilerClose = close;
|
|
95
|
+
compilerUpdateAlias = updateAlias;
|
|
96
|
+
config.dev ??= {};
|
|
97
|
+
config.dev.setupMiddlewares ??= [];
|
|
98
|
+
config.dev.setupMiddlewares.push((middlewares) => {
|
|
99
|
+
mockMiddleware(middlewares);
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
api.onAfterCreateCompiler(({ compiler }) => {
|
|
103
|
+
if (compilerUpdateAlias) {
|
|
104
|
+
if ("compilers" in compiler) {
|
|
105
|
+
compiler.compilers.forEach((compiler2) => {
|
|
106
|
+
_optionalChain([compilerUpdateAlias, 'optionalCall', _11 => _11(_optionalChain([compiler2, 'access', _12 => _12.options, 'access', _13 => _13.resolve, 'optionalAccess', _14 => _14.alias]) || {})]);
|
|
107
|
+
});
|
|
108
|
+
} else {
|
|
109
|
+
_optionalChain([compilerUpdateAlias, 'optionalCall', _15 => _15(_optionalChain([compiler, 'access', _16 => _16.options, 'access', _17 => _17.resolve, 'optionalAccess', _18 => _18.alias]) || {})]);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
api.onAfterStartDevServer(() => _optionalChain([compilerRun, 'optionalCall', _19 => _19()]));
|
|
114
|
+
api.onAfterStartProdServer(() => _optionalChain([compilerRun, 'optionalCall', _20 => _20()]));
|
|
115
|
+
api.onExit(() => _optionalChain([compilerClose, 'optionalCall', _21 => _21()]));
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
exports.pluginMockServer = pluginMockServer;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { RsbuildPlugin } from '@rsbuild/core';
|
|
2
|
+
import { M as MockServerPluginOptions } from './types-DhT3pRJ3.cjs';
|
|
3
|
+
import 'node:buffer';
|
|
4
|
+
import 'node:http';
|
|
5
|
+
import 'node:stream';
|
|
6
|
+
import 'cookies';
|
|
7
|
+
import 'cors';
|
|
8
|
+
import 'formidable';
|
|
9
|
+
import 'ws';
|
|
10
|
+
import 'co-body';
|
|
11
|
+
|
|
12
|
+
declare function pluginMockServer(options?: MockServerPluginOptions): RsbuildPlugin;
|
|
13
|
+
|
|
14
|
+
export { pluginMockServer };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { RsbuildPlugin } from '@rsbuild/core';
|
|
2
|
+
import { M as MockServerPluginOptions } from './types-DhT3pRJ3.js';
|
|
3
|
+
import 'node:buffer';
|
|
4
|
+
import 'node:http';
|
|
5
|
+
import 'node:stream';
|
|
6
|
+
import 'cookies';
|
|
7
|
+
import 'cors';
|
|
8
|
+
import 'formidable';
|
|
9
|
+
import 'ws';
|
|
10
|
+
import 'co-body';
|
|
11
|
+
|
|
12
|
+
declare function pluginMockServer(options?: MockServerPluginOptions): RsbuildPlugin;
|
|
13
|
+
|
|
14
|
+
export { pluginMockServer };
|
package/dist/rsbuild.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createManuallyMockMiddleware,
|
|
3
|
+
rewriteRequest
|
|
4
|
+
} from "./chunk-5MGZAMDI.js";
|
|
5
|
+
|
|
6
|
+
// src/rsbuild.ts
|
|
7
|
+
import process from "process";
|
|
8
|
+
import { isArray, toArray } from "@pengzhanbo/utils";
|
|
9
|
+
import rspack from "@rspack/core";
|
|
10
|
+
function pluginMockServer(options = {}) {
|
|
11
|
+
return {
|
|
12
|
+
name: "plugin-mock-server",
|
|
13
|
+
setup(api) {
|
|
14
|
+
if (process.env.NODE_ENV === "production")
|
|
15
|
+
return;
|
|
16
|
+
api.modifyRsbuildConfig((config) => {
|
|
17
|
+
if (!config.server?.proxy)
|
|
18
|
+
return;
|
|
19
|
+
const onProxyError = (err, req, res) => {
|
|
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
|
+
}
|
|
65
|
+
});
|
|
66
|
+
let compilerRun;
|
|
67
|
+
let compilerClose;
|
|
68
|
+
let compilerUpdateAlias;
|
|
69
|
+
api.modifyRsbuildConfig((config) => {
|
|
70
|
+
const proxy = config.server?.proxy || [];
|
|
71
|
+
const proxies = [];
|
|
72
|
+
if (isArray(proxy)) {
|
|
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 || {})],
|
|
89
|
+
alias: {},
|
|
90
|
+
proxies,
|
|
91
|
+
context: api.context.rootPath
|
|
92
|
+
}, options);
|
|
93
|
+
compilerRun = run;
|
|
94
|
+
compilerClose = close;
|
|
95
|
+
compilerUpdateAlias = updateAlias;
|
|
96
|
+
config.dev ??= {};
|
|
97
|
+
config.dev.setupMiddlewares ??= [];
|
|
98
|
+
config.dev.setupMiddlewares.push((middlewares) => {
|
|
99
|
+
mockMiddleware(middlewares);
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
api.onAfterCreateCompiler(({ compiler }) => {
|
|
103
|
+
if (compilerUpdateAlias) {
|
|
104
|
+
if ("compilers" in compiler) {
|
|
105
|
+
compiler.compilers.forEach((compiler2) => {
|
|
106
|
+
compilerUpdateAlias?.(compiler2.options.resolve?.alias || {});
|
|
107
|
+
});
|
|
108
|
+
} else {
|
|
109
|
+
compilerUpdateAlias?.(compiler.options.resolve?.alias || {});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
api.onAfterStartDevServer(() => compilerRun?.());
|
|
114
|
+
api.onAfterStartProdServer(() => compilerRun?.());
|
|
115
|
+
api.onExit(() => compilerClose?.());
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
export {
|
|
120
|
+
pluginMockServer
|
|
121
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
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 };
|
|
@@ -0,0 +1,28 @@
|
|
|
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 };
|
package/dist/rspack.cjs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunk4BGDHRTOcjs = require('./chunk-4BGDHRTO.cjs');
|
|
5
|
+
require('./chunk-G53QRHGV.cjs');
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
exports.MockServerPlugin = _chunk4BGDHRTOcjs.MockServerPlugin; exports.resolveMiddleOptions = _chunk4BGDHRTOcjs.resolveMiddleOptions;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import '@rspack/core';
|
|
2
|
+
import './types-DhT3pRJ3.cjs';
|
|
3
|
+
export { M as MockServerPlugin, r as resolveMiddleOptions } from './rspack-BI-Ifj4a.cjs';
|
|
4
|
+
import 'node:buffer';
|
|
5
|
+
import 'node:http';
|
|
6
|
+
import 'node:stream';
|
|
7
|
+
import 'cookies';
|
|
8
|
+
import 'cors';
|
|
9
|
+
import 'formidable';
|
|
10
|
+
import 'ws';
|
|
11
|
+
import 'co-body';
|