Skip to content

Commit 5156349

Browse files
authored
Merge pull request #123 from dev-five-git/add-debug-mode
Implement debug mode
2 parents 826cf40 + 11c4257 commit 5156349

File tree

6 files changed

+236
-33
lines changed

6 files changed

+236
-33
lines changed

.changeset/few-items-yell.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@devup-ui/webpack-plugin": patch
3+
"@devup-ui/wasm": patch
4+
"@devup-ui/vite-plugin": patch
5+
---
6+
7+
Implement debug mode

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ impl Output {
8686
}
8787
}
8888

89+
#[wasm_bindgen(js_name = "setDebug")]
90+
pub fn set_debug(debug: bool) {
91+
css::set_debug(debug);
92+
}
93+
94+
#[wasm_bindgen(js_name = "isDebug")]
95+
pub fn is_debug() {
96+
css::is_debug();
97+
}
98+
8999
#[wasm_bindgen(js_name = "importSheet")]
90100
pub fn import_sheet(sheet_object: JsValue) -> Result<(), JsValue> {
91101
*GLOBAL_STYLE_SHEET.lock().unwrap() = serde_wasm_bindgen::from_value(sheet_object)

libs/css/src/lib.rs

Lines changed: 205 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::cmp::Ordering;
55
use std::collections::{HashMap, HashSet};
66
use std::fmt;
77
use std::fmt::{Display, Formatter};
8+
use std::hash::{DefaultHasher, Hash, Hasher};
89
use std::sync::Mutex;
910

1011
static SELECTOR_ORDER_MAP: Lazy<HashMap<String, u8>> = Lazy::new(|| {
@@ -17,6 +18,17 @@ static SELECTOR_ORDER_MAP: Lazy<HashMap<String, u8>> = Lazy::new(|| {
1718
map
1819
});
1920

21+
static DEBUG: Lazy<Mutex<bool>> = Lazy::new(|| Mutex::new(false));
22+
23+
pub fn set_debug(value: bool) {
24+
let mut debug = DEBUG.lock().unwrap();
25+
*debug = value;
26+
}
27+
28+
pub fn is_debug() -> bool {
29+
*DEBUG.lock().unwrap()
30+
}
31+
2032
#[derive(Debug, PartialEq, Clone, Hash, Eq, Serialize, Deserialize)]
2133
pub enum StyleSelector {
2234
Media(String),
@@ -327,52 +339,102 @@ pub fn sheet_to_classname(
327339
selector: Option<&str>,
328340
style_order: Option<u8>,
329341
) -> String {
330-
let key = format!(
331-
"{}-{}-{}-{}-{}",
332-
property.trim(),
333-
level,
334-
F_SPACE_RE.replace_all(value.unwrap_or(""), ",").trim(),
335-
selector.unwrap_or("").trim(),
336-
style_order.unwrap_or(255)
337-
);
338-
let mut map = GLOBAL_CLASS_MAP.lock().unwrap();
339-
map.get(&key).map(|v| format!("d{}", v)).unwrap_or_else(|| {
340-
let len = map.len();
341-
map.insert(key, len as i32);
342-
format!("d{}", map.len() - 1)
343-
})
342+
if *DEBUG.lock().unwrap() {
343+
let selector = selector.unwrap_or("").trim();
344+
format!(
345+
"{}-{}-{}-{}-{}",
346+
property.trim(),
347+
level,
348+
F_SPACE_RE.replace_all(value.unwrap_or(""), ",").trim(),
349+
if selector.is_empty() {
350+
"".to_string()
351+
} else {
352+
let mut hasher = DefaultHasher::new();
353+
selector.hash(&mut hasher);
354+
hasher.finish().to_string()
355+
},
356+
style_order.unwrap_or(255)
357+
)
358+
} else {
359+
let key = format!(
360+
"{}-{}-{}-{}-{}",
361+
property.trim(),
362+
level,
363+
F_SPACE_RE.replace_all(value.unwrap_or(""), ",").trim(),
364+
selector.unwrap_or("").trim(),
365+
style_order.unwrap_or(255)
366+
);
367+
let mut map = GLOBAL_CLASS_MAP.lock().unwrap();
368+
map.get(&key).map(|v| format!("d{}", v)).unwrap_or_else(|| {
369+
let len = map.len();
370+
map.insert(key, len as i32);
371+
format!("d{}", map.len() - 1)
372+
})
373+
}
344374
}
345375

346376
pub fn css_to_classname(css: &str) -> String {
347-
let mut map = GLOBAL_CLASS_MAP.lock().unwrap();
348-
map.get(css).map(|v| format!("d{}", v)).unwrap_or_else(|| {
349-
let len = map.len();
350-
map.insert(css.to_string(), len as i32);
351-
format!("d{}", map.len() - 1)
352-
})
377+
if *DEBUG.lock().unwrap() {
378+
let mut hasher = DefaultHasher::new();
379+
css.hash(&mut hasher);
380+
format!("css-{}", hasher.finish())
381+
} else {
382+
let mut map = GLOBAL_CLASS_MAP.lock().unwrap();
383+
map.get(css).map(|v| format!("d{}", v)).unwrap_or_else(|| {
384+
let len = map.len();
385+
map.insert(css.to_string(), len as i32);
386+
format!("d{}", map.len() - 1)
387+
})
388+
}
353389
}
354390

355391
pub fn sheet_to_variable_name(property: &str, level: u8, selector: Option<&str>) -> String {
356-
let key = format!("{}-{}-{}", property, level, selector.unwrap_or(""));
357-
let mut map = GLOBAL_CLASS_MAP.lock().unwrap();
358-
map.get(&key)
359-
.map(|v| format!("--d{}", v))
360-
.unwrap_or_else(|| {
361-
let len = map.len();
362-
map.insert(key, len as i32);
363-
format!("--d{}", map.len() - 1)
364-
})
392+
if *DEBUG.lock().unwrap() {
393+
let selector = selector.unwrap_or("").trim();
394+
format!(
395+
"--{}-{}-{}",
396+
property,
397+
level,
398+
if selector.is_empty() {
399+
"".to_string()
400+
} else {
401+
let mut hasher = DefaultHasher::new();
402+
selector.hash(&mut hasher);
403+
hasher.finish().to_string()
404+
}
405+
)
406+
} else {
407+
let key = format!("{}-{}-{}", property, level, selector.unwrap_or("").trim());
408+
let mut map = GLOBAL_CLASS_MAP.lock().unwrap();
409+
map.get(&key)
410+
.map(|v| format!("--d{}", v))
411+
.unwrap_or_else(|| {
412+
let len = map.len();
413+
map.insert(key, len as i32);
414+
format!("--d{}", map.len() - 1)
415+
})
416+
}
365417
}
366418

367419
#[cfg(test)]
368420
mod tests {
369421
use super::*;
370422
use serial_test::serial;
371423

424+
#[test]
425+
#[serial]
426+
fn test_set_debug() {
427+
set_debug(true);
428+
assert!(is_debug());
429+
set_debug(false);
430+
assert!(!is_debug());
431+
}
432+
372433
#[test]
373434
#[serial]
374435
fn test_sheet_to_variable_name() {
375436
reset_class_map();
437+
set_debug(false);
376438
assert_eq!(sheet_to_variable_name("background", 0, None), "--d0");
377439
assert_eq!(
378440
sheet_to_variable_name("background", 0, Some("hover")),
@@ -385,18 +447,44 @@ mod tests {
385447
);
386448
}
387449

450+
#[test]
451+
#[serial]
452+
fn test_debug_sheet_to_variable_name() {
453+
set_debug(true);
454+
assert_eq!(
455+
sheet_to_variable_name("background", 0, None),
456+
"--background-0-"
457+
);
458+
assert_eq!(
459+
sheet_to_variable_name("background", 0, Some("hover".into())),
460+
"--background-0-12448419602614487988"
461+
);
462+
assert_eq!(
463+
sheet_to_variable_name("background", 1, None),
464+
"--background-1-"
465+
);
466+
assert_eq!(
467+
sheet_to_variable_name("background", 1, Some("hover".into())),
468+
"--background-1-12448419602614487988"
469+
);
470+
}
471+
388472
#[test]
389473
#[serial]
390474
fn test_sheet_to_classname() {
475+
set_debug(false);
391476
reset_class_map();
392-
assert_eq!(sheet_to_classname("background", 0, None, None, None), "d0");
393477
assert_eq!(
394-
sheet_to_classname("background", 0, Some("hover"), None, None),
478+
sheet_to_classname("background", 0, Some("red"), None, None),
479+
"d0"
480+
);
481+
assert_eq!(
482+
sheet_to_classname("background", 0, Some("red"), Some("hover"), None),
395483
"d1"
396484
);
397485
assert_eq!(sheet_to_classname("background", 1, None, None, None), "d2");
398486
assert_eq!(
399-
sheet_to_classname("background", 1, Some("hover"), None, None),
487+
sheet_to_classname("background", 1, None, Some("hover"), None),
400488
"d3"
401489
);
402490

@@ -438,6 +526,56 @@ mod tests {
438526
);
439527
}
440528

529+
#[test]
530+
#[serial]
531+
fn test_debug_sheet_to_classname() {
532+
set_debug(true);
533+
assert_eq!(
534+
sheet_to_classname("background", 0, None, None, None),
535+
"background-0---255"
536+
);
537+
assert_eq!(
538+
sheet_to_classname("background", 0, Some("red"), Some("hover"), None),
539+
"background-0-red-12448419602614487988-255"
540+
);
541+
assert_eq!(
542+
sheet_to_classname("background", 1, None, None, None),
543+
"background-1---255"
544+
);
545+
assert_eq!(
546+
sheet_to_classname("background", 1, Some("red"), Some("hover"), None),
547+
"background-1-red-12448419602614487988-255"
548+
);
549+
}
550+
551+
#[test]
552+
#[serial]
553+
fn test_css_to_classname() {
554+
set_debug(false);
555+
reset_class_map();
556+
assert_eq!(css_to_classname("background: red"), "d0");
557+
assert_eq!(css_to_classname("background: blue"), "d1");
558+
}
559+
#[test]
560+
#[serial]
561+
fn test_debug_css_to_classname() {
562+
set_debug(true);
563+
assert_eq!(
564+
css_to_classname("background: red"),
565+
"css-10773204219957113694"
566+
);
567+
assert_eq!(
568+
css_to_classname("background: blue"),
569+
"css-1226995032436176700"
570+
);
571+
set_debug(true);
572+
reset_class_map();
573+
assert_eq!(
574+
css_to_classname("background: red"),
575+
"css-10773204219957113694"
576+
);
577+
}
578+
441579
#[test]
442580
fn test_convert_property() {
443581
assert_eq!(
@@ -726,6 +864,41 @@ mod tests {
726864
":root[data-theme=dark] .cls:hover"
727865
);
728866
}
867+
#[test]
868+
fn test_get_selector_separator() {
869+
assert!(matches!(
870+
get_selector_separator("placeholder"),
871+
SelectorSeparator::Double
872+
));
873+
assert!(matches!(
874+
get_selector_separator("before"),
875+
SelectorSeparator::Double
876+
));
877+
assert!(matches!(
878+
get_selector_separator("after"),
879+
SelectorSeparator::Double
880+
));
881+
882+
assert!(matches!(
883+
get_selector_separator("hover"),
884+
SelectorSeparator::Single
885+
));
886+
887+
assert!(matches!(
888+
get_selector_separator(":hover"),
889+
SelectorSeparator::None
890+
));
891+
892+
assert!(matches!(
893+
get_selector_separator("::placeholder"),
894+
SelectorSeparator::None
895+
));
896+
897+
assert!(matches!(
898+
get_selector_separator("[aria-disabled='true']"),
899+
SelectorSeparator::None
900+
));
901+
}
729902

730903
#[test]
731904
#[serial]

packages/vite-plugin/src/plugin.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
getCss,
88
getThemeInterface,
99
registerTheme,
10+
setDebug,
1011
} from '@devup-ui/wasm'
1112
import { type PluginOption } from 'vite'
1213

@@ -19,9 +20,12 @@ export interface DevupUIPluginOptions {
1920
devupPath: string
2021
interfacePath: string
2122
extractCss: boolean
23+
debug: boolean
2224
}
2325

24-
function writeDataFiles(options: Omit<DevupUIPluginOptions, 'extractCss'>) {
26+
function writeDataFiles(
27+
options: Omit<DevupUIPluginOptions, 'extractCss' | 'debug'>,
28+
) {
2529
registerTheme(JSON.parse(readFileSync(options.devupPath, 'utf-8'))?.['theme'])
2630
const interfaceCode = getThemeInterface(
2731
options.package,
@@ -46,7 +50,9 @@ export function DevupUI({
4650
devupPath = 'devup.json',
4751
interfacePath = '.df',
4852
extractCss = true,
53+
debug = false,
4954
}: Partial<DevupUIPluginOptions> = {}): PluginOption {
55+
setDebug(debug)
5056
if (existsSync(devupPath)) {
5157
try {
5258
writeDataFiles({

packages/webpack-plugin/src/__tests__/plugin.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ describe('devupUIPlugin', () => {
3333
devupPath: 'devup.json',
3434
interfacePath: '.df',
3535
watch: false,
36+
debug: false,
3637
})
3738
})
3839

@@ -52,6 +53,7 @@ describe('devupUIPlugin', () => {
5253
devupPath: 'new-devup-path',
5354
interfacePath: 'new-interface-path',
5455
watch: false,
56+
debug: false,
5557
})
5658
})
5759

0 commit comments

Comments
 (0)