mock plugin for vite
npm i vite-plugin-reload --save-dev
import viteMockerPlugin from 'unplugin-mock/vite'
export default defineConfig({
plugins: [
// ... etc
viteMockerPlugin({
mockPath: './mock',
}),
],
// ...etc
})
- Enable mock plugin
- Type:
boolean
- Default:
true
- Path to mock directory
- Type:
string
- Default:
''
- Example:
'./mock'
- File extension of mock files
- Type:
string[]
- Defualt:
['.js', '.ts']
- Ignore files
- Type:
string
|RegExp
|(RegExp | string)[]
- default:
[]
- Refresh page when mock file changed
- Type:
boolean
- default:
false
Mock Service in webpack is not suitable for plugin implementation, because it is hard to affect webpack-dev-server
config in webpack-plugin.
You can build a local mocker by DevServer hook or pick some readily available libs for devServer, suck as mocker-api
vite.d.ts
and types.d.ts
will be removed in the future, please use typescript >= 4.7.2
instead.
- the implementation first for vite
- logger for hmr
- ignore files option
- restful url support
- Keep url unique for module handler
- cache handler
- enhance response for function mockHandler
- cache source code
- analyze source code for avoiding duplicate mock import