const _ = require('lodash');
const overrideCssLoader = () => (config) => {
  const cssRule = config.module.rules.find((v) => Array.isArray(v.oneOf)).oneOf.find((v) => _.isRegExp(v.test) && v.test.test('*.css'));
  const lessRule = config.module.rules.find((v) => Array.isArray(v.oneOf)).oneOf.find((v) => _.isRegExp(v.test) && v.test.test('*.less'));
  cssRule.use.forEach((item) => {
    if (item.loader && item.loader.includes('css-loader')) {
      item.options.url = (url) => !url.startsWith('/');
    }
  });
  lessRule.use.forEach((item) => {
    if (item.loader && item.loader.includes('css-loader')) {
      item.options.url = (url) => !url.startsWith('/');
    }
  });
  return config;
};

module.exports = overrideCssLoader;
