use lightningcss::printer::PrinterOptions;
use lightningcss::stylesheet::{ParserFlags, ParserOptions, StyleSheet};
use lightningcss::targets::Targets;
use vize_carton::{String, ToCompactString};
pub(crate) fn version_to_u32(major: u32) -> u32 {
major << 16
}
pub(crate) fn compile_css_internal(
css: &str,
filename: &str,
minify: bool,
targets: Targets,
custom_media: bool,
) -> (String, Vec<String>) {
let mut flags = ParserFlags::NESTING | ParserFlags::DEEP_SELECTOR_COMBINATOR;
if custom_media {
flags |= ParserFlags::CUSTOM_MEDIA;
}
let parser_options = ParserOptions {
filename: filename.into(),
flags,
..Default::default()
};
let mut stylesheet = match StyleSheet::parse(css, parser_options) {
Ok(ss) => ss,
Err(e) => {
let mut errors = Vec::with_capacity(1);
let mut message = String::from("CSS parse error: ");
message.push_str(&e.to_compact_string());
errors.push(message);
return (css.to_compact_string(), errors);
}
};
if minify {
if let Err(e) = stylesheet.minify(lightningcss::stylesheet::MinifyOptions {
targets,
..Default::default()
}) {
let mut errors = Vec::with_capacity(1);
let mut message = String::from("CSS minify error: ");
use std::fmt::Write as _;
let _ = write!(&mut message, "{:?}", e);
errors.push(message);
return (css.to_compact_string(), errors);
}
}
let printer_options = PrinterOptions {
minify,
targets,
..Default::default()
};
match stylesheet.to_css(printer_options) {
Ok(result) => (result.code.into(), vec![]),
Err(e) => {
let mut errors = Vec::with_capacity(1);
let mut message = String::from("CSS print error: ");
use std::fmt::Write as _;
let _ = write!(&mut message, "{:?}", e);
errors.push(message);
(css.to_compact_string(), errors)
}
}
}