"use strict";

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

function _react() {
  const data = _interopRequireDefault(require("react"));

  _react = function _react() {
    return data;
  };

  return data;
}

function _terserWebpackPlugin() {
  const data = _interopRequireDefault(require("terser-webpack-plugin"));

  _terserWebpackPlugin = function _terserWebpackPlugin() {
    return data;
  };

  return data;
}

function _uglifyjsWebpackPlugin() {
  const data = _interopRequireDefault(require("uglifyjs-webpack-plugin"));

  _uglifyjsWebpackPlugin = function _uglifyjsWebpackPlugin() {
    return data;
  };

  return data;
}

function _lodash() {
  const data = require("lodash");

  _lodash = function _lodash() {
    return data;
  };

  return data;
}

var _terserOptions = _interopRequireDefault(require("./terserOptions"));

var _uglifyOptions = _interopRequireDefault(require("./uglifyOptions"));

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

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(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function mergeConfig(config, userConfig) {
  if (typeof userConfig === 'function') {
    return userConfig(config);
  } else if ((0, _lodash().isPlainObject)(userConfig)) {
    return _objectSpread({}, config, {}, userConfig);
  } else {
    return config;
  }
}

function _default(webpackConfig, opts) {
  const disableCompress = process.env.COMPRESS === 'none';
  webpackConfig.mode('production').devtool(opts.devtool);

  if (disableCompress && !process.env.__FROM_UMI_TEST) {
    webpackConfig.output.pathinfo(true);
    webpackConfig.optimization.namedModules(true).namedChunks(true);
  }

  if (opts.hash) {
    webpackConfig.output.filename(`[name].[contenthash:8].js`).chunkFilename(`[name].[contenthash:8].async.js`);
  }

  webpackConfig.performance.hints(false); // server build not generate manifest
  // TODO: better manifest for ssr,
  // {
  //   "common": { "umi.js": "...", "umi.css": "..." }
  //   "pages": { "/about": { "about_index.js": "...", "about_index.css": "..." } }
  // }

  if (opts.manifest && !opts.ssr) {
    webpackConfig.plugin('manifest').use(require('webpack-manifest-plugin'), [_objectSpread({
      fileName: 'asset-manifest.json'
    }, opts.manifest)]);
  }

  webpackConfig.optimization // don't emit files if have error
  .noEmitOnErrors(true);

  if (disableCompress || process.env.__FROM_UMI_TEST) {
    webpackConfig.optimization.minimize(false);
  } else {
    webpackConfig.plugin('hash-module-ids').use(require('webpack/lib/HashedModuleIdsPlugin'));
    let minimizerName = 'uglifyjs';

    let minimizerPlugin = _uglifyjsWebpackPlugin().default;

    let minimizerOptions = [mergeConfig(_objectSpread({}, _uglifyOptions.default, {
      sourceMap: !!opts.devtool
    }), opts.uglifyJSOptions)];

    if (opts.minimizer === 'terserjs') {
      minimizerName = 'terserjs';
      minimizerPlugin = _terserWebpackPlugin().default;
      minimizerOptions = [mergeConfig(_objectSpread({}, _terserOptions.default, {
        sourceMap: !!opts.devtool
      }), opts.terserJSOptions)];
    }

    webpackConfig.optimization.minimizer(minimizerName).use(minimizerPlugin, minimizerOptions);
  }
}