Skip to content

Commit c0d6030

Browse files
committed
extract nested styles
1 parent cb114cc commit c0d6030

File tree

3 files changed

+77
-115
lines changed

3 files changed

+77
-115
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@devup-ui/wasm": patch
3+
---
4+
5+
Extract nested styles
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
source: libs/extractor/src/lib.rs
3+
expression: "extract(\"test.js\",\nr#\"import {Flex} from '@devup-ui/core'\n;<Flex gap={{ \"a\": [1, 2, 3], \"b\": [4, 5, 6] }[idx]} />\n \"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap()"
4+
---
5+
ExtractOutput {
6+
styles: [
7+
Static(
8+
ExtractStaticStyle {
9+
property: "display",
10+
value: "flex",
11+
level: 0,
12+
selector: None,
13+
basic: true,
14+
},
15+
),
16+
Static(
17+
ExtractStaticStyle {
18+
property: "gap",
19+
value: "4px",
20+
level: 0,
21+
selector: None,
22+
basic: false,
23+
},
24+
),
25+
Static(
26+
ExtractStaticStyle {
27+
property: "gap",
28+
value: "8px",
29+
level: 1,
30+
selector: None,
31+
basic: false,
32+
},
33+
),
34+
Static(
35+
ExtractStaticStyle {
36+
property: "gap",
37+
value: "12px",
38+
level: 2,
39+
selector: None,
40+
basic: false,
41+
},
42+
),
43+
Static(
44+
ExtractStaticStyle {
45+
property: "gap",
46+
value: "16px",
47+
level: 0,
48+
selector: None,
49+
basic: false,
50+
},
51+
),
52+
Static(
53+
ExtractStaticStyle {
54+
property: "gap",
55+
value: "20px",
56+
level: 1,
57+
selector: None,
58+
basic: false,
59+
},
60+
),
61+
Static(
62+
ExtractStaticStyle {
63+
property: "gap",
64+
value: "24px",
65+
level: 2,
66+
selector: None,
67+
basic: false,
68+
},
69+
),
70+
],
71+
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className={`d0 ${{\n\t\"a\": \"d1 d2 d3\",\n\t\"b\": \"d4 d5 d6\"\n}[idx]}`} />;\n",
72+
}

libs/extractor/src/style_extractor.rs

Lines changed: 0 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -531,15 +531,13 @@ fn extract_style_from_member_expression<'a>(
531531
) -> ExtractResult<'a> {
532532
let mem_expression = &mem.expression.clone_in(ast_builder.allocator);
533533
let mut ret: Vec<ExtractStyleProp> = vec![];
534-
println!("어푸어푸 mem: {:?}", mem);
535534

536535
match &mut mem.object {
537536
Expression::ArrayExpression(array) => {
538537
if array.elements.is_empty() {
539538
return ExtractResult::Remove;
540539
}
541540

542-
println!("{:?}", mem_expression);
543541
if let Some(num) = get_number_by_literal_expression(mem_expression) {
544542
if num < 0f64 {
545543
return ExtractResult::Remove;
@@ -583,7 +581,6 @@ fn extract_style_from_member_expression<'a>(
583581
};
584582
}
585583

586-
println!("flag1");
587584
let mut map = BTreeMap::new();
588585
for (idx, p) in array.elements.iter_mut().enumerate() {
589586
if let ArrayExpressionElement::SpreadElement(ref sp) = p {
@@ -618,51 +615,10 @@ fn extract_style_from_member_expression<'a>(
618615
}
619616
}
620617

621-
println!("flag2");
622618
ret.push(ExtractStyleProp::MemberExpression {
623619
expression: mem_expression.clone_in(ast_builder.allocator),
624620
map,
625621
});
626-
println!("flag3");
627-
628-
// match mem_expression {
629-
// Expression::NumericLiteral(v) => {
630-
// if array.elements.len() < v.value as usize {
631-
// // wrong indexing case
632-
// ExtractResult::Remove
633-
// } else {
634-
// extract_style_from_expression(
635-
// ast_builder,
636-
// name,
637-
// array.elements[v.value as usize]
638-
// .clone_in(ast_builder.allocator)
639-
// .to_expression_mut(),
640-
// level,
641-
// selector,
642-
// )
643-
// }
644-
// }
645-
// // wrong indexing case
646-
// Expression::UnaryExpression(unary) => {
647-
// if let Expression::NumericLiteral(_) = &unary.argument {
648-
// ExtractResult::Remove
649-
// } else {
650-
// ExtractResult::Maintain
651-
// }
652-
// }
653-
// _ => name
654-
// .map(|name| {
655-
// ExtractResult::ExtractStyle(vec![ExtractStyleProp::Static(Dynamic(
656-
// ExtractDynamicStyle::new(
657-
// name,
658-
// level,
659-
// expression_to_code(&mem.expression).as_str(),
660-
// selector.map(|s| s.into()),
661-
// ),
662-
// ))])
663-
// })
664-
// .unwrap_or_else(|| ExtractResult::Maintain),
665-
// };
666622
}
667623
Expression::ObjectExpression(obj) => {
668624
if obj.properties.is_empty() {
@@ -739,77 +695,6 @@ fn extract_style_from_member_expression<'a>(
739695
expression: mem_expression.clone_in(ast_builder.allocator),
740696
map,
741697
});
742-
743-
// for p in obj.properties.iter_mut() {
744-
// if let ObjectPropertyKind::ObjectProperty(ref mut o) = p {
745-
// if let PropertyKey::StaticIdentifier(_) = o.key {
746-
// if let Expression::StringLiteral(str) = &mut o.value {
747-
// if let Some(rest) = str.value.strip_prefix("$") {
748-
// str.value = ast_builder.atom(&format!("var(--{})", rest));
749-
// }
750-
// } else if let Expression::TemplateLiteral(tmp) = &mut o.value {
751-
// if tmp.quasis.len() == 1 {
752-
// if let Some(rest) = tmp.quasis[0].value.raw.strip_prefix("$") {
753-
// tmp.quasis[0].value.raw =
754-
// ast_builder.atom(&format!("var(--{})", rest));
755-
// }
756-
// }
757-
// }
758-
// }
759-
// }
760-
// }
761-
// match mem_expression {
762-
// Expression::StringLiteral(str) => {
763-
// let key = str.value.as_str();
764-
// for p in obj.properties.iter() {
765-
// match p {
766-
// ObjectPropertyKind::ObjectProperty(o) => {
767-
// if let PropertyKey::StaticIdentifier(ident) = &o.key {
768-
// if ident.name == key {
769-
// return extract_style_from_expression(
770-
// ast_builder,
771-
// name,
772-
// &mut o.value.clone_in(ast_builder.allocator),
773-
// level,
774-
// selector,
775-
// );
776-
// }
777-
// }
778-
// }
779-
// ObjectPropertyKind::SpreadProperty(_) => {
780-
// if let Some(name) = name {
781-
// return ExtractResult::ExtractStyle(vec![
782-
// ExtractStyleProp::Static(Dynamic(
783-
// ExtractDynamicStyle::new(
784-
// name,
785-
// level,
786-
// expression_to_code(&mem.expression).as_str(),
787-
// selector.map(|s| s.into()),
788-
// ),
789-
// )),
790-
// ]);
791-
// }
792-
// }
793-
// }
794-
// }
795-
// ExtractResult::Remove
796-
// }
797-
// Expression::Identifier(_) => {
798-
// if let Some(name) = name {
799-
// ExtractResult::ExtractStyle(vec![ExtractStyleProp::Static(Dynamic(
800-
// ExtractDynamicStyle::new(
801-
// name,
802-
// level,
803-
// expression_to_code(&mem.expression).as_str(),
804-
// selector.map(|s| s.into()),
805-
// ),
806-
// ))])
807-
// } else {
808-
// ExtractResult::Maintain
809-
// }
810-
// }
811-
// _ => ExtractResult::Maintain,
812-
// }
813698
}
814699
Expression::Identifier(_) => {
815700
if let Some(name) = name {

0 commit comments

Comments
 (0)