Skip to content

Commit 2247d57

Browse files
committed
Impl split css
1 parent 45d4a1b commit 2247d57

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1849
-875
lines changed

.changeset/modern-kids-obey.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+
Optimize keyframe name

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/landing/next.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export default withMDX(
1515
pageExtensions: ['js', 'jsx', 'md', 'mdx', 'ts', 'tsx'],
1616
output: 'export',
1717
},
18-
{ include: ['@devup-ui/components', '@devup-ui/reset-css'] },
18+
{
19+
include: ['@devup-ui/components', '@devup-ui/reset-css'],
20+
},
1921
),
2022
)

bindings/devup-ui-wasm/src/lib.rs

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use css::class_map::{get_class_map, set_class_map};
2+
use css::file_map::{get_file_map, get_filename_by_file_num};
23
use extractor::extract_style::ExtractStyleProperty;
34
use extractor::extract_style::extract_style_value::ExtractStyleValue;
45
use extractor::extract_style::style_property::StyleProperty;
@@ -18,6 +19,9 @@ pub struct Output {
1819
styles: HashSet<ExtractStyleValue>,
1920
map: Option<String>,
2021
default_collected: bool,
22+
split_css: bool,
23+
filename: String,
24+
css_file: String,
2125
}
2226
#[wasm_bindgen]
2327
extern "C" {
@@ -39,6 +43,11 @@ impl Output {
3943
self.code.clone()
4044
}
4145

46+
#[wasm_bindgen(getter)]
47+
pub fn css_file(&self) -> String {
48+
self.css_file.clone()
49+
}
50+
4251
#[wasm_bindgen(getter)]
4352
pub fn map(&self) -> Option<String> {
4453
self.map.clone()
@@ -52,7 +61,11 @@ impl Output {
5261
for style in self.styles.iter() {
5362
match style {
5463
ExtractStyleValue::Static(st) => {
55-
let (cls, _) = match style.extract() {
64+
let (cls, _) = match style.extract(if self.split_css {
65+
Some(self.filename.as_str())
66+
} else {
67+
None
68+
}) {
5669
Some(StyleProperty::ClassName(cls)) => (cls, None),
5770
Some(StyleProperty::Variable {
5871
class_name,
@@ -68,12 +81,21 @@ impl Output {
6881
st.value(),
6982
st.selector(),
7083
st.style_order(),
84+
if self.split_css {
85+
Some(self.filename.as_str())
86+
} else {
87+
None
88+
},
7189
) {
7290
collected = true;
7391
}
7492
}
7593
ExtractStyleValue::Dynamic(dy) => {
76-
let (cls, variable) = match style.extract() {
94+
let (cls, variable) = match style.extract(if self.split_css {
95+
Some(self.filename.as_str())
96+
} else {
97+
None
98+
}) {
7799
Some(StyleProperty::ClassName(cls)) => (cls, None),
78100
Some(StyleProperty::Variable {
79101
class_name,
@@ -89,14 +111,25 @@ impl Output {
89111
&format!("var({})", variable.unwrap()),
90112
dy.selector(),
91113
dy.style_order(),
114+
if self.split_css {
115+
Some(self.filename.as_str())
116+
} else {
117+
None
118+
},
92119
) {
93120
collected = true;
94121
}
95122
}
96123

97124
ExtractStyleValue::Keyframes(keyframes) => {
98125
if sheet.add_keyframes(
99-
&keyframes.extract().to_string(),
126+
&keyframes
127+
.extract(if self.split_css {
128+
Some(self.filename.as_str())
129+
} else {
130+
None
131+
})
132+
.to_string(),
100133
keyframes
101134
.keyframes
102135
.iter()
@@ -115,6 +148,11 @@ impl Output {
115148
)
116149
})
117150
.collect(),
151+
if self.split_css {
152+
Some(self.filename.as_str())
153+
} else {
154+
None
155+
},
118156
) {
119157
collected = true;
120158
}
@@ -138,7 +176,11 @@ impl Output {
138176
return None;
139177
}
140178

141-
Some(sheet.create_css())
179+
Some(sheet.create_css(if self.split_css {
180+
Some(self.filename.as_str())
181+
} else {
182+
None
183+
}))
142184
}
143185
}
144186

@@ -179,12 +221,27 @@ pub fn export_class_map() -> Result<String, JsValue> {
179221
serde_json::to_string(&get_class_map()).map_err(|e| JsValue::from_str(e.to_string().as_str()))
180222
}
181223

224+
#[wasm_bindgen(js_name = "importFileMap")]
225+
pub fn import_file_map(sheet_object: JsValue) -> Result<(), JsValue> {
226+
set_class_map(
227+
serde_wasm_bindgen::from_value(sheet_object)
228+
.map_err(|e| JsValue::from_str(e.to_string().as_str()))?,
229+
);
230+
Ok(())
231+
}
232+
233+
#[wasm_bindgen(js_name = "exportFileMap")]
234+
pub fn export_file_map() -> Result<String, JsValue> {
235+
serde_json::to_string(&get_file_map()).map_err(|e| JsValue::from_str(e.to_string().as_str()))
236+
}
237+
182238
#[wasm_bindgen(js_name = "codeExtract")]
183239
pub fn code_extract(
184240
filename: &str,
185241
code: &str,
186242
package: &str,
187-
css_file: &str,
243+
css_dir: String,
244+
split_css: bool,
188245
) -> Result<Output, JsValue> {
189246
let mut sheet = GLOBAL_STYLE_SHEET.lock().unwrap();
190247

@@ -193,14 +250,18 @@ pub fn code_extract(
193250
code,
194251
ExtractOption {
195252
package: package.to_string(),
196-
css_file: Some(css_file.to_string()),
253+
css_dir,
254+
split_css,
197255
},
198256
) {
199257
Ok(output) => Ok(Output {
200258
code: output.code,
201259
styles: output.styles,
202260
map: output.map,
203261
default_collected: sheet.rm_global_css(filename),
262+
split_css,
263+
filename: filename.to_string(),
264+
css_file: output.css_file,
204265
}),
205266
Err(error) => Err(JsValue::from_str(error.to_string().as_str())),
206267
}
@@ -222,9 +283,9 @@ pub fn get_default_theme() -> Result<Option<String>, JsValue> {
222283
}
223284

224285
#[wasm_bindgen(js_name = "getCss")]
225-
pub fn get_css() -> Result<String, JsValue> {
286+
pub fn get_css(file_num: Option<usize>) -> Result<String, JsValue> {
226287
let sheet = GLOBAL_STYLE_SHEET.lock().unwrap();
227-
Ok(sheet.create_css())
288+
Ok(sheet.create_css(file_num.map(|num| get_filename_by_file_num(num)).as_deref()))
228289
}
229290

230291
#[wasm_bindgen(js_name = "getThemeInterface")]
@@ -259,7 +320,7 @@ mod tests {
259320
let mut sheet = GLOBAL_STYLE_SHEET.lock().unwrap();
260321
*sheet = StyleSheet::default();
261322
}
262-
assert_eq!(get_css().unwrap(), "");
323+
assert_eq!(get_css(None).unwrap(), "");
263324

264325
{
265326
let mut sheet = GLOBAL_STYLE_SHEET.lock().unwrap();
@@ -274,7 +335,7 @@ mod tests {
274335
sheet.set_theme(theme);
275336
}
276337

277-
assert_debug_snapshot!(get_css().unwrap());
338+
assert_debug_snapshot!(get_css(None).unwrap());
278339
}
279340

280341
#[test]

libs/css/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ phf = { version = "0.13", features = ["macros"] }
99
serial_test = "3.2.0"
1010
serde = { version = "1.0.219", features = ["derive"] }
1111
regex = "1.11.2"
12+
bimap = { version = "0.6.3", features = ["serde"] }
1213

1314
[dev-dependencies]
1415
rstest = "0.26.1"

libs/css/src/class_map.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{collections::HashMap, sync::Mutex};
22

33
use once_cell::sync::Lazy;
44

5-
pub(crate) static GLOBAL_CLASS_MAP: Lazy<Mutex<HashMap<String, i32>>> =
5+
pub(crate) static GLOBAL_CLASS_MAP: Lazy<Mutex<HashMap<String, HashMap<String, usize>>>> =
66
Lazy::new(|| Mutex::new(HashMap::new()));
77

88
/// for test
@@ -11,12 +11,12 @@ pub fn reset_class_map() {
1111
map.clear();
1212
}
1313

14-
pub fn set_class_map(map: HashMap<String, i32>) {
14+
pub fn set_class_map(map: HashMap<String, HashMap<String, usize>>) {
1515
let mut global_map = GLOBAL_CLASS_MAP.lock().unwrap();
1616
*global_map = map;
1717
}
1818

19-
pub fn get_class_map() -> HashMap<String, i32> {
19+
pub fn get_class_map() -> HashMap<String, HashMap<String, usize>> {
2020
GLOBAL_CLASS_MAP.lock().unwrap().clone()
2121
}
2222

@@ -30,17 +30,17 @@ mod tests {
3030
#[serial]
3131
fn test_set_and_get_class_map() {
3232
let mut test_map = HashMap::new();
33-
test_map.insert("test-key".to_string(), 42);
33+
test_map.insert("".to_string(), HashMap::new());
3434
set_class_map(test_map.clone());
3535
let got = get_class_map();
36-
assert_eq!(got.get("test-key"), Some(&42));
36+
assert_eq!(got.get(""), Some(&HashMap::new()));
3737
}
3838

3939
#[test]
4040
#[serial]
4141
fn test_reset_class_map() {
4242
let mut test_map = HashMap::new();
43-
test_map.insert("reset-key".to_string(), 1);
43+
test_map.insert("".to_string(), HashMap::new());
4444
set_class_map(test_map);
4545
reset_class_map();
4646
let got = get_class_map();

libs/css/src/constant.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ pub(super) static N_BASE_ARRAY: [char; 27] = [
168168
't', 'u', 'v', 'w', 'x', 'y', 'z', '_',
169169
];
170170

171-
pub(super) static M_BASE_ARRAY: [char; 38] = [
171+
pub(super) static M_BASE_ARRAY: [char; 37] = [
172172
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
173-
't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_',
173+
't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_',
174174
];

libs/css/src/file_map.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use bimap::BiHashMap;
2+
use std::sync::Mutex;
3+
4+
use once_cell::sync::Lazy;
5+
6+
pub(crate) static GLOBAL_FILE_MAP: Lazy<Mutex<BiHashMap<String, usize>>> =
7+
Lazy::new(|| Mutex::new(BiHashMap::new()));
8+
9+
/// for test
10+
pub fn reset_file_map() {
11+
let mut map = GLOBAL_FILE_MAP.lock().unwrap();
12+
map.clear();
13+
}
14+
15+
pub fn set_file_map(map: BiHashMap<String, usize>) {
16+
let mut global_map = GLOBAL_FILE_MAP.lock().unwrap();
17+
*global_map = map;
18+
}
19+
20+
pub fn get_file_map() -> BiHashMap<String, usize> {
21+
GLOBAL_FILE_MAP.lock().unwrap().clone()
22+
}
23+
24+
pub fn get_file_num_by_filename(filename: &str) -> usize {
25+
let mut map = GLOBAL_FILE_MAP.lock().unwrap();
26+
let len = map.len();
27+
if !map.contains_left(filename) {
28+
map.insert(filename.to_string(), len as usize);
29+
}
30+
*map.get_by_left(filename).unwrap()
31+
}
32+
33+
pub fn get_filename_by_file_num(file_num: usize) -> String {
34+
let map = GLOBAL_FILE_MAP.lock().unwrap();
35+
map.get_by_right(&file_num)
36+
.map(|s| s.as_str())
37+
.unwrap_or_default()
38+
.to_string()
39+
}
40+
41+
#[cfg(test)]
42+
mod tests {
43+
use serial_test::serial;
44+
45+
use super::*;
46+
47+
#[test]
48+
#[serial]
49+
fn test_set_and_get_file_map() {
50+
let mut test_map = BiHashMap::new();
51+
test_map.insert("test-key".to_string(), 42);
52+
set_file_map(test_map.clone());
53+
let got = get_file_map();
54+
assert_eq!(got.get_by_left("test-key"), Some(&42));
55+
assert_eq!(got.get_by_right(&42), Some(&"test-key".to_string()));
56+
}
57+
58+
#[test]
59+
#[serial]
60+
fn test_reset_file_map() {
61+
let mut test_map = BiHashMap::new();
62+
test_map.insert("reset-key".to_string(), 1);
63+
set_file_map(test_map);
64+
reset_file_map();
65+
let got = get_file_map();
66+
assert!(got.is_empty());
67+
}
68+
}

0 commit comments

Comments
 (0)