"use strict";

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

var _circularJson = _interopRequireDefault(require("circular-json"));

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

var _lodash = _interopRequireDefault(require("lodash"));

var _fsExtra = _interopRequireDefault(require("fs-extra"));

var _path = _interopRequireDefault(require("path"));

var _webpack = _interopRequireDefault(require("webpack"));

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

var _webpack2 = require("../webpack");

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

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

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

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

function _build() {
  _build = _asyncToGenerator(function* (config, {
    spinner,
    webpackConfig,
    platform,
    socket
  }) {
    const paths = config.paths,
          options = config.options;
    const webpackClientConfig = (0, _webpack2.createWebpackConfig)(config, {
      platform,
      target: 'client',
      webpackConfig
    });

    if (options.debug) {
      _fsExtra.default.mkdirsSync(paths.debug);

      _fsExtra.default.writeFileSync(_path.default.resolve(paths.debug, 'webpack.client.json'), _circularJson.default.stringify(webpackConfig, null, 2));
    }

    _core.log.debug('webpackClientConfig', webpackClientConfig);

    const webpackServerConfig = (0, _webpack2.createWebpackConfig)(config, {
      platform,
      target: 'server',
      webpackConfig
    });

    if (options.debug) {
      _fsExtra.default.mkdirsSync(paths.debug);

      _fsExtra.default.writeFileSync(_path.default.resolve(paths.debug, 'webpack.server.json'), _circularJson.default.stringify(webpackConfig, null, 2));
    }

    _core.log.debug('webpackServerConfig', webpackServerConfig);

    if (_fsExtra.default.existsSync(_path.default.resolve(paths.src, 'public'))) {
      _fsExtra.default.copySync(_path.default.resolve(paths.src, 'public'), _path.default.resolve(paths.dist, 'public'), {
        dereference: true
      });
    } else {
      _fsExtra.default.mkdirsSync(_path.default.resolve(paths.dist, 'public'));
    }

    const _ref = yield runBuild(config, webpackServerConfig, webpackClientConfig, (yield measureFileSizesBeforeBuild(_path.default.resolve(paths.dist, 'public')))),
          stats = _ref.stats,
          previousFileSizes = _ref.previousFileSizes,
          warnings = _ref.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('');

    socket.stop();
    return spinner.succeed();
  });
  return _build.apply(this, arguments);
}

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

function _runBuild() {
  _runBuild = _asyncToGenerator(function* (config, webpackServerConfig, webpackClientConfig, previousFileSizes) {
    process.noDeprecation = true;
    const webStats = yield compile(webpackClientConfig);
    const webMessages = (0, _webpack2.handleStats)(webStats, config);
    const nodeStats = yield compile(webpackServerConfig);
    const nodeMessages = (0, _webpack2.handleStats)(nodeStats, config);
    return {
      errors: _lodash.default.assign({}, webMessages.errors, nodeMessages.errors),
      previousFileSizes,
      stats: webStats,
      warnings: _lodash.default.assign({}, webMessages.warnings, nodeMessages.warnings)
    };
  });
  return _runBuild.apply(this, arguments);
}

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

function _compile() {
  _compile = _asyncToGenerator(function* (config) {
    return new Promise((resolve, reject) => {
      try {
        return (0, _webpack.default)(config).run((err, stats) => {
          if (err) return reject(err);
          return resolve(stats);
        });
      } catch (err) {
        return reject(err);
      }
    });
  });
  return _compile.apply(this, arguments);
}