Removing the hash from generated classNames #273
-
I appreciate this kind of goes against the whole point of scoped CSS modules but is it possible? I've tried to play around with the Webpack css-loader I'm interested in being able to use the generated classes outside of a JS environment - in server side templates - and the BEM style format you get in dev mode (with webpack) could work well, however obviously the hashes mean the classNames change on every build. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 7 replies
-
I have a few of thoughts on this. Firstly I'd note that the classNames don't change on each build, they should be consistent between builds as long as you don't change the file name or re-order the styles. This is designed to ensure long-term caching works as well as possible. On your use-case of using the classNames on the server, I'm assuming your server isn't running node? Because if it is you'd be better off importing the If we removed hashes from the generated classes you'd be extremely likely to cause class collisions. Also the debug id feature (i.e. the thing that names the classes after your variable names) isn't a perfect system and probably shouldn't be relied on to make your production code work. |
Beta Was this translation helpful? Give feedback.
-
It is still expected that the ability to remove hash will be supported, because in real business systems(for example, in the ui component library project), where refactoring is often done, the directory structure and names of components are likely to change and the resulting hash will be disastrous for the real environment. This is because higher level components are likely to overwrite the old classname_hash. |
Beta Was this translation helpful? Give feedback.
-
@mattcompiles Agree this certainly introduces that risk. Looking at the possibilities from the perspective of applications that aren't necessarily JS-/Node-based, I wonder whether there's some value in offering the ability to customise classnames. As an example: One might be designing a component library, knowing components will mostly be used within React applications. However to afford reuse through static HTML sites and, say, Ruby or Django templating, components would benefit from a developer-friendly HTML classname API of some kind. Traditionally we'd leverage a convention (e.g. BEM) to mitigate some of the risk of class collisions, granted it's not bulletproof. E.g. As an example from Braid, we might hope for:
Does that seem like something that could be a consideration of Any input you have would be much appreciated. Cheers 👍 Edit: Related to #234 (reply in thread) |
Beta Was this translation helpful? Give feedback.
-
For future reference, fully custom hashes are now possible as of #1160. |
Beta Was this translation helpful? Give feedback.
I have a few of thoughts on this. Firstly I'd note that the classNames don't change on each build, they should be consistent between builds as long as you don't change the file name or re-order the styles. This is designed to ensure long-term caching works as well as possible.
On your use-case of using the classNames on the server, I'm assuming your server isn't running node? Because if it is you'd be better off importing the
.css.ts
files directly and using them just like you do in React. If you're using a different server setup then I can see why this isn't a simple issue.If we removed hashes from the generated classes you'd be extremely likely to cause class collisions. Also the debug…