@@ -9,17 +9,30 @@ use crate::selector::Selectors;
99use serde:: Serialize ;
1010use crate :: error:: PrinterError ;
1111
12- #[ derive( PartialEq , Eq , Hash , Debug , Clone , Serialize ) ]
13- #[ serde( tag = "type" , content = "value" , rename_all = "lowercase" ) ]
14- pub enum CssModuleExport {
15- Local ( String ) ,
12+ #[ derive( PartialEq , Debug , Clone , Serialize ) ]
13+ #[ serde( tag = "type" , rename_all = "lowercase" ) ]
14+ pub enum CssModuleReference {
15+ Local {
16+ name : String ,
17+ } ,
18+ Global {
19+ name : String
20+ } ,
1621 Dependency {
1722 name : String ,
1823 specifier : String
1924 }
2025}
2126
22- pub type CssModuleExports = HashMap < String , Vec < CssModuleExport > > ;
27+ #[ derive( PartialEq , Debug , Clone , Serialize ) ]
28+ #[ serde( rename_all = "camelCase" ) ]
29+ pub struct CssModuleExport {
30+ pub name : String ,
31+ pub composes : Vec < CssModuleReference > ,
32+ pub is_referenced : bool
33+ }
34+
35+ pub type CssModuleExports = HashMap < String , CssModuleExport > ;
2336
2437lazy_static ! {
2538 static ref ENCODER : Encoding = {
@@ -35,50 +48,49 @@ pub(crate) struct CssModule<'a> {
3548}
3649
3750impl < ' a > CssModule < ' a > {
38- pub fn add_export ( & mut self , name : String , export : CssModuleExport ) {
39- match self . exports . entry ( name) {
51+ pub fn add_local ( & mut self , exported : & str , local : & str ) {
52+ let hash = & self . hash ;
53+ self . exports . entry ( exported. into ( ) )
54+ . or_insert_with ( || CssModuleExport {
55+ name : format ! ( "{}_{}" , local, hash) ,
56+ composes : vec ! [ ] ,
57+ is_referenced : false
58+ } ) ;
59+ }
60+
61+ pub fn reference ( & mut self , name : & str ) {
62+ match self . exports . entry ( name. into ( ) ) {
4063 std:: collections:: hash_map:: Entry :: Occupied ( mut entry) => {
41- if !entry. get ( ) . contains ( & export) {
42- entry. get_mut ( ) . push ( export) ;
43- }
64+ entry. get_mut ( ) . is_referenced = true ;
4465 }
4566 std:: collections:: hash_map:: Entry :: Vacant ( entry) => {
46- let mut items = Vec :: new ( ) ;
47- if !items . contains ( & export ) {
48- items . push ( export ) ;
49- }
50- entry . insert ( items ) ;
67+ entry . insert ( CssModuleExport {
68+ name : format ! ( "{}_{}" , name , self . hash ) ,
69+ composes : vec ! [ ] ,
70+ is_referenced : true
71+ } ) ;
5172 }
5273 }
5374 }
5475
55- pub fn add_local ( & mut self , exported : & str , local : & str ) {
56- let local = CssModuleExport :: Local ( format ! ( "{}_{}" , local, self . hash) ) ;
57- self . add_export ( exported. into ( ) , local) ;
58- }
59-
60- pub fn add_global ( & mut self , exported : & str , global : & str ) {
61- self . add_export ( exported. into ( ) , CssModuleExport :: Local ( global. into ( ) ) )
62- }
63-
64- pub fn add_dependency ( & mut self , exported : & str , name : & str , specifier : & str ) {
65- let dependency = CssModuleExport :: Dependency {
66- name : name. into ( ) ,
67- specifier : specifier. into ( )
68- } ;
69- self . add_export ( exported. into ( ) , dependency)
70- }
71-
7276 pub fn handle_composes ( & mut self , selectors : & SelectorList < Selectors > , composes : & Composes ) -> Result < ( ) , PrinterError > {
7377 for sel in & selectors. 0 {
7478 if sel. len ( ) == 1 {
7579 match sel. iter_raw_match_order ( ) . next ( ) . unwrap ( ) {
7680 parcel_selectors:: parser:: Component :: Class ( ref id) => {
7781 for name in & composes. names {
78- match & composes. from {
79- None => self . add_local ( & id. 0 , & name. 0 ) ,
80- Some ( ComposesFrom :: Global ) => self . add_global ( & id. 0 , & name. 0 ) ,
81- Some ( ComposesFrom :: File ( file) ) => self . add_dependency ( & id. 0 , & name. 0 , & file)
82+ let reference = match & composes. from {
83+ None => CssModuleReference :: Local { name : format ! ( "{}_{}" , name. 0 , self . hash) } ,
84+ Some ( ComposesFrom :: Global ) => CssModuleReference :: Global { name : name. 0 . clone ( ) } ,
85+ Some ( ComposesFrom :: File ( file) ) => CssModuleReference :: Dependency {
86+ name : name. 0 . clone ( ) ,
87+ specifier : file. clone ( )
88+ }
89+ } ;
90+
91+ let export = self . exports . get_mut ( & id. 0 ) . unwrap ( ) ;
92+ if !export. composes . contains ( & reference) {
93+ export. composes . push ( reference) ;
8294 }
8395 }
8496 continue ;
0 commit comments