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/README.md
CHANGED
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
# rspack-plugin-mock
|
|
2
2
|
|
|
3
|
-
[
|
|
3
|
+
[Rspack](https://rspack.dev) and [Rsbuild](https://rsbuild.dev) plugin for API mock dev server.
|
|
4
4
|
|
|
5
5
|
Implement a mock-dev-server in `rspack` and `rsbuild` that is fully consistent with [vite-plugin-mock-dev-server](https://github.com/pengzhanbo/vite-plugin-mock-dev-server).
|
|
6
6
|
|
|
7
|
+
<p align="center">
|
|
8
|
+
<a href="https://www.npmjs.com/package/rspack-plugin-mock"><img alt="npm" src="https://img.shields.io/npm/v/rspack-plugin-mock?style=flat-square&colorA=564341&colorB=EDED91"></a>
|
|
9
|
+
<img alt="node-current" src="https://img.shields.io/node/v/rspack-plugin-mock?style=flat-square&colorA=564341&colorB=EDED91">
|
|
10
|
+
<img alt="npm peer dependency version" src="https://img.shields.io/npm/dependency-version/rspack-plugin-mock/peer/@rspack/core?style=flat-square&colorA=564341&colorB=EDED91">
|
|
11
|
+
<img alt="npm peer dependency version" src="https://img.shields.io/npm/dependency-version/rspack-plugin-mock/peer/@rsbuild/core?style=flat-square&colorA=564341&colorB=EDED91">
|
|
12
|
+
<img alt="npm" src="https://img.shields.io/npm/dm/rspack-plugin-mock?style=flat-square&colorA=564341&colorB=EDED91">
|
|
13
|
+
<img alt="GitHub Workflow Status" src="https://img.shields.io/github/actions/workflow/status/pengzhanbo/rspack-plugin-mock/lint.yml?style=flat-square&colorA=564341&colorB=EDED91">
|
|
14
|
+
</p>
|
|
15
|
+
|
|
16
|
+
<p align="center">
|
|
17
|
+
<span>English</span> | <a href="./README.zh-CN.md">简体中文</a>
|
|
18
|
+
</p>
|
|
19
|
+
|
|
7
20
|
## Features
|
|
8
21
|
|
|
9
22
|
- ⚡️ Lightweight, Flexible, Fast.
|
|
@@ -11,21 +24,21 @@ Implement a mock-dev-server in `rspack` and `rsbuild` that is fully consistent w
|
|
|
11
24
|
- 💡 ESModule/commonjs.
|
|
12
25
|
- 🦾 Typescript.
|
|
13
26
|
- 🔥 HMR
|
|
14
|
-
- 🏷 Support `.[cm]js`/ `.ts` /`.json` / `.json5`.
|
|
27
|
+
- 🏷 Support `.[cm]?js`/ `.ts` /`.json` / `.json5`.
|
|
15
28
|
- 📦 Auto import mock file.
|
|
16
29
|
- 🎨 Support any lib, like `mockjs`, or do not use it.
|
|
17
30
|
- 📥 Path rule matching, request parameter matching.
|
|
18
31
|
- ⚙️ Support Enabled/Disabled any one of the API mock.
|
|
19
32
|
- 📀 Supports response body content type such as `text/json/buffer/stream`.
|
|
20
33
|
- ⚖️ Use `devServer.proxy` in rspack, or `server.proxy` in rsbuild.
|
|
21
|
-
- 🍕 Support `
|
|
22
|
-
- ⚓️ Support `
|
|
34
|
+
- 🍕 Support `define` in the mock file.
|
|
35
|
+
- ⚓️ Support `alias` in the mock file.
|
|
23
36
|
- 📤 Support `multipart` content-type, mock upload file.
|
|
24
37
|
- 📥 Support mock download file.
|
|
38
|
+
- ⚜️ Support `WebSocket Mock`
|
|
39
|
+
- 🗂 Support building small independent deployable mock services.
|
|
25
40
|
|
|
26
|
-
##
|
|
27
|
-
|
|
28
|
-
### Install
|
|
41
|
+
## Install
|
|
29
42
|
|
|
30
43
|
```sh
|
|
31
44
|
# npm
|
|
@@ -36,7 +49,7 @@ yarn add rspack-plugin-mock
|
|
|
36
49
|
pnpm add -D rspack-plugin-mock
|
|
37
50
|
```
|
|
38
51
|
|
|
39
|
-
###
|
|
52
|
+
### Usage
|
|
40
53
|
|
|
41
54
|
**In Rspack**
|
|
42
55
|
|
|
@@ -79,7 +92,7 @@ export default defineConfig({
|
|
|
79
92
|
|
|
80
93
|
### Edit Mock file
|
|
81
94
|
|
|
82
|
-
By default, write mock data in the mock directory of your project's root directory:
|
|
95
|
+
By default, write mock data in the `mock` directory of your project's root directory:
|
|
83
96
|
|
|
84
97
|
`mock/**/*.mock.ts` :
|
|
85
98
|
|
|
@@ -162,6 +175,8 @@ export default defineMock({
|
|
|
162
175
|
Return a custom defineMock function to support preprocessing of mock config.
|
|
163
176
|
|
|
164
177
|
```ts
|
|
178
|
+
import { createDefineMock } from 'rspack-plugin-mock/helper'
|
|
179
|
+
|
|
165
180
|
const definePostMock = createDefineMock((mock) => {
|
|
166
181
|
mock.url = `/api/post/${mock.url}`
|
|
167
182
|
})
|
|
@@ -183,6 +198,18 @@ export default definePostMock({
|
|
|
183
198
|
any request path starting with prefix will be intercepted and proxied.
|
|
184
199
|
If the prefix starts with `^`, it will be recognized as a `RegExp`.
|
|
185
200
|
|
|
201
|
+
### options.wsPrefix
|
|
202
|
+
|
|
203
|
+
- **Type:** `string | string[]`
|
|
204
|
+
- **Details:**
|
|
205
|
+
|
|
206
|
+
Configure path matching rules for WebSocket mock service.
|
|
207
|
+
Any ws/wss requests with a request path starting with wsPrefix
|
|
208
|
+
will be intercepted by the proxy.
|
|
209
|
+
If wsPrefix starts with `^`, it will be recognized as a `RegExp`.
|
|
210
|
+
|
|
211
|
+
Please avoid having the configurations in `wsPrefix` appear in `devServer.proxy` / `server.proxy`, as this may lead to conflicts in the rules.
|
|
212
|
+
|
|
186
213
|
### options.cwd
|
|
187
214
|
|
|
188
215
|
- **Type:** `string`
|
|
@@ -215,6 +242,15 @@ export default definePostMock({
|
|
|
215
242
|
|
|
216
243
|
Enable log and configure log level
|
|
217
244
|
|
|
245
|
+
### options.reload
|
|
246
|
+
|
|
247
|
+
- **Type:** `boolean`
|
|
248
|
+
- **Default:** `false`
|
|
249
|
+
- **Details:**
|
|
250
|
+
|
|
251
|
+
If you want to refresh the page every time you modify a mock file,
|
|
252
|
+
you can open this option.
|
|
253
|
+
|
|
218
254
|
### options.cors
|
|
219
255
|
|
|
220
256
|
- **Type:** `boolean | CorsOptions`
|
|
@@ -245,6 +281,36 @@ export default definePostMock({
|
|
|
245
281
|
|
|
246
282
|
Configure to [co-body](https://github.com/cojs/co-body#options)
|
|
247
283
|
|
|
284
|
+
## options.build
|
|
285
|
+
|
|
286
|
+
- **Type:** `boolean | ServerBuildOption`
|
|
287
|
+
|
|
288
|
+
```ts
|
|
289
|
+
interface ServerBuildOption {
|
|
290
|
+
/**
|
|
291
|
+
* Service startup port
|
|
292
|
+
* @default 8080
|
|
293
|
+
*/
|
|
294
|
+
serverPort?: number
|
|
295
|
+
/**
|
|
296
|
+
* Service application output directory
|
|
297
|
+
* @default 'mockServer'
|
|
298
|
+
*/
|
|
299
|
+
dist?: string
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Service application log level
|
|
303
|
+
* @default 'error'
|
|
304
|
+
*/
|
|
305
|
+
log?: LogLevel
|
|
306
|
+
}
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
- **Default:** `false`
|
|
310
|
+
- **Details:**
|
|
311
|
+
|
|
312
|
+
When you need to build a small mock service, you can configure this option.
|
|
313
|
+
|
|
248
314
|
## Mock Options
|
|
249
315
|
|
|
250
316
|
### options.url
|
|
@@ -266,15 +332,16 @@ export default definePostMock({
|
|
|
266
332
|
### options.method
|
|
267
333
|
|
|
268
334
|
- **Type:** `Method | Method[]`
|
|
335
|
+
|
|
336
|
+
```ts
|
|
337
|
+
type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'PATCH'
|
|
338
|
+
```
|
|
339
|
+
|
|
269
340
|
- **Default:** `['GET', 'POST']`
|
|
270
341
|
- **Details:**
|
|
271
342
|
|
|
272
343
|
The interface allows request methods
|
|
273
344
|
|
|
274
|
-
```ts
|
|
275
|
-
type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'PATCH'
|
|
276
|
-
```
|
|
277
|
-
|
|
278
345
|
### options.type
|
|
279
346
|
|
|
280
347
|
- **Type:** `'text' | 'json' | 'buffer' | string`
|
|
@@ -349,15 +416,84 @@ type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'PATCH'
|
|
|
349
416
|
|
|
350
417
|
- **Type:** `CookiesOptions | (request: MockRequest) => CookiesOptions | Promise<CookiesOptions>`
|
|
351
418
|
|
|
352
|
-
```ts
|
|
353
|
-
type CookiesOptions = Record<string, CookieValue>
|
|
354
|
-
|
|
355
|
-
|
|
419
|
+
```ts
|
|
420
|
+
type CookiesOptions = Record<string, CookieValue>
|
|
421
|
+
|
|
422
|
+
type CookieValue = string | [string, SetCookie]
|
|
423
|
+
```
|
|
356
424
|
|
|
357
425
|
- **Details:**
|
|
358
426
|
|
|
359
427
|
Configure response body cookies
|
|
360
428
|
|
|
429
|
+
### options.validator
|
|
430
|
+
|
|
431
|
+
- **类型:** `Validator | (request: MockRequest) => boolean`
|
|
432
|
+
|
|
433
|
+
```ts
|
|
434
|
+
interface Validator {
|
|
435
|
+
/**
|
|
436
|
+
* 请求地址中位于 `?` 后面的 queryString,已解析为 json
|
|
437
|
+
*/
|
|
438
|
+
query: Record<string, any>
|
|
439
|
+
/**
|
|
440
|
+
* 请求 referer 中位于 `?` 后面的 queryString
|
|
441
|
+
*/
|
|
442
|
+
refererQuery: Record<string, any>
|
|
443
|
+
/**
|
|
444
|
+
* 请求体中 body 数据
|
|
445
|
+
*/
|
|
446
|
+
body: Record<string, any>
|
|
447
|
+
/**
|
|
448
|
+
* 请求地址中,`/api/id/:id` 解析后的 params 参数
|
|
449
|
+
*/
|
|
450
|
+
params: Record<string, any>
|
|
451
|
+
/**
|
|
452
|
+
* 请求体中 headers
|
|
453
|
+
*/
|
|
454
|
+
headers: Headers
|
|
455
|
+
}
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
- **详情:**
|
|
459
|
+
|
|
460
|
+
Request Validator
|
|
461
|
+
|
|
462
|
+
Sometimes, for the same API request, data needs to be returned based
|
|
463
|
+
on different request parameters.
|
|
464
|
+
|
|
465
|
+
However, if all of this is written in a single mock's body or response,
|
|
466
|
+
the content can become cumbersome and difficult to manage.
|
|
467
|
+
The function of a validator allows you to configure multiple mocks with
|
|
468
|
+
the same URL simultaneously and determine which mock should be used through validation.
|
|
469
|
+
|
|
470
|
+
### options.ws
|
|
471
|
+
|
|
472
|
+
- **Type:** `boolean`
|
|
473
|
+
- **Default:** `false`
|
|
474
|
+
- **Details:**
|
|
475
|
+
|
|
476
|
+
Enable WebSocket interface simulation
|
|
477
|
+
|
|
478
|
+
### options.setup
|
|
479
|
+
|
|
480
|
+
- **Type:** `(wss: WebSocketServer, ctx: WebSocketSetupContext) => void`
|
|
481
|
+
- **Details:**
|
|
482
|
+
|
|
483
|
+
Configure Websocket Server
|
|
484
|
+
|
|
485
|
+
```ts
|
|
486
|
+
interface WebSocketSetupContext {
|
|
487
|
+
/**
|
|
488
|
+
* When defining WSS, you may perform some automatic or looping tasks.
|
|
489
|
+
* However, when hot updating, the plugin will re-execute `setup()`,
|
|
490
|
+
* which may result in duplicate registration of listening events and looping tasks
|
|
491
|
+
* such as setTimeout. You can use `onCleanup()` to clear these automatic or looping tasks.
|
|
492
|
+
*/
|
|
493
|
+
onCleanup: (cleanup: () => void) => void
|
|
494
|
+
}
|
|
495
|
+
```
|
|
496
|
+
|
|
361
497
|
### Types
|
|
362
498
|
|
|
363
499
|
```ts
|
|
@@ -679,7 +815,73 @@ fetch('/api/graphql', {
|
|
|
679
815
|
})
|
|
680
816
|
```
|
|
681
817
|
|
|
682
|
-
|
|
818
|
+
**exp:** WebSocket Mock
|
|
819
|
+
|
|
820
|
+
``` ts
|
|
821
|
+
// ws.mock.ts
|
|
822
|
+
export default defineMock({
|
|
823
|
+
url: '/socket.io',
|
|
824
|
+
ws: true,
|
|
825
|
+
setup(wss, { onCleanup }) {
|
|
826
|
+
const wsMap = new Map()
|
|
827
|
+
wss.on('connection', (ws, req) => {
|
|
828
|
+
const token = req.getCookie('token')
|
|
829
|
+
wsMap.set(token, ws)
|
|
830
|
+
ws.on('message', (raw) => {
|
|
831
|
+
const data = JSON.parse(String(raw))
|
|
832
|
+
if (data.type === 'ping')
|
|
833
|
+
return
|
|
834
|
+
// Broadcast
|
|
835
|
+
for (const [_token, _ws] of wsMap.entires()) {
|
|
836
|
+
if (_token !== token)
|
|
837
|
+
_ws.send(raw)
|
|
838
|
+
}
|
|
839
|
+
})
|
|
840
|
+
})
|
|
841
|
+
wss.on('error', (err) => {
|
|
842
|
+
console.error(err)
|
|
843
|
+
})
|
|
844
|
+
onCleanup(() => wsMap.clear())
|
|
845
|
+
}
|
|
846
|
+
})
|
|
847
|
+
```
|
|
848
|
+
|
|
849
|
+
``` ts
|
|
850
|
+
// app.ts
|
|
851
|
+
const ws = new WebSocket('ws://localhost:3000/socket.io')
|
|
852
|
+
ws.addEventListener('open', () => {
|
|
853
|
+
setInterval(() => {
|
|
854
|
+
// heartbeat
|
|
855
|
+
ws.send(JSON.stringify({ type: 'ping' }))
|
|
856
|
+
}, 1000)
|
|
857
|
+
}, { once: true })
|
|
858
|
+
ws.addEventListener('message', (raw) => {
|
|
859
|
+
console.log(raw)
|
|
860
|
+
})
|
|
861
|
+
```
|
|
862
|
+
|
|
863
|
+
## Mock Services
|
|
864
|
+
|
|
865
|
+
In some scenarios, it may be necessary to use the data provided by mock services for display purposes, but the project may have already been packaged, built and deployed without support from `rspack/rsbuild` and this plugin's mock service. Since this plugin supports importing various node modules in mock files at the design stage, the mock file cannot be inline into client build code.
|
|
866
|
+
|
|
867
|
+
The plugin support for builds a small independent mock service application that can be deployed to relevant environments during `production build`. This can then be forwarded through other HTTP servers like Nginx to actual ports for mock support.
|
|
868
|
+
|
|
869
|
+
The default output is built into the directory dist/mockServer, generating files as follows:
|
|
870
|
+
|
|
871
|
+
```sh
|
|
872
|
+
./mockServer
|
|
873
|
+
├── index.js
|
|
874
|
+
├── mock-data.js
|
|
875
|
+
└── package.json
|
|
876
|
+
```
|
|
877
|
+
|
|
878
|
+
In this directory, execute `npm install` to install dependencies, and then execute npm start to start the mock server.
|
|
879
|
+
|
|
880
|
+
The default port is `8080`.
|
|
881
|
+
|
|
882
|
+
You can access related mock interfaces through `localhost:8080/`.
|
|
883
|
+
|
|
884
|
+
## Links
|
|
683
885
|
|
|
684
886
|
- [rspack](https://rspack.dev)
|
|
685
887
|
- [rsbuild](https://rsbuild.dev)
|
|
@@ -687,4 +889,4 @@ fetch('/api/graphql', {
|
|
|
687
889
|
|
|
688
890
|
## License
|
|
689
891
|
|
|
690
|
-
[MIT License](
|
|
892
|
+
rspack-plugin-mock is licensed under the [MIT License](./LICENSE)
|