I use webpack
and css-loader
, and in my css-loader
config I use these options:
options: {
importLoaders: 1,
modules: true,
localIdentName: '[hash:base64:3]'
}
Just like you see, it is obvious that I desire all of my class name will have 3 characters, and after build absolutely my desire come true but there is a very big issue.
Some class names has same name! (conflict!)
for example:
._1mk { /*dev name was .home*/
color: red;
} /*line 90*/
and
._1mk { /*dev name was .news*/
color: blue;
}
This is a big issue, but when I use [hash:base64:5]
everything would be ok and each class has its own hash name without any conflict.
I search this issue about 4 hours and saw all developers use number 5 as less of length of hash for their config. I don't know why! I calculate that 64 characters [a-z][A-Z][0-9][-,_] in three length can has 262144 different words, so why it can not some different names?
how can I settle this conflict? Really should I miss the number 3 ? and use 5 like others?
finally I find the right way, it is
hash
, notrandomNaming
function. this is made to hash so it is so obviously in short length with vast naming maybe it produce collision. so I write my ownwebpack
naming function and use the variables and the function top onwebpack
config file. these are steps of my solution:At first, two variables for
cache
andqueue
. cache for easy accessing toLocalName
and its newrandomName
and queue for holding variable entries that involve all new random names for avoiding collision.At second, we declare
randomNaming
function. I know, maybe it is not very optimize but it works well. the export file is awesome without any collision.At Third, in
css-loader
scope inside ofwebpack
config I usedgetLocalIdent
notlocalIdentName
.And now all of names are hashed and
CSS
file is in minimal possible volume size. And theHTML
is so light weight.