- 🔧 同时支持
webpack 4
与webpack 5
- 🔑 同时支持
ESM
规范与CommonJS
规范 - ✒️ 内置mockjs, 多种数据结构随意组合
- 🔥 支持热更新 ,修改 Mock 数据无需重新启动服务
- 💡
Typescript
编写,更好的类型提示 - 🐣 开发友好,无跨域问题,无需依赖其他服务
- 🔨 (实验性功能)解析 OpenApi 3 规范数据文件,快速生成可用 Mock 服务
Warning
由于 Mock 与实际后端模拟数据存在较大差异, 原因在于逻辑的复杂度不同, 以及使用目的不同, 因此建议您在使用 openApi 3 解析产生的 mock 服务时, 应更加注重逻辑依赖较弱且数据量较多的接口, 如果必须, 请使用 mockPath 模式进行自定义逻辑与数据
npm install mock-server-webpack-plugin -D
or
pnpm add mock-server-webpack-plugin -D
ESM
import path from 'path'
import { Configuration } from 'webpack'
import MockServerWebpackPlugin from 'mock-server-webpack-plugin'
const config: Configuration = {
mode: 'development',
// ...
plugins: [
new MockServerWebpackPlugin({
port: 3636,
mockPath: path.resolve('./mock.js'),
// or
// openApi: 'http://127.0.0.1:4000/file/default_OpenAPI.json',
}),
],
}
export default config
CommonJS
const path = require('path')
const { Configuration } = require('webpack')
const MockServerWebpackPlugin = require('mock-server-webpack-plugin')
/**
* @type {Configuration}
*/
const config = {
mode: 'development',
// ...
plugins: [
new MockServerWebpackPlugin({
port: 3000,
mockPath: path.resolve('./mock.js'),
// or
// openApi: 'http://127.0.0.1:4000/file/default_OpenAPI.json',
}),
],
}
module.exports = config
const Mockjs = require('mockjs')
const mockUserList = Mockjs.mock({
'data|50': [
{
'id|+1': 1,
name: '@cname',
age: '@integer(10, 60)',
},
],
})
module.exports = [
{
url: '/user-list',
method: 'get',
response: () => {
return {
code: 200,
data: mockUserList.data,
}
},
},
{
url: '/add-user',
method: 'post',
response: (req) => {
const { name, age } = req.body
mockUserList.data.push({
id: mockUserList.data.length + 1,
name,
age,
})
return {
code: 200,
message: '添加成功',
}
},
},
{
url: '/delete-user',
method: 'delete',
response: (req) => {
const { id } = req.query
const index = mockUserList.data.findIndex((item) => item.id === Number(id))
mockUserList.data.splice(index, 1)
return {
code: 200,
message: '删除成功',
}
},
},
]
module.exports = {
// ...
plugins: [
new MockServerWebpackPlugin({
openApi: 'http://127.0.0.1:4000/file/default_OpenAPI.json', // openApi 模式(本地/在线文件)
}),
// ...
],
}
module.exports = {
// ...
plugins: [
new MockServerWebpackPlugin({
port: 3636, // 端口号
host: '127.0.0.1', // 主机
prefix: '/dev-api', // api 前缀
mockPath: path.resolve('./mock.js'), // mockPath 模式
openApi: 'http://127.0.0.1:4000/file/default_OpenAPI.json',, // openApi 模式(本地/在线文件)
}),
// ...
]
}