swc/crates/swc_css_modules/tests/fixture.rs
2022-10-31 16:59:29 +09:00

113 lines
3.2 KiB
Rust

use std::path::PathBuf;
use swc_atoms::JsWord;
use swc_css_codegen::{
writer::basic::{BasicCssWriter, BasicCssWriterConfig, IndentType},
CodeGenerator, CodegenConfig, Emit,
};
use swc_css_parser::parser::ParserConfig;
use testing::NormalizedOutput;
#[testing::fixture("tests/fixture/**/*.css", exclude("compiled\\.css"))]
fn imports(input: PathBuf) {
testing::run_test(false, |cm, _| {
let fm = cm.load_file(&input).unwrap();
let mut errors = vec![];
let ss = swc_css_parser::parse_file(
&fm,
ParserConfig {
css_modules: true,
..Default::default()
},
&mut errors,
)
.unwrap();
let result = swc_css_modules::imports::analyze_imports(&ss);
if result.is_empty() {
return Ok(());
}
let s = serde_json::to_string_pretty(&result).unwrap();
NormalizedOutput::from(s)
.compare_to_file(input.with_file_name(format!(
"{}.imports.json",
input.file_stem().unwrap().to_string_lossy()
)))
.unwrap();
Ok(())
})
.unwrap();
}
#[testing::fixture("tests/fixture/**/*.css", exclude("compiled\\.css"))]
fn compile(input: PathBuf) {
testing::run_test(false, |cm, _| {
let fm = cm.load_file(&input).unwrap();
let mut errors = vec![];
let mut ss = swc_css_parser::parse_file(
&fm,
ParserConfig {
css_modules: true,
..Default::default()
},
&mut errors,
)
.unwrap();
let _result = swc_css_modules::imports::analyze_imports(&ss);
let transform_result = swc_css_modules::compile(&mut ss, TestConfig {});
let mut buf = String::new();
{
let wr = BasicCssWriter::new(
&mut buf,
None,
BasicCssWriterConfig {
indent_type: IndentType::Space,
indent_width: 2,
..Default::default()
},
);
let mut g = CodeGenerator::new(
wr,
CodegenConfig {
..Default::default()
},
);
g.emit(&ss).unwrap();
}
NormalizedOutput::from(buf)
.compare_to_file(input.with_file_name(format!(
"{}.compiled.css",
input.file_stem().unwrap().to_string_lossy()
)))
.unwrap();
if !transform_result.renamed.is_empty() {
let transformed_classes =
serde_json::to_string_pretty(&transform_result.renamed).unwrap();
NormalizedOutput::from(transformed_classes)
.compare_to_file(input.with_file_name(format!(
"{}.transform.json",
input.file_stem().unwrap().to_string_lossy()
)))
.unwrap();
}
Ok(())
})
.unwrap();
}
struct TestConfig {}
impl swc_css_modules::TransformConfig for TestConfig {
fn new_name_for(&self, local: &JsWord) -> JsWord {
format!("__local__{}", local).into()
}
}