# webpack-mock-service-plugin

> mockjs适用于webpack的插件

Latest version **1.1.2** (published 2022-05-15) · ISC license · 0 weekly downloads

## Install

```sh
npm install webpack-mock-service-plugin
pnpm add webpack-mock-service-plugin
yarn add webpack-mock-service-plugin
bun add webpack-mock-service-plugin
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2022-05-15 |
| First published | 2021-04-27 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 147.6 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | springrain |

## Links

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

## Dependencies (2)

- [mockjs](https://npm.io/package/mockjs.md) ^1.1.0
- [express](https://npm.io/package/express.md) ^4.17.1

## Recent versions

- 1.1.2 (latest) — 2022-05-15
- 1.1.1 — 2022-05-15
- 1.1.0 — 2022-05-15
- 1.0.9 — 2022-05-14
- 1.0.8 — 2021-04-30
- 1.0.7 — 2021-04-30
- 1.0.6 — 2021-04-30
- 1.0.5 — 2021-04-29
- 1.0.4 — 2021-04-29
- 1.0.3 — 2021-04-28
- 1.0.2 — 2021-04-27
- 1.0.1 — 2021-04-27
- 1.0.0 — 2021-04-27

## README

# webpack-mock-service-plugin

mockjs适用于webpack的插件

## 简介

1、启动一个新的接口服务，真实的接口访问

2、根据目录自动生成接口

3、支持mockjs格式生成数据

4、支持get、post、delete、put方式请求

## 安装下载

- 下载地址：https://github.com/yinMrsir/webpack-mock-service-plugin/releases

```
npm install webpack-mock-service-plugin --save-dev
or
yarn add webpack-mock-service-plugin -D
```

## 快速使用

```
const WebpackMockServicePlugin = require('webpack-mock-service-plugin')

module.exports = {
    ...
    plugins: [
        new WebpackMockServicePlugin({
            port: 8000, // 不传默认3000
            mockDir: '' // 不传默认mock目录
        })
    ]
}
```

目录结构：

```
├─ mock
│  ├─ user
│  │  ├─ index.js                        // [get]     /user
│  │  ├─ del.delete.js                   // [delete]  /user/del
│  │  ├─ update.put.js                   // [put]     /user/update
│  │  └─ add.post.js                     // [post]    /user/add
│  ├─ news
│  │  ├─ hot
│  │  │  ├─ update.post.js               // [post]    /news/hot/update
│  │  │  └─ index.js                     // [get]     /news/hot
│  │  └─ index.js                        // [get]     /news
```

生成数据：

- js文件生成数据(推荐)

req：请求信息

```
module.exports = req => {
  const obj = {
    "goodsClass": "女装",
    "goodsId|+1": 1,
    "goodsName": "@ctitle(10)",
    "goodsAddress": "@county(true)",
    "goodsStar|1-5": "★",
    "goodsImg": "@Image('100x100','@color','小甜甜')",
    "goodsSale|30-500": 30
  }
  const list = []
  const pageSize = req.query.pageSize || 10
  for (let i = 0; i < pageSize; i++) {
    list.push(obj)
  }
  return {
    code: 200,
    list,
    page: req.query.page || 1,
    pageSize
  }
}
```

- json文件生成数据

```
{
    "code": 200,
    "list": {
        "goodsClass": "女装",
        "goodsId|+1": 1,
        "goodsName": "@ctitle(10)",
        "goodsAddress": "@county(true)",
        "goodsStar|1-5": "★",
        "goodsImg": "@Image('100x100','@color','小甜甜')",
        "goodsSale|30-500": 30
  }
}
```

## 注意事项

- webpack-mock-service-plugin主要是为了在开发过程中，后端还未提供接口，前端开发者可以通过模拟接口进行开发，所以主要用于开发环境。

- demo: https://github.com/yinMrsir/typescript-admin

## 交流 & 提问

提问：https://github.com/yinMrsir/webpack-mock-service-plugin/issues

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