-
Notifications
You must be signed in to change notification settings - Fork 414
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
Minify classnames #183
Minify classnames #183
Conversation
@@ -76,4 +87,18 @@ describe('preval-extract/prevalStyles', () => { | |||
"css.named('header', 'filename.js')`color: #ffffff`" | |||
); | |||
}); | |||
|
|||
it('test', () => { |
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.
nit: it('minifies class names',
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.
Sorry about that, I've noticed it just now 🙏
} | ||
); | ||
|
||
expect(className.startsWith('ln')).toBeTruthy(); |
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.
You could also assert on the digits count
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.
Good call!
|
||
import getReplacement from './getReplacement'; | ||
import { | ||
instantiateModule, | ||
clearLocalModulesFromCache, | ||
} from '../lib/moduleSystem'; | ||
|
||
function getMinifiedClassName(className: string) { | ||
return `ln${slugify(className).substr(0, 6)}`; |
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.
I'm afraid this may lead to clashing hashes
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.
Me too, but I'm not capable to calculate in which case. Should I introduce some algorithm which targets only 6 chars long string to avoid substr?
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.
I think it's safer to stick with our 7-char hash, at least we stay consistent with dev vs prod.
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.
@thymikee I'm lame, I don't get it where I got this code. Slugify return 6 chars hash implicitly. Substr is not needed so I assume everything will be OK. 🤷♀️
); | ||
|
||
expect(/ln[\w]{6}/.test(className)).toBeTruthy(); | ||
expect(className.startsWith('ln')).toBeTruthy(); |
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.
I think this assertion is unnecessary when the previous one already tests this.
|
||
import getReplacement from './getReplacement'; | ||
import { | ||
instantiateModule, | ||
clearLocalModulesFromCache, | ||
} from '../lib/moduleSystem'; | ||
|
||
function getMinifiedClassName(className: string) { | ||
return `ln${slugify(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.
Not really sure if slugifying an already slugified string is a good idea. I'd prefer to change https://github.com/callstack/linaria/blob/master/src/babel/preval-extract/index.js#L111 to set the title to ln
and work from there.
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.
Just replace slugify
with short-hash
} | ||
); | ||
|
||
expect(/ln[\w]{6}/.test(className)).toBeTruthy(); |
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 regex should be [a-zA-Z0-9]
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.
You are right, I forget that \w match also _
. 👶
@jukben please don't run prettier over package.json |
cc5eb8b
to
9dd2c52
Compare
It looks like I have to turn off prettier for Linaria package, sorry about that. 😔 |
This PR added option to minify class names. Closes #175
Added corresponding test.
You can use it like
will produce classs names within pattern
/ln[a-z0-9]{6}/