# mocker-webpack-plugin

> 🖐Mock data easily with webpack

Latest version **1.3.1** (published 2019-12-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install mocker-webpack-plugin
pnpm add mocker-webpack-plugin
yarn add mocker-webpack-plugin
bun add mocker-webpack-plugin
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.1 |
| Published | 2019-12-07 |
| First published | 2019-09-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | zphhhhh |
| Maintainers | zphhhhh |
| Keywords | mock, mocker, webpack, plugin |

## Links

- npm: https://www.npmjs.com/package/mocker-webpack-plugin
- Repository: https://github.com/zphhhhh/mocker-webpack-plugin
- Homepage: https://github.com/zphhhhh/mocker-webpack-plugin#readme
- Issues: https://github.com/zphhhhh/mocker-webpack-plugin/issues
- npm.io page: https://npm.io/package/mocker-webpack-plugin

## Dependencies (2)

- [micromatch](https://npm.io/package/micromatch.md) ^4.0.2
- [body-parser](https://npm.io/package/body-parser.md) ^1.19.0

## Alternatives

- [pagerjs](https://npm.io/package/pagerjs.md) — 60 weekly downloads
- [whistle.savefor-mock](https://npm.io/package/whistle.savefor-mock.md) — 4 weekly downloads
- [react-component-mocker](https://npm.io/package/react-component-mocker.md) — 0 weekly downloads
- [pbmockx](https://npm.io/package/pbmockx.md) — 0 weekly downloads
- [chapterize-js](https://npm.io/package/chapterize-js.md) — 0 weekly downloads

## Recent versions

- 1.3.1 (latest) — 2019-12-07
- 1.3.0 — 2019-12-06
- 1.2.0 — 2019-12-06
- 1.1.0 — 2019-12-04
- 1.0.0 — 2019-09-28

## README

# mocker-webpack-plugin

🖐Mock data easily with webpack

### Install

``` shell
npm i -D mocker-webpack-plugin
```

> webpack >= 4.x is supported

### Use

``` javascript
// import
const MockerWebpackPlugin = require("mocker-webpack-plugin")

// Webpack plugin config
new MockerWebpackPlugin()
```

### How to mock?

If you want to request `/api/user`, consider your project path is:

```
Project
├── build
│   └── webpack.conf.js
├── mocks
│   └── api
│       └── user.js     <--- It's here
├── node_modules
└── src
    ├── components
    ├── configs
    ├── pages
    ├── public
    └── routers
```

And if you want to request `/multi/level/url/like/this`, just create multi-level directory.

This plugin supports files like:

- `user.js`
- `user.json`
- `user/index.js`
- `user/index.json`
- `user`

If using `.js` file:

- support returning pure object (JSON)
- support accessing `request` and `response` object (ref: [express api - req](http://expressjs.com/en/4x/api.html#req))

To specify your mock catalogue, pass `path` option like `new MockerWebpackPlugin({path})`, default is `${WebpackConfigContext}/mocks`.

### Examples

json

```json
{
    "status": 0,
    "msg": "",
    "data": {}
}
```

js

```js
modules.exports = {
    status: 0,
    msg: "",
    data: {
        // ...
    }
}
```

js (with express)

```js
// GET /search?user=zphhhhh
module.exports = (req, res) => {
    if (req.query.user) {
        return {
            status: 0,
            msg: "",
            data: {
                greet: `hello, ${req.query.user}!`
            }
        }
    } else {
        return {
            status: 1,
            msg: "please login",
            data: null
        }
    }
}
```

### Options

``` typescript
interface MockerOptions {
    /** set mock path (absolute path!), default to `${WebpackConfigContext}/mocks` */
    path: String;
    /** set webpack-dev-server mode, default to `before`, ref `WebpackConfig.devServer.before` */
    mode: 'before' | 'after' | 'setup';
}
```

---
_Source: https://npm.io/package/mocker-webpack-plugin · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
