Skip to content

Commit b9b94c5

Browse files
committed
Minify css
Add Grid
1 parent 4414dab commit b9b94c5

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

.changeset/cyan-houses-visit.md

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+
Add grid, minify css

libs/css/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ pub fn sheet_to_classname(
123123
})
124124
}
125125

126+
pub fn css_to_classname(css: &str) -> String {
127+
let mut map = GLOBAL_CLASS_MAP.lock().unwrap();
128+
map.get(css).map(|v| format!("d{}", v)).unwrap_or_else(|| {
129+
let len = map.len();
130+
map.insert(css.to_string(), len as i32);
131+
format!("d{}", map.len() - 1)
132+
})
133+
}
134+
126135
pub fn sheet_to_variable_name(property: &str, level: u8, selector: Option<&str>) -> String {
127136
let key = format!("{}-{}-{}", property, level, selector.unwrap_or(""));
128137
let mut map = GLOBAL_CLASS_MAP.lock().unwrap();

libs/extractor/src/component.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub enum ExportVariableKind {
1212
VStack,
1313
Center,
1414
Image,
15+
Grid,
1516
}
1617

1718
impl ExportVariableKind {
@@ -20,6 +21,7 @@ impl ExportVariableKind {
2021
match self {
2122
ExportVariableKind::Center
2223
| ExportVariableKind::VStack
24+
| ExportVariableKind::Grid
2325
| ExportVariableKind::Flex
2426
| ExportVariableKind::Box => Ok("div"),
2527
ExportVariableKind::Text => Ok("span"),
@@ -82,6 +84,12 @@ impl ExportVariableKind {
8284
}),
8385
]
8486
}
87+
ExportVariableKind::Grid => vec![Static(ExtractStaticStyle {
88+
value: "grid".to_string(),
89+
property: "display".to_string(),
90+
level: 0,
91+
selector: None,
92+
})],
8593
}
8694
}
8795
}
@@ -99,6 +107,7 @@ impl TryFrom<String> for ExportVariableKind {
99107
"Flex" => Ok(ExportVariableKind::Flex),
100108
"VStack" => Ok(ExportVariableKind::VStack),
101109
"Center" => Ok(ExportVariableKind::Center),
110+
"Grid" => Ok(ExportVariableKind::Grid),
102111
_ => Err(()),
103112
}
104113
}
@@ -142,6 +151,10 @@ mod tests {
142151
ExportVariableKind::try_from("Center".to_string()),
143152
Ok(ExportVariableKind::Center)
144153
);
154+
assert_eq!(
155+
ExportVariableKind::try_from("Grid".to_string()),
156+
Ok(ExportVariableKind::Grid)
157+
);
145158
assert!(ExportVariableKind::try_from("css".to_string()).is_err());
146159
assert!(ExportVariableKind::try_from("foo".to_string()).is_err());
147160
}
@@ -156,6 +169,7 @@ mod tests {
156169
assert_eq!(ExportVariableKind::Flex.to_tag(), Ok("div"));
157170
assert_eq!(ExportVariableKind::VStack.to_tag(), Ok("div"));
158171
assert_eq!(ExportVariableKind::Center.to_tag(), Ok("div"));
172+
assert_eq!(ExportVariableKind::Grid.to_tag(), Ok("div"));
159173
}
160174

161175
#[test]
@@ -214,5 +228,14 @@ mod tests {
214228
})
215229
]
216230
);
231+
assert_eq!(
232+
ExportVariableKind::Grid.extract(),
233+
vec![Static(ExtractStaticStyle {
234+
value: "grid".to_string(),
235+
property: "display".to_string(),
236+
level: 0,
237+
selector: None,
238+
})]
239+
);
217240
}
218241
}

libs/extractor/src/lib.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ use oxc_codegen::Codegen;
1111

1212
use crate::utils::convert_value;
1313
use crate::visit::DevupVisitor;
14-
use css::{sheet_to_classname, sheet_to_variable_name};
14+
use css::{css_to_classname, sheet_to_classname, sheet_to_variable_name};
1515
use oxc_allocator::Allocator;
1616
use oxc_ast::ast::Expression;
1717
use oxc_ast::VisitMut;
1818
use oxc_parser::{Parser, ParserReturn};
1919
use oxc_span::SourceType;
2020
use std::error::Error;
21-
use std::hash::{DefaultHasher, Hasher};
2221

2322
/// result of extracting style properties from props
2423
#[derive(Debug)]
@@ -109,9 +108,7 @@ pub struct ExtractCss {
109108
impl ExtractStyleProperty for ExtractCss {
110109
/// hashing css code to class name
111110
fn extract(&self) -> StyleProperty {
112-
let mut hasher = DefaultHasher::new();
113-
hasher.write(self.css.as_bytes());
114-
StyleProperty::ClassName(format!("d{}", hasher.finish()))
111+
StyleProperty::ClassName(css_to_classname(self.css.as_str()))
115112
}
116113
}
117114

@@ -234,7 +231,6 @@ mod tests {
234231
use css::reset_class_map;
235232
use insta::assert_debug_snapshot;
236233
use serial_test::serial;
237-
use std::hash::{DefaultHasher, Hasher};
238234

239235
#[test]
240236
#[serial]
@@ -842,8 +838,6 @@ mod tests {
842838
#[test]
843839
#[serial]
844840
fn extract_static_css_class_name_props() {
845-
let mut hasher = DefaultHasher::new();
846-
hasher.write("background-color: red;".as_bytes());
847841
reset_class_map();
848842
assert_debug_snapshot!(extract(
849843
"test.tsx",

libs/extractor/src/snapshots/extractor__tests__extract_static_css_class_name_props.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ ExtractOutput {
1010
},
1111
),
1212
],
13-
code: "import \"@devup-ui/core/devup-ui.css\";\nimport { css } from \"@devup-ui/core\";\n<Box className={css`d10128267434031712411`} />;\n",
13+
code: "import \"@devup-ui/core/devup-ui.css\";\nimport { css } from \"@devup-ui/core\";\n<Box className={css`d0`} />;\n",
1414
}

0 commit comments

Comments
 (0)