Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/bundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,11 +736,11 @@ mod tests {
"#
}, "/a.css");
assert_eq!(res, indoc! { r#"
.a_6lixEq_1 {
._6lixEq_a {
color: green;
}

.a_6lixEq {
._6lixEq_a {
color: red;
}
"#});
Expand Down
20 changes: 15 additions & 5 deletions src/css_modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ pub(crate) struct CssModule<'a> {

impl<'a> CssModule<'a> {
pub fn add_local(&mut self, exported: &str, local: &str) {
let hash = &self.hash;
self.exports.entry(exported.into())
.or_insert_with(|| CssModuleExport {
name: format!("{}_{}", local, hash),
name: get_hashed_name(self.hash, local),
composes: vec![],
is_referenced: false
});
Expand All @@ -65,7 +64,7 @@ impl<'a> CssModule<'a> {
}
std::collections::hash_map::Entry::Vacant(entry) => {
entry.insert(CssModuleExport {
name: format!("{}_{}", name, self.hash),
name: get_hashed_name(self.hash, name),
composes: vec![],
is_referenced: true
});
Expand All @@ -80,7 +79,7 @@ impl<'a> CssModule<'a> {
parcel_selectors::parser::Component::Class(ref id) => {
for name in &composes.names {
let reference = match &composes.from {
None => CssModuleReference::Local { name: format!("{}_{}", name.0, self.hash) },
None => CssModuleReference::Local { name: get_hashed_name(self.hash, name.0.as_ref()) },
Some(ComposesFrom::Global) => CssModuleReference::Global { name: name.0.as_ref().into() },
Some(ComposesFrom::File(file)) => CssModuleReference::Dependency {
name: name.0.to_string(),
Expand All @@ -107,10 +106,21 @@ impl<'a> CssModule<'a> {
}
}

fn get_hashed_name(hash: &str, name: &str) -> String {
// Hash must come first so that CSS grid identifiers work.
// This is because grid lines may have an implicit -start or -end appended.
format!("{}_{}", hash, name)
}

pub(crate) fn hash(s: &str) -> String {
let mut hasher = DefaultHasher::new();
s.hash(&mut hasher);
let hash = hasher.finish() as u32;

ENCODER.encode(&hash.to_le_bytes())
let hash = ENCODER.encode(&hash.to_le_bytes());
if matches!(hash.as_bytes()[0], b'0'..=b'9') {
format!("_{}", hash)
} else {
hash
}
}
Loading