"use strict";

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

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

var _mergeConfiguration = _interopRequireDefault(require("merge-configuration"));

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

var _pkgDir = _interopRequireDefault(require("pkg-dir"));

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

var _webpack2 = require("webpack");

var _createClientConfig = _interopRequireDefault(require("./createClientConfig"));

var _createServerConfig = _interopRequireDefault(require("./createServerConfig"));

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

function createWebConfig(config, {
  webpackConfig,
  target = 'web',
  platform
}) {
  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 = _objectSpread({}, webpackConfig, {
    context: process.cwd(),
    target,
    resolve: _objectSpread({}, webpackConfig.resolve, {
      alias: _objectSpread({}, _lodash.default.get(webpackConfig, 'resolve.alias'), {
        'webpack/hot/poll': require.resolve('webpack/hot/poll')
      })
    }),
    module: {
      strictExportPresence: true,
      rules: [...(webpackConfig.rules || []), ...(0, _webpack.getRules)(config, {
        platform
      }), // {
      //   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, _createClientConfig.default)(config, {
      platform,
      webpackConfig
    });
  } else {
    webpackConfig = (0, _createServerConfig.default)(config, {
      platform,
      webpackConfig
    });
  }

  return (0, _mergeConfiguration.default)(webpackConfig, config.webpack, {}, config);
}

function getModuleIncludes(modules, config) {
  const paths = config.paths;
  const includes = [];
  modules.forEach(module => {
    try {
      const modulePath = _pkgDir.default.sync(require.resolve(_path.default.resolve(paths.root, 'node_modules', module)));

      includes.push(modulePath);
    } catch (err) {}
  });
  return includes;
}