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

Add compile-time option for class minification #246

Merged
merged 3 commits into from
Jun 13, 2017

Conversation

gilbox
Copy link
Contributor

@gilbox gilbox commented Jun 12, 2017

address this existing todo:

// TODO(emily): Make a 'production' mode which doesn't prepend
// the class name here, to make the generated CSS smaller.

@lencioni

@khanbot
Copy link

khanbot commented Jun 12, 2017

Hey @gilbox,

Thanks for the PR! Mind signing our Contributor License Agreement?

When you've done so, go ahead and comment [clabot:check] and I'll check again.

Yours truly,
khanbot

@gilbox gilbox force-pushed the gil--minify-class-names branch from 0e12b8a to 84ce1cf Compare June 12, 2017 21:31
@gilbox
Copy link
Contributor Author

gilbox commented Jun 12, 2017

[clabot:check]

@khanbot
Copy link

khanbot commented Jun 12, 2017

CLA signature looks good 👍

Copy link
Contributor

@xymostech xymostech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @gilbox! Thanks for the PR! I left some questions inline that I'd like answered before landing this, but the code and test looks good :)

README.md Outdated
@@ -594,6 +594,11 @@ css(styles2.globals);

It isn't determinate whether divs will be red or blue.

## Minify class names

Minify class names by setting the environment variable `process.env.APHRODITE_KEYS`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we want to add an additional env variable, instead of just using process.env.NODE_ENV === "production"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I added another env variable is because it seems reasonable that someone might not want to minimize class names in production in order to make debugging production-only problems easier.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little hesitant to require that folks use a new env variable in order to benefit from this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no problem, i'll change it to use NODE_ENV

src/exports.js Outdated
// TODO(emily): Make a 'production' mode which doesn't prepend
// the class name here, to make the generated CSS smaller.
_name: `${key}_${hashObject(val)}`,
_name: process.env.APHRODITE_KEYS !== 'MINIFIED' ?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thing that we were considering doing is making something like css(styles.a, styles.b) generate only a single hash class name, like _abcdef instead of combining two hashes like _abcdef-O_o-_012345. Not sure if you'd like to try tackling that here as well, but leaving a TODO about it would be nice if not. :)

If we do want to tackle it, we should figure out whether we need to make our hash longer. Currently, our hash is 6 base-36 characters, which means we'd expect collisions once there's ~47k styles on the page. Is that enough? On the largest KA pages, we end up seeing ~500 independent styles, but our website isn't very SPA-y, so the stylesheet gets cleared out pretty often. If we add uppercase letters, that bumps us up to ~240k styles on a page before seeing a collision.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping to combine hashes in a separate PR. I'll add the TODO for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, great! Doing it separately sounds good :)

@xymostech xymostech merged commit a32fd44 into Khan:master Jun 13, 2017
@xymostech
Copy link
Contributor

Thanks! Looking forward to your other PR :)

@jlfwong
Copy link
Collaborator

jlfwong commented Jun 13, 2017

One potential issue with using this optimization is that it dramatically increases the probability of hash collision. I believe when we were originally considering doing this, we planned on using a longer hash when omitting the class name.

_name: `${key}_${hashObject(val)}`,
// TODO(gil): Further minify the -O_o--combined hashes
_name: process.env.NODE_ENV === 'production' ?
`_${hashObject(val)}` : `${key}_${hashObject(val)}`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to keep the leading _ here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class names can't start w/digits

@xymostech
Copy link
Contributor

@jlfwong In this PR, we're still doing the -O_o- concatenation of class names, so there still isn't a huge chance of collisions because you'd need a very large number of actually unique style objects, instead of just a large number of combinations. It sounds like @gilbox is going to do the further step of hashing the final class names down to a single hash, so we can discuss using a longer hash there?

@luixxiul
Copy link

Is it possible to avoid minifying even if the variable process.env.NODE_ENV is set to production?

brave/browser-laptop#10029 (comment)

@gilbox
Copy link
Contributor Author

gilbox commented Jul 31, 2017

@luixxiul when this was discussed we didn't have a specific use-case in mind. Maybe open a PR to add a new environment variable that turns off minification?

@luixxiul
Copy link

That would be great. IMHO the info that the option is enabled by default should have been added to CHANGELOG.md.

@gilbox
Copy link
Contributor Author

gilbox commented Jul 31, 2017

Yeah, unfortunately the CHANGELOG.md is a couple of versions out of date.

@luixxiul
Copy link

luixxiul commented Aug 1, 2017

I opened a new issue for that: #261

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants