Skip to content

Commit a5d4cf2

Browse files
authored
Merge pull request #13 from dev-five-git/minify-css-add-grid
Minify css
2 parents 4414dab + 242a6c2 commit a5d4cf2

File tree

11 files changed

+90
-35
lines changed

11 files changed

+90
-35
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

.changeset/mean-zebras-raise.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@devup-ui/webpack-plugin": patch
3+
"@devup-ui/wasm": patch
4+
"@devup-ui/next-plugin": patch
5+
"@devup-ui/vite-plugin": patch
6+
"@devup-ui/react": patch
7+
---
8+
9+
Update package

bindings/devup-ui-wasm/package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
{
22
"name": "@devup-ui/wasm",
33
"version": "0.1.4",
4+
"scripts": {
5+
"build": "wasm-pack build --target nodejs --out-dir ./pkg --out-name index",
6+
"test": "wasm-pack test --node"
7+
},
8+
"sideEffects": false,
9+
"main": "./pkg/index.js",
10+
"module": "./pkg/index.js",
411
"keywords": [
512
"react",
613
"css-in-js",
@@ -22,9 +29,5 @@
2229
"types": "./pkg/index.d.ts"
2330
}
2431
},
25-
"types": "./pkg/index.d.ts",
26-
"scripts": {
27-
"build": "wasm-pack build --target nodejs --out-dir ./pkg --out-name index",
28-
"test": "wasm-pack test --node"
29-
}
32+
"types": "./pkg/index.d.ts"
3033
}

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
}

packages/next-plugin/package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
{
22
"name": "@devup-ui/next-plugin",
3-
"version": "0.1.6",
43
"type": "module",
5-
"dependencies": {
6-
"@devup-ui/webpack-plugin": "workspace:*",
7-
"next": "^15.1"
8-
},
4+
"version": "0.1.6",
95
"scripts": {
106
"lint": "eslint",
117
"build": "tsc && vite build"
128
},
9+
"sideEffects": false,
10+
"main": "./dist/index.cjs",
11+
"module": "./dist/index.js",
1312
"exports": {
1413
".": {
1514
"import": "./dist/index.js",
@@ -20,6 +19,10 @@
2019
"dist"
2120
],
2221
"types": "./dist/index.d.ts",
22+
"dependencies": {
23+
"@devup-ui/webpack-plugin": "workspace:*",
24+
"next": "^15.1"
25+
},
2326
"devDependencies": {
2427
"vite": "^6.0.7",
2528
"vite-plugin-dts": "^4.4.0",

packages/react/package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
"name": "@devup-ui/react",
33
"version": "0.1.1",
44
"type": "module",
5-
"dependencies": {
6-
"react": "^19.0",
7-
"csstype": "^3.1"
8-
},
95
"scripts": {
106
"lint": "eslint",
117
"build": "vite build"
128
},
9+
"sideEffects": false,
10+
"main": "./dist/index.cjs",
11+
"module": "./dist/index.js",
1312
"exports": {
1413
".": {
1514
"import": "./dist/index.js",
@@ -20,6 +19,10 @@
2019
"dist"
2120
],
2221
"types": "./dist/index.d.ts",
22+
"dependencies": {
23+
"react": "^19.0",
24+
"csstype": "^3.1"
25+
},
2326
"devDependencies": {
2427
"vite": "^6.0.7",
2528
"vite-plugin-dts": "^4.4.0",

packages/vite-plugin/package.json

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66
"lint": "eslint",
77
"build": "tsc && vite build"
88
},
9-
"dependencies": {
10-
"@devup-ui/wasm": "workspace:*"
11-
},
12-
"devDependencies": {
13-
"vite-plugin-dts": "^4.5.0",
14-
"typescript": "^5.7.2"
15-
},
9+
"sideEffects": false,
10+
"main": "./dist/index.cjs",
11+
"module": "./dist/index.js",
12+
"types": "./dist/index.d.ts",
1613
"exports": {
1714
".": {
1815
"import": "./dist/index.js",
@@ -22,7 +19,13 @@
2219
"files": [
2320
"dist"
2421
],
25-
"types": "./dist/index.d.ts",
22+
"dependencies": {
23+
"@devup-ui/wasm": "workspace:*"
24+
},
25+
"devDependencies": {
26+
"vite-plugin-dts": "^4.5.0",
27+
"typescript": "^5.7.2"
28+
},
2629
"peerDependencies": {
2730
"vite": "*"
2831
}

0 commit comments

Comments
 (0)