All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.cjs.configs.webpack.helpers.customize-default-rules.js Maven / Gradle / Ivy

Go to download

A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.

There is a newer version: 4.12.3
Show newest version
/*
  Customize default webpack rules/loaders configuration.
  A custom helpers till shakapacker introduces the feature.
  Track the following the issues to get the status of feature development in shakapacker:
  https://github.com/shakacode/shakapacker/issues/80
  https://github.com/shakacode/shakapacker/issues/87
  Usage:
    const customizeWebpackDefaultRules = require("./helpers/customize-default-rules");
    const defaultRules = {
      "asset/resource": {
        test: /\.(bmp|gif|jpe?g|png|tiff|ico|avif|webp|eot|otf|ttf|woff|woff2)$/
      },
      "asset/source": {
        exclude: /\.(js|mjs|jsx|ts|tsx|js)$/,
      },
    };
    const customWebpackConfig = customizeWebpackDefaultRules(webpackConfig, defaultRules);
  NOTE:
    - Check if the option to output default configuration is introduced or not before upgrading further.
    - Update the defaultRuleType constant whenever shakapacker introduces a new default rule/loader.
*/
const { findBy, findIndexBy } = require("@bigbinary/neeto-cist");

const modifyDefaultRulesConfig = (webpackConfig, rules = {}) => {
  const defaultRuleType = ["asset/resource", "asset/source"];

  Object.keys(rules).forEach(ruleName => {
    if (defaultRuleType.includes(ruleName)) {
      const rule = findBy(
        { type: "asset/resource" },
        webpackConfig.module.rules
      );

      const rulePosition = findIndexBy(
        { type: ruleName },
        webpackConfig.module.rules
      );

      Object.keys(rules[ruleName]).forEach(attribute => {
        if (rule[attribute] !== undefined) {
          rule[attribute] = rules[ruleName][attribute];
        }
      });

      webpackConfig.module.rules[rulePosition] = rule;
    } else {
      throw new Error("Invalid default rule type");
    }
  });

  return webpackConfig;
};

module.exports = modifyDefaultRulesConfig;




© 2015 - 2024 Weber Informatics LLC | Privacy Policy