Repository navigation
Add component display name along with generated class - #160
JamieDixon wants to merge 13 commits into
Conversation
…nt mode to make it easier to debug from the browser console
Generated by 🚫 danger |
|
@JamieDixon This is great! I’ve also had a discussion with @mxstbr and @geelen on Twitter about this. I think this would pair very well with a babel-transform that adds |
|
@ehd Yes! We've tested this locally with the babel-transform (https://github.com/vdanchenkov/babel-plugin-styled-components-named) and it works great. |
|
Hot damn, I want this 😄 Let me know if I can assist with this PR in any way. It would help in two projects I’m working on with a team where people had trouble finding components by name. |
|
What's the reason for not prepending the name to the hash? I read #153 but couldn't quite make out why that was changed. That would also guarantee that the name doesn't exist in the global namespace, without having to have tons of emojis in the DevTools. (not a fan of that) Let's get this landed asap! |
Yeah, that’s a good point. If there’s no objection to concatenating I’d prefer that, too. Bonus: People are discouraged from writing external CSS matching on 💅FooBar💅 when it’s concatenated with the random string. |
|
One of the problems I had was that the check for whether the className had been generated already was based on the hash ( However, you're absolutely right that this isn't necessary and might cause other problems like name clashes. We can get around this by reverting to a similar proposal as earlier and have a As for the emojis in the DevTools, I thought it was cute but I've removed it now :) |
…production. Since the tests run with an env not-equal to production, the tests would fail with the previous check
|
I think this PR is now ready and inline with your comments above. Let me know if there's more to be done here. 👍 |
|
Btw, I’d be happy to contribute a test case for this, but not sure where to push 😄 |
…ts into display-name-retake
|
@ehd push a new branch based on this branch to your fork, and submit a PR against JamieDixon's fork! |
|
@mxstbr @JedWatson I shall! |
|
@mxstbr @JamieDixon Good morning/evening/ it('should add displayName as className suffix', () => {
const Comp = styled.div``
Comp.displayName = 'Comp'
shallow(<Comp />)
expectCSSMatches('.a-Comp { }')
})However, something came to my attention: The current implementation only attaches the displayName suffix when However, when running the tests with This probably also means that when using styled-components in development, all current styled-components classNames will have a suffix of Two questions:
|
Can we somehow test that it does the appending in dev, but doesn't do it prod? (i.e. test both cases?)
I like this. |
I think 3 is the smallest change but could break, 2 is the cleanest, 1 doesn’t seem worth it. Also, we should check for
That was my first impression, but then I thought it might hint at where these otherwise random classNames are originating from 🤔 |
|
Thanks for this feedback @ehd. Loving it :) I switched For the cases where RE the environment during a test, I'm a big fan of tests being clear and easy to understand at a glance. This is one of the reasons I never use I'm happy to change the test to Thoughts? |
Thank you, I appreciate that! I love this PR! 😄
Gotcha!
I was planning on adding the transform to the projects where I’m using styled-components. I reckon @mxstbr was thinking of cases when the transform hasn’t been added. I’m personally still leaning towards adding display name suffixes even if they’re just going to result int
Yeah, that sounds good! Also allows you to see the expected behavior around NODE_ENV at a glance instead of having to look at two test runs in
Btw, NODE_ENV isn’t even set to Only need to agree on classNames generated from default displayNames now :) |
I don't have a hugely strict opinion on this, if you prefer it so be it! 👍 |
|
Is there anything left to be done here? |
|
Everything works as it is however if we want to change the check to I'm happy to do that and could even do it as a separate PR once this is merged in? Might be best if people would get a lot of value from this right now. What do you think? |
|
Let's do that fix first, I don't want to ship a version just to ship another one straight afterwards that works differently! |
…modified tests to include the dislayName as part of the selector
|
The fix is now done. I've changed to using The next step is to add new tests that check that the display name is not added when we're in production mode. Happy to do that as part of this PR or as a separate update. |
|
Looking great 😄 I’m also happy to contribute the additional tests then! |
|
Please add that test so we got it, then we can merge and ship this! 🎉 |
|
@JamieDixon Will send a PR your way tonight! |
Add test cases for displayName suffix in dev and production
|
Thanks @ehd. Awesome stuff! Looking forward to landing this one. The people on our team will be so happy 💃 |
|
Hey just caught up on this issue, had a couple of thoughts. I don't think the Also, be careful with anything that affects caching behaviour between dev and prod. There could be some spectacular bugs introduced that way, which people will only see when pushing to prod. Rare, sure, but high pain. Personally I'd inject an unselectable classname instead of concatenating the hashed class. Like, starting with a number or a zero-width space character or something. TBH the 💅 would be sufficient. Looks great otherwise. Very keen to try to make the dev env here as good as possible 👍 |
How does this affect caching behaviour between dev and prod? When would there ever be an overlap between those two? |
|
Any update here? |
|
This will superseded by #227 which solves this problem in a more thorough way. Together with the new Sorry for letting you go through all of this effort @JamieDixon just to then not merge this PR, but it set us off in the right direction! Thanks so much! |
|
@mxstbr Oh hey this is even more awesome, thank you so much!
…On Sat, 26 Nov 2016 at 10:34 Max Stoiber ***@***.***> wrote:
Closed #160
<#160>.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#160 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAEpsBL8Kvw2BQjfYzwwdvwzvllTIJX7ks5rB_0XgaJpZM4KnFyT>
.
|
|
Aha! Well great, glad that there's a better solution to this problem. Being new to OSS it feels hard when a PR doesn't get merged but the learning from feeling bad is always good. New personal discoveries made. 💃 |
|
Really sorry for not merging it @JamieDixon, seriously thanks so much for taking the time to make this happen! |
I'm raising this PR to replace #153 which I'll close. The commit log got into a bit of a mess and I couldn't pick it apart. This PR contains only the commits necessary for the final outcome of this change.
Add
displayNameas a class value in the output HTML when in development mode to make it easier to debug from the browser consoleThis PR adds the component
displayNameas a class whenprocess.env.NODE_ENV !== 'production'