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

webpack-mock-dev-server

v2.0.1

Published

Mocks webpack dev server request in an easy configurable way

Downloads

14

Readme

Table of Contents

  1. Install
  2. Introduction
  3. Get started
  4. Configuration

Install with npm:

npm install --save-dev webpack-mock-dev-server

Install with yarn:

yarn add webpack-mock-dev-server --dev

webpack-mock-dev-server is an easy to configure webpack helper which allows to mock request passing through the webpack-dev-server. In the configuration file you can specify which requests should be mocked and what response you would like to return. It also allows to set specific headers on the response.

TL;DR

  • Mock wepack-dev-server responses
  • Easy configuration
  • Allows to specify headers on the response

Open the webpack configuration file in which you have specified the dev-server configuration. Modify the configuration like the example (1) below. Most of the time it would be needed to just add the 'before' configuration option as shown in example 1. If you already have specified a function on the before configuration option, call the MockDevServer function al shown in example 2. Example 1:

const { MockDevServer } = require('webpack-mock-dev-server');

module.exports = {
    ...,

    devServer: {
        ...,
        before: MockDevServer(path.join(__dirname, 'mock-dev-server.config')),
    }
};

Example 2:

const { MockDevServer } = require('webpack-mock-dev-server');

module.exports = {
    ...,

    devServer: {
        ...,
        before: function before(app, server) {
            MockDevServer(path.join(__dirname, 'mock-dev-server.config'))(app);
        },
    }
};

The only argument needed is the path to the configuration file. If not specified it will look for the file in the same directory as the webpack config file. By default this will mock all response and throw warnings on all requests that do not have a rule specified in the config file. See Configuration to specify responses for specific requests.

Create a file (mock-dev-server.config.js by default) in which you would like to specify the mocking configurations. See the example below.

module.exports = {
    src: path.join(__dirname, 'fixtures'),
    entry: '/api/*',
    rules: [
        {
            method: 'GET',
            test: '**/news',
            filename: 'news.json',
            headers: {
                'Content-Type': 'application/json'
            }
        },
    ],
}

Property list:

  1. src: the base path to use when requesting mock files (fixtures). So the filename specified in the rules is relative to this path.
  2. entry: the base path to use when targeting requests in the dev-server. So each test in the rules is relative to this path.
  3. rules: The set of rules to check when a requests occurs.
  4. method (optional, default: GET): the method of the request
  5. test: the glob expression to match against
  6. filename: the name of the file to return it's content from when the test succeeds.
  7. headers (optional): any headers that need to be set them mocking the response.