22#[ global_allocator]
33static GLOBAL : jemallocator:: Jemalloc = jemallocator:: Jemalloc ;
44
5- use std:: collections:: HashSet ;
6- use std:: path:: Path ;
7- use serde:: { Serialize , Deserialize } ;
8- use parcel_css:: stylesheet:: { StyleSheet , StyleAttribute , ParserOptions , PrinterOptions , MinifyOptions , PseudoClasses } ;
9- use parcel_css:: targets:: Browsers ;
5+ use parcel_css:: bundler:: { BundleErrorKind , Bundler , FileProvider , SourceProvider } ;
106use parcel_css:: css_modules:: CssModuleExports ;
117use parcel_css:: dependencies:: Dependency ;
12- use parcel_css:: error:: { ParserError , Error , ErrorLocation , MinifyErrorKind , PrinterErrorKind } ;
13- use parcel_css:: bundler:: { FileProvider , Bundler , BundleErrorKind , SourceProvider } ;
8+ use parcel_css:: error:: { Error , ErrorLocation , MinifyErrorKind , ParserError , PrinterErrorKind } ;
9+ use parcel_css:: stylesheet:: {
10+ MinifyOptions , ParserOptions , PrinterOptions , PseudoClasses , StyleAttribute , StyleSheet ,
11+ } ;
12+ use parcel_css:: targets:: Browsers ;
1413use parcel_sourcemap:: SourceMap ;
14+ use serde:: { Deserialize , Serialize } ;
15+ use std:: collections:: HashSet ;
16+ use std:: path:: Path ;
1517
1618// ---------------------------------------------
1719
@@ -24,7 +26,7 @@ use wasm_bindgen::prelude::*;
2426#[ wasm_bindgen]
2527pub fn transform ( config_val : JsValue ) -> Result < JsValue , JsValue > {
2628 let config: Config = from_value ( config_val) . map_err ( JsValue :: from) ?;
27- let code = unsafe { std:: str:: from_utf8_unchecked ( & config. code ) } ;
29+ let code = unsafe { std:: str:: from_utf8_unchecked ( & config. code ) } ;
2830 let res = compile ( code, & config) ?;
2931 let serializer = Serializer :: new ( ) . serialize_maps_as_objects ( true ) ;
3032 res. serialize ( & serializer) . map_err ( JsValue :: from)
@@ -42,10 +44,10 @@ pub fn transform_style_attribute(config_val: JsValue) -> Result<JsValue, JsValue
4244
4345// ---------------------------------------------
4446
45- #[ cfg( not( target_arch = "wasm32" ) ) ]
46- use napi_derive:: { js_function, module_exports} ;
4747#[ cfg( not( target_arch = "wasm32" ) ) ]
4848use napi:: { CallContext , JsObject , JsUnknown } ;
49+ #[ cfg( not( target_arch = "wasm32" ) ) ]
50+ use napi_derive:: { js_function, module_exports} ;
4951
5052#[ derive( Serialize ) ]
5153#[ serde( rename_all = "camelCase" ) ]
@@ -54,7 +56,7 @@ struct SourceMapJson<'a> {
5456 mappings : String ,
5557 sources : & ' a Vec < String > ,
5658 sources_content : & ' a Vec < String > ,
57- names : & ' a Vec < String >
59+ names : & ' a Vec < String > ,
5860}
5961
6062#[ derive( Serialize ) ]
@@ -65,20 +67,20 @@ struct TransformResult {
6567 #[ serde( with = "serde_bytes" ) ]
6668 map : Option < Vec < u8 > > ,
6769 exports : Option < CssModuleExports > ,
68- dependencies : Option < Vec < Dependency > >
70+ dependencies : Option < Vec < Dependency > > ,
6971}
7072
7173#[ cfg( not( target_arch = "wasm32" ) ) ]
7274#[ js_function( 1 ) ]
7375fn transform ( ctx : CallContext ) -> napi:: Result < JsUnknown > {
7476 let opts = ctx. get :: < JsObject > ( 0 ) ?;
7577 let config: Config = ctx. env . from_js_value ( opts) ?;
76- let code = unsafe { std:: str:: from_utf8_unchecked ( & config. code ) } ;
78+ let code = unsafe { std:: str:: from_utf8_unchecked ( & config. code ) } ;
7779 let res = compile ( code, & config) ;
7880
7981 match res {
8082 Ok ( res) => ctx. env . to_js_value ( & res) ,
81- Err ( err) => err. throw ( ctx, Some ( code) )
83+ Err ( err) => err. throw ( ctx, Some ( code) ) ,
8284 }
8385}
8486
@@ -92,7 +94,7 @@ fn transform_style_attribute(ctx: CallContext) -> napi::Result<JsUnknown> {
9294
9395 match res {
9496 Ok ( res) => ctx. env . to_js_value ( & res) ,
95- Err ( err) => err. throw ( ctx, Some ( code) )
97+ Err ( err) => err. throw ( ctx, Some ( code) ) ,
9698 }
9799}
98100
@@ -108,13 +110,23 @@ fn bundle(ctx: CallContext) -> napi::Result<JsUnknown> {
108110 Ok ( res) => ctx. env . to_js_value ( & res) ,
109111 Err ( err) => {
110112 let code = match & err {
111- CompileError :: ParseError ( Error { loc : Some ( ErrorLocation { filename, .. } ) , .. } ) |
112- CompileError :: PrinterError ( Error { loc : Some ( ErrorLocation { filename, .. } ) , .. } ) |
113- CompileError :: MinifyError ( Error { loc : Some ( ErrorLocation { filename, .. } ) , .. } ) |
114- CompileError :: BundleError ( Error { loc : Some ( ErrorLocation { filename, .. } ) , .. } ) => {
115- Some ( fs. read ( Path :: new ( filename) ) ?)
116- } ,
117- _ => None
113+ CompileError :: ParseError ( Error {
114+ loc : Some ( ErrorLocation { filename, .. } ) ,
115+ ..
116+ } )
117+ | CompileError :: PrinterError ( Error {
118+ loc : Some ( ErrorLocation { filename, .. } ) ,
119+ ..
120+ } )
121+ | CompileError :: MinifyError ( Error {
122+ loc : Some ( ErrorLocation { filename, .. } ) ,
123+ ..
124+ } )
125+ | CompileError :: BundleError ( Error {
126+ loc : Some ( ErrorLocation { filename, .. } ) ,
127+ ..
128+ } ) => Some ( fs. read ( Path :: new ( filename) ) ?) ,
129+ _ => None ,
118130 } ;
119131 err. throw ( ctx, code)
120132 }
@@ -146,7 +158,7 @@ struct Config {
146158 pub css_modules : Option < bool > ,
147159 pub analyze_dependencies : Option < bool > ,
148160 pub pseudo_classes : Option < OwnedPseudoClasses > ,
149- pub unused_symbols : Option < HashSet < String > >
161+ pub unused_symbols : Option < HashSet < String > > ,
150162}
151163
152164#[ derive( Debug , Deserialize ) ]
@@ -160,7 +172,7 @@ struct BundleConfig {
160172 pub css_modules : Option < bool > ,
161173 pub analyze_dependencies : Option < bool > ,
162174 pub pseudo_classes : Option < OwnedPseudoClasses > ,
163- pub unused_symbols : Option < HashSet < String > >
175+ pub unused_symbols : Option < HashSet < String > > ,
164176}
165177
166178#[ derive( Debug , Deserialize ) ]
@@ -170,7 +182,7 @@ struct OwnedPseudoClasses {
170182 pub active : Option < String > ,
171183 pub focus : Option < String > ,
172184 pub focus_visible : Option < String > ,
173- pub focus_within : Option < String >
185+ pub focus_within : Option < String > ,
174186}
175187
176188impl < ' a > Into < PseudoClasses < ' a > > for & ' a OwnedPseudoClasses {
@@ -180,7 +192,7 @@ impl<'a> Into<PseudoClasses<'a>> for &'a OwnedPseudoClasses {
180192 active : self . active . as_deref ( ) ,
181193 focus : self . focus . as_deref ( ) ,
182194 focus_visible : self . focus_visible . as_deref ( ) ,
183- focus_within : self . focus_within . as_deref ( )
195+ focus_within : self . focus_within . as_deref ( ) ,
184196 }
185197 }
186198}
@@ -191,20 +203,24 @@ struct Drafts {
191203 #[ serde( default ) ]
192204 nesting : bool ,
193205 #[ serde( default ) ]
194- custom_media : bool
206+ custom_media : bool ,
195207}
196208
197209fn compile < ' i > ( code : & ' i str , config : & Config ) -> Result < TransformResult , CompileError < ' i > > {
198210 let drafts = config. drafts . as_ref ( ) ;
199- let mut stylesheet = StyleSheet :: parse ( config. filename . clone ( ) , & code, ParserOptions {
200- nesting : matches ! ( drafts, Some ( d) if d. nesting) ,
201- custom_media : matches ! ( drafts, Some ( d) if d. custom_media) ,
202- css_modules : config. css_modules . unwrap_or ( false ) ,
203- source_index : 0
204- } ) ?;
211+ let mut stylesheet = StyleSheet :: parse (
212+ config. filename . clone ( ) ,
213+ & code,
214+ ParserOptions {
215+ nesting : matches ! ( drafts, Some ( d) if d. nesting) ,
216+ custom_media : matches ! ( drafts, Some ( d) if d. custom_media) ,
217+ css_modules : config. css_modules . unwrap_or ( false ) ,
218+ source_index : 0 ,
219+ } ,
220+ ) ?;
205221 stylesheet. minify ( MinifyOptions {
206222 targets : config. targets ,
207- unused_symbols : config. unused_symbols . clone ( ) . unwrap_or_default ( )
223+ unused_symbols : config. unused_symbols . clone ( ) . unwrap_or_default ( ) ,
208224 } ) ?;
209225
210226 let mut source_map = if config. source_map . unwrap_or ( false ) {
@@ -221,7 +237,7 @@ fn compile<'i>(code: &'i str, config: &Config) -> Result<TransformResult, Compil
221237 source_map : source_map. as_mut ( ) ,
222238 targets : config. targets ,
223239 analyze_dependencies : config. analyze_dependencies . unwrap_or ( false ) ,
224- pseudo_classes : config. pseudo_classes . as_ref ( ) . map ( |p| p. into ( ) )
240+ pseudo_classes : config. pseudo_classes . as_ref ( ) . map ( |p| p. into ( ) ) ,
225241 } ) ?;
226242
227243 let map = if let Some ( mut source_map) = source_map {
@@ -234,7 +250,7 @@ fn compile<'i>(code: &'i str, config: &Config) -> Result<TransformResult, Compil
234250 code : res. code . into_bytes ( ) ,
235251 map,
236252 exports : res. exports ,
237- dependencies : res. dependencies
253+ dependencies : res. dependencies ,
238254 } )
239255}
240256
@@ -258,15 +274,15 @@ fn compile_bundle<'i>(fs: &'i FileProvider, config: &BundleConfig) -> Result<Tra
258274
259275 stylesheet. minify ( MinifyOptions {
260276 targets : config. targets ,
261- unused_symbols : config. unused_symbols . clone ( ) . unwrap_or_default ( )
277+ unused_symbols : config. unused_symbols . clone ( ) . unwrap_or_default ( ) ,
262278 } ) ?;
263279
264280 let res = stylesheet. to_css ( PrinterOptions {
265281 minify : config. minify . unwrap_or ( false ) ,
266282 source_map : source_map. as_mut ( ) ,
267283 targets : config. targets ,
268284 analyze_dependencies : config. analyze_dependencies . unwrap_or ( false ) ,
269- pseudo_classes : config. pseudo_classes . as_ref ( ) . map ( |p| p. into ( ) )
285+ pseudo_classes : config. pseudo_classes . as_ref ( ) . map ( |p| p. into ( ) ) ,
270286 } ) ?;
271287
272288 let map = if let Some ( source_map) = & mut source_map {
@@ -279,7 +295,7 @@ fn compile_bundle<'i>(fs: &'i FileProvider, config: &BundleConfig) -> Result<Tra
279295 code : res. code . into_bytes ( ) ,
280296 map,
281297 exports : res. exports ,
282- dependencies : res. dependencies
298+ dependencies : res. dependencies ,
283299 } )
284300}
285301
@@ -293,7 +309,7 @@ fn source_map_to_json<'i>(source_map: &mut SourceMap) -> Result<Vec<u8>, Compile
293309 mappings : unsafe { String :: from_utf8_unchecked ( vlq_output) } ,
294310 sources : source_map. get_sources ( ) ,
295311 sources_content : source_map. get_sources_content ( ) ,
296- names : source_map. get_names ( )
312+ names : source_map. get_names ( ) ,
297313 } ;
298314
299315 Ok ( serde_json:: to_vec ( & sm) . unwrap ( ) )
@@ -306,15 +322,15 @@ struct AttrConfig {
306322 pub code : Vec < u8 > ,
307323 pub targets : Option < Browsers > ,
308324 pub minify : Option < bool > ,
309- pub analyze_dependencies : Option < bool >
325+ pub analyze_dependencies : Option < bool > ,
310326}
311327
312328#[ derive( Serialize ) ]
313329#[ serde( rename_all = "camelCase" ) ]
314330struct AttrResult {
315331 #[ serde( with = "serde_bytes" ) ]
316332 code : Vec < u8 > ,
317- dependencies : Option < Vec < Dependency > >
333+ dependencies : Option < Vec < Dependency > > ,
318334}
319335
320336fn compile_attr < ' i > ( code : & ' i str , config : & AttrConfig ) -> Result < AttrResult , CompileError < ' i > > {
@@ -328,11 +344,11 @@ fn compile_attr<'i>(code: &'i str, config: &AttrConfig) -> Result<AttrResult, Co
328344 source_map : None ,
329345 targets : config. targets ,
330346 analyze_dependencies : config. analyze_dependencies . unwrap_or ( false ) ,
331- pseudo_classes : None
347+ pseudo_classes : None ,
332348 } ) ?;
333349 Ok ( AttrResult {
334350 code : res. code . into_bytes ( ) ,
335- dependencies : res. dependencies
351+ dependencies : res. dependencies ,
336352 } )
337353}
338354
@@ -341,7 +357,7 @@ enum CompileError<'i> {
341357 MinifyError ( Error < MinifyErrorKind > ) ,
342358 PrinterError ( Error < PrinterErrorKind > ) ,
343359 SourceMapError ( parcel_sourcemap:: SourceMapError ) ,
344- BundleError ( Error < BundleErrorKind < ' i > > )
360+ BundleError ( Error < BundleErrorKind < ' i > > ) ,
345361}
346362
347363impl < ' i > CompileError < ' i > {
@@ -351,7 +367,7 @@ impl<'i> CompileError<'i> {
351367 CompileError :: MinifyError ( err) => err. kind . reason ( ) ,
352368 CompileError :: PrinterError ( err) => err. kind . reason ( ) ,
353369 CompileError :: BundleError ( err) => err. kind . reason ( ) ,
354- _ => "Unknown error" . into ( )
370+ _ => "Unknown error" . into ( ) ,
355371 }
356372 }
357373
@@ -363,17 +379,16 @@ impl<'i> CompileError<'i> {
363379 CompileError :: PrinterError ( Error { kind, .. } ) => ctx. env . to_js_value ( kind) ?,
364380 CompileError :: MinifyError ( Error { kind, .. } ) => ctx. env . to_js_value ( kind) ?,
365381 CompileError :: BundleError ( Error { kind, .. } ) => ctx. env . to_js_value ( kind) ?,
366- _ => ctx. env . get_null ( ) ?. into_unknown ( )
382+ _ => ctx. env . get_null ( ) ?. into_unknown ( ) ,
367383 } ;
368384
369385 match self {
370- CompileError :: ParseError ( Error { loc, .. } ) |
371- CompileError :: PrinterError ( Error { loc, .. } ) |
372- CompileError :: MinifyError ( Error { loc, .. } ) |
373- CompileError :: BundleError ( Error { loc, .. } ) => {
386+ CompileError :: ParseError ( Error { loc, .. } )
387+ | CompileError :: PrinterError ( Error { loc, .. } )
388+ | CompileError :: MinifyError ( Error { loc, .. } )
389+ | CompileError :: BundleError ( Error { loc, .. } ) => {
374390 // Generate an error with location information.
375- let syntax_error = ctx. env . get_global ( ) ?
376- . get_named_property :: < napi:: JsFunction > ( "SyntaxError" ) ?;
391+ let syntax_error = ctx. env . get_global ( ) ?. get_named_property :: < napi:: JsFunction > ( "SyntaxError" ) ?;
377392 let reason = ctx. env . create_string_from_std ( reason) ?;
378393 let mut obj = syntax_error. new ( & [ reason] ) ?;
379394 if let Some ( loc) = loc {
@@ -393,8 +408,8 @@ impl<'i> CompileError<'i> {
393408 obj. set_named_property ( "data" , data) ?;
394409 ctx. env . throw ( obj) ?;
395410 Ok ( ctx. env . get_undefined ( ) ?. into_unknown ( ) )
396- } ,
397- _ => Err ( self . into ( ) )
411+ }
412+ _ => Err ( self . into ( ) ) ,
398413 }
399414 }
400415}
@@ -434,7 +449,7 @@ impl<'i> From<CompileError<'i>> for napi::Error {
434449 fn from ( e : CompileError ) -> napi:: Error {
435450 match e {
436451 CompileError :: SourceMapError ( e) => napi:: Error :: from_reason ( e. to_string ( ) ) ,
437- _ => napi:: Error :: new ( napi:: Status :: GenericFailure , e. reason ( ) )
452+ _ => napi:: Error :: new ( napi:: Status :: GenericFailure , e. reason ( ) ) ,
438453 }
439454 }
440455}
@@ -444,7 +459,7 @@ impl<'i> From<CompileError<'i>> for wasm_bindgen::JsValue {
444459 fn from ( e : CompileError ) -> wasm_bindgen:: JsValue {
445460 match e {
446461 CompileError :: SourceMapError ( e) => js_sys:: Error :: new ( & e. to_string ( ) ) . into ( ) ,
447- _ => js_sys:: Error :: new ( & e. reason ( ) ) . into ( )
462+ _ => js_sys:: Error :: new ( & e. reason ( ) ) . into ( ) ,
448463 }
449464 }
450465}
0 commit comments