Skip to content

Commit 99abfaf

Browse files
committed
chore: release 3.0.0
1 parent b8c0e22 commit 99abfaf

File tree

5 files changed

+143
-121
lines changed

5 files changed

+143
-121
lines changed

.github/workflows/publish.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
run: |
2424
npm i -g pnpm
2525
pnpm install
26-
pnpm publish --filter=vite-mock-plugin
26+
pnpm publish --filter=./packages/vite-plugin-mock
2727
2828
env:
2929
NODE_AUTH_TOKEN: ${{secrets.npm_token}}

CHANGELOG.md

+67
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,70 @@
1+
## [3.0.0]
2+
3+
### Breaking Change
4+
5+
- Remove the `localEnabled` and `prodEnabled` configuration and use the `enable` configuration instead, no longer distinguishing the environment
6+
- Remove the `injectFile``injectCode` and `supportTs` configuration
7+
- Minimum requirement NodeJs 16
8+
- Production Environment Syntax Changes
9+
10+
```ts
11+
// vite-plugin-mock/es/createProdMockServer => vite-plugin-mock/client
12+
import { createProdMockServer } from 'vite-plugin-mock/client'
13+
import roleMock from '../mock/dep/role'
14+
// functional form
15+
import userMockFn from '../mock/user'
16+
17+
export async function setupProdMockServer() {
18+
const mockModules = [...roleMock, ...userMockFn()]
19+
createProdMockServer(mockModules)
20+
}
21+
```
22+
23+
### Features
24+
25+
- Upgrade all dependencies to latest
26+
- support `vite4` and `rollup3`
27+
- The function module supports returning a function
28+
29+
```ts
30+
import type { MockConfig } from 'vite-plugin-mock'
31+
32+
export default (config?: MockConfig) => {
33+
return [
34+
{
35+
url: '/api/createUser',
36+
method: 'post',
37+
response: ({ body, query }) => {
38+
console.log('body>>>>>>>>', body)
39+
console.log('query>>>>>>>>', query)
40+
41+
return {
42+
code: 0,
43+
message: 'ok',
44+
data: { a: 21, 'import.meta.url': import.meta.url },
45+
}
46+
},
47+
},
48+
]
49+
}
50+
```
51+
52+
## [2.9.8](https://github.com/vbenjs/vite-plugin-mock/compare/v2.9.0...v2.9.8) (2023-04-17)
53+
54+
### Bug Fixes
55+
56+
- can't update in real time, fix [#40](https://github.com/vbenjs/vite-plugin-mock/issues/40) ([f25c6ac](https://github.com/vbenjs/vite-plugin-mock/commit/f25c6ac8544991457368746bf6ec5fdfd8b4e083))
57+
- error handle, fix [#39](https://github.com/vbenjs/vite-plugin-mock/issues/39) ([874318c](https://github.com/vbenjs/vite-plugin-mock/commit/874318ce399dc78c33d91161b73e3ced7bb6e9b6))
58+
- fix js parsing error ([1dbee45](https://github.com/vbenjs/vite-plugin-mock/commit/1dbee452ec7d90b07ac86ce8530430e864589ab5))
59+
- fix node version ([#38](https://github.com/vbenjs/vite-plugin-mock/issues/38)) ([f91dea1](https://github.com/vbenjs/vite-plugin-mock/commit/f91dea1be943aa1145727bfecd273ef45cdff8e9))
60+
- inappropriate type annotation ([#90](https://github.com/vbenjs/vite-plugin-mock/issues/90)) ([a3fc4aa](https://github.com/vbenjs/vite-plugin-mock/commit/a3fc4aab3dd1864f640918516c10d7d1530f5d90))
61+
- typo, close [#42](https://github.com/vbenjs/vite-plugin-mock/issues/42) ([5b4e946](https://github.com/vbenjs/vite-plugin-mock/commit/5b4e9469060109a2a28c079eec8384fe654be68e))
62+
63+
### Features
64+
65+
- **prod:** response 在 prod 模式下入参对象加上 url ([#83](https://github.com/vbenjs/vite-plugin-mock/issues/83)) ([cd0b86f](https://github.com/vbenjs/vite-plugin-mock/commit/cd0b86fce20228094cf18be14194498a2cbd490e))
66+
- **server:** add this for response, rawResponse function ([#43](https://github.com/vbenjs/vite-plugin-mock/issues/43)) ([2f4d6d2](https://github.com/vbenjs/vite-plugin-mock/commit/2f4d6d2984c7fe15236cb8b0d2ec5479930d5668))
67+
168
## [2.9.8](https://github.com/anncwb/vite-plugin-mock/compare/v2.9.0...v2.9.8) (2023-04-16)
269

370
### Bug Fixes

README.md

+37-56
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@ A mock plugin for vite, developed based on mockjs. And support the local environ
1818
yarn add mockjs
1919
# or
2020
npm i mockjs -S
21+
# or
22+
pnpm add mockjs
2123
```
2224

25+
and
26+
2327
```bash
2428
yarn add vite-plugin-mock -D
2529
# or
2630
npm i vite-plugin-mock -D
31+
# or
32+
pnpm add vite-plugin-mock -D
2733
```
2834

2935
### Example
@@ -71,7 +77,7 @@ export default ({ command }: ConfigEnv): UserConfigExport => {
7177
viteMockServe({
7278
// default
7379
mockPath: 'mock',
74-
localEnabled: command === 'serve',
80+
enable: true,
7581
}),
7682
],
7783
}
@@ -83,15 +89,11 @@ export default ({ command }: ConfigEnv): UserConfigExport => {
8389
```ts
8490
{
8591
mockPath?: string;
86-
supportTs?: boolean;
8792
ignore?: RegExp | ((fileName: string) => boolean);
8893
watchFiles?: boolean;
89-
localEnabled?: boolean;
94+
enable?: boolean;
9095
ignoreFiles?: string[];
9196
configPath?: string;
92-
prodEnabled?: boolean;
93-
injectFile?: string;
94-
injectCode?: string;
9597
}
9698
```
9799

@@ -109,14 +111,6 @@ If `watchFiles:true`, the file changes in the folder will be monitored. And sync
109111

110112
If configPath has a value, it is invalid
111113

112-
### supportTs
113-
114-
**type:** `boolean`
115-
116-
**default:** `true`
117-
118-
After opening, the ts file module can be read. Note that you will not be able to monitor .js files after opening.
119-
120114
### ignore
121115

122116
**type:** `RegExp | ((fileName: string) => boolean);`
@@ -133,41 +127,13 @@ When automatically reading analog .ts files, ignore files in the specified forma
133127

134128
Set whether to monitor changes in mock .ts files
135129

136-
### localEnabled
137-
138-
**type:** `boolean`
139-
140-
**default:** `command === 'serve'`
141-
142-
Set whether to enable the local mock .ts file, do not open it in the production environment
143-
144-
### prodEnabled
130+
### enable
145131

146132
**type:** `boolean`
147133

148-
**default:** `command !=='serve'`
134+
**default:** true
149135

150-
Set whether to enable mock function for packaging
151-
152-
### injectCode
153-
154-
**type:** `string`
155-
156-
**default:**''
157-
158-
If the mock function is enabled in the production environment, that is, `prodEnabled=true`, the code will be injected into the bottom of the file corresponding to `injectFile`. The default is `main.{ts,js}`
159-
160-
The advantage of this is that you can dynamically control whether mock is enabled in the production environment and mock.js will not be packaged when it is not enabled.
161-
162-
If the code is written directly in `main.ts`, no matter whether it is turned on or not, the final package will include `mock.js`
163-
164-
### injectFile
165-
166-
**type:** `string`
167-
168-
**default:** `path.resolve(process.cwd(),'src/main.{ts,js}')`
169-
170-
The file injected by `injectCode` code, the default is `src/main.{ts,js}` in the project root directory
136+
Whether to enable the mock function
171137

172138
### configPath
173139

@@ -192,7 +158,7 @@ Whether to display the request log on the console
192158
```ts
193159
// test.ts
194160

195-
import { MockMethod } from 'vite-plugin-mock'
161+
import { MockMethod, MockConfig } from 'vite-plugin-mock'
196162
export default [
197163
{
198164
url: '/api/get',
@@ -234,6 +200,27 @@ export default [
234200
},
235201
},
236202
] as MockMethod[]
203+
204+
export default function (config: MockConfig) {
205+
return [
206+
{
207+
url: '/api/text',
208+
method: 'post',
209+
rawResponse: async (req, res) => {
210+
let reqbody = ''
211+
await new Promise((resolve) => {
212+
req.on('data', (chunk) => {
213+
reqbody += chunk
214+
})
215+
req.on('end', () => resolve(undefined))
216+
})
217+
res.setHeader('Content-Type', 'text/plain')
218+
res.statusCode = 200
219+
res.end(`hello, ${reqbody}`)
220+
},
221+
},
222+
]
223+
}
237224
```
238225

239226
### MockMethod
@@ -256,14 +243,14 @@ export default [
256243

257244
```
258245

259-
### Example (2.0.0 recommended)
246+
### Example (3.0.0 recommended)
260247

261248
Create the `mockProdServer.ts` file
262249

263250
```ts
264251
// mockProdServer.ts
265252

266-
import { createProdMockServer } from 'vite-plugin-mock/es/createProdMockServer'
253+
import { createProdMockServer } from 'vite-plugin-mock/client'
267254

268255
// Import your mock .ts files one by one
269256
// If you use vite.mock.config.ts, just import the file directly
@@ -283,18 +270,12 @@ import { viteMockServe } from 'vite-plugin-mock'
283270
import { UserConfigExport, ConfigEnv } from 'vite'
284271

285272
export default ({ command }: ConfigEnv): UserConfigExport => {
286-
// According to the project configuration. Can be configured in the .env file
287-
let prodMock = true
288273
return {
289274
plugins: [
290275
viteMockServe({
291276
mockPath: 'mock',
292-
localEnabled: command === 'serve',
293-
prodEnabled: command !== 'serve' && prodMock,
294-
injectCode: `
295-
import { setupProdMockServer } from './mockProdServer';
296-
setupProdMockServer();
297-
`,
277+
// According to the project configuration. Can be configured in the .env file
278+
enable: true,
298279
}),
299280
],
300281
}

0 commit comments

Comments
 (0)