{"size": 1313, "ext": "js", "max_stars_repo_path": "index.js", "max_stars_repo_name": "crazyurus/craco-css-modules", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "index.js", "max_issues_repo_name": "crazyurus/craco-css-modules", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "index.js", "max_forks_repo_name": "crazyurus/craco-css-modules", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.7450980392, "max_line_length": 113, "alphanum_fraction": 0.6542269612}
const { throwUnexpectedConfigError } = require('@craco/craco');
const AutoCSSModulesWebpackPlugin = require('auto-css-modules-webpack-plugin');

function throwError(message, githubIssueQuery) {
  return throwUnexpectedConfigError({
    packageName: 'craco-css-modules',
    githubRepo: 'crazyurus/craco-css-modules',
    message,
    githubIssueQuery,
  });
}

function overrideWebpackConfig({ context, webpackConfig }) {
  const oneOfRule = webpackConfig.module.rules.find(rule => rule.oneOf);

  if (!oneOfRule) {
    throwError(
      "Can't find a 'oneOf' rule under module.rules in the " +
      `${context.env} webpack config!`,
      "webpack+rules+oneOf"
    );
  }

  const extensions = ['css', 'less', '(scss|sass)'];

  extensions.forEach(extension => {
    const styleRule = oneOfRule.oneOf.find(rule => rule.test.toString() === `/\\.${extension}$/`);
    const styleModuleRule = oneOfRule.oneOf.find(rule => rule.test.toString() === `/\\.module\\.${extension}$/`);

    styleRule.oneOf = [
      {
        resourceQuery: /modules/,
        use: styleModuleRule.use,
      },
      {
        use: styleRule.use,
      }
    ];

    delete styleRule.use;
  });

  webpackConfig.plugins.push(new AutoCSSModulesWebpackPlugin());

  return webpackConfig;
}

module.exports = {
  overrideWebpackConfig,
};
