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

feat(styled-components): Implement transpileTemplateLiterals #237

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d806f38
implement `template_literals`
ciffelia Nov 25, 2023
df70bc9
enable previously disabled test fixtures
ciffelia Nov 25, 2023
f716f9b
update test fixtures output
ciffelia Nov 25, 2023
20b4ced
create string literal directly
ciffelia Nov 25, 2023
26a980e
Bump npm package: ./packages/constify
kdy1 Nov 25, 2023
df784d9
Bump npm package: ./packages/emotion
kdy1 Nov 25, 2023
e297b35
Bump npm package: ./packages/jest
kdy1 Nov 25, 2023
c536673
Bump npm package: ./packages/loadable-components
kdy1 Nov 25, 2023
f4eb817
Bump npm package: ./packages/noop
kdy1 Nov 25, 2023
727c50d
Bump npm package: ./packages/react-remove-properties
kdy1 Nov 25, 2023
cdf1b30
Bump npm package: ./packages/relay
kdy1 Nov 25, 2023
08c15d3
Bump npm package: ./packages/remove-console
kdy1 Nov 25, 2023
8d65b04
Bump npm package: ./packages/styled-components
kdy1 Nov 25, 2023
66fb63a
Bump npm package: ./packages/styled-jsx
kdy1 Nov 25, 2023
ae09022
Bump npm package: ./packages/swc-magic
kdy1 Nov 25, 2023
a88ee72
Bump npm package: ./packages/transform-imports
kdy1 Nov 25, 2023
72e1b71
Bump cargo crate: modularize_imports
kdy1 Nov 25, 2023
8da897e
Bump cargo crate: react_remove_properties
kdy1 Nov 25, 2023
af5818a
Bump cargo crate: remove_console
kdy1 Nov 25, 2023
fea5a52
Bump cargo crate: styled_components
kdy1 Nov 25, 2023
b12d94d
Bump cargo crate: styled_jsx
kdy1 Nov 25, 2023
fbafb93
Bump cargo crate: swc_constify
kdy1 Nov 25, 2023
3a783d9
Bump cargo crate: swc_emotion
kdy1 Nov 25, 2023
f77ac66
Bump cargo crate: swc_magic
kdy1 Nov 25, 2023
8b44f9e
Bump cargo crate: swc_relay
kdy1 Nov 25, 2023
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
29 changes: 14 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions packages/styled-components/transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use crate::{
utils::{analyze, analyzer, State},
visitors::{
display_name_and_id::display_name_and_id, minify::visitor::minify,
transpile_css_prop::transpile::transpile_css_prop,
template_literals::template_literals, transpile_css_prop::transpile::transpile_css_prop,
},
};

Expand Down Expand Up @@ -72,8 +72,7 @@ impl Config {

/// NOTE: **This is not complete**.
///
/// Only [transpile_css_prop], [minify] and [display_name_and_id] is
/// implemented.
/// [pure] is not implemented.
pub fn styled_components(
file_name: FileName,
src_file_hash: u128,
Expand All @@ -92,6 +91,10 @@ pub fn styled_components(
enabled: config.minify,
visitor: minify(state.clone())
},
display_name_and_id(file_name, src_file_hash, config.clone(), state)
display_name_and_id(file_name, src_file_hash, config.clone(), state.clone()),
Optional {
enabled: config.transpile_template_literals,
visitor: template_literals(state)
}
)
}
1 change: 1 addition & 0 deletions packages/styled-components/transform/src/visitors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod assign_style_required;
pub mod display_name_and_id;
pub mod minify;
pub mod template_literals;
pub mod transpile_css_prop;
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//! Port of https://github.com/styled-components/babel-plugin-styled-components/blob/4e2eb388d9c90f2921c306c760657d059d01a518/src/visitors/templateLiterals/transpile.js

use std::{cell::RefCell, iter, rc::Rc};

use swc_common::{util::take::Take, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith};

use crate::utils::State;

pub fn template_literals(state: Rc<RefCell<State>>) -> impl Fold + VisitMut {
as_folder(TemplateLiterals { state })
}

#[derive(Debug)]
struct TemplateLiterals {
state: Rc<RefCell<State>>,
}

impl VisitMut for TemplateLiterals {
noop_visit_mut_type!();

fn visit_mut_expr(&mut self, expr: &mut Expr) {
expr.visit_mut_children_with(self);

let Expr::TaggedTpl(tagged) = expr else {
return;
};
if !self.state.borrow().is_styled(&tagged.tag)
&& !self.state.borrow().is_helper(&tagged.tag)
{
return;
}

expr.map_with_mut(|expr| {
let tagged = expr.expect_tagged_tpl();

let quasis = tagged
.tpl
.quasis
.into_iter()
.map(|q| {
Expr::Lit(Lit::Str(Str {
span: q.span,
value: q.cooked.unwrap_or(q.raw),
raw: None,
}))
})
.map(ExprOrSpread::from)
.map(Some);
let exprs = tagged.tpl.exprs.into_iter().map(ExprOrSpread::from);
let args = iter::once(
Expr::Array(ArrayLit {
span: DUMMY_SP,
elems: quasis.collect(),
})
.into(),
)
.chain(exprs)
.collect();

Expr::Call(CallExpr {
span: tagged.span,
callee: tagged.tag.into(),
args,
type_args: tagged.type_params,
})
});
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import styled from 'styled-components';
const Simple = styled.div`width:100%;`;
const Interpolation = styled.div`content:" ${(props)=>props.text} ";`;
const SpecialCharacters = styled.div`content:" ${(props)=>props.text} ";color:red;`;
const Comment = styled.div`color:red;`;
const Parens = styled.div`&:hover{color:blue;}`;
const Simple = styled.div([
"width:100%;"
]);
const Interpolation = styled.div([
'content:" ',
' ";'
], (props)=>props.text);
const SpecialCharacters = styled.div([
'content:" ',
' ";color:red;'
], (props)=>props.text);
const Comment = styled.div([
"color:red;"
]);
const Parens = styled.div([
"&:hover{color:blue;}"
]);
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import styled from 'styled-components';
const Simple = styled.div`width:100%;`;
const Interpolation = styled.div`content:"https://test.com/${props => props.endpoint}";`;
const SpecialCharacters = styled.div`content:" ${props => props.text} ";color:red;`;
const Comment = styled.div`width:100%;color:red;`;
const Parens = styled.div`&:hover{color:blue;}color:red;`;
const UrlComments = styled.div`color:red;background:red;border:1px solid green;`;
const Simple = styled.div([
"width:100%;"
]);
const Interpolation = styled.div([
'content:"https://test.com/',
'";'
], (props)=>props.endpoint);
const SpecialCharacters = styled.div([
'content:" ',
' ";color:red;'
], (props)=>props.text);
const Comment = styled.div([
"width:100%;color:red;"
]);
const Parens = styled.div([
"&:hover{color:blue;}color:red;"
]);
const UrlComments = styled.div([
"color:red;background:red;border:1px solid green;"
]);
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,47 @@ import styled from 'styled-components';
const Test1 = styled.div.withConfig({
displayName: "code__Test1",
componentId: "sc-bfa13f91-0"
})`width:100%;`;
})([
"width:100%;"
]);
const Test2 = styled.div.withConfig({
displayName: "code__Test2",
componentId: "sc-bfa13f91-1"
})`width:100%;`;
})([
"width:100%;"
]);
const Test3 = styled.div.withConfig({
displayName: "code__Test3",
componentId: "sc-bfa13f91-2"
})`width:100%;${'red'};`;
})([
"width:100%;",
";"
], 'red');
const Test4 = styled.div.withConfig({
displayName: "code__Test4",
componentId: "sc-bfa13f91-3"
})`width:100%;`;
})([
"width:100%;"
]);
const Test5 = styled.div.withConfig({
displayName: "code__Test5",
componentId: "sc-bfa13f91-4"
})`width:100%;`;
})([
"width:100%;"
]);
const Test6 = styled.div.withConfig({
displayName: "code__Test6",
componentId: "sc-bfa13f91-5"
})`background:url("https://google.com");width:100%;${'green'} `;
})([
'background:url("https://google.com");width:100%;',
" "
], 'green');
const Test7 = styled.div.withConfig({
displayName: "code__Test7",
componentId: "sc-bfa13f91-6"
})`background:url("https://google.com");width:${(p)=>p.props.width};${'green'} height:${(p)=>p.props.height};`;
})([
'background:url("https://google.com");width:',
";",
" height:",
";"
], (p)=>p.props.width, 'green', (p)=>p.props.height);