"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.webpackCompileDev = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _cliCore = require("@bluebase/cli-core");

var _deepmerge = _interopRequireDefault(require("deepmerge"));

var _webpackServe = _interopRequireDefault(require("webpack-serve"));

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

// tslint:disable-next-line:no-var-requires
const history = require('connect-history-api-fallback'); // tslint:disable-next-line:no-var-requires


const convert = require('koa-connect');
/**
 * compiles a webpack config.
 * @param configs
 */


const webpackCompileDev = async (configs, label) => {
  const defaultConfigs = {
    // open: true,
    devMiddleware: {
      publicPath: '/',
      writeToDisk: true
    },
    add: app => {
      const historyOptions = {// ... see: https://github.com/bripkens/connect-history-api-fallback#options
      };
      app.use(convert(history(historyOptions)));
    },
    on: {
      'build-finished': () => {
        _cliCore.Utils.logger.log({
          label,
          level: 'info',
          message: 'Running with latest changes.',
          notify: true
        });
      },
      'compiler-error': () => {
        _cliCore.Utils.logger.log({
          label,
          level: 'error',
          message: 'Build failed, please check the console for more information.',
          notify: true
        });
      }
    }
  };

  const mergedConfigs = _objectSpread({}, (0, _deepmerge.default)(defaultConfigs, configs), {
    // If we rely on deepmerge, webpack messes up
    config: configs.config
  });

  return (0, _webpackServe.default)({}, mergedConfigs);
};

exports.webpackCompileDev = webpackCompileDev;