Skip to content

Instantly share code, notes, and snippets.

@evgenyt1
Last active June 21, 2022 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evgenyt1/be787b0510bb5654cdf1309d3c4ae5a1 to your computer and use it in GitHub Desktop.
Save evgenyt1/be787b0510bb5654cdf1309d3c4ae5a1 to your computer and use it in GitHub Desktop.
Webpack5 and CSS Modules config to enable human readable stable CSS class names
{
loader: "css-loader",
options: {
modules: {
getLocalIdent: (loaderContext, _localIdentName, localName, options) => {
const relativeResourcePath = path.relative(
options.context,
loaderContext.resourcePath
);
// no need for /src and and .module.scss in
const pathInfo = relativeResourcePath
.replace(/^src\//, "")
.replace(/\.module\.s?css/, "");
const localNameRegex = new RegExp(
`(^|/)${localName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}$`,
"i"
);
// no need for `fest-Comp1-comp1` classes
// replace those with `fest-Comp1`
if (pathInfo.match(localNameRegex)) return `fest-${pathInfo}`;
return `fest-${pathInfo}-${localName}`;
},
},
},
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment