npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mockjs-webpack-plugin

v3.0.1

Published

A mockjs webpack plugin

Downloads

495

Readme

MockjsWebpackPlugin

中文readme

Quickly build mock service for your project as a webpack plugin based on mockjs

What is the problem this plugin solved

Through the way of webpack plugin, we can quickly build a mock service of project for parallel development in front-end and back-end separation mode.

Start up

Install

npm install --save-dev webpack mockjs-webpack-plugin

Config

Add a mock data storage folder in your project.

.
├── app         //project folder
    ├── dist
    ├── config
    ├── src
    ├── mock    //mock data folder
    ⎪   ├── data.js
    ⎪   ├── data.json
        ...

config proxy and mock-webpck-plugin in webpack.config.js

// import plugin
const MockjsWebpackPlugin = require("mockjs-webpack-plugin");

module.exports = {
  entry: "./index.js",
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "my-first-webpack.bundle.js"
  },
  // config plugin
  plugins: [
    new MockjsWebpackPlugin({
      // mock data folder path
      path: path.join(__dirname, "./mock"),
      // mock server port, avoid collision with application port
      port: 3000
    })
  ],
  // config proxy
  devServer: {
    // application port
    port: 5001,
    proxy: {
      // mock server url
      "/": "http://localhost:3000/"
    }
  }
};

if you want to set a special url for mock service, you can use webpack proxy like this:

...
module.exports = {
  ...
  // config proxy
  devServer: {
    // application port
    port: 5001,
    proxy: {
      '/api': {
        target: 'http://localhost:3000/',
        pathRewrite: {
          // set url rewrite, for example, 
          // http://localhost:5001/api/getData -> http://localhost:3000/getData
          '^/api': ''
        }
      }
    }
  }
};

When you add a mock data file, do not need to modify the webpack config file but restart the application

OPTIONS

new MockjsWebpackPlugin(options);
  • options.path mock data folder path
  • options.port the port of the mock server, default 3000

Mock Data

Mock Data here is not a real JSON file, and more like a JS file. When we just want to return data without any processing, a json mock file will be proper. So you want to use the following format.

/**
 * Json data file
 *
 * @url /json/data
 *
 * Here you can write a detailed description
 * of the parameters of the information.
 *
 * Parameter description and other instructions.
 * uid: user ID
 * name: username
 * email: the email
 * etc.
 */
{
  "code": 0,
  "result|5": [
    {
      "uid|+1": 1,
      "name": "@name",
      "email": "@email"
    }
  ]
}

You can read the file content like this

  • title: Json data file
  • url: /json/data
  • Description:
Here you can write a detailed description
of the parameters of the information.

Parameter description and other instructions.
 uid: user ID
 name: username
 email: the email
etc.
  • data content: the rest part

Then you can access the http://[localhost]:[3000]/json/data through the browser.

In addition, we can use the JS file directly, which is very useful when we need to check the input parameters.

/**
 * JS data file
 *
 * @url /js/js-data-file
 *
 * Export data by using the JS file directly.
 */

module.exports = {
    "code": function () { // simulation error code, 1/10 probability of error code 1.
        return Math.random() < 0.1 ? 1 : 0;
    },
    "list|5-10": [
        {"title": "@title", "link": "@url"}
    ]
};

Or export function.

/**
 * JS function file
 *
 * @url /js/js-func-file/user?uid=233
 *
 * GET: Request method and parameter
 *   uid This is the requested userID
 *
 * Here you can write a detailed description
 * of the parameters of the information.
 */
module.exports = function(req) {
  var uid = req.query.uid;

  if (!uid) {
    return {
      code: -1,
      msg: "no uid"
    };
  }

  return {
    code: 0,
    data: {
      uid: +uid,
      name: "@name",
      "age|20-30": 1,
      email: "@email",
      date: "@date"
    }
  };
};

All above data comes from mockjs syntax,you can read docs and samples from website of mockjs to get more

mock data desciption comes from 52cik/express-mockjs

Mock JSON

#ChangeLog version 3.0.0 -- 2019.04.07

  1. nothing updated! Just use npm version <update_type> and then i find my package.json has been updated to 3.0.0 version 2.0.0 -- 2019.04.06
  2. support hot reload mock files changes, like add new file or update content.

Support

This repo is coming from MarxJiao/mock-webpack-plugin and 52cik/express-mockjs.

Thanks this two author, Marx(MarxJiao) and 楼教主(52cik)