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

Minify classnames #183

Merged
merged 11 commits into from
Dec 5, 2017
Merged

Minify classnames #183

merged 11 commits into from
Dec 5, 2017

Conversation

jukben
Copy link
Contributor

@jukben jukben commented Dec 4, 2017

This PR added option to minify class names. Closes #175

Added corresponding test.

You can use it like

{
  "presets": [
    "env",
    "react",
    ["linaria/babel", {
      "single": true,
      "filename": "styles.css",
      "outDir": "dist",
      "minifyClassnames": true,
    }]
  ]
}

will produce classs names within pattern /ln[a-z0-9]{6}/

@@ -76,4 +87,18 @@ describe('preval-extract/prevalStyles', () => {
"css.named('header', 'filename.js')`color: #ffffff`"
);
});

it('test', () => {
Copy link
Member

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',

Copy link
Contributor Author

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();
Copy link
Member

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

Copy link
Contributor Author

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)}`;
Copy link
Member

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

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor Author

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();
Copy link
Member

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)}`;
Copy link
Member

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.

Copy link
Contributor

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();
Copy link
Member

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]

Copy link
Contributor Author

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 _. 👶

@thymikee
Copy link
Member

thymikee commented Dec 5, 2017

@jukben please don't run prettier over package.json

@jukben
Copy link
Contributor Author

jukben commented Dec 5, 2017

It looks like I have to turn off prettier for Linaria package, sorry about that. 😔

@zamotany zamotany merged commit 6bd05de into master Dec 5, 2017
@zamotany zamotany deleted the feat/minify-classnames branch December 5, 2017 10:41
@johanholmerin johanholmerin added this to the 1.0.0 milestone Dec 3, 2018
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.

None yet

5 participants