11use css:: class_map:: { get_class_map, set_class_map} ;
2+ use css:: file_map:: { get_file_map, get_filename_by_file_num} ;
23use extractor:: extract_style:: ExtractStyleProperty ;
34use extractor:: extract_style:: extract_style_value:: ExtractStyleValue ;
45use extractor:: extract_style:: style_property:: StyleProperty ;
@@ -18,6 +19,9 @@ pub struct Output {
1819 styles : HashSet < ExtractStyleValue > ,
1920 map : Option < String > ,
2021 default_collected : bool ,
22+ split_css : bool ,
23+ filename : String ,
24+ css_file : String ,
2125}
2226#[ wasm_bindgen]
2327extern "C" {
@@ -39,6 +43,11 @@ impl Output {
3943 self . code . clone ( )
4044 }
4145
46+ #[ wasm_bindgen( getter) ]
47+ pub fn css_file ( & self ) -> String {
48+ self . css_file . clone ( )
49+ }
50+
4251 #[ wasm_bindgen( getter) ]
4352 pub fn map ( & self ) -> Option < String > {
4453 self . map . clone ( )
@@ -52,7 +61,11 @@ impl Output {
5261 for style in self . styles . iter ( ) {
5362 match style {
5463 ExtractStyleValue :: Static ( st) => {
55- let ( cls, _) = match style. extract ( ) {
64+ let ( cls, _) = match style. extract ( if self . split_css {
65+ Some ( self . filename . as_str ( ) )
66+ } else {
67+ None
68+ } ) {
5669 Some ( StyleProperty :: ClassName ( cls) ) => ( cls, None ) ,
5770 Some ( StyleProperty :: Variable {
5871 class_name,
@@ -68,12 +81,21 @@ impl Output {
6881 st. value ( ) ,
6982 st. selector ( ) ,
7083 st. style_order ( ) ,
84+ if self . split_css {
85+ Some ( self . filename . as_str ( ) )
86+ } else {
87+ None
88+ } ,
7189 ) {
7290 collected = true ;
7391 }
7492 }
7593 ExtractStyleValue :: Dynamic ( dy) => {
76- let ( cls, variable) = match style. extract ( ) {
94+ let ( cls, variable) = match style. extract ( if self . split_css {
95+ Some ( self . filename . as_str ( ) )
96+ } else {
97+ None
98+ } ) {
7799 Some ( StyleProperty :: ClassName ( cls) ) => ( cls, None ) ,
78100 Some ( StyleProperty :: Variable {
79101 class_name,
@@ -89,14 +111,25 @@ impl Output {
89111 & format ! ( "var({})" , variable. unwrap( ) ) ,
90112 dy. selector ( ) ,
91113 dy. style_order ( ) ,
114+ if self . split_css {
115+ Some ( self . filename . as_str ( ) )
116+ } else {
117+ None
118+ } ,
92119 ) {
93120 collected = true ;
94121 }
95122 }
96123
97124 ExtractStyleValue :: Keyframes ( keyframes) => {
98125 if sheet. add_keyframes (
99- & keyframes. extract ( ) . to_string ( ) ,
126+ & keyframes
127+ . extract ( if self . split_css {
128+ Some ( self . filename . as_str ( ) )
129+ } else {
130+ None
131+ } )
132+ . to_string ( ) ,
100133 keyframes
101134 . keyframes
102135 . iter ( )
@@ -115,6 +148,11 @@ impl Output {
115148 )
116149 } )
117150 . collect ( ) ,
151+ if self . split_css {
152+ Some ( self . filename . as_str ( ) )
153+ } else {
154+ None
155+ } ,
118156 ) {
119157 collected = true ;
120158 }
@@ -138,7 +176,11 @@ impl Output {
138176 return None ;
139177 }
140178
141- Some ( sheet. create_css ( ) )
179+ Some ( sheet. create_css ( if self . split_css {
180+ Some ( self . filename . as_str ( ) )
181+ } else {
182+ None
183+ } ) )
142184 }
143185}
144186
@@ -179,12 +221,27 @@ pub fn export_class_map() -> Result<String, JsValue> {
179221 serde_json:: to_string ( & get_class_map ( ) ) . map_err ( |e| JsValue :: from_str ( e. to_string ( ) . as_str ( ) ) )
180222}
181223
224+ #[ wasm_bindgen( js_name = "importFileMap" ) ]
225+ pub fn import_file_map ( sheet_object : JsValue ) -> Result < ( ) , JsValue > {
226+ set_class_map (
227+ serde_wasm_bindgen:: from_value ( sheet_object)
228+ . map_err ( |e| JsValue :: from_str ( e. to_string ( ) . as_str ( ) ) ) ?,
229+ ) ;
230+ Ok ( ( ) )
231+ }
232+
233+ #[ wasm_bindgen( js_name = "exportFileMap" ) ]
234+ pub fn export_file_map ( ) -> Result < String , JsValue > {
235+ serde_json:: to_string ( & get_file_map ( ) ) . map_err ( |e| JsValue :: from_str ( e. to_string ( ) . as_str ( ) ) )
236+ }
237+
182238#[ wasm_bindgen( js_name = "codeExtract" ) ]
183239pub fn code_extract (
184240 filename : & str ,
185241 code : & str ,
186242 package : & str ,
187- css_file : & str ,
243+ css_dir : String ,
244+ split_css : bool ,
188245) -> Result < Output , JsValue > {
189246 let mut sheet = GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) ;
190247
@@ -193,14 +250,18 @@ pub fn code_extract(
193250 code,
194251 ExtractOption {
195252 package : package. to_string ( ) ,
196- css_file : Some ( css_file. to_string ( ) ) ,
253+ css_dir,
254+ split_css,
197255 } ,
198256 ) {
199257 Ok ( output) => Ok ( Output {
200258 code : output. code ,
201259 styles : output. styles ,
202260 map : output. map ,
203261 default_collected : sheet. rm_global_css ( filename) ,
262+ split_css,
263+ filename : filename. to_string ( ) ,
264+ css_file : output. css_file ,
204265 } ) ,
205266 Err ( error) => Err ( JsValue :: from_str ( error. to_string ( ) . as_str ( ) ) ) ,
206267 }
@@ -222,9 +283,9 @@ pub fn get_default_theme() -> Result<Option<String>, JsValue> {
222283}
223284
224285#[ wasm_bindgen( js_name = "getCss" ) ]
225- pub fn get_css ( ) -> Result < String , JsValue > {
286+ pub fn get_css ( file_num : Option < usize > ) -> Result < String , JsValue > {
226287 let sheet = GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) ;
227- Ok ( sheet. create_css ( ) )
288+ Ok ( sheet. create_css ( file_num . map ( |num| get_filename_by_file_num ( num ) ) . as_deref ( ) ) )
228289}
229290
230291#[ wasm_bindgen( js_name = "getThemeInterface" ) ]
@@ -259,7 +320,7 @@ mod tests {
259320 let mut sheet = GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) ;
260321 * sheet = StyleSheet :: default ( ) ;
261322 }
262- assert_eq ! ( get_css( ) . unwrap( ) , "" ) ;
323+ assert_eq ! ( get_css( None ) . unwrap( ) , "" ) ;
263324
264325 {
265326 let mut sheet = GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) ;
@@ -274,7 +335,7 @@ mod tests {
274335 sheet. set_theme ( theme) ;
275336 }
276337
277- assert_debug_snapshot ! ( get_css( ) . unwrap( ) ) ;
338+ assert_debug_snapshot ! ( get_css( None ) . unwrap( ) ) ;
278339 }
279340
280341 #[ test]
0 commit comments