"use strict";

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

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

var _os = _interopRequireDefault(require("os"));

var _utils = require("../utils");

var _testPattern = require("./testPattern");

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

const isWindows = _os.default.platform().toLowerCase() === 'win32';
const ps = _path.default.sep;
const options = (0, _utils.getOptions)();
const {
  app: {
    vendorExclude,
    customChunks,
    vendorInclude,
    customChunksBaseConfig
  }
} = options;
const reactBundleIncludeList = ['react', 'react-dom', 'react-redux', 'react-transition-group', 'scheduler', 'prop-types'];
const vendorExcludeList = ['script-loader', 'raw-loader', ...reactBundleIncludeList, ...vendorExclude.map(vendorPath => vendorPath.replace(/[/\\]/g, _path.default.sep))];
const vendorIncludeList = [...vendorInclude].map(vendorPath => vendorPath.replace(/[/\\]/g, _path.default.sep));

const isVendor = function isVendor(module) {
  const {
    userRequest
  } = module;
  return userRequest && (vendorIncludeList.some(item => userRequest.indexOf(item) !== -1) || userRequest.indexOf('node_modules') >= 0 && userRequest.endsWith('.css') === false && userRequest.endsWith('publicPathConfig.js') === false && vendorExcludeList.every(item => userRequest.indexOf(`node_modules${ps}${item}${ps}`) === -1));
};

const isReact = module => {
  const {
    userRequest
  } = module;
  return userRequest && reactBundleIncludeList.some(pkg => userRequest.indexOf(`node_modules${ps}${pkg}${ps}`) >= 0);
};

function rulesMatcher(pattern) {
  if (typeof pattern === 'function') {
    return pattern;
  }

  if (Array.isArray(pattern)) {
    return ({
      userRequest
    }) => (0, _testPattern.testPattern)(userRequest, pattern);
  }

  return new RegExp(isWindows ? pattern.replace(/\//g, '\\') : pattern);
}

const baseSplitChunkConfig = Object.assign({
  minSize: 15000
}, customChunksBaseConfig);
const specificCacheGroupConfig = {
  'react.vendor': {
    name: 'react.vendor',
    chunks: 'all',
    minChunks: 1,
    test: isReact,
    enforce: true,
    // priority: 10
    priority: -10
  },
  vendor: {
    name: 'vendor',
    chunks: 'all',
    // chunks: 'initial',
    minChunks: 1,
    test: isVendor,
    // priority: 19,
    priority: -10
  }
};
const customChunksConfig = {};
customChunks.map((obj, index) => ({
  name: obj.name,
  chunks: obj.chunks || 'all',
  minChunks: obj.minChunks || obj.size || 2,
  pattern: obj.pattern,
  priority: obj.priority || -10 * (index + 2),
  enforce: obj.enforce,
  minSize: obj.minSize,
  // || 20000,
  maxSize: obj.maxSize,
  // || 0,
  reuseExistingChunk: obj.reuseExistingChunk,
  cacheGroupName: obj.cacheGroupName || obj.name
})).forEach(({
  name,
  chunks = 'all',
  minChunks,
  pattern,
  priority,
  enforce,
  minSize,
  maxSize,
  reuseExistingChunk,
  cacheGroupName
}) => {
  const obj = {
    test: rulesMatcher(pattern),
    chunks,
    minChunks,
    priority
  };

  if (minSize !== undefined) {
    obj.minSize = minSize;
  }

  if (maxSize !== undefined) {
    obj.maxSize = maxSize;
  }

  if (enforce !== false) {
    obj.enforce = true;
  }

  if (name !== undefined) {
    obj.name = name;
  }

  if (reuseExistingChunk !== undefined) {
    obj.reuseExistingChunk = reuseExistingChunk;
  }

  customChunksConfig[cacheGroupName] = obj;
});
const splitChunkConfig = Object.assign({}, baseSplitChunkConfig, {
  cacheGroups: Object.assign({
    default: false,
    vendors: false
  }, specificCacheGroupConfig, customChunksConfig)
});
var _default = splitChunkConfig;
exports.default = _default;