"use strict";

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

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

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

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

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

var _webpackNodeExternals = _interopRequireDefault(require("webpack-node-externals"));

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

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, 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 LimitChunkCountPlugin = _webpack.default.optimize.LimitChunkCountPlugin;

function createServerConfig(config, {
  platform,
  webpackConfig
}) {
  const paths = config.paths,
        host = config.host,
        ports = config.ports,
        options = config.options,
        action = config.action,
        platformName = config.platformName;
  webpackConfig = _objectSpread({}, webpackConfig, {
    entry: [_path.default.resolve(__dirname, '../server.js')],
    output: {
      path: paths.dist,
      publicPath: action === 'start' ? `http://${host}:${ports.dev}/` : '/',
      filename: 'server.js'
    },
    resolve: _objectSpread({}, webpackConfig.resolve, {
      alias: _objectSpread({}, _lodash.default.get(webpackConfig, 'resolve.alias'), {
        '@reactant/web-isomorphic/index.html': _path.default.resolve(paths.platform, 'index.html'),
        '@reactant/web-isomorphic/server': _path.default.resolve(paths.platform, 'server.js')
      }),
      extensions: _lodash.default.uniq([`.${platform.properties.name}.server.js`, `.${platform.properties.name}.server.json`, `.${platform.properties.name}.server.jsx`, `.${platform.properties.name}.server.mjs`, `.${platformName}.server.js`, `.${platformName}.server.json`, `.${platformName}.server.jsx`, `.${platformName}.server.mjs`, ..._lodash.default.get(webpackConfig, 'resolve.extensions', [])])
    }),
    node: {
      console: true,
      __filename: true,
      __dirname: true
    },
    externals: _objectSpread({}, webpackConfig.externals, (0, _webpackNodeExternals.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 = _objectSpread({}, webpackConfig, {
      watch: true,
      entry: [...webpackConfig.entry, 'webpack/hot/poll?300'],
      plugins: [...webpackConfig.plugins, new _startServerWebpackPlugin.default({
        name: 'server.js',
        nodeArgs: [...(options.inspect ? ['--inspect'] : []), ...(options.inspectBrk ? ['--inspect-brk'] : [])],
        keyboard: true
      })]
    });
  }

  return webpackConfig;
}