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

Fix styled transform in next-swc/emotion #35527

Merged
merged 5 commits into from Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion examples/with-emotion-swc/pages/index.js
@@ -1,5 +1,13 @@
import { css } from '@emotion/react'
import { Animated, Basic, bounce, Combined, Pink } from '../shared/styles'
import {
Animated,
Basic,
bounce,
Combined,
Pink,
BasicExtended,
ComponentSelectorsExtended,
} from '../shared/styles'

const Home = () => (
<div
Expand All @@ -14,6 +22,9 @@ const Home = () => (
With <code>:hover</code>.
</Combined>
<Animated animation={bounce}>Let's bounce.</Animated>
<ComponentSelectorsExtended>
<BasicExtended>Nested</BasicExtended>
</ComponentSelectorsExtended>
</div>
)

Expand Down
11 changes: 10 additions & 1 deletion examples/with-emotion-swc/shared/styles.js
Expand Up @@ -58,10 +58,19 @@ export const Combined = styled.div`
}
`

export const Pink = styled.div(basicStyles, {
export const Pink = styled(Basic)({
color: 'hotpink',
})

export const BasicExtended = styled(Basic)``

export const ComponentSelectorsExtended = styled.div`
${BasicExtended} {
color: green;
}
box-shadow: -5px -5px 0 0 green;
`

export const Animated = styled.div`
${basicStyles};
${hoverStyles};
Expand Down
30 changes: 19 additions & 11 deletions packages/next-swc/crates/core/src/emotion/mod.rs
Expand Up @@ -442,7 +442,7 @@ impl<C: Comments> Fold for EmotionTransformer<C> {
)));
}
if let Some(cm) = self.create_sourcemap(expr.span.lo()) {
c.args.push(cm.as_arg());
expr.args.push(cm.as_arg());
}
c.args.push(
Expr::Object(ObjectLit {
Expand Down Expand Up @@ -489,7 +489,7 @@ impl<C: Comments> Fold for EmotionTransformer<C> {
);
if let Some(cm) = self.create_sourcemap(expr.span.lo())
{
args.push(cm.as_arg());
expr.args.push(cm.as_arg());
}
}
return CallExpr {
Expand Down Expand Up @@ -556,9 +556,7 @@ impl<C: Comments> Fold for EmotionTransformer<C> {
}),
)));
}
if let Some(cm) = self.create_sourcemap(call.span.lo()) {
callee.args.push(cm.as_arg());
}

callee.args.push(
Expr::Object(ObjectLit {
span: DUMMY_SP,
Expand All @@ -569,11 +567,19 @@ impl<C: Comments> Fold for EmotionTransformer<C> {
return Expr::Call(CallExpr {
span: DUMMY_SP,
callee: callee.as_callee(),
args: self
.create_args_from_tagged_tpl(&mut tagged_tpl.tpl)
.into_iter()
.map(|exp| exp.fold_children_with(self))
.collect(),
args: {
let mut args: Vec<ExprOrSpread> = self
.create_args_from_tagged_tpl(&mut tagged_tpl.tpl)
.into_iter()
.map(|exp| exp.fold_children_with(self))
.collect();
if let Some(cm) =
self.create_sourcemap(tagged_tpl.span.lo())
{
args.push(cm.as_arg());
}
args
},
type_args: None,
});
}
Expand Down Expand Up @@ -626,11 +632,13 @@ impl<C: Comments> Fold for EmotionTransformer<C> {
}
let mut args =
self.create_args_from_tagged_tpl(&mut tagged_tpl.tpl);

if let Some(cm) =
self.create_sourcemap(member_expr.span.lo())
self.create_sourcemap(tagged_tpl.span.lo())
{
args.push(cm.as_arg());
}

self.comments.add_pure_comment(member_expr.span.lo());
return Expr::Call(CallExpr {
span: DUMMY_SP,
Expand Down
Expand Up @@ -32,6 +32,9 @@ const SpanContainer = styled('span')({
background: 'yellow',
})

export const DivContainerExtended = styled(DivContainer)``
export const DivContainerExtended2 = styled(DivContainer)({})

const Container = styled('button')`
background: red;
${stylesInCallback}
Expand Down