Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with class name generation #147

Closed
satya164 opened this issue Oct 20, 2017 · 1 comment
Closed

Issues with class name generation #147

satya164 opened this issue Oct 20, 2017 · 1 comment
Labels
bug 🐛 Issue is a confirmed bug
Milestone

Comments

@satya164
Copy link
Member

In development, the class name is generated from the file hash and the variable name, while in production it's generated from the hash of the styles and variable name. This might result in inconsistent output between production and development, and cause hard to track specificity issues.

For example, in production, multiple styles won't be inserted for a style block if they are the same, even if they are in separate files. In development, multiple styles will always be inserted if they are in separate files regardless of their content.

This might cause specificity issues if a style is exported for use in a separate file, or passed as a prop, since the order of declaration matters.

We have the following problems to solve here:

  • The output stylesheet should be consistent between production and development
  • HMR should work, which means the class names in a file cannot change
  • There should be no collision between class names

Appending line numbers have the same issue as hashing the content, because they can easily change just like the content.

Possible solution

  • Always use hash of file name instead of the content. AFAIK this is how CSS modules does it too.
  • Keep track of class names generated per file, and if there are multiple instances of class names in a file which has the same name, append the count of the class name. For example, if there are 2 header__grefwr5, then append 1 to the second one and so on.
  • This logic can be inside the sheet module, where it can keep a cache of css per file, e.g. - { [filepath: string]: { [selector: string]: string } }
  • Even with this approach, HMR can still break if the declarations are moved around, but the frequency of this happening should be low.
  • The downside of this approach is that styles aren't de-duplicated in production, but this would be easily optimized by gzipping the CSS.

Lemme know if you have any better ideas.

@zamotany
Copy link
Contributor

zamotany commented Nov 9, 2017

Ok, we can change it. Class name hashes should be generated from filename.

@zamotany zamotany added this to the Version 1 milestone Nov 9, 2017
@satya164 satya164 added bug 🐛 Issue is a confirmed bug and removed rfc labels Nov 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Issue is a confirmed bug
Projects
None yet
Development

No branches or pull requests

2 participants