-
Notifications
You must be signed in to change notification settings - Fork 186
Minify combined class names #248
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
Changes from all commits
e7ae179
bce58e9
fbe5ec3
dd218b7
d6727a3
a5168d7
af553e3
263b35f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ import asap from 'asap'; | |
|
|
||
| import OrderedElements from './ordered-elements'; | ||
| import {generateCSS} from './generate'; | ||
| import {hashObject} from './util'; | ||
| import {hashObject, hashString} from './util'; | ||
|
|
||
| /* :: | ||
| import type { SheetDefinition, SheetDefinitions } from './index.js'; | ||
|
|
@@ -243,6 +243,13 @@ const processStyleDefinitions = ( | |
| } | ||
| }; | ||
|
|
||
| // Sum up the lengths of the stringified style definitions (which was saved as _len property) | ||
| // and use modulus to return a single byte hash value. | ||
| // We append this extra byte to the 32bit hash to decrease the chance of hash collisions. | ||
| const getStyleDefinitionsLengthHash = (styleDefinitions /* : any[] */) /* : string */ => ( | ||
| styleDefinitions.reduce((length, styleDefinition) => length + styleDefinition._len, 0) % 36 | ||
| ).toString(36); | ||
|
|
||
| /** | ||
| * Inject styles associated with the passed style definition objects, and return | ||
| * an associated CSS class name. | ||
|
|
@@ -269,7 +276,16 @@ export const injectAndGetClassName = ( | |
| if (processedStyleDefinitions.classNameBits.length === 0) { | ||
| return ""; | ||
| } | ||
| const className = processedStyleDefinitions.classNameBits.join("-o_O-"); | ||
|
|
||
| let className; | ||
| if (process.env.NODE_ENV === 'production') { | ||
| className = processedStyleDefinitions.classNameBits.length === 1 ? | ||
| `_${processedStyleDefinitions.classNameBits[0]}` : | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we maybe be adding the length of the style definitions when there's only one class name, too? If only to make the hashes the same size/consistent?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what the advantage of that would be. Also, before this PR the hashes were different lengths--for example, from our homepage: --so I imagine they still won't be the same size.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, the other thing is that we'd be hashing a hash unnecessarily. is the additional expense justified?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We wouldn't be hashing anything new, just adding the extra length char on, right? You're right though, this probably isn't important. :)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, you're right double-hashing shouldn't be a thing |
||
| `_${hashString(processedStyleDefinitions.classNameBits.join())}${ | ||
| getStyleDefinitionsLengthHash(styleDefinitions)}`; | ||
| } else { | ||
| className = processedStyleDefinitions.classNameBits.join("-o_O-"); | ||
| } | ||
|
|
||
| injectStyleOnce( | ||
| className, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment would be 💯 if it explained why we are adding the length to the hash.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
263b35f