1.0.0 • Published 4 years ago

webpack-webhooks-plugin v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

Webpack Webhooks Plugin

GitHub stars LICENSE npm downloads npm package size

forthebadge forthebadge forthebadge

Usage | Example | Releases | Buy Me a Coffee

Webpack Webhooks Plugin is a webpack plugin that notifies external services with HTTP POST requests when certain build events happen.

Installation

Use npm to install this package:

npm install --save-dev webpack-webhooks-plugin

Usage

// webpack.config.js

const WebpackWebhooksPlugin = require('webpack-webhooks-plugin');

module.exports = {
  // ...
  plugins: [
    new WebpackWebhooksPlugin(),
  ],
};

The WebpackWebhooksPlugin constructor accepts an optional object with two fields: options and events.

options

new WebpackWebhooksPlugin({
  options: {
    // `proxy` defines the hostname, port, and protocol of the proxy server.
    // default is `null` (disable proxy)
    proxy: {
      protocol: 'http',
      host: '127.0.0.1',
      port: 1080,
      auth: {
        username: 'admin',
        password: 'test@proxy'
      }
    },

    // `timeout` specifies the number of milliseconds before the request times out.
    // default is `0` (no timeout)
    timeout: 1000,
  },
});

events

We could create request objects to define the details of the HTTP POST request. The url parameter is required and the other properties are optional.

// Postman request object
const postman = {
  // The server URL that will be used for the request (required)
  url: 'https://postman-echo.com/post',

  // Custom headers to be sent (optional)
  headers: {
    'Content-Type': 'application/json',
  },

  // The URL parameters to be sent with the request (optional)
  params: {
    id: 114514
  },

  // The data to be sent as the request body (optional)
  data: {
    status: 'Hey, Postman!',
  },
};

The plugin supports sending requests when these events occurs:

  • beforeRun: Send requests before running the compiler.
  • watchRun: Send requests before running the compiler during watch mode.
  • success: Send requests when the compilation has completed successfully.
  • error: Send requests when the compilation fails.

Each event accepts a request object or an array of request objects. All request objects in the array will be called parallelly by Promise.allSettled.

new WebpackWebhooksPlugin({
  events: {
    beforeRun: postman,
    success: [postman, postman],
  },
});

Example

// webpack.config.js

const WebpackWebhooksPlugin = require('../src/index');

const postman = {
  url: 'https://postman-echo.com/post',
  headers: {
    'Content-Type': 'application/json',
  },
  data: {
    status: 'Hey, Postman!',
  },
};

const httpbin = {
  url: 'https://httpbin.org/post',
  headers: {
    'Content-Type': 'application/json',
  },
  data: {
    message: 'Hello, httpbin!',
  },
}

const config = {
  options: {
    proxy: {
      protocol: 'http',
      host: '127.0.0.1',
      port: 1080,
    },
    timeout: 1000,
  },

  events: {
    success: postman,
    fail: [postman, httpbin],
  },
};

module.exports = {
  // ...
  plugins: [
    new WebpackWebhooksPlugin(config),
  ],
};
1.0.0

4 years ago