simple-mock-webpack-plugin
Binds a local service to your webpack-dev-server
for mocking data.
Features
- Config of mocking supports hot reloading
- No more new listening port, no more CORS
- Mocks APIs in a succinct and simple way
- Stores data in the service(in memory), like a database
- Uses
mockjs
to support data mocking, learn more about nuysoft/Mock
P.S.: This repo is called
simple-mock-webpack-plugin
, but it isn't a stantardwebpack-plugin
because of its usage. 😂
Installation
Yarn
yarn add -D simple-mock-webpack-plugin
NPM
npm i -D simple-mock-webpack-plugin
Getting Started
1. Sets devServer.before
in webpack.config.js
const buildBefore = moduleexports = devServer: before:
2. Create a config file named mock.js
in the same directory
moduleexports = apis: url: '/test' template: code: '@integer(100,600)' msg: 'ok' url: '/test-promise' { return { } } url: '/count' { statecounter = statecounter || 0 + 1 return data: statecounter } url: '/get-count-number' { return count: statecounter || 0 }
3. Have a try
const _fetch = // {code: 264, msg: "ok"} // {data: 1} // {data: 2}
Options
Name | Type | Required | Default | Description |
---|---|---|---|---|
configPath | string |
No | ./mock.js |
The path of config file |
log | boolean |
No | true |
Whether prints logs |
before | Function |
No | null |
Customized before |
reloadDelay | number |
No | 300 |
Dealy of service's reloading |
Example
moduleexports = devServer: before:
Configuration of Mocking
Learn more about the configuration at examples.
MockConfig
Name | Type | Required | Default | Description |
---|---|---|---|---|
prefix | string |
No | / |
The prefix of all APIs |
delay | boolean |
No | 0 |
Dealy of the reponse for all APIs |
apis | MockAPI[] |
Yes | The configuration of APIs |
MockAPI
Name | Type | Required | Default | Description |
---|---|---|---|---|
url | string |
Yes | Path relative to the prefix |
|
method | string |
No | all |
The method of API |
template | object \| MockAPIHandler |
Yes | The tempalte of response. It will be regared as mockjs template if it is an object .(Learn more at Syntax). You can custmize reponse if you set it as a function. |
MockAPIHandler
object|Promise<object>
ExtraOptions
Name | Type | Default | Description |
---|---|---|---|
state | object |
{} |
Temporary states in the service. It will not be affected from config changing |
request | Express.Request |
Express Request | |
response | Express.Response |
Express Response | |
Mock | Mock.Mockjs |
Mock = require('mockjs') |