Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/grumpy-heads-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@devup-ui/wasm": patch
---

Optimize aspect-ratio by gcd
17 changes: 15 additions & 2 deletions libs/extractor/src/extract_style/extract_static_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
extract_style::{
ExtractStyleProperty, constant::MAINTAIN_VALUE_PROPERTIES, style_property::StyleProperty,
},
utils::convert_value,
utils::{convert_value, gcd},
};

#[derive(Debug, PartialEq, Clone, Eq, Hash, Ord, PartialOrd)]
Expand All @@ -30,7 +30,20 @@ impl ExtractStaticStyle {
pub fn new(property: &str, value: &str, level: u8, selector: Option<StyleSelector>) -> Self {
Self {
value: optimize_value(&if MAINTAIN_VALUE_PROPERTIES.contains(property) {
value.to_string()
if property == "aspect-ratio" && value.contains("/") {
if let [Ok(a), Ok(b)] = value
.split('/')
.map(|v| v.trim().parse::<u32>())
.collect::<Vec<_>>()[..]
{
let gcd = gcd(a, b);
format!("{}/{}", a / gcd, b / gcd)
} else {
value.to_string()
}
} else {
value.to_string()
}
} else {
convert_value(value)
}),
Expand Down
49 changes: 49 additions & 0 deletions libs/extractor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2962,6 +2962,55 @@ e(o, { className: "a", bg: variable, style: { color: "blue" }, ...props })
));
}

#[test]
#[serial]
fn optimize_aspect_ratio() {
reset_class_map();
assert_debug_snapshot!(ToBTreeSet::from(
extract(
"test.jsx",
r#"import {Flex} from '@devup-ui/core'
<Flex aspectRatio={"200/400"} />
"#,
ExtractOption {
package: "@devup-ui/core".to_string(),
css_file: None
}
)
.unwrap()
));

reset_class_map();
assert_debug_snapshot!(ToBTreeSet::from(
extract(
"test.jsx",
r#"import {Flex} from '@devup-ui/core'
<Flex aspectRatio={" 200 / 400 "} />
"#,
ExtractOption {
package: "@devup-ui/core".to_string(),
css_file: None
}
)
.unwrap()
));

reset_class_map();
assert_debug_snapshot!(ToBTreeSet::from(
extract(
"test.jsx",
r#"import {Flex} from '@devup-ui/core'
<Flex aspectRatio={" 200.2 / 400.2 "} />
"#,
ExtractOption {
package: "@devup-ui/core".to_string(),
css_file: None
}
)
.unwrap()
));
}

#[test]
#[serial]
fn ternary_operator_in_selector() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
source: libs/extractor/src/lib.rs
expression: "ToBTreeSet::from(extract(\"test.jsx\",\nr#\"import {Flex} from '@devup-ui/core'\n <Flex aspectRatio={\" 200 / 400 \"} />\n \"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap())"
---
ToBTreeSet {
styles: {
Static(
ExtractStaticStyle {
property: "aspect-ratio",
value: "1/2",
level: 0,
selector: None,
style_order: None,
},
),
Static(
ExtractStaticStyle {
property: "display",
value: "flex",
level: 0,
selector: None,
style_order: Some(
0,
),
},
),
},
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className=\"d0 d1\" />;\n",
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
source: libs/extractor/src/lib.rs
expression: "ToBTreeSet::from(extract(\"test.jsx\",\nr#\"import {Flex} from '@devup-ui/core'\n <Flex aspectRatio={\" 200.2 / 400.2 \"} />\n \"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap())"
---
ToBTreeSet {
styles: {
Static(
ExtractStaticStyle {
property: "aspect-ratio",
value: "200.2 / 400.2",
level: 0,
selector: None,
style_order: None,
},
),
Static(
ExtractStaticStyle {
property: "display",
value: "flex",
level: 0,
selector: None,
style_order: Some(
0,
),
},
),
},
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className=\"d0 d1\" />;\n",
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
source: libs/extractor/src/lib.rs
expression: "ToBTreeSet::from(extract(\"test.jsx\",\nr#\"import {Flex} from '@devup-ui/core'\n <Flex aspectRatio={\"200/400\"} />\n \"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap())"
---
ToBTreeSet {
styles: {
Static(
ExtractStaticStyle {
property: "aspect-ratio",
value: "1/2",
level: 0,
selector: None,
style_order: None,
},
),
Static(
ExtractStaticStyle {
property: "display",
value: "flex",
level: 0,
selector: None,
style_order: Some(
0,
),
},
),
},
code: "import \"@devup-ui/core/devup-ui.css\";\n<div className=\"d0 d1\" />;\n",
}
4 changes: 4 additions & 0 deletions libs/extractor/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ pub(super) fn get_string_by_literal_expression(expr: &Expression) -> Option<Stri
})
}

pub fn gcd(a: u32, b: u32) -> u32 {
if b == 0 { a } else { gcd(b, a % b) }
}

#[cfg(test)]
mod tests {
use oxc_allocator::Vec;
Expand Down