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

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _mergeConfiguration = require('merge-configuration');

var _mergeConfiguration2 = _interopRequireDefault(_mergeConfiguration);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _pkgDir = require('pkg-dir');

var _pkgDir2 = _interopRequireDefault(_pkgDir);

var _webpack = require('@reactant/cli/lib/webpack');

var _webpack2 = require('webpack');

var _createClientConfig = require('./createClientConfig');

var _createClientConfig2 = _interopRequireDefault(_createClientConfig);

var _createServerConfig = require('./createServerConfig');

var _createServerConfig2 = _interopRequireDefault(_createServerConfig);

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

function createWebConfig(config, webpackConfig, target = 'web') {
  const paths = config.paths,
        babel = config.babel,
        env = config.env,
        action = config.action;

  process.env.NODE_ENV = env;
  if (target === 'client') target = 'web';
  if (target === 'server') target = 'node';
  webpackConfig = _extends({}, webpackConfig, {
    context: process.cwd(),
    target,
    devtool: env === 'development' ? 'cheap-module-eval-source-map' : 'nosources-source-map',
    resolve: _extends({}, webpackConfig.resolve, {
      alias: _extends({}, _lodash2.default.get(webpackConfig, 'resolve.alias'), {
        'webpack/hot/poll': require.resolve('webpack/hot/poll')
      })
    }),
    module: {
      strictExportPresence: true,
      rules: [...(webpackConfig.rules || []), ...(0, _webpack.getRules)(config),
      // {
      //   test: /\.(js|jsx|mjs)$/,
      //   include: [paths.android, paths.ios, paths.src, paths.web],
      //   loader: require.resolve('eslint-loader'),
      //   options: eslint,
      //   enforce: 'pre'
      // },
      {
        test: /\.(js|jsx|mjs)$/,
        include: [paths.src, paths.platform, ...getModuleIncludes(['@reactant/core', 'react-navigation', 'static-container'], config)],
        loader: require.resolve('babel-loader'),
        options: babel
      }]
    },
    plugins: [...webpackConfig.plugins, ...(env !== 'production' ? [new _webpack2.NamedModulesPlugin()] : []), ...(action === 'start' ? [new _webpack2.HotModuleReplacementPlugin(), new _webpack2.NoEmitOnErrorsPlugin()] : [])]
  });
  if (target === 'web') {
    webpackConfig = (0, _createClientConfig2.default)(config, webpackConfig);
  } else {
    webpackConfig = (0, _createServerConfig2.default)(config, webpackConfig);
  }
  return (0, _mergeConfiguration2.default)(webpackConfig, config.webpack, {}, config);
}

function getModuleIncludes(modules, config) {
  const paths = config.paths;

  const includes = [];
  modules.forEach(module => {
    try {
      const modulePath = _pkgDir2.default.sync(require.resolve(_path2.default.resolve(paths.root, 'node_modules', module)));
      includes.push(modulePath);
    } catch (err) {}
  });
  return includes;
}