|
| 1 | +# vite-plugin-mock |
| 2 | + |
| 3 | +Provide local and prod mocks for vite. |
| 4 | + |
| 5 | +A mock plugin for vite, developed based on mockjs. And support the local environment and production environment at the same time. Koa service middleware is used locally, mockjs is used online |
| 6 | + |
| 7 | +## Getting Started |
| 8 | + |
| 9 | +### Install (yarn or npm) |
| 10 | + |
| 11 | +**node version:** >=12.0.0 |
| 12 | + |
| 13 | +`yarn add mockjs` or `yarn add mockjs -S` |
| 14 | + |
| 15 | +and |
| 16 | + |
| 17 | +`yarn add vite-plugin-mock -D` or `npm i vite-plugin-mock -D` |
| 18 | + |
| 19 | +**Run Example** |
| 20 | + |
| 21 | +```bash |
| 22 | + |
| 23 | +# ts example |
| 24 | +cd ./examples/ts-examples |
| 25 | + |
| 26 | +yarn install |
| 27 | + |
| 28 | +yarn serve |
| 29 | + |
| 30 | +# js example |
| 31 | + |
| 32 | +cd ./examples/js-examples |
| 33 | + |
| 34 | +yarn install |
| 35 | + |
| 36 | +yarn serve |
| 37 | +``` |
| 38 | + |
| 39 | +## Usage |
| 40 | + |
| 41 | +**Development environment** |
| 42 | + |
| 43 | +The development environment is implemented using koa middleware。 |
| 44 | + |
| 45 | +Different from the production environment, you can view the network request record in the Google Chrome console |
| 46 | + |
| 47 | +- Config plugin in vite.config.ts |
| 48 | + |
| 49 | +```ts |
| 50 | +import { createMockServer } from 'vite-plugin-mock'; |
| 51 | + |
| 52 | +export default { |
| 53 | + plugins: [ |
| 54 | + createMockServer({ |
| 55 | + ignore: /^\_/, |
| 56 | + mockPath: 'mock', |
| 57 | + watchFiles: true, |
| 58 | + localEnabled: process.env.NODE_ENV === 'development', |
| 59 | + }), |
| 60 | + ], |
| 61 | +}; |
| 62 | +``` |
| 63 | + |
| 64 | +- createMockServer Options |
| 65 | + |
| 66 | +```ts |
| 67 | +{ |
| 68 | + mockPath?: string; |
| 69 | + supportTs?: boolean; |
| 70 | + ignore?: RegExp | ((fileName: string) => boolean); |
| 71 | + watchFiles?: boolean; |
| 72 | + localEnabled?: boolean; |
| 73 | + ignoreFiles?: string[]; |
| 74 | + configPath?: string; |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +## Option description |
| 79 | + |
| 80 | +### mockPath |
| 81 | + |
| 82 | +**default:** true |
| 83 | + |
| 84 | +Set the folder where the mock .ts file is stored |
| 85 | + |
| 86 | +If `watchFiles:true`, the file changes in the folder will be monitored. And synchronize to the request result in real time |
| 87 | + |
| 88 | +If configPath has a value, it is invalid |
| 89 | + |
| 90 | +### supportTs |
| 91 | + |
| 92 | +**default:** true |
| 93 | + |
| 94 | +After opening, the ts file module can be read. Note that you will not be able to monitor .js files after opening. |
| 95 | + |
| 96 | +### ignore |
| 97 | + |
| 98 | +**default:** undefined |
| 99 | + |
| 100 | +Ignore files in the specified format when automatically reading mock .ts files |
| 101 | + |
| 102 | +### watchFiles |
| 103 | + |
| 104 | +**default:** true |
| 105 | + |
| 106 | +Set whether to monitor changes in mock .ts files |
| 107 | + |
| 108 | +### localEnabled |
| 109 | + |
| 110 | +**default:** process.env.NODE_ENV==='developments' |
| 111 | + |
| 112 | +Set whether to enable the local mock .ts file, do not open it in the production environment |
| 113 | + |
| 114 | +### configPath |
| 115 | + |
| 116 | +**default:** vite.mock.config.ts |
| 117 | + |
| 118 | +Set the data entry that the mock reads. When the file exists and is located in the project root directory, the file will be read and used first. The configuration file returns an array |
| 119 | + |
| 120 | +### ignoreFiles |
| 121 | + |
| 122 | +**default:** [] |
| 123 | + |
| 124 | +The project uses glob to read the folder set by mockPath. This parameter is used as the parameter of the glob module |
| 125 | + |
| 126 | +## Mock file example |
| 127 | + |
| 128 | +`/path/mock` |
| 129 | + |
| 130 | +```ts |
| 131 | +// test.ts |
| 132 | + |
| 133 | +import { MockMethod } from 'vite-plugin-mock'; |
| 134 | +export default [ |
| 135 | + { |
| 136 | + url: '/api/get', |
| 137 | + method: 'get', |
| 138 | + response: ({ query }) => { |
| 139 | + return { |
| 140 | + code: 0, |
| 141 | + data: { |
| 142 | + name: 'vben' |
| 143 | + } |
| 144 | + }; |
| 145 | + }, |
| 146 | + }, |
| 147 | + { |
| 148 | + url: '/api/post', |
| 149 | + method: 'post', |
| 150 | + timeout:2000, |
| 151 | + response: { |
| 152 | + code: 0, |
| 153 | + data: { |
| 154 | + name: 'vben' |
| 155 | + } |
| 156 | + }; |
| 157 | + }, |
| 158 | +] as MockMethod[]; |
| 159 | + |
| 160 | + |
| 161 | +``` |
| 162 | + |
| 163 | +### MockMethod |
| 164 | + |
| 165 | +```ts |
| 166 | +{ |
| 167 | + // request url |
| 168 | + url: string; |
| 169 | + // request method |
| 170 | + method?: MethodType; |
| 171 | + // Request time in milliseconds |
| 172 | + timeout?: number; |
| 173 | + // response data |
| 174 | + response: ((opt: { [key: string]: string; body: any; query: any }) => any) | any; |
| 175 | +} |
| 176 | + |
| 177 | +``` |
| 178 | + |
| 179 | +## Usage in Production environment |
| 180 | + |
| 181 | +### Example |
| 182 | + |
| 183 | +Create a new mockProdServer.ts file |
| 184 | + |
| 185 | +```ts |
| 186 | +// mockProdServer.ts |
| 187 | + |
| 188 | +import { createProdMockServer } from 'vite-plugin-mock/es/createProdMockServer'; |
| 189 | + |
| 190 | +// Import your mock .ts files one by one |
| 191 | +// If you use vite.mock.config.ts, just import the file directly |
| 192 | +import testModule from '../mock/test'; |
| 193 | + |
| 194 | +export function setupProdMockServer() { |
| 195 | + createProdMockServer([...testModule]); |
| 196 | +} |
| 197 | +``` |
| 198 | + |
| 199 | +In mian.ts |
| 200 | + |
| 201 | +```ts |
| 202 | +// src/main.ts |
| 203 | + |
| 204 | +import { setupProdMockServer } from './mockProdServer'; |
| 205 | + |
| 206 | +if (process.env.NODE_ENV === 'production') { |
| 207 | + setupProdMockServer(); |
| 208 | +} |
| 209 | +``` |
| 210 | + |
| 211 | +## Note |
| 212 | + |
| 213 | +The node module cannot be used in the mock .ts file, otherwise the production environment will fail |
| 214 | + |
| 215 | +## License |
| 216 | + |
| 217 | +MIT |
0 commit comments