aboutsummaryrefslogtreecommitdiff
path: root/node_modules/webpack-serve/lib/bus.js
blob: 57c834bc32ee7f482bb5a75ef0774396c8949eb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const isPlainObject = require('lodash/isPlainObject');
const nanobus = require('nanobus');
const weblog = require('webpack-log');

const WebpackServeError = require('./WebpackServeError');

module.exports = (options) => {
  const log = weblog({ name: 'serve', id: 'webpack-serve' });
  const bus = nanobus();

  if (isPlainObject(options.on)) {
    for (const event of Object.keys(options.on)) {
      const fn = options.on[event];

      if (typeof fn === 'function') {
        log.info(`Subscribed to '${event}' event`);
        bus.on(event, fn);
      } else {
        throw new WebpackServeError(
          `The value for an \`on\` event handler must be a Function. event: ${event}`
        );
      }
    }
  } else if (options.on) {
    throw new WebpackServeError(
      'The value for the `on` option must be an Object. Please see the README.'
    );
  }

  return bus;
};