mocker-webpack-plugin
馃枑Mock data easily with webpack
Last updated 10 months ago by zphhhhh .
MITRepositoryBugsOriginal npmTarballpackage.json
$ cnpm install mocker-webpack-plugin 
SYNC missed versions from official npm registry.

mocker-webpack-plugin

????Mock data easily with webpack

Install

npm i -D mocker-webpack-plugin

webpack >= 4.x is supported

Use

// 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)

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

Examples

json

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

js

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

js (with express)

// 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

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';
}

Current Tags

  • 1.3.1                                ...           latest (7 months ago)

5 Versions

  • 1.3.1                                ...           7 months ago
  • 1.3.0                                ...           7 months ago
  • 1.2.0                                ...           7 months ago
  • 1.1.0                                ...           7 months ago
  • 1.0.0                                ...           10 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (0)
None
Dependents (0)
None

Copyright 2014 - 2016 漏 taobao.org |