webpack.config.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. let foundCSSRule = false;
  2. function removeDuplicateCSSRule(rule) {
  3. if (!rule || !rule.test) {
  4. return;
  5. }
  6. if (rule.test.toString() !== '/\\.css$/') {
  7. return;
  8. }
  9. if (!foundCSSRule) {
  10. foundCSSRule = true;
  11. return;
  12. }
  13. return false;
  14. }
  15. function updateRules(oldRules) {
  16. const newRules = [];
  17. if (!oldRules || !oldRules.length) {
  18. return newRules;
  19. }
  20. for (let i = 0; i < oldRules.length; i++) {
  21. const rule = oldRules[i];
  22. if (rule.oneOf) {
  23. rule.oneOf = updateRules(rule.oneOf);
  24. newRules.push(rule);
  25. } else {
  26. const newRule = removeDuplicateCSSRule(oldRules[i]);
  27. if (newRule) {
  28. // If a new rule was returned, push it into the new rules list
  29. newRules.push(newRule);
  30. }
  31. if (newRule !== false) {
  32. // If any function returned false, it means remove it from the list of rules
  33. // Otherwise, we just use the old rule that was there previously
  34. newRules.push(oldRules[i]);
  35. }
  36. }
  37. }
  38. return newRules;
  39. }
  40. module.exports = async ({ config, mode }) => {
  41. console.log('*** Custom webpack running ****');
  42. config.module.rules = updateRules(config.module.rules);
  43. // Uncomment this line if you would like to view the final webpack config so you can alter it. Useful for debugging
  44. // console.dir(config, { depth: null });
  45. return config;
  46. };