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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.NODE_ENV`
to the string value `production`.

# Tools

- [Aphrodite output tool](https://output.jsbin.com/qoseye) - Paste what you pass to `StyleSheet.create` and see the generated CSS
Expand Down
6 changes: 3 additions & 3 deletions src/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ const StyleSheet = {
create(sheetDefinition /* : SheetDefinition */) {
return mapObj(sheetDefinition, ([key, val]) => {
return [key, {
// TODO(emily): Make a 'production' mode which doesn't prepend
// the class name here, to make the generated CSS smaller.
_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

_definition: val
}];
});
Expand Down
26 changes: 26 additions & 0 deletions tests/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,32 @@ describe('StyleSheet.create', () => {

assert.ok(sheet.empty._name);
});

describe('process.env.NODE_ENV === \'production\'', () => {
beforeEach(() => {
process.env.NODE_ENV = 'production';
});

afterEach(() => {
delete process.env.NODE_ENV;
});

it('hashes style names correctly', () => {
const sheet = StyleSheet.create({
test: {
color: 'red',
height: 20,

':hover': {
color: 'blue',
width: 40,
},
},
});

assert.equal(sheet.test._name, '_j5rvvh');
});
})
});

describe('rehydrate', () => {
Expand Down