"use strict";

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

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

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

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

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

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

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

var _chalk = _interopRequireDefault(require("chalk"));

var _makeWebpackConfig = _interopRequireDefault(require("./makeWebpackConfig"));

var _clientStats = require("../clientStats");

var _utils = require("../../utils");

/* eslint-disable import/no-dynamic-require, react/no-danger, import/no-mutable-exports */
//
function buildProductionBundles(_x) {
  return _buildProductionBundles.apply(this, arguments);
}

function _buildProductionBundles() {
  _buildProductionBundles = (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee3(state) {
    var allWebpackConfigs;
    return _regenerator.default.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            // Build static pages and JSON
            console.log('Bundling App...');
            (0, _utils.time)(_chalk.default.green("[\u2713] App Bundled"));
            _context3.next = 4;
            return (0, _makeWebpackConfig.default)(state);

          case 4:
            _context3.t0 = _context3.sent;
            _context3.next = 7;
            return (0, _makeWebpackConfig.default)((0, _objectSpread2.default)({}, state, {
              stage: 'node'
            }));

          case 7:
            _context3.t1 = _context3.sent;
            allWebpackConfigs = [_context3.t0, _context3.t1];
            _context3.next = 11;
            return new Promise(
            /*#__PURE__*/
            function () {
              var _ref = (0, _asyncToGenerator2.default)(
              /*#__PURE__*/
              _regenerator.default.mark(function _callee2(resolve, reject) {
                return _regenerator.default.wrap(function _callee2$(_context2) {
                  while (1) {
                    switch (_context2.prev = _context2.next) {
                      case 0:
                        (0, _webpack.default)(allWebpackConfigs).run(
                        /*#__PURE__*/
                        function () {
                          var _ref2 = (0, _asyncToGenerator2.default)(
                          /*#__PURE__*/
                          _regenerator.default.mark(function _callee(err, stats) {
                            var _stats$stats, prodStats, nodeStats, checkBuildStats;

                            return _regenerator.default.wrap(function _callee$(_context) {
                              while (1) {
                                switch (_context.prev = _context.next) {
                                  case 0:
                                    checkBuildStats = function _ref3(stage, stageStats) {
                                      var buildErrors = stageStats.hasErrors();
                                      var buildWarnings = stageStats.hasWarnings();

                                      if (buildErrors || buildWarnings) {
                                        console.log(stageStats.toString({
                                          context: state.config.context,
                                          performance: false,
                                          hash: false,
                                          timings: true,
                                          entrypoints: false,
                                          chunkOrigins: false,
                                          chunkModules: false,
                                          colors: true
                                        }));

                                        if (buildErrors) {
                                          console.log(_chalk.default.red.bold("\n                => There were ERRORS during the ".concat(stage, " build stage! :(\n                => Fix them and try again!\n              ")));
                                        } else if (buildWarnings) {
                                          console.log(_chalk.default.yellow("\n=> There were WARNINGS during the ".concat(stage, " build stage. Your site will still function, but you may achieve better performance by addressing the warnings above.\n")));
                                        }
                                      }
                                    };

                                    if (!err) {
                                      _context.next = 5;
                                      break;
                                    }

                                    console.log(_chalk.default.red(err.stack || err));

                                    if (err.details) {
                                      console.log(_chalk.default.red(err.details));
                                    }

                                    return _context.abrupt("return", reject(err));

                                  case 5:
                                    stats.toJson('verbose');
                                    _stats$stats = (0, _slicedToArray2.default)(stats.stats, 2), prodStats = _stats$stats[0], nodeStats = _stats$stats[1];
                                    checkBuildStats('prod', prodStats);
                                    checkBuildStats('node', nodeStats);
                                    _context.next = 11;
                                    return (0, _clientStats.outputClientStats)(state, prodStats.toJson());

                                  case 11:
                                    state = _context.sent;
                                    resolve(state);

                                  case 13:
                                  case "end":
                                    return _context.stop();
                                }
                              }
                            }, _callee);
                          }));

                          return function (_x4, _x5) {
                            return _ref2.apply(this, arguments);
                          };
                        }());

                      case 1:
                      case "end":
                        return _context2.stop();
                    }
                  }
                }, _callee2);
              }));

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

          case 11:
            state = _context3.sent;
            (0, _utils.timeEnd)(_chalk.default.green("[\u2713] App Bundled"));
            return _context3.abrupt("return", state);

          case 14:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _buildProductionBundles.apply(this, arguments);
}
//# sourceMappingURL=data:application/json;charset=utf-8;base64, 