'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 = createClientConfig;

var _assetsWebpackPlugin = require('assets-webpack-plugin');

var _assetsWebpackPlugin2 = _interopRequireDefault(_assetsWebpackPlugin);

var _offlinePlugin = require('offline-plugin');

var _offlinePlugin2 = _interopRequireDefault(_offlinePlugin);

var _uglifyjsWebpackPlugin = require('uglifyjs-webpack-plugin');

var _uglifyjsWebpackPlugin2 = _interopRequireDefault(_uglifyjsWebpackPlugin);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _errorOverlayMiddleware = require('react-dev-utils/errorOverlayMiddleware');

var _errorOverlayMiddleware2 = _interopRequireDefault(_errorOverlayMiddleware);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _webpackBundleAnalyzer = require('webpack-bundle-analyzer');

var _webpack = require('webpack');

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

function createClientConfig(config, webpackConfig) {
  const action = config.action,
        env = config.env,
        host = config.host,
        offline = config.offline,
        options = config.options,
        paths = config.paths,
        platform = config.platform,
        platformType = config.platformType,
        ports = config.ports;

  webpackConfig = _extends({}, webpackConfig, {
    entry: {
      client: [_path2.default.resolve(paths.platform, 'client.js')]
    },
    externals: _extends({}, webpackConfig.externals, {
      'react-art': {}
    }),
    resolve: _extends({}, webpackConfig.resolve, {
      alias: _extends({}, _lodash2.default.get(webpackConfig, 'resolve.alias'), {
        '@reactant/web-isomorphic/ClientRoot': _path2.default.resolve(paths.platform, 'ClientRoot.js')
      }),
      extensions: _lodash2.default.uniq([`.${platform}.client.js`, `.${platform}.client.jsx`, `.${platform}.client.mjs`, `.${platform}.client.json`, `.${platformType}.client.js`, `.${platformType}.client.jsx`, `.${platformType}.client.mjs`, `.${platformType}.client.json`, ..._lodash2.default.get(webpackConfig, 'resolve.extensions', [])])
    }),
    plugins: [...webpackConfig.plugins, new _assetsWebpackPlugin2.default({
      path: paths.dist,
      filename: 'assets.json'
    }), new _webpack.IgnorePlugin(/^child_process$/), new _webpack.IgnorePlugin(/^deasync$/), new _webpack.IgnorePlugin(/^fs$/), new _webpack.IgnorePlugin(/^winston$/)]
  });
  if (env === 'development') {
    webpackConfig = _extends({}, webpackConfig, {
      output: {
        path: _path2.default.resolve(paths.dist, 'public'),
        publicPath: action === 'start' ? `http://${host}:${ports.dev}/` : '/',
        pathinfo: true,
        filename: 'scripts/bundle.js',
        chunkFilename: 'scripts/[name].chunk.js',
        devtoolModuleFilenameTemplate: info => {
          return _path2.default.resolve(info.resourcePath).replace(/\\/g, '/');
        }
      }
    });
  } else {
    webpackConfig = _extends({}, webpackConfig, {
      optimization: {
        concatenateModules: true,
        flagIncludedChunks: true,
        mangleWasmImports: true,
        mergeDuplicateChunks: true,
        minimize: true,
        occurrenceOrder: true,
        providedExports: true,
        removeAvailableModules: true,
        removeEmptyChunks: true,
        sideEffects: true,
        minimizer: [new _uglifyjsWebpackPlugin2.default({
          uglifyOptions: {
            compress: {
              warnings: false,
              comparisons: false,
              drop_console: true
            },
            output: {
              comments: false
            }
          },
          sourceMap: true
        })],
        splitChunks: {
          chunks: 'all'
        }
      },
      output: {
        path: _path2.default.resolve(paths.dist, 'public'),
        publicPath: '/',
        filename: 'scripts/bundle.[hash:8].js',
        chunkFilename: 'scripts/[name].[hash:8].chunk.js'
      }
    });
  }
  if (action === 'start') {
    webpackConfig = _extends({}, webpackConfig, {
      entry: {
        client: [...webpackConfig.entry.client, require.resolve('../hotDevClient')]
      },
      devServer: {
        before(app) {
          app.use((0, _errorOverlayMiddleware2.default)());
        },
        compress: true,
        disableHostCheck: true,
        headers: { 'Access-Control-Allow-Origin': '*' },
        historyApiFallback: { disableDotRule: true },
        host,
        hot: true,
        noInfo: !options.debug,
        overlay: false,
        port: ports.dev,
        quiet: false,
        watchOptions: { ignored: /node_modules/ }
      }
    });
  }
  if (config.options.analyze) {
    webpackConfig = _extends({}, webpackConfig, {
      plugins: [...webpackConfig.plugins, new _webpackBundleAnalyzer.BundleAnalyzerPlugin({
        analyzerPort: ports.analyzer,
        analyzerMode: 'static'
      })]
    });
  }
  if (offline) {
    webpackConfig = _extends({}, webpackConfig, {
      plugins: [...webpackConfig.plugins, new _offlinePlugin2.default({ publicPath: _path2.default.resolve(paths.dist, 'public') })]
    });
  } else {
    webpackConfig = _extends({}, webpackConfig, {
      plugins: [...webpackConfig.plugins, new _webpack.IgnorePlugin(/^offline-plugin\/runtime/)]
    });
  }
  return webpackConfig;
}