"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 _getConfig() {
  const data = _interopRequireDefault(require("af-webpack/getConfig"));

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

  return data;
}

function _assert() {
  const data = _interopRequireDefault(require("assert"));

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

  return data;
}

function _chalk() {
  const data = _interopRequireDefault(require("chalk"));

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

  return data;
}

function _webpackNodeExternals() {
  const data = _interopRequireDefault(require("webpack-node-externals"));

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

  return data;
}

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; }

const debug = require('debug')('umi-build-dev:getWebpackConfig');

function _default(service, opts = {}) {
  const ssr = opts.ssr,
        watch = opts.watch;
  const config = service.config;
  const afWebpackOpts = service.applyPlugins('modifyAFWebpackOpts', {
    initialValue: {
      cwd: service.cwd
    },
    args: {
      ssr
    }
  });
  (0, _assert().default)(!('chainConfig' in afWebpackOpts), `chainConfig should not supplied in modifyAFWebpackOpts`);

  afWebpackOpts.chainConfig = webpackConfig => {
    service.applyPlugins('chainWebpackConfig', {
      args: webpackConfig
    });

    if (config.chainWebpack) {
      config.chainWebpack(webpackConfig, {
        webpack: require('af-webpack/webpack')
      });
    }
  };

  const webpackConfig = service.applyPlugins('modifyWebpackConfig', {
    initialValue: (0, _getConfig().default)(_objectSpread({}, afWebpackOpts, {
      ssr
    }))
  });

  if (ssr) {
    // ssr in beta hint
    console.warn(_chalk().default.keyword('orange')(`WARNING: UmiJS SSR is still in beta, you can open issues or PRs in https://github.com/umijs/umi`));

    const nodeExternalsOpts = _objectSpread({
      whitelist: [/\.(css|less|sass|scss|styl(us)?)$/, /^umi(\/.*)?$/, 'umi-plugin-locale', ...(typeof ssr === 'object' && ssr.externalWhitelist ? ssr.externalWhitelist : [])]
    }, typeof ssr === 'object' && ssr.nodeExternalsOpts ? ssr.nodeExternalsOpts : {});

    webpackConfig.target = 'node';
    debug(`nodeExternalOpts:`, nodeExternalsOpts);
    const defaultExternals = typeof ssr === 'object' && ssr.disableExternalWhiteList || webpackConfig.externals || [];
    webpackConfig.externals = typeof ssr === 'object' && ssr.disableExternal ? defaultExternals : [(0, _webpackNodeExternals().default)(nodeExternalsOpts)];
    webpackConfig.output.libraryTarget = 'commonjs2';
    webpackConfig.output.filename = '[name].server.js';
    webpackConfig.output.chunkFilename = '[name].server.async.js';
    webpackConfig.plugins.push(new (require('write-file-webpack-plugin'))({
      // not only `umi.server.js`
      // if addEntry across chainWebpack
      test: /\.server\.js/
    }));
  }

  const isDev = process.env.NODE_ENV === 'development';

  if (!isDev && watch) {
    webpackConfig.devtool = 'eval-source-map';
    webpackConfig.watch = true;
  }

  return webpackConfig;
}