{"size": 1842, "ext": "js", "max_stars_repo_path": "packages/beidou-webpack/agent.js", "max_stars_repo_name": "chenmingjia/beidou", "max_stars_repo_stars_event_min_datetime": "2018-12-24T05:31:54.000Z", "max_stars_repo_stars_event_max_datetime": "2018-12-24T05:31:54.000Z", "max_issues_repo_path": "packages/beidou-webpack/agent.js", "max_issues_repo_name": "chenmingjia/beidou", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "packages/beidou-webpack/agent.js", "max_forks_repo_name": "chenmingjia/beidou", "max_forks_repo_forks_event_min_datetime": "2018-08-22T08:56:44.000Z", "max_forks_repo_forks_event_max_datetime": "2018-08-22T08:56:44.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 28.78125, "max_line_length": 77, "alphanum_fraction": 0.6237785016}
'use strict';

const chokidar = require('chokidar');
const equal = require('deep-equal');
const debug = require('debug')('beidou:webpack');
const helper = require('./lib/utils');
const entryLoader = require('./lib/loader/entry-loader');

module.exports = (agent) => {
  helper.injectPlugin(agent);

  // start webpack server util agent ready
  agent.ready(() => {
    const { logger } = agent;
    const config = agent.config.webpack;

    debug('create webpack server with config: %O', config);
    const webpackConfig = helper.getWebpackConfig(agent, config);

    debug('Webpack config: %O', webpackConfig);

    // const webpackServer = http.createServer(agent.callback());

    const port = webpackConfig.devServer.port || 0;
    helper.startServer(webpackConfig, port, logger, agent);

    function watcher() {
      const updatedEntry = entryLoader(agent, webpackConfig.devServer, true);
      if (!equal(updatedEntry, webpackConfig.entry)) {
        webpackConfig.entry = updatedEntry;
        helper.restartServer(webpackConfig, port, logger, agent);
        logger.info('[webpack:watcher] entry updated');
      }
    }

    chokidar
      .watch(agent.config.client, {
        ignored: /(^|[/\\])\../,
        persistent: true,
        ignoreInitial: true,
      })
      .on('add', watcher)
      .on('addDir', watcher)
      .on('unlinkDir', watcher)
      .on('unlink', watcher);

    process.stdin.resume();
    process.stdin.setEncoding('utf8');
    process.stdin.on('data', (data) => {
      data = `${data}`.trim().toLowerCase();

      // if the keys entered match the restartable value, then restart!
      if (data === 'rs') {
        helper.restartServer(webpackConfig, port, logger, agent);
      }
    });

    // close server when exit
    process.on('exit', () => {
      helper.closeServer(agent);
    });
  });
};
