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

Rewrite to the new, private API #5

Merged
merged 4 commits into from
Nov 26, 2016
Merged

Rewrite to the new, private API #5

merged 4 commits into from
Nov 26, 2016

Conversation

mxstbr
Copy link
Member

@mxstbr mxstbr commented Nov 24, 2016

With the new private API, all the IIFEs are unnecessary since we can just pass an object to the styled() calls. This commit updates the plugin and the tests to reflect the new result. Need to wait for styled-components/styled-components#227 (which introduces the new API) to be merged before releasing.


Please review the code @vdanchenkov, I'm not very familiar with writing babel plugins, so if you see things that could be done easier please let me know!

@geelen please review the result of the conversion in the after.js files of the fixtures!

With the new private API, all the IIFEs are unnecessary since we can
just pass an object to the styled() calls. This commit updates the
plugin and the tests to reflect the new, much better behaviour. This way
we can take full advantage of styled-components/styled-components#227
when it's merged!
}
// styled call without variable
if (!target) {
target = getTarget(path.node.tag)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getting target from path.node.tag works for all tests. In which cases you need to determine it differently?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, once it's path.node.right.tag, once path.node.init.tag and every other time path.node.tag. Or are you saying we can just always use path.node.tag?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in my understanding it's always path.node.tag of TaggedTemplateExpression

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, thanks! Updated!

)
path._styledComponentsSeen = true

path.replaceWith(styledCall)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we got rid of wrapping, code above can be reduced to

          // Put together the styled call with the template literal
          // to get the finished styled({ })`` form! 🎉
          path.node.tag = call.expression

No need to mark with _styledComponentsSeen

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing, thanks!

@vdanchenkov
Copy link
Member

I've been thinking of similar change, but I thought of less intrusive conversion of styled.div to styled('div', {displayName, identifier}) It would make plugin backward compatible with older styled components versions meaning it would not break them. Not sure if it's important.

@mxstbr
Copy link
Member Author

mxstbr commented Nov 25, 2016

I don't think that's a concern, since we can just publish a new major version with a peerDep to the new styled-components version that supports it. I don't remember why we decided to not do it that way though, but now it doesn't really matter and I don't want to rewrite it 😉

@mxstbr mxstbr merged commit 16e83f1 into master Nov 26, 2016
@mxstbr mxstbr deleted the rewrite-to-new-api branch November 26, 2016 10:46
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.

2 participants