"use strict";

require("@babel/polyfill");

var _circularJson = _interopRequireDefault(require("circular-json"));

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

var _fsExtra = _interopRequireDefault(require("fs-extra"));

var _log = _interopRequireWildcard(require("@reactant/core/log"));

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

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

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

var _config = require("@reactant/cli/config");

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

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 mergeConfiguration = require(require.resolve('merge-configuration', {
  paths: [_path.default.resolve(_pkgDir.default.sync(process.cwd()), 'node_modules/@reactant/web-isomorphic')]
})).default;

let debug = false;
if (_lodash.default.includes(process.argv, '--verbose')) (0, _log.setLevel)('verbose');

if (_lodash.default.includes(process.argv, '--debug')) {
  (0, _log.setLevel)('debug');
  debug = true;
}

module.exports = webpackConfig => {
  const _rebuildConfig = (0, _config.rebuildConfig)({
    options: {
      platform: 'web',
      debug
    }
  }),
        config = _rebuildConfig.config,
        platform = _rebuildConfig.platform;

  const paths = config.paths,
        babel = config.babel,
        options = config.options;
  webpackConfig = (0, _webpack.createWebpackConfig)(config, {
    platform,
    webpackConfig
  });
  webpackConfig.resolve.extensions.unshift('.web.js');
  webpackConfig.externals = _objectSpread({}, webpackConfig.externals, {
    child_process: {},
    deasync: {},
    fs: {},
    winston: {}
  });
  webpackConfig = replaceBabelRule(webpackConfig, {
    test: /\.(js|jsx|mjs)$/,
    include: [paths.src, paths.stories, ...getModuleIncludes(['react-navigation', 'static-container'], config)],
    loader: require.resolve('babel-loader'),
    options: babel
  });
  webpackConfig = mergeConfiguration(webpackConfig, config.storybook, {}, config);

  if (options.debug) {
    _fsExtra.default.mkdirsSync(paths.debug);

    _fsExtra.default.writeFileSync(_path.default.resolve(paths.debug, 'webpack.storybook.json'), _circularJson.default.stringify(webpackConfig, null, 2));
  }

  _log.default.debug('webpackConfig', webpackConfig);

  return webpackConfig;
};

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

function replaceBabelRule(webpackConfig, rule) {
  const babelRule = _lodash.default.find(webpackConfig.module.rules, rule => {
    return !!_lodash.default.find(_lodash.default.isArray(rule.use) ? rule.use : [], rule => rule.loader.indexOf('babel-loader') > -1);
  });

  _lodash.default.each(_lodash.default.keys(babelRule), key => {
    delete babelRule[key];
  });

  _lodash.default.assign(babelRule, rule);

  return webpackConfig;
}