vite-plugin-mock-dev-server 1.4.2 → 1.4.5
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 +28 -46
- package/README.zh-CN.md +18 -27
- package/dist/index.cjs +52 -43
- package/dist/index.d.cts +1 -1
- package/dist/index.js +26 -17
- package/package.json +12 -12
package/README.md
CHANGED
|
@@ -8,22 +8,21 @@
|
|
|
8
8
|
|
|
9
9
|
<br>
|
|
10
10
|
<p align="center">
|
|
11
|
-
<a href="https://www.npmjs.com/package/vite-plugin-mock-dev-server"><img alt="npm" src="https://img.shields.io/npm/v/vite-plugin-mock-dev-server?style=flat-square"></a>
|
|
12
|
-
<img alt="node-current" src="https://img.shields.io/node/v/vite-plugin-mock-dev-server?style=flat-square">
|
|
13
|
-
<img alt="npm peer dependency version" src="https://img.shields.io/npm/dependency-version/vite-plugin-mock-dev-server/peer/vite?style=flat-square">
|
|
14
|
-
<img alt="npm" src="https://img.shields.io/npm/dt/vite-plugin-mock-dev-server?style=flat-square">
|
|
15
|
-
<br>
|
|
16
|
-
<img alt="GitHub Workflow Status" src="https://img.shields.io/github/actions/workflow/status/pengzhanbo/vite-plugin-mock-dev-server/lint.yml?style=flat-square">
|
|
17
|
-
<a href="https://app.fossa.com/projects/git%2Bgithub.com%2Fpengzhanbo%2Fvite-plugin-mock-dev-server?ref=badge_shield"><img alt="fossa status" src="https://app.fossa.com/api/projects/git%2Bgithub.com%2Fpengzhanbo%2Fvite-plugin-mock-dev-server.svg?type=shield"></a>
|
|
11
|
+
<a href="https://www.npmjs.com/package/vite-plugin-mock-dev-server"><img alt="npm" src="https://img.shields.io/npm/v/vite-plugin-mock-dev-server?style=flat-square"></a>
|
|
12
|
+
<img alt="node-current" src="https://img.shields.io/node/v/vite-plugin-mock-dev-server?style=flat-square">
|
|
13
|
+
<img alt="npm peer dependency version" src="https://img.shields.io/npm/dependency-version/vite-plugin-mock-dev-server/peer/vite?style=flat-square">
|
|
14
|
+
<img alt="npm" src="https://img.shields.io/npm/dt/vite-plugin-mock-dev-server?style=flat-square">
|
|
15
|
+
<br>
|
|
16
|
+
<img alt="GitHub Workflow Status" src="https://img.shields.io/github/actions/workflow/status/pengzhanbo/vite-plugin-mock-dev-server/lint.yml?style=flat-square">
|
|
17
|
+
<a href="https://app.fossa.com/projects/git%2Bgithub.com%2Fpengzhanbo%2Fvite-plugin-mock-dev-server?ref=badge_shield"><img alt="fossa status" src="https://app.fossa.com/api/projects/git%2Bgithub.com%2Fpengzhanbo%2Fvite-plugin-mock-dev-server.svg?type=shield"></a>
|
|
18
18
|
</p>
|
|
19
19
|
<br>
|
|
20
20
|
<p align="center">
|
|
21
|
-
<span>English</span> | <a href="/README.zh-CN.md">简体中文</a>
|
|
21
|
+
<span>English</span> | <a href="/README.zh-CN.md">简体中文</a>
|
|
22
22
|
</p>
|
|
23
23
|
<br>
|
|
24
24
|
<br>
|
|
25
25
|
|
|
26
|
-
|
|
27
26
|
## Feature
|
|
28
27
|
|
|
29
28
|
- ⚡️ Lightweight, Flexible, Fast.
|
|
@@ -46,7 +45,6 @@
|
|
|
46
45
|
- ⚜️ Support `WebSocket Mock`
|
|
47
46
|
- 🗂 Support building small independent deployable mock services.
|
|
48
47
|
|
|
49
|
-
|
|
50
48
|
## Documentation
|
|
51
49
|
|
|
52
50
|
See the [documentation](https://vite-plugin-mock-dev-server.netlify.app/en/) to learn more.
|
|
@@ -60,7 +58,7 @@ See the [documentation](https://vite-plugin-mock-dev-server.netlify.app/en/) to
|
|
|
60
58
|
``` sh
|
|
61
59
|
# npm
|
|
62
60
|
npm i -D vite-plugin-mock-dev-server
|
|
63
|
-
# yarn
|
|
61
|
+
# yarn
|
|
64
62
|
yarn add vite-plugin-mock-dev-server
|
|
65
63
|
# pnpm
|
|
66
64
|
pnpm add -D vite-plugin-mock-dev-server
|
|
@@ -114,7 +112,6 @@ export default defineMock({
|
|
|
114
112
|
|
|
115
113
|
Vite plugin
|
|
116
114
|
|
|
117
|
-
|
|
118
115
|
`vite.config.ts`
|
|
119
116
|
``` ts
|
|
120
117
|
import { defineConfig } from 'vite'
|
|
@@ -127,13 +124,12 @@ export default defineConfig({
|
|
|
127
124
|
})
|
|
128
125
|
```
|
|
129
126
|
|
|
130
|
-
|
|
131
127
|
#### options
|
|
132
128
|
|
|
133
129
|
- `options.prefix`
|
|
134
130
|
|
|
135
131
|
**Type:** `string | string[]`
|
|
136
|
-
|
|
132
|
+
|
|
137
133
|
Configure custom matching rules for the mock server. Any request path starting with the value of `prefix` will be proxied to the corresponding target. If the `prefix` value starts with ^, it will be recognized as a RegExp.
|
|
138
134
|
|
|
139
135
|
> In general, `server.proxy` is sufficient to meet the needs. Adding this item is for compatibility with certain scenarios.
|
|
@@ -141,7 +137,7 @@ export default defineConfig({
|
|
|
141
137
|
**Default:** `[]`
|
|
142
138
|
|
|
143
139
|
- `options.wsPrefix`
|
|
144
|
-
|
|
140
|
+
|
|
145
141
|
**Type:** `string | string[]`
|
|
146
142
|
|
|
147
143
|
Configure the matching rules for the WebSocket service. Any request path starting with the value of `wsPrefix` and using the `ws/wss` protocol will be proxied to the corresponding target.
|
|
@@ -151,11 +147,10 @@ export default defineConfig({
|
|
|
151
147
|
> Different from using `viteConfig.server.proxy` by default for http mock, `websocket mock` does not use the ws-related configuration in `viteConfig.server.proxy`. Also, rules configured in `wsPrefix` cannot be configured simultaneously in `viteConfig.server.proxy`, as it will cause conflicts when starting the vite server because multiple instances of WebSocketServer cannot be implemented for the same request.
|
|
152
148
|
> This conflict is neither a problem with Vite nor with the plugin; it belongs to a reasonable error type. When switching between WebSocket Mock and WebSocket Proxy, please pay attention to avoid duplicate configurations that may cause conflicts.
|
|
153
149
|
|
|
154
|
-
|
|
155
|
-
- `option.include`
|
|
150
|
+
- `option.include`
|
|
156
151
|
|
|
157
152
|
**Type:** `string | string[]`
|
|
158
|
-
|
|
153
|
+
|
|
159
154
|
Configure to read mock files, which can be a directory, glob, or an array.
|
|
160
155
|
|
|
161
156
|
**Default:** `['mock/**/*.mock.{js,ts,cjs,mjs,json,json5}']` (Relative to the root directory.)
|
|
@@ -163,36 +158,35 @@ export default defineConfig({
|
|
|
163
158
|
- `options.exclude`
|
|
164
159
|
|
|
165
160
|
**Type:** `string | string[]`
|
|
166
|
-
|
|
161
|
+
|
|
167
162
|
When reading mock files for configuration, the files that need to be excluded can be a directory, glob, or array.
|
|
168
163
|
|
|
169
164
|
**Default:** `['**/node_modules/**','**/.vscode/**','**/.git/**']`
|
|
170
165
|
|
|
171
166
|
- `options.reload`
|
|
172
|
-
|
|
167
|
+
|
|
173
168
|
**Type:** `boolean`
|
|
174
169
|
|
|
175
170
|
When the mock resource is hot updated, only the data content is updated, but the page is not refreshed by default. If you want to refresh the page every time you modify the mock file, you can open this option.
|
|
176
171
|
|
|
177
172
|
- `options.cors`
|
|
178
|
-
|
|
173
|
+
|
|
179
174
|
**Type:** `boolean | CorsOptions`
|
|
180
175
|
|
|
181
176
|
Enable by default.
|
|
182
177
|
|
|
183
178
|
Configure to `cors`, see [cors](https://github.com/expressjs/cors#configuration-options)
|
|
184
179
|
|
|
185
|
-
|
|
186
180
|
**Default:** `true`
|
|
187
181
|
|
|
188
182
|
- `options.log`
|
|
189
|
-
|
|
183
|
+
|
|
190
184
|
**Type:** `boolean | 'info' | 'warn' | 'error' | 'silent'`
|
|
191
185
|
|
|
192
186
|
Enable log and configure log level.
|
|
193
187
|
|
|
194
188
|
- `options.formidableOptions`
|
|
195
|
-
|
|
189
|
+
|
|
196
190
|
Configure to `formidable`, see [formidable options](https://github.com/node-formidable/formidable#options)
|
|
197
191
|
|
|
198
192
|
**Default:** `{}`
|
|
@@ -207,7 +201,7 @@ export default defineConfig({
|
|
|
207
201
|
```
|
|
208
202
|
|
|
209
203
|
- `options.cookiesOptions`
|
|
210
|
-
|
|
204
|
+
|
|
211
205
|
Configure to `cookies`, see [cookies](https://github.com/pillarjs/cookies#new-cookiesrequest-response--options)
|
|
212
206
|
|
|
213
207
|
**Default:** `{}`
|
|
@@ -216,7 +210,7 @@ export default defineConfig({
|
|
|
216
210
|
|
|
217
211
|
The configuration needed to build a small, independently deployable mock service.
|
|
218
212
|
|
|
219
|
-
**Type:** `boolean | ServerBuildOptions`
|
|
213
|
+
**Type:** `boolean | ServerBuildOptions`
|
|
220
214
|
|
|
221
215
|
**Default:** `false`
|
|
222
216
|
|
|
@@ -241,7 +235,7 @@ export default defineConfig({
|
|
|
241
235
|
```
|
|
242
236
|
|
|
243
237
|
- `options.priority`
|
|
244
|
-
|
|
238
|
+
|
|
245
239
|
Custom path matching rule priority。[read more](#Custom-Path-Matching-Priority)
|
|
246
240
|
|
|
247
241
|
**Default:** `undefined`
|
|
@@ -473,11 +467,10 @@ type Response = http.ServerResponse<http.IncomingMessage> & {
|
|
|
473
467
|
}
|
|
474
468
|
```
|
|
475
469
|
|
|
476
|
-
|
|
477
470
|
> **Tips:**
|
|
478
|
-
>
|
|
479
|
-
> If you write mock files using json/json5,
|
|
480
|
-
> the 'response' method is not supported,
|
|
471
|
+
>
|
|
472
|
+
> If you write mock files using json/json5,
|
|
473
|
+
> the 'response' method is not supported,
|
|
481
474
|
> as is the function form that uses other fields.
|
|
482
475
|
|
|
483
476
|
## Share Mock Data
|
|
@@ -525,8 +518,8 @@ export default defineMock([
|
|
|
525
518
|
```
|
|
526
519
|
|
|
527
520
|
> **Tips:**
|
|
528
|
-
>
|
|
529
|
-
> The `defineMockData` function relies solely on the shared data support provided by `memory`.
|
|
521
|
+
>
|
|
522
|
+
> The `defineMockData` function relies solely on the shared data support provided by `memory`.
|
|
530
523
|
> If persistent mock data is required, it is recommended to use a `nosql` database like `lowdb` or `level`.
|
|
531
524
|
|
|
532
525
|
## Custom-Path-Matching-Priority
|
|
@@ -576,7 +569,6 @@ export default defineConfig({
|
|
|
576
569
|
> you can use static rules instead of `priority`,
|
|
577
570
|
> as static rules always have the highest priority.
|
|
578
571
|
|
|
579
|
-
|
|
580
572
|
## Example
|
|
581
573
|
|
|
582
574
|
`mock/**/*.mock.{ts,js,mjs,cjs,json,json5}`
|
|
@@ -598,7 +590,6 @@ export default defineMock({
|
|
|
598
590
|
})
|
|
599
591
|
```
|
|
600
592
|
|
|
601
|
-
|
|
602
593
|
**exp:** Only Support `GET` Method
|
|
603
594
|
``` ts
|
|
604
595
|
export default defineMock({
|
|
@@ -627,7 +618,6 @@ export default defineMock({
|
|
|
627
618
|
})
|
|
628
619
|
```
|
|
629
620
|
|
|
630
|
-
|
|
631
621
|
**exp:** Define multiple mock requests for the same URL and match valid rules with validators
|
|
632
622
|
``` ts
|
|
633
623
|
export default defineMock([
|
|
@@ -662,7 +652,6 @@ export default defineMock([
|
|
|
662
652
|
])
|
|
663
653
|
```
|
|
664
654
|
|
|
665
|
-
|
|
666
655
|
**exp:** Response Delay
|
|
667
656
|
``` ts
|
|
668
657
|
export default defineMock({
|
|
@@ -671,7 +660,6 @@ export default defineMock({
|
|
|
671
660
|
})
|
|
672
661
|
```
|
|
673
662
|
|
|
674
|
-
|
|
675
663
|
**exp:** The interface request failed
|
|
676
664
|
``` ts
|
|
677
665
|
export default defineMock({
|
|
@@ -681,7 +669,6 @@ export default defineMock({
|
|
|
681
669
|
})
|
|
682
670
|
```
|
|
683
671
|
|
|
684
|
-
|
|
685
672
|
**exp:** Dynamic route matching
|
|
686
673
|
``` ts
|
|
687
674
|
export default defineMock({
|
|
@@ -749,7 +736,6 @@ export default defineMock({
|
|
|
749
736
|
```
|
|
750
737
|
You need install `mockjs`
|
|
751
738
|
|
|
752
|
-
|
|
753
739
|
**exp:** Use `response` to customize the response
|
|
754
740
|
``` ts
|
|
755
741
|
export default defineMock({
|
|
@@ -769,7 +755,6 @@ export default defineMock({
|
|
|
769
755
|
})
|
|
770
756
|
```
|
|
771
757
|
|
|
772
|
-
|
|
773
758
|
**exp:** Use json / json5
|
|
774
759
|
``` json
|
|
775
760
|
{
|
|
@@ -780,8 +765,6 @@ export default defineMock({
|
|
|
780
765
|
}
|
|
781
766
|
```
|
|
782
767
|
|
|
783
|
-
|
|
784
|
-
|
|
785
768
|
**exp:** multipart, upload files.
|
|
786
769
|
|
|
787
770
|
use [`formidable`](https://www.npmjs.com/package/formidable#readme) to support.
|
|
@@ -789,7 +772,7 @@ use [`formidable`](https://www.npmjs.com/package/formidable#readme) to support.
|
|
|
789
772
|
<form action="/api/upload" method="post" enctype="multipart/form-data">
|
|
790
773
|
<p>
|
|
791
774
|
<span>file: </span>
|
|
792
|
-
<input type="file" name="files" multiple>
|
|
775
|
+
<input type="file" name="files" multiple="multiple">
|
|
793
776
|
</p>
|
|
794
777
|
<p>
|
|
795
778
|
<span>name:</span>
|
|
@@ -801,7 +784,7 @@ use [`formidable`](https://www.npmjs.com/package/formidable#readme) to support.
|
|
|
801
784
|
</form>
|
|
802
785
|
```
|
|
803
786
|
|
|
804
|
-
fields `files` mapping to `formidable.File`
|
|
787
|
+
fields `files` mapping to `formidable.File`
|
|
805
788
|
``` ts
|
|
806
789
|
export default defineMock({
|
|
807
790
|
url: '/api/upload',
|
|
@@ -948,5 +931,4 @@ You can access related `mock` interfaces through `localhost:8080/`.
|
|
|
948
931
|
|
|
949
932
|
[MIT](/LICENSE)
|
|
950
933
|
|
|
951
|
-
|
|
952
934
|
[](https://app.fossa.com/projects/git%2Bgithub.com%2Fpengzhanbo%2Fvite-plugin-mock-dev-server?ref=badge_large)
|
package/README.zh-CN.md
CHANGED
|
@@ -47,7 +47,6 @@
|
|
|
47
47
|
- ⚜️ 支持模拟 `WebSocket`
|
|
48
48
|
- 🗂 支持构建可独立部署的小型mock服务
|
|
49
49
|
|
|
50
|
-
|
|
51
50
|
## 文档
|
|
52
51
|
|
|
53
52
|
查看 [Documentation](https://vite-plugin-mock-dev-server.netlify.app/) 了解更多。
|
|
@@ -61,7 +60,7 @@
|
|
|
61
60
|
```sh
|
|
62
61
|
# npm
|
|
63
62
|
npm i -D vite-plugin-mock-dev-server
|
|
64
|
-
# yarn
|
|
63
|
+
# yarn
|
|
65
64
|
yarn add vite-plugin-mock-dev-server
|
|
66
65
|
# pnpm
|
|
67
66
|
pnpm add -D vite-plugin-mock-dev-server
|
|
@@ -92,7 +91,7 @@ export default defineConfig({
|
|
|
92
91
|
插件也会读取 `define` 配置, 支持在 mock 文件中直接使用。
|
|
93
92
|
|
|
94
93
|
> 因为一般场景下,我们只需要对有代理的url进行mock,这样才能通过 vite 提供的 http 服务进行 代理和 mock,
|
|
95
|
-
> 但你也可以使用 `options.prefix`配置 mock
|
|
94
|
+
> 但你也可以使用 `options.prefix`配置 mock
|
|
96
95
|
|
|
97
96
|
### 编写mock文件
|
|
98
97
|
|
|
@@ -114,7 +113,6 @@ export default defineMock({
|
|
|
114
113
|
|
|
115
114
|
vite plugin
|
|
116
115
|
|
|
117
|
-
|
|
118
116
|
`vite.config.ts`
|
|
119
117
|
```ts
|
|
120
118
|
import { defineConfig } from 'vite'
|
|
@@ -132,7 +130,7 @@ export default defineConfig({
|
|
|
132
130
|
- `options.prefix`
|
|
133
131
|
|
|
134
132
|
**类型:** `string | string[]`
|
|
135
|
-
|
|
133
|
+
|
|
136
134
|
为mock服务器配置自定义匹配规则。任何请求路径以 `prefix` 值开头的请求将被代理到对应的目标。如果 `prefix` 值以 `^` 开头,将被识别为 RegExp。
|
|
137
135
|
|
|
138
136
|
> 一般情况下, `server.proxy` 已经足够满足需求,添加此项是为了与某些场景兼容。
|
|
@@ -140,19 +138,19 @@ export default defineConfig({
|
|
|
140
138
|
**默认值:** `[]`
|
|
141
139
|
|
|
142
140
|
- `options.wsPrefix`
|
|
143
|
-
|
|
141
|
+
|
|
144
142
|
**类型:** `string | string[]`
|
|
145
143
|
|
|
146
144
|
配置 webSocket 服务 匹配规则。任何请求路径以 `wsPrefix` 值开头的 `ws/wss` 协议请求,将被代理到对应的目标。
|
|
147
145
|
如果`wsPrefix`值以 `^` 开头,将被识别为 RegExp。
|
|
148
146
|
|
|
149
147
|
> 与 http mock 默认使用 `viteConfig.server.proxy` 不同的是,`websocket mock` 不会使用 `viteConfig.server.proxy` 中的 ws 相关的配置,且配置在 `wsPrefix` 中的规则,不能同时配置在 `viteConfig.server.proxy`中,因为会导致在 vite 在启动服务时产生冲突,因为不能对同一个请求实现多个的 `WebSocketServer`实例。
|
|
150
|
-
> 该冲突既不是 `vite` 的问题,也不是插件的问题,这属于合理的错误类型。在进行 `WebSocket Mock`和 `WebSocket Proxy` 切换时,请注意配置不要出现重复导致冲突。
|
|
148
|
+
> 该冲突既不是 `vite` 的问题,也不是插件的问题,这属于合理的错误类型。在进行 `WebSocket Mock`和 `WebSocket Proxy` 切换时,请注意配置不要出现重复导致冲突。
|
|
151
149
|
|
|
152
|
-
- `option.include`
|
|
150
|
+
- `option.include`
|
|
153
151
|
|
|
154
152
|
**类型:** `string | string[]`
|
|
155
|
-
|
|
153
|
+
|
|
156
154
|
配置读取 mock文件,可以是一个 目录,glob,或者一个数组
|
|
157
155
|
|
|
158
156
|
**默认值:** `['mock/**/*.mock.{js,ts,cjs,mjs,json,json5}']` (相对于根目录)
|
|
@@ -160,7 +158,7 @@ export default defineConfig({
|
|
|
160
158
|
- `options.exclude`
|
|
161
159
|
|
|
162
160
|
**类型:** `string | string[]`
|
|
163
|
-
|
|
161
|
+
|
|
164
162
|
配置读取 mock文件时,需要排除的文件, 可以是一个 目录、glob、或者一个数组
|
|
165
163
|
|
|
166
164
|
**默认值:** `['**/node_modules/**', '**/.vscode/**', '**/.git/**']`
|
|
@@ -174,7 +172,7 @@ export default defineConfig({
|
|
|
174
172
|
**Default:** `false`
|
|
175
173
|
|
|
176
174
|
- `options.cors`
|
|
177
|
-
|
|
175
|
+
|
|
178
176
|
**Type:** `boolean | CorsOptions`
|
|
179
177
|
|
|
180
178
|
默认启用.
|
|
@@ -182,13 +180,13 @@ export default defineConfig({
|
|
|
182
180
|
配置 `cors`, 查看 [cors](https://github.com/expressjs/cors#configuration-options)
|
|
183
181
|
|
|
184
182
|
- `options.log`
|
|
185
|
-
|
|
183
|
+
|
|
186
184
|
**Type:** `boolean | 'info' | 'warn' | 'error' | 'silent'`
|
|
187
185
|
|
|
188
186
|
启动日志,以及配置日志打印级别
|
|
189
187
|
|
|
190
188
|
- `options.formidableOptions`
|
|
191
|
-
|
|
189
|
+
|
|
192
190
|
配置 `formidable`,查看 [formidable options](https://github.com/node-formidable/formidable#options)
|
|
193
191
|
|
|
194
192
|
**默认值:** `{}`
|
|
@@ -202,16 +200,16 @@ export default defineConfig({
|
|
|
202
200
|
})
|
|
203
201
|
```
|
|
204
202
|
- `options.cookiesOptions`
|
|
205
|
-
|
|
203
|
+
|
|
206
204
|
配置 `cookies`, 查看 [cookies](https://github.com/pillarjs/cookies#new-cookiesrequest-response--options)
|
|
207
205
|
|
|
208
206
|
**默认值:** `{}`
|
|
209
207
|
|
|
210
208
|
- `options.build`
|
|
211
|
-
|
|
209
|
+
|
|
212
210
|
需要构建可独立部署的小型mock服务时配置。
|
|
213
211
|
|
|
214
|
-
**类型:** `boolean | ServerBuildOptions`
|
|
212
|
+
**类型:** `boolean | ServerBuildOptions`
|
|
215
213
|
|
|
216
214
|
**默认值:**`false`
|
|
217
215
|
|
|
@@ -237,7 +235,7 @@ export default defineConfig({
|
|
|
237
235
|
```
|
|
238
236
|
|
|
239
237
|
- `options.priority`
|
|
240
|
-
|
|
238
|
+
|
|
241
239
|
自定义 路径匹配规则优先级。[查看更多](#自定义匹配优先级)
|
|
242
240
|
|
|
243
241
|
**默认值:** `undefined`
|
|
@@ -464,9 +462,8 @@ type Response = http.ServerResponse<http.IncomingMessage> & {
|
|
|
464
462
|
}
|
|
465
463
|
```
|
|
466
464
|
|
|
467
|
-
|
|
468
465
|
> **注意:**
|
|
469
|
-
>
|
|
466
|
+
>
|
|
470
467
|
> 如果使用 json/json5 编写 mock文件,则不支持使用 `response` 方法,以及不支持使用其他字段的函数形式。
|
|
471
468
|
|
|
472
469
|
## 共享 Mock 数据
|
|
@@ -516,11 +513,10 @@ export default defineMock([
|
|
|
516
513
|
```
|
|
517
514
|
|
|
518
515
|
> **注意:**
|
|
519
|
-
>
|
|
516
|
+
>
|
|
520
517
|
> `defineMockData` 仅是基于 `memory` 提供的共享数据支持,
|
|
521
518
|
> 如果需要做 mock 数据持久化,建议使用 `nosql`, 如 `lowdb` 或 `level` 等。
|
|
522
519
|
|
|
523
|
-
|
|
524
520
|
## 自定义匹配优先级
|
|
525
521
|
|
|
526
522
|
> 自定义规则仅影响包含动态参数的链接,如: `/api/user/:id`
|
|
@@ -599,7 +595,6 @@ export default defineMock({
|
|
|
599
595
|
})
|
|
600
596
|
```
|
|
601
597
|
|
|
602
|
-
|
|
603
598
|
**exp:** 在返回的响应头中,添加自定义 header 和 cookie
|
|
604
599
|
```ts
|
|
605
600
|
export default defineMock({
|
|
@@ -653,7 +648,6 @@ export default defineMock([
|
|
|
653
648
|
])
|
|
654
649
|
```
|
|
655
650
|
|
|
656
|
-
|
|
657
651
|
**exp:** 延迟接口响应:
|
|
658
652
|
```ts
|
|
659
653
|
export default defineMock({
|
|
@@ -737,7 +731,6 @@ export default defineMock({
|
|
|
737
731
|
```
|
|
738
732
|
请先安装 `mockjs`
|
|
739
733
|
|
|
740
|
-
|
|
741
734
|
**exp:** 使用 `response` 自定义响应
|
|
742
735
|
```ts
|
|
743
736
|
export default defineMock({
|
|
@@ -757,7 +750,6 @@ export default defineMock({
|
|
|
757
750
|
})
|
|
758
751
|
```
|
|
759
752
|
|
|
760
|
-
|
|
761
753
|
**exp:** 使用 json / json5
|
|
762
754
|
```json
|
|
763
755
|
{
|
|
@@ -768,7 +760,6 @@ export default defineMock({
|
|
|
768
760
|
}
|
|
769
761
|
```
|
|
770
762
|
|
|
771
|
-
|
|
772
763
|
**exp:** multipart, 文件上传.
|
|
773
764
|
|
|
774
765
|
通过 [`formidable`](https://www.npmjs.com/package/formidable#readme) 支持。
|
|
@@ -776,7 +767,7 @@ export default defineMock({
|
|
|
776
767
|
<form action="/api/upload" method="post" enctype="multipart/form-data">
|
|
777
768
|
<p>
|
|
778
769
|
<span>file: </span>
|
|
779
|
-
<input type="file" name="files" multiple>
|
|
770
|
+
<input type="file" name="files" multiple="multiple">
|
|
780
771
|
</p>
|
|
781
772
|
<p>
|
|
782
773
|
<span>name:</span>
|
package/dist/index.cjs
CHANGED
|
@@ -6,8 +6,8 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
|
6
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
8
|
var __export = (target, all) => {
|
|
9
|
-
for (var
|
|
10
|
-
__defProp(target,
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
11
|
};
|
|
12
12
|
var __copyProps = (to, from, except, desc) => {
|
|
13
13
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
@@ -60,10 +60,7 @@ var import_utils3 = require("@pengzhanbo/utils");
|
|
|
60
60
|
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
61
61
|
var import_is_core_module = __toESM(require("is-core-module"), 1);
|
|
62
62
|
var import_pluginutils = require("@rollup/pluginutils");
|
|
63
|
-
|
|
64
|
-
// package.json
|
|
65
|
-
var name = "vite-plugin-mock-dev-server";
|
|
66
|
-
var version = "1.4.2";
|
|
63
|
+
var import_picocolors3 = __toESM(require("picocolors"), 1);
|
|
67
64
|
|
|
68
65
|
// src/compiler.ts
|
|
69
66
|
var import_node_fs2 = __toESM(require("fs"), 1);
|
|
@@ -214,7 +211,7 @@ async function transformWithEsbuild(entryPoint, options) {
|
|
|
214
211
|
entryPoints: [entryPoint],
|
|
215
212
|
outfile: "out.js",
|
|
216
213
|
write: false,
|
|
217
|
-
target: ["
|
|
214
|
+
target: ["node16"],
|
|
218
215
|
platform: "node",
|
|
219
216
|
bundle: true,
|
|
220
217
|
metafile: true,
|
|
@@ -420,11 +417,19 @@ async function generateMockServer(ctx, config, options) {
|
|
|
420
417
|
];
|
|
421
418
|
try {
|
|
422
419
|
if (import_node_path3.default.isAbsolute(outputDir)) {
|
|
423
|
-
for (const { filename } of outputList)
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
420
|
+
for (const { filename } of outputList) {
|
|
421
|
+
if (import_node_fs3.default.existsSync(filename))
|
|
422
|
+
await import_promises.default.rm(filename);
|
|
423
|
+
}
|
|
424
|
+
config.logger.info(`${import_picocolors3.default.green("\u2713")} generate mock server in ${import_picocolors3.default.cyan(outputDir)}`);
|
|
425
|
+
for (const { filename, source } of outputList) {
|
|
426
|
+
import_node_fs3.default.mkdirSync(import_node_path3.default.dirname(filename), { recursive: true });
|
|
427
427
|
await import_promises.default.writeFile(filename, source, "utf-8");
|
|
428
|
+
const sourceSize = (source.length / 1024).toFixed(2);
|
|
429
|
+
const name = import_node_path3.default.relative(outputDir, filename);
|
|
430
|
+
const space = name.length < 30 ? " ".repeat(30 - name.length) : "";
|
|
431
|
+
config.logger.info(` ${import_picocolors3.default.green(name)}${space}${import_picocolors3.default.bold(import_picocolors3.default.dim(`${sourceSize} kB`))}`);
|
|
432
|
+
}
|
|
428
433
|
} else {
|
|
429
434
|
for (const { filename, source } of outputList) {
|
|
430
435
|
ctx.emitFile({
|
|
@@ -434,12 +439,13 @@ async function generateMockServer(ctx, config, options) {
|
|
|
434
439
|
});
|
|
435
440
|
}
|
|
436
441
|
}
|
|
437
|
-
} catch {
|
|
442
|
+
} catch (e) {
|
|
443
|
+
console.error(e);
|
|
438
444
|
}
|
|
439
445
|
}
|
|
440
446
|
function getMockDependencies(deps, alias) {
|
|
441
447
|
const list = /* @__PURE__ */ new Set();
|
|
442
|
-
const excludeDeps = [
|
|
448
|
+
const excludeDeps = ["vite-plugin-mock-dev-server", "connect", "cors"];
|
|
443
449
|
const isAlias = (p) => alias.find(({ find }) => aliasMatches(find, p));
|
|
444
450
|
Object.keys(deps).forEach((mPath) => {
|
|
445
451
|
const imports = deps[mPath].imports.filter((_) => _.external && !_.path.startsWith("<define:") && !isAlias(_.path)).map((_) => _.path);
|
|
@@ -461,7 +467,7 @@ function generatePackageJson(pkg, mockDeps) {
|
|
|
461
467
|
},
|
|
462
468
|
dependencies: {
|
|
463
469
|
"connect": "^3.7.0",
|
|
464
|
-
"vite-plugin-mock-dev-server": `^${
|
|
470
|
+
"vite-plugin-mock-dev-server": `^${"1.4.5"}`,
|
|
465
471
|
"cors": "^2.8.5"
|
|
466
472
|
},
|
|
467
473
|
pnpm: { peerDependencyRules: { ignoreMissing: ["vite"] } }
|
|
@@ -476,7 +482,7 @@ function generatorServerEntryCode(httpProxies, wsProxies, cookiesOptions = {}, p
|
|
|
476
482
|
return `import { createServer } from 'node:http';
|
|
477
483
|
import connect from 'connect';
|
|
478
484
|
import corsMiddleware from 'cors';
|
|
479
|
-
import { baseMiddleware,
|
|
485
|
+
import { baseMiddleware, createLogger, mockWebSocket } from 'vite-plugin-mock-dev-server';
|
|
480
486
|
import mockData from './mock-data.js';
|
|
481
487
|
|
|
482
488
|
const app = connect();
|
|
@@ -547,6 +553,7 @@ export default transformMockData(mockList);`;
|
|
|
547
553
|
var import_utils14 = require("@pengzhanbo/utils");
|
|
548
554
|
var import_cors = __toESM(require("cors"), 1);
|
|
549
555
|
var import_path_to_regexp5 = require("path-to-regexp");
|
|
556
|
+
var import_picocolors6 = __toESM(require("picocolors"), 1);
|
|
550
557
|
|
|
551
558
|
// src/baseMiddleware.ts
|
|
552
559
|
var import_node_buffer2 = require("buffer");
|
|
@@ -555,7 +562,7 @@ var import_cookies = __toESM(require("cookies"), 1);
|
|
|
555
562
|
var import_http_status = __toESM(require("http-status"), 1);
|
|
556
563
|
var mime = __toESM(require("mime-types"), 1);
|
|
557
564
|
var import_path_to_regexp3 = require("path-to-regexp");
|
|
558
|
-
var
|
|
565
|
+
var import_picocolors4 = __toESM(require("picocolors"), 1);
|
|
559
566
|
|
|
560
567
|
// src/matchingWeight.ts
|
|
561
568
|
var import_utils5 = require("@pengzhanbo/utils");
|
|
@@ -627,11 +634,11 @@ function defaultPriority(rules) {
|
|
|
627
634
|
pattern = "",
|
|
628
635
|
modifier,
|
|
629
636
|
prefix,
|
|
630
|
-
name
|
|
637
|
+
name
|
|
631
638
|
} = isDynamic ? token : {};
|
|
632
639
|
const isGlob = pattern && pattern.includes(".*");
|
|
633
640
|
const isSlash = prefix === "/";
|
|
634
|
-
const isNamed = (0, import_utils5.isString)(
|
|
641
|
+
const isNamed = (0, import_utils5.isString)(name);
|
|
635
642
|
exp += isDynamic && isSlash ? 1 : 0;
|
|
636
643
|
if (i === tokens.length - 1 && isGlob) {
|
|
637
644
|
weight += 5 * 10 ** (tokens.length === 1 ? highest + 1 : highest);
|
|
@@ -853,10 +860,10 @@ function baseMiddleware(mockLoader, {
|
|
|
853
860
|
}
|
|
854
861
|
if (!mock) {
|
|
855
862
|
const matched = mockUrls.map(
|
|
856
|
-
(m) => m === _mockUrl ?
|
|
863
|
+
(m) => m === _mockUrl ? import_picocolors4.default.underline(import_picocolors4.default.bold(m)) : import_picocolors4.default.dim(m)
|
|
857
864
|
).join(", ");
|
|
858
865
|
logger.warn(
|
|
859
|
-
`${
|
|
866
|
+
`${import_picocolors4.default.green(
|
|
860
867
|
pathname
|
|
861
868
|
)} matches ${matched} , but mock data is not found.`
|
|
862
869
|
);
|
|
@@ -885,10 +892,10 @@ function baseMiddleware(mockLoader, {
|
|
|
885
892
|
await provideCookies(request, response, mock, logger);
|
|
886
893
|
logger.info(requestLog(request, filepath), logLevel);
|
|
887
894
|
logger.debug(
|
|
888
|
-
`${
|
|
895
|
+
`${import_picocolors4.default.magenta("DEBUG")} ${import_picocolors4.default.underline(
|
|
889
896
|
pathname
|
|
890
897
|
)} matches: [ ${mockUrls.map(
|
|
891
|
-
(m) => m === _mockUrl ?
|
|
898
|
+
(m) => m === _mockUrl ? import_picocolors4.default.underline(import_picocolors4.default.bold(m)) : import_picocolors4.default.dim(m)
|
|
892
899
|
).join(", ")} ]
|
|
893
900
|
`
|
|
894
901
|
);
|
|
@@ -899,11 +906,11 @@ function baseMiddleware(mockLoader, {
|
|
|
899
906
|
sendData(response, content, type);
|
|
900
907
|
} catch (e) {
|
|
901
908
|
logger.error(
|
|
902
|
-
`${
|
|
909
|
+
`${import_picocolors4.default.red(
|
|
903
910
|
`mock error at ${pathname}`
|
|
904
911
|
)}
|
|
905
912
|
${e}
|
|
906
|
-
at body (${
|
|
913
|
+
at body (${import_picocolors4.default.underline(filepath)})`,
|
|
907
914
|
logLevel
|
|
908
915
|
);
|
|
909
916
|
responseStatus(response, 500);
|
|
@@ -917,11 +924,11 @@ ${e}
|
|
|
917
924
|
await responseFn(request, response, next);
|
|
918
925
|
} catch (e) {
|
|
919
926
|
logger.error(
|
|
920
|
-
`${
|
|
927
|
+
`${import_picocolors4.default.red(
|
|
921
928
|
`mock error at ${pathname}`
|
|
922
929
|
)}
|
|
923
930
|
${e}
|
|
924
|
-
at response (${
|
|
931
|
+
at response (${import_picocolors4.default.underline(filepath)})`,
|
|
925
932
|
logLevel
|
|
926
933
|
);
|
|
927
934
|
responseStatus(response, 500);
|
|
@@ -954,11 +961,11 @@ function fineMock(mockList, logger, {
|
|
|
954
961
|
} catch (e) {
|
|
955
962
|
const file = mock.__filepath__;
|
|
956
963
|
logger.error(
|
|
957
|
-
`${
|
|
964
|
+
`${import_picocolors4.default.red(
|
|
958
965
|
`mock error at ${pathname}`
|
|
959
966
|
)}
|
|
960
967
|
${e}
|
|
961
|
-
at validator (${
|
|
968
|
+
at validator (${import_picocolors4.default.underline(file)})`,
|
|
962
969
|
mock.log
|
|
963
970
|
);
|
|
964
971
|
return false;
|
|
@@ -989,11 +996,11 @@ async function provideHeaders(req, res, mock, logger) {
|
|
|
989
996
|
});
|
|
990
997
|
} catch (e) {
|
|
991
998
|
logger.error(
|
|
992
|
-
`${
|
|
999
|
+
`${import_picocolors4.default.red(
|
|
993
1000
|
`mock error at ${req.url.split("?")[0]}`
|
|
994
1001
|
)}
|
|
995
1002
|
${e}
|
|
996
|
-
at headers (${
|
|
1003
|
+
at headers (${import_picocolors4.default.underline(filepath)})`,
|
|
997
1004
|
mock.log
|
|
998
1005
|
);
|
|
999
1006
|
}
|
|
@@ -1016,11 +1023,11 @@ async function provideCookies(req, res, mock, logger) {
|
|
|
1016
1023
|
});
|
|
1017
1024
|
} catch (e) {
|
|
1018
1025
|
logger.error(
|
|
1019
|
-
`${
|
|
1026
|
+
`${import_picocolors4.default.red(
|
|
1020
1027
|
`mock error at ${req.url.split("?")[0]}`
|
|
1021
1028
|
)}
|
|
1022
1029
|
${e}
|
|
1023
|
-
at cookies (${
|
|
1030
|
+
at cookies (${import_picocolors4.default.underline(filepath)})`,
|
|
1024
1031
|
mock.log
|
|
1025
1032
|
);
|
|
1026
1033
|
}
|
|
@@ -1054,15 +1061,15 @@ function getHTTPStatusText(status) {
|
|
|
1054
1061
|
function requestLog(request, filepath) {
|
|
1055
1062
|
const { url, method, query, params, body } = request;
|
|
1056
1063
|
let { pathname } = new URL(url, "http://example.com");
|
|
1057
|
-
pathname =
|
|
1064
|
+
pathname = import_picocolors4.default.green(decodeURIComponent(pathname));
|
|
1058
1065
|
const format = (prefix, data) => {
|
|
1059
|
-
return !data || (0, import_utils7.isEmptyObject)(data) ? "" : ` ${
|
|
1066
|
+
return !data || (0, import_utils7.isEmptyObject)(data) ? "" : ` ${import_picocolors4.default.gray(`${prefix}:`)}${JSON.stringify(data)}`;
|
|
1060
1067
|
};
|
|
1061
|
-
const ms =
|
|
1068
|
+
const ms = import_picocolors4.default.magenta(import_picocolors4.default.bold(method));
|
|
1062
1069
|
const qs = format("query", query);
|
|
1063
1070
|
const ps = format("params", params);
|
|
1064
1071
|
const bs = format("body", body);
|
|
1065
|
-
const file = ` ${
|
|
1072
|
+
const file = ` ${import_picocolors4.default.dim(import_picocolors4.default.underline(`(${filepath})`))}`;
|
|
1066
1073
|
return `${ms} ${pathname}${qs}${ps}${bs}${file}`;
|
|
1067
1074
|
}
|
|
1068
1075
|
|
|
@@ -1305,7 +1312,7 @@ var MockLoader = class extends import_node_events.default {
|
|
|
1305
1312
|
// src/ws.ts
|
|
1306
1313
|
var import_cookies2 = __toESM(require("cookies"), 1);
|
|
1307
1314
|
var import_path_to_regexp4 = require("path-to-regexp");
|
|
1308
|
-
var
|
|
1315
|
+
var import_picocolors5 = __toESM(require("picocolors"), 1);
|
|
1309
1316
|
var import_ws = require("ws");
|
|
1310
1317
|
function mockWebSocket({
|
|
1311
1318
|
loader,
|
|
@@ -1340,7 +1347,7 @@ function mockWebSocket({
|
|
|
1340
1347
|
wss.on("close", () => wssMap.delete(pathname));
|
|
1341
1348
|
wss.on("error", (e) => {
|
|
1342
1349
|
logger.error(
|
|
1343
|
-
`${
|
|
1350
|
+
`${import_picocolors5.default.red(
|
|
1344
1351
|
`WebSocket mock error at ${wss.path}`
|
|
1345
1352
|
)}
|
|
1346
1353
|
${e}
|
|
@@ -1350,7 +1357,7 @@ ${e}
|
|
|
1350
1357
|
});
|
|
1351
1358
|
} catch (e) {
|
|
1352
1359
|
logger.error(
|
|
1353
|
-
`${
|
|
1360
|
+
`${import_picocolors5.default.red(
|
|
1354
1361
|
`WebSocket mock error at ${wss.path}`
|
|
1355
1362
|
)}
|
|
1356
1363
|
${e}
|
|
@@ -1431,9 +1438,9 @@ ${e}
|
|
|
1431
1438
|
request.getCookie = cookies.get.bind(cookies);
|
|
1432
1439
|
wss.handleUpgrade(request, socket, head, (ws) => {
|
|
1433
1440
|
logger.info(
|
|
1434
|
-
`${
|
|
1441
|
+
`${import_picocolors5.default.magenta(import_picocolors5.default.bold("WebSocket"))} ${import_picocolors5.default.green(
|
|
1435
1442
|
req.url
|
|
1436
|
-
)} connected ${
|
|
1443
|
+
)} connected ${import_picocolors5.default.dim(`(${filepath})`)}`,
|
|
1437
1444
|
mock.log
|
|
1438
1445
|
);
|
|
1439
1446
|
wssContext.connectionList.push({ req: request, ws });
|
|
@@ -1479,7 +1486,9 @@ function mockServerMiddleware(config, options, httpServer, ws) {
|
|
|
1479
1486
|
httpServer == null ? void 0 : httpServer.on("close", () => loader.close());
|
|
1480
1487
|
const { httpProxies } = ensureProxies(config.server.proxy || {});
|
|
1481
1488
|
const prefix = (0, import_utils14.toArray)(options.prefix);
|
|
1482
|
-
const proxies = [...prefix, ...httpProxies];
|
|
1489
|
+
const proxies = (0, import_utils14.uniq)([...prefix, ...httpProxies]);
|
|
1490
|
+
if (!proxies.length)
|
|
1491
|
+
logger.warn(`No proxy was configured, mock server will not work. See ${import_picocolors6.default.cyan("https://vite-plugin-mock-dev-server.netlify.app/guide/usage")}`);
|
|
1483
1492
|
mockWebSocket({
|
|
1484
1493
|
loader,
|
|
1485
1494
|
httpServer,
|
|
@@ -1737,7 +1746,7 @@ function warnCjsUsage() {
|
|
|
1737
1746
|
const log = process.env.VITE_CJS_TRACE ? console.trace : console.warn
|
|
1738
1747
|
log(
|
|
1739
1748
|
yellow(
|
|
1740
|
-
`The CJS build of
|
|
1749
|
+
`The CJS build of vite-plugin-mock-dev-server's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details.`,
|
|
1741
1750
|
),
|
|
1742
1751
|
)
|
|
1743
1752
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -549,7 +549,7 @@ type FormidableFile = formidable.File | formidable.File[];
|
|
|
549
549
|
type LogType = 'info' | 'warn' | 'error' | 'debug';
|
|
550
550
|
type LogLevel = LogType | 'silent';
|
|
551
551
|
|
|
552
|
-
/** @deprecated The CJS build of
|
|
552
|
+
/** @deprecated The CJS build of vite-plugin-mock-dev-server's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details. */
|
|
553
553
|
declare function mockDevServerPlugin({ prefix, wsPrefix, include, exclude, reload, log, cors, formidableOptions, build, cookiesOptions, priority, }?: MockServerPluginOptions): Plugin[];
|
|
554
554
|
|
|
555
555
|
/**
|
package/dist/index.js
CHANGED
|
@@ -10,10 +10,7 @@ import { toArray } from "@pengzhanbo/utils";
|
|
|
10
10
|
import fg from "fast-glob";
|
|
11
11
|
import isCore from "is-core-module";
|
|
12
12
|
import { createFilter } from "@rollup/pluginutils";
|
|
13
|
-
|
|
14
|
-
// package.json
|
|
15
|
-
var name = "vite-plugin-mock-dev-server";
|
|
16
|
-
var version = "1.4.2";
|
|
13
|
+
import c from "picocolors";
|
|
17
14
|
|
|
18
15
|
// src/compiler.ts
|
|
19
16
|
import fs2, { promises as fsp } from "node:fs";
|
|
@@ -164,7 +161,7 @@ async function transformWithEsbuild(entryPoint, options) {
|
|
|
164
161
|
entryPoints: [entryPoint],
|
|
165
162
|
outfile: "out.js",
|
|
166
163
|
write: false,
|
|
167
|
-
target: ["
|
|
164
|
+
target: ["node16"],
|
|
168
165
|
platform: "node",
|
|
169
166
|
bundle: true,
|
|
170
167
|
metafile: true,
|
|
@@ -370,11 +367,19 @@ async function generateMockServer(ctx, config, options) {
|
|
|
370
367
|
];
|
|
371
368
|
try {
|
|
372
369
|
if (path3.isAbsolute(outputDir)) {
|
|
373
|
-
for (const { filename } of outputList)
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
370
|
+
for (const { filename } of outputList) {
|
|
371
|
+
if (fs3.existsSync(filename))
|
|
372
|
+
await fsp2.rm(filename);
|
|
373
|
+
}
|
|
374
|
+
config.logger.info(`${c.green("\u2713")} generate mock server in ${c.cyan(outputDir)}`);
|
|
375
|
+
for (const { filename, source } of outputList) {
|
|
376
|
+
fs3.mkdirSync(path3.dirname(filename), { recursive: true });
|
|
377
377
|
await fsp2.writeFile(filename, source, "utf-8");
|
|
378
|
+
const sourceSize = (source.length / 1024).toFixed(2);
|
|
379
|
+
const name = path3.relative(outputDir, filename);
|
|
380
|
+
const space = name.length < 30 ? " ".repeat(30 - name.length) : "";
|
|
381
|
+
config.logger.info(` ${c.green(name)}${space}${c.bold(c.dim(`${sourceSize} kB`))}`);
|
|
382
|
+
}
|
|
378
383
|
} else {
|
|
379
384
|
for (const { filename, source } of outputList) {
|
|
380
385
|
ctx.emitFile({
|
|
@@ -384,12 +389,13 @@ async function generateMockServer(ctx, config, options) {
|
|
|
384
389
|
});
|
|
385
390
|
}
|
|
386
391
|
}
|
|
387
|
-
} catch {
|
|
392
|
+
} catch (e) {
|
|
393
|
+
console.error(e);
|
|
388
394
|
}
|
|
389
395
|
}
|
|
390
396
|
function getMockDependencies(deps, alias) {
|
|
391
397
|
const list = /* @__PURE__ */ new Set();
|
|
392
|
-
const excludeDeps = [
|
|
398
|
+
const excludeDeps = ["vite-plugin-mock-dev-server", "connect", "cors"];
|
|
393
399
|
const isAlias = (p) => alias.find(({ find }) => aliasMatches(find, p));
|
|
394
400
|
Object.keys(deps).forEach((mPath) => {
|
|
395
401
|
const imports = deps[mPath].imports.filter((_) => _.external && !_.path.startsWith("<define:") && !isAlias(_.path)).map((_) => _.path);
|
|
@@ -411,7 +417,7 @@ function generatePackageJson(pkg, mockDeps) {
|
|
|
411
417
|
},
|
|
412
418
|
dependencies: {
|
|
413
419
|
"connect": "^3.7.0",
|
|
414
|
-
"vite-plugin-mock-dev-server": `^${
|
|
420
|
+
"vite-plugin-mock-dev-server": `^${"1.4.5"}`,
|
|
415
421
|
"cors": "^2.8.5"
|
|
416
422
|
},
|
|
417
423
|
pnpm: { peerDependencyRules: { ignoreMissing: ["vite"] } }
|
|
@@ -426,7 +432,7 @@ function generatorServerEntryCode(httpProxies, wsProxies, cookiesOptions = {}, p
|
|
|
426
432
|
return `import { createServer } from 'node:http';
|
|
427
433
|
import connect from 'connect';
|
|
428
434
|
import corsMiddleware from 'cors';
|
|
429
|
-
import { baseMiddleware,
|
|
435
|
+
import { baseMiddleware, createLogger, mockWebSocket } from 'vite-plugin-mock-dev-server';
|
|
430
436
|
import mockData from './mock-data.js';
|
|
431
437
|
|
|
432
438
|
const app = connect();
|
|
@@ -494,9 +500,10 @@ export default transformMockData(mockList);`;
|
|
|
494
500
|
}
|
|
495
501
|
|
|
496
502
|
// src/mockMiddleware.ts
|
|
497
|
-
import { isBoolean as isBoolean2, toArray as toArray3 } from "@pengzhanbo/utils";
|
|
503
|
+
import { isBoolean as isBoolean2, toArray as toArray3, uniq as uniq2 } from "@pengzhanbo/utils";
|
|
498
504
|
import cors from "cors";
|
|
499
505
|
import { pathToRegexp as pathToRegexp4 } from "path-to-regexp";
|
|
506
|
+
import c2 from "picocolors";
|
|
500
507
|
|
|
501
508
|
// src/baseMiddleware.ts
|
|
502
509
|
import { Buffer as Buffer2 } from "node:buffer";
|
|
@@ -590,11 +597,11 @@ function defaultPriority(rules) {
|
|
|
590
597
|
pattern = "",
|
|
591
598
|
modifier,
|
|
592
599
|
prefix,
|
|
593
|
-
name
|
|
600
|
+
name
|
|
594
601
|
} = isDynamic ? token : {};
|
|
595
602
|
const isGlob = pattern && pattern.includes(".*");
|
|
596
603
|
const isSlash = prefix === "/";
|
|
597
|
-
const isNamed = isString(
|
|
604
|
+
const isNamed = isString(name);
|
|
598
605
|
exp += isDynamic && isSlash ? 1 : 0;
|
|
599
606
|
if (i === tokens.length - 1 && isGlob) {
|
|
600
607
|
weight += 5 * 10 ** (tokens.length === 1 ? highest + 1 : highest);
|
|
@@ -1448,7 +1455,9 @@ function mockServerMiddleware(config, options, httpServer, ws) {
|
|
|
1448
1455
|
httpServer == null ? void 0 : httpServer.on("close", () => loader.close());
|
|
1449
1456
|
const { httpProxies } = ensureProxies(config.server.proxy || {});
|
|
1450
1457
|
const prefix = toArray3(options.prefix);
|
|
1451
|
-
const proxies = [...prefix, ...httpProxies];
|
|
1458
|
+
const proxies = uniq2([...prefix, ...httpProxies]);
|
|
1459
|
+
if (!proxies.length)
|
|
1460
|
+
logger.warn(`No proxy was configured, mock server will not work. See ${c2.cyan("https://vite-plugin-mock-dev-server.netlify.app/guide/usage")}`);
|
|
1452
1461
|
mockWebSocket({
|
|
1453
1462
|
loader,
|
|
1454
1463
|
httpServer,
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-mock-dev-server",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.4.
|
|
5
|
-
"packageManager": "pnpm@8.
|
|
4
|
+
"version": "1.4.5",
|
|
5
|
+
"packageManager": "pnpm@8.14.0",
|
|
6
6
|
"author": "pengzhanbo <q942450674@outlook.com> (https://github.com/pengzhanbo)",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"homepage": "https://vite-plugin-mock-dev-server.netlify.app",
|
|
@@ -47,22 +47,22 @@
|
|
|
47
47
|
"@rollup/pluginutils": "^5.1.0",
|
|
48
48
|
"chokidar": "^3.5.3",
|
|
49
49
|
"co-body": "^6.1.0",
|
|
50
|
-
"cookies": "^0.
|
|
50
|
+
"cookies": "^0.9.1",
|
|
51
51
|
"cors": "^2.8.5",
|
|
52
52
|
"debug": "^4.3.4",
|
|
53
|
-
"esbuild": "^0.19.
|
|
53
|
+
"esbuild": "^0.19.11",
|
|
54
54
|
"fast-glob": "^3.3.2",
|
|
55
|
-
"formidable": "2.1.
|
|
55
|
+
"formidable": "2.1.2",
|
|
56
56
|
"http-status": "^1.7.3",
|
|
57
57
|
"is-core-module": "^2.13.1",
|
|
58
58
|
"json5": "^2.2.3",
|
|
59
59
|
"mime-types": "^2.1.35",
|
|
60
60
|
"path-to-regexp": "^6.2.1",
|
|
61
61
|
"picocolors": "^1.0.0",
|
|
62
|
-
"ws": "^8.
|
|
62
|
+
"ws": "^8.16.0"
|
|
63
63
|
},
|
|
64
64
|
"devDependencies": {
|
|
65
|
-
"@pengzhanbo/eslint-config": "^1.
|
|
65
|
+
"@pengzhanbo/eslint-config": "^1.5.1",
|
|
66
66
|
"@types/co-body": "^6.1.3",
|
|
67
67
|
"@types/cookies": "^0.7.10",
|
|
68
68
|
"@types/cors": "^2.8.17",
|
|
@@ -70,17 +70,17 @@
|
|
|
70
70
|
"@types/formidable": "2.0.6",
|
|
71
71
|
"@types/is-core-module": "^2.2.2",
|
|
72
72
|
"@types/mime-types": "^2.1.4",
|
|
73
|
-
"@types/node": "^20.10.
|
|
73
|
+
"@types/node": "^20.10.8",
|
|
74
74
|
"@types/ws": "^8.5.10",
|
|
75
75
|
"bumpp": "^9.2.1",
|
|
76
76
|
"conventional-changelog-cli": "^4.1.0",
|
|
77
|
-
"eslint": "^8.
|
|
77
|
+
"eslint": "^8.56.0",
|
|
78
78
|
"mockjs": "^1.1.0",
|
|
79
79
|
"tsup": "^8.0.1",
|
|
80
80
|
"typescript": "^5.3.3",
|
|
81
|
-
"vite": "^5.0.
|
|
82
|
-
"vitepress": "^1.0.0-rc.
|
|
83
|
-
"vitest": "^1.
|
|
81
|
+
"vite": "^5.0.11",
|
|
82
|
+
"vitepress": "^1.0.0-rc.36",
|
|
83
|
+
"vitest": "^1.1.3"
|
|
84
84
|
},
|
|
85
85
|
"scripts": {
|
|
86
86
|
"build": "tsup && node ./deprecate.mjs",
|