|
| 1 | +use std::collections::HashMap; |
| 2 | +use std::collections::hash_map::DefaultHasher; |
| 3 | +use std::hash::{Hash, Hasher}; |
| 4 | +use data_encoding::{Specification, Encoding}; |
| 5 | +use lazy_static::lazy_static; |
| 6 | +use crate::properties::css_modules::{Composes, ComposesFrom}; |
| 7 | +use parcel_selectors::SelectorList; |
| 8 | +use crate::selector::Selectors; |
| 9 | +use serde::Serialize; |
| 10 | + |
| 11 | +#[derive(PartialEq, Eq, Hash, Debug, Clone, Serialize)] |
| 12 | +#[serde(tag = "type", content = "value", rename_all = "lowercase")] |
| 13 | +pub enum CssModuleExport { |
| 14 | + Local(String), |
| 15 | + Dependency { |
| 16 | + name: String, |
| 17 | + specifier: String |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +pub type CssModuleExports = HashMap<String, Vec<CssModuleExport>>; |
| 22 | + |
| 23 | +lazy_static! { |
| 24 | + static ref ENCODER: Encoding = { |
| 25 | + let mut spec = Specification::new(); |
| 26 | + spec.symbols.push_str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-"); |
| 27 | + spec.encoding().unwrap() |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +pub(crate) struct CssModule<'a> { |
| 32 | + pub hash: &'a str, |
| 33 | + pub exports: &'a mut CssModuleExports |
| 34 | +} |
| 35 | + |
| 36 | +impl<'a> CssModule<'a> { |
| 37 | + pub fn add_export(&mut self, name: String, export: CssModuleExport) { |
| 38 | + match self.exports.entry(name) { |
| 39 | + std::collections::hash_map::Entry::Occupied(mut entry) => { |
| 40 | + if !entry.get().contains(&export) { |
| 41 | + entry.get_mut().push(export); |
| 42 | + } |
| 43 | + } |
| 44 | + std::collections::hash_map::Entry::Vacant(entry) => { |
| 45 | + let mut items = Vec::new(); |
| 46 | + if !items.contains(&export) { |
| 47 | + items.push(export); |
| 48 | + } |
| 49 | + entry.insert(items); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + pub fn add_local(&mut self, exported: &str, local: &str) { |
| 55 | + let local = CssModuleExport::Local(format!("{}_{}", local, self.hash)); |
| 56 | + self.add_export(exported.into(), local); |
| 57 | + } |
| 58 | + |
| 59 | + pub fn add_global(&mut self, exported: &str, global: &str) { |
| 60 | + self.add_export(exported.into(), CssModuleExport::Local(global.into())) |
| 61 | + } |
| 62 | + |
| 63 | + pub fn add_dependency(&mut self, exported: &str, name: &str, specifier: &str) { |
| 64 | + let dependency = CssModuleExport::Dependency { |
| 65 | + name: name.into(), |
| 66 | + specifier: specifier.into() |
| 67 | + }; |
| 68 | + self.add_export(exported.into(), dependency) |
| 69 | + } |
| 70 | + |
| 71 | + pub fn handle_composes(&mut self, selectors: &SelectorList<Selectors>, composes: &Composes) -> Result<(), ()> { |
| 72 | + for sel in &selectors.0 { |
| 73 | + if sel.len() == 1 { |
| 74 | + match sel.iter_raw_match_order().next().unwrap() { |
| 75 | + parcel_selectors::parser::Component::Class(ref id) => { |
| 76 | + for name in &composes.names { |
| 77 | + match &composes.from { |
| 78 | + None => self.add_local(&id.0, &name.0), |
| 79 | + Some(ComposesFrom::Global) => self.add_global(&id.0, &name.0), |
| 80 | + Some(ComposesFrom::File(file)) => self.add_dependency(&id.0, &name.0, &file) |
| 81 | + } |
| 82 | + } |
| 83 | + continue; |
| 84 | + } |
| 85 | + _ => {} |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // The composes property can only be used within a simple class selector. |
| 90 | + return Err(()) // TODO: custom error |
| 91 | + } |
| 92 | + |
| 93 | + Ok(()) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +pub(crate) fn hash(s: &str) -> String { |
| 98 | + let mut hasher = DefaultHasher::new(); |
| 99 | + s.hash(&mut hasher); |
| 100 | + let hash = hasher.finish() as u32; |
| 101 | + |
| 102 | + ENCODER.encode(&hash.to_le_bytes()) |
| 103 | +} |
0 commit comments