Skip to content

Commit 84ce1cf

Browse files
author
Gil Birman
committed
Add compile-time option for class minification
1 parent deb4f45 commit 84ce1cf

3 files changed

Lines changed: 33 additions & 3 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,11 @@ css(styles2.globals);
594594

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

597+
## Minify class names
598+
599+
Minify class names by setting the environment variable `process.env.APHRODITE_KEYS`
600+
to the string value `MINIFIED`.
601+
597602
# Tools
598603

599604
- [Aphrodite output tool](https://output.jsbin.com/qoseye) - Paste what you pass to `StyleSheet.create` and see the generated CSS

src/exports.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ const StyleSheet = {
2121
create(sheetDefinition /* : SheetDefinition */) {
2222
return mapObj(sheetDefinition, ([key, val]) => {
2323
return [key, {
24-
// TODO(emily): Make a 'production' mode which doesn't prepend
25-
// the class name here, to make the generated CSS smaller.
26-
_name: `${key}_${hashObject(val)}`,
24+
_name: process.env.APHRODITE_KEYS !== 'MINIFIED' ?
25+
`${key}_${hashObject(val)}` : `_${hashObject(val)}`,
2726
_definition: val
2827
}];
2928
});

tests/index_test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,32 @@ describe('StyleSheet.create', () => {
229229

230230
assert.ok(sheet.empty._name);
231231
});
232+
233+
describe('process.env.APHRODITE_KEYS === \'MINIFIED\'', () => {
234+
beforeEach(() => {
235+
process.env.APHRODITE_KEYS = 'MINIFIED';
236+
});
237+
238+
afterEach(() => {
239+
delete process.env.APHRODITE_KEYS;
240+
});
241+
242+
it('hashes style names correctly', () => {
243+
const sheet = StyleSheet.create({
244+
test: {
245+
color: 'red',
246+
height: 20,
247+
248+
':hover': {
249+
color: 'blue',
250+
width: 40,
251+
},
252+
},
253+
});
254+
255+
assert.equal(sheet.test._name, '_j5rvvh');
256+
});
257+
})
232258
});
233259

234260
describe('rehydrate', () => {

0 commit comments

Comments
 (0)