'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

let runBuild = (() => {
  var _ref3 = _asyncToGenerator(function* (config, webpackServerConfig, webpackClientConfig, previousFileSizes) {
    process.noDeprecation = true;
    const webStats = yield compile(webpackClientConfig);
    const webMessages = (0, _webpack3.handleStats)(webStats);
    const nodeStats = yield compile(webpackServerConfig);
    const nodeMessages = (0, _webpack3.handleStats)(nodeStats);
    return {
      errors: _lodash2.default.assign({}, webMessages.errors, nodeMessages.errors),
      previousFileSizes,
      stats: webStats,
      warnings: _lodash2.default.assign({}, webMessages.warnings, nodeMessages.warnings)
    };
  });

  return function runBuild(_x3, _x4, _x5, _x6) {
    return _ref3.apply(this, arguments);
  };
})();

let compile = (() => {
  var _ref4 = _asyncToGenerator(function* (config) {
    return new Promise(function (resolve, reject) {
      try {
        return (0, _webpack2.default)(config).run(function (err, stats) {
          if (err) return reject(err);
          return resolve(stats);
        });
      } catch (err) {
        return reject(err);
      }
    });
  });

  return function compile(_x7) {
    return _ref4.apply(this, arguments);
  };
})();

var _circularJson = require('circular-json');

var _circularJson2 = _interopRequireDefault(_circularJson);

var _FileSizeReporter = require('react-dev-utils/FileSizeReporter');

var _FileSizeReporter2 = _interopRequireDefault(_FileSizeReporter);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _fsExtra = require('fs-extra');

var _fsExtra2 = _interopRequireDefault(_fsExtra);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _webpack = require('webpack');

var _webpack2 = _interopRequireDefault(_webpack);

var _core = require('@reactant/core');

var _webpack3 = require('../webpack');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

const measureFileSizesBeforeBuild = _FileSizeReporter2.default.measureFileSizesBeforeBuild,
      printFileSizesAfterBuild = _FileSizeReporter2.default.printFileSizesAfterBuild;

exports.default = (() => {
  var _ref = _asyncToGenerator(function* (config, { spinner, webpackConfig }) {
    const paths = config.paths,
          options = config.options;

    const webpackClientConfig = (0, _webpack3.createWebpackConfig)(config, webpackConfig, 'client');
    if (options.debug) {
      _fsExtra2.default.mkdirsSync(paths.debug);
      _fsExtra2.default.writeFileSync(_path2.default.resolve(paths.debug, 'webpack.client.json'), _circularJson2.default.stringify(webpackConfig, null, 2));
    }
    _core.log.debug('webpackClientConfig', webpackClientConfig);
    const webpackServerConfig = (0, _webpack3.createWebpackConfig)(config, webpackConfig, 'server');
    if (options.debug) {
      _fsExtra2.default.mkdirsSync(paths.debug);
      _fsExtra2.default.writeFileSync(_path2.default.resolve(paths.debug, 'webpack.server.json'), _circularJson2.default.stringify(webpackConfig, null, 2));
    }
    _core.log.debug('webpackServerConfig', webpackServerConfig);
    if (_fsExtra2.default.existsSync(_path2.default.resolve(paths.src, 'public'))) {
      _fsExtra2.default.copySync(_path2.default.resolve(paths.src, 'public'), _path2.default.resolve(paths.dist, 'public'), {
        dereference: true
      });
    } else {
      _fsExtra2.default.mkdirsSync(_path2.default.resolve(paths.dist, 'public'));
    }

    var _ref2 = yield runBuild(config, webpackServerConfig, webpackClientConfig, (yield measureFileSizesBeforeBuild(_path2.default.resolve(paths.dist, 'public'))));

    const stats = _ref2.stats,
          previousFileSizes = _ref2.previousFileSizes,
          warnings = _ref2.warnings;

    if (warnings.length) {
      spinner.warn('built web');
      _core.log.info(warnings.join('\n\n'));
    } else {
      spinner.succeed('built web');
    }
    _core.log.info('file sizes after gzip:\n');
    printFileSizesAfterBuild(stats, previousFileSizes, paths.dist);
    _core.log.info('');
    return spinner.succeed();
  });

  function build(_x, _x2) {
    return _ref.apply(this, arguments);
  }

  return build;
})();