'use strict';

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

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

exports.default = createServerConfig;

var _startServerWebpackPlugin = require('start-server-webpack-plugin');

var _startServerWebpackPlugin2 = _interopRequireDefault(_startServerWebpackPlugin);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _webpack = require('webpack');

var _webpack2 = _interopRequireDefault(_webpack);

var _webpackNodeExternals = require('webpack-node-externals');

var _webpackNodeExternals2 = _interopRequireDefault(_webpackNodeExternals);

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

const LimitChunkCountPlugin = _webpack2.default.optimize.LimitChunkCountPlugin;
function createServerConfig(config, webpackConfig) {
  const paths = config.paths,
        host = config.host,
        ports = config.ports,
        options = config.options,
        action = config.action,
        platform = config.platform,
        platformType = config.platformType;

  webpackConfig = _extends({}, webpackConfig, {
    entry: [_path2.default.resolve(__dirname, '../server.js')],
    output: {
      path: paths.dist,
      publicPath: action === 'start' ? `http://${host}:${ports.dev}/` : '/',
      filename: 'server.js'
    },
    resolve: _extends({}, webpackConfig.resolve, {
      alias: _extends({}, _lodash2.default.get(webpackConfig, 'resolve.alias'), {
        '@reactant/web-isomorphic/index.html': _path2.default.resolve(paths.platform, 'index.html'),
        '@reactant/web-isomorphic/server': _path2.default.resolve(paths.platform, 'server.js')
      }),
      extensions: _lodash2.default.uniq([`.${platform}.server.js`, `.${platform}.server.jsx`, `.${platform}.server.mjs`, `.${platform}.server.json`, `.${platformType}.server.js`, `.${platformType}.server.jsx`, `.${platformType}.server.mjs`, `.${platformType}.server.json`, ..._lodash2.default.get(webpackConfig, 'resolve.extensions', [])])
    }),
    node: {
      console: true,
      __filename: true,
      __dirname: true
    },
    externals: _extends({}, webpackConfig.externals, (0, _webpackNodeExternals2.default)({
      whitelist: [...(action === 'start' ? ['webpack/hot/poll?300'] : []), /\.(eot|woff|woff2|ttf|otf)$/, /\.(svg|png|jpg|jpeg|gif|ico)$/, /\.(mp4|mp3|ogg|swf|webp)$/]
    }), {
      '@reactant/core/assets': "require('./assets.json')"
    }),
    plugins: [...webpackConfig.plugins, new LimitChunkCountPlugin({
      maxChunks: 1
    })]
  });
  if (action === 'start') {
    webpackConfig = _extends({}, webpackConfig, {
      watch: true,
      entry: [...webpackConfig.entry, 'webpack/hot/poll?300'],
      plugins: [...webpackConfig.plugins, new _startServerWebpackPlugin2.default({
        name: 'server.js',
        nodeArgs: [...(options.inspect ? ['--inspect'] : []), ...(options.inspectBrk ? ['--inspect-brk'] : [])],
        keyboard: true
      })]
    });
  }
  return webpackConfig;
}