Files
Donny/강동윤andGitHub 51a7592f9a build: Update rustc to nightly-2025-05-06 (#10443)
**Description:**

I'll merge this as we are not heavily using `css` modules.
The HTML regression looks like it's related to CSS regressions, but we are not actively maintaining `swc_css` anyway, and `@swc/html` uses `lightningcss` under a flag.
2025-05-07 18:26:29 +00:00

158 lines
4.6 KiB
Rust

use std::path::PathBuf;
use indexmap::IndexMap;
use serde::Serialize;
use swc_atoms::Atom;
use swc_css_codegen::{
writer::basic::{BasicCssWriter, BasicCssWriterConfig, IndentType},
CodeGenerator, Emit,
};
use swc_css_modules::CssClassName;
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::new();
let ss = swc_css_parser::parse_file(
&fm,
None,
ParserConfig {
css_modules: true,
..Default::default()
},
&mut errors,
)
.unwrap();
let result = swc_css_modules::imports::analyze_imports(&ss);
if result.is_empty() {
return Ok(());
}
NormalizedOutput::compare_json_to_file(
&result,
&input.with_file_name(format!(
"{}.imports.json",
input.file_stem().unwrap().to_string_lossy()
)),
);
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::new();
let mut ss = swc_css_parser::parse_file(
&fm,
None,
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, 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 mut transformed_classes = transform_result
.renamed
.into_iter()
.map(|(k, v)| {
(
k,
v.into_iter()
.map(|v| match v {
CssClassName::Global { name } => {
CssClassNameForTest::Global { name: name.value }
}
CssClassName::Local { name } => {
CssClassNameForTest::Local { name: name.value }
}
CssClassName::Import { name, from } => {
CssClassNameForTest::Import {
name: name.value,
from,
}
}
})
.collect::<Vec<_>>(),
)
})
.collect::<IndexMap<_, _>>();
transformed_classes.sort_keys();
NormalizedOutput::compare_json_to_file(
&transformed_classes,
&input.with_file_name(format!(
"{}.transform.json",
input.file_stem().unwrap().to_string_lossy()
)),
);
}
Ok(())
})
.unwrap();
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
#[serde(tag = "type", rename_all = "camelCase")]
enum CssClassNameForTest {
Local {
/// Tranformed css class name
name: Atom,
},
Global {
name: Atom,
},
Import {
/// The exported class name. This is the value specified by the user.
name: Atom,
/// The module specifier.
from: Atom,
},
}
struct TestConfig {}
impl swc_css_modules::TransformConfig for TestConfig {
fn new_name_for(&self, local: &Atom) -> Atom {
format!("__local__{local}").into()
}
}