Last active
June 21, 2022 13:45
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
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