2021-11-27 08:33:33 +03:00
|
|
|
use std::path::PathBuf;
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2021-11-27 08:33:33 +03:00
|
|
|
use swc_css_ast::Stylesheet;
|
|
|
|
use swc_css_codegen::{
|
|
|
|
writer::basic::{BasicCssWriter, BasicCssWriterConfig},
|
|
|
|
CodeGenerator, CodegenConfig, Emit,
|
|
|
|
};
|
|
|
|
use swc_css_minifier::minify;
|
|
|
|
use swc_css_parser::parse_file;
|
|
|
|
use testing::NormalizedOutput;
|
|
|
|
|
2022-02-11 06:33:08 +03:00
|
|
|
#[testing::fixture("tests/fixture/**/input.css")]
|
|
|
|
fn minify_fixtures(input: PathBuf) {
|
|
|
|
let dir = input.parent().unwrap();
|
|
|
|
let output = dir.join(format!(
|
|
|
|
"output.min.{}",
|
|
|
|
input.extension().unwrap().to_string_lossy()
|
|
|
|
));
|
2021-11-27 08:33:33 +03:00
|
|
|
|
2022-10-27 15:43:01 +03:00
|
|
|
testing::run_test(false, |cm, handler| {
|
2021-11-27 08:33:33 +03:00
|
|
|
let fm = cm.load_file(&input).unwrap();
|
|
|
|
|
|
|
|
let mut errors = vec![];
|
|
|
|
let res: Result<Stylesheet, _> = parse_file(&fm, Default::default(), &mut errors);
|
|
|
|
|
2022-10-27 15:43:01 +03:00
|
|
|
for err in errors {
|
|
|
|
err.to_diagnostics(handler).emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
if handler.has_errors() {
|
|
|
|
return Err(());
|
2021-11-27 08:33:33 +03:00
|
|
|
}
|
2022-02-11 06:33:08 +03:00
|
|
|
|
2021-11-27 08:33:33 +03:00
|
|
|
let mut ss = res.unwrap();
|
|
|
|
|
|
|
|
// Apply transforms
|
2022-07-11 16:08:40 +03:00
|
|
|
minify(&mut ss, Default::default());
|
2021-11-27 08:33:33 +03:00
|
|
|
|
|
|
|
let mut css_str = String::new();
|
|
|
|
{
|
2022-03-11 08:22:14 +03:00
|
|
|
let wr = BasicCssWriter::new(&mut css_str, None, BasicCssWriterConfig::default());
|
2021-11-27 08:33:33 +03:00
|
|
|
let mut gen = CodeGenerator::new(wr, CodegenConfig { minify: true });
|
|
|
|
|
|
|
|
gen.emit(&ss).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
NormalizedOutput::from(css_str)
|
2022-02-11 06:33:08 +03:00
|
|
|
.compare_to_file(&output)
|
2021-11-27 08:33:33 +03:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
}
|