@@ -17,13 +17,14 @@ use rspack_core::{
1717 AssetGeneratorOptions , AssetInlineGeneratorOptions , AssetParserDataUrl ,
1818 AssetParserDataUrlOptions , AssetParserOptions , AssetResourceGeneratorOptions ,
1919 CssAutoGeneratorOptions , CssAutoParserOptions , CssGeneratorOptions , CssModuleGeneratorOptions ,
20- CssModuleParserOptions , CssParserOptions , DescriptionData , DynamicImportFetchPriority ,
21- DynamicImportMode , ExportPresenceMode , FuncUseCtx , GeneratorOptions , GeneratorOptionsMap ,
22- JavascriptParserCommonjsExportsOption , JavascriptParserCommonjsOptions , JavascriptParserOptions ,
23- JavascriptParserOrder , JavascriptParserUrl , JsonGeneratorOptions , JsonParserOptions ,
24- ModuleNoParseRule , ModuleNoParseRules , ModuleNoParseTestFn , ModuleOptions , ModuleRule ,
25- ModuleRuleEffect , ModuleRuleEnforce , ModuleRuleUse , ModuleRuleUseLoader , OverrideStrict ,
26- ParseOption , ParserOptions , ParserOptionsMap , TypeReexportPresenceMode , UnsafeCachePredicate ,
20+ CssModuleParserOptions , CssParserImport , CssParserImportFnArgs , CssParserOptions ,
21+ DescriptionData , DynamicImportFetchPriority , DynamicImportMode , ExportPresenceMode , FuncUseCtx ,
22+ GeneratorOptions , GeneratorOptionsMap , JavascriptParserCommonjsExportsOption ,
23+ JavascriptParserCommonjsOptions , JavascriptParserOptions , JavascriptParserOrder ,
24+ JavascriptParserUrl , JsonGeneratorOptions , JsonParserOptions , ModuleNoParseRule ,
25+ ModuleNoParseRules , ModuleNoParseTestFn , ModuleOptions , ModuleRule , ModuleRuleEffect ,
26+ ModuleRuleEnforce , ModuleRuleUse , ModuleRuleUseLoader , OverrideStrict , ParseOption ,
27+ ParserOptions , ParserOptionsMap , TypeReexportPresenceMode , UnsafeCachePredicate ,
2728} ;
2829use rspack_error:: error;
2930use rspack_napi:: threadsafe_function:: ThreadsafeFunction ;
@@ -461,50 +462,96 @@ impl From<RawAssetParserDataUrlOptions> for AssetParserDataUrlOptions {
461462 }
462463}
463464
465+ // Use tuple to pass arguments separately to JS function
466+ type RawCssImportFn = ThreadsafeFunction <
467+ (
468+ String ,
469+ Option < String > ,
470+ String ,
471+ Option < String > ,
472+ Option < String > ,
473+ ) ,
474+ bool ,
475+ > ;
476+
477+ fn convert_import_option ( import : Option < Either < bool , RawCssImportFn > > ) -> Option < CssParserImport > {
478+ import. map ( |i| match i {
479+ Either :: A ( b) => CssParserImport :: Bool ( b) ,
480+ Either :: B ( f) => CssParserImport :: Func ( Arc :: new ( move |args : CssParserImportFnArgs | {
481+ let f = f. clone ( ) ;
482+ Box :: pin ( async move {
483+ f. call_with_sync ( (
484+ args. url ,
485+ args. media ,
486+ args. resource_path ,
487+ args. supports ,
488+ args. layer ,
489+ ) )
490+ . await
491+ } )
492+ } ) ) ,
493+ } )
494+ }
495+
464496#[ derive( Debug , Default ) ]
465- #[ napi( object) ]
497+ #[ napi( object, object_to_js = false ) ]
466498pub struct RawCssParserOptions {
467499 pub named_exports : Option < bool > ,
468500 pub url : Option < bool > ,
501+ #[ napi(
502+ ts_type = "boolean | ((args: [string, string | undefined, string, string | undefined, string | undefined]) => boolean)"
503+ ) ]
504+ pub resolve_import : Option < Either < bool , RawCssImportFn > > ,
469505}
470506
471507impl From < RawCssParserOptions > for CssParserOptions {
472508 fn from ( value : RawCssParserOptions ) -> Self {
473509 Self {
474510 named_exports : value. named_exports ,
475511 url : value. url ,
512+ resolve_import : convert_import_option ( value. resolve_import ) ,
476513 }
477514 }
478515}
479516
480517#[ derive( Debug , Default ) ]
481- #[ napi( object) ]
518+ #[ napi( object, object_to_js = false ) ]
482519pub struct RawCssAutoParserOptions {
483520 pub named_exports : Option < bool > ,
484521 pub url : Option < bool > ,
522+ #[ napi(
523+ ts_type = "boolean | ((args: [string, string | undefined, string, string | undefined, string | undefined]) => boolean)"
524+ ) ]
525+ pub resolve_import : Option < Either < bool , RawCssImportFn > > ,
485526}
486527
487528impl From < RawCssAutoParserOptions > for CssAutoParserOptions {
488529 fn from ( value : RawCssAutoParserOptions ) -> Self {
489530 Self {
490531 named_exports : value. named_exports ,
491532 url : value. url ,
533+ resolve_import : convert_import_option ( value. resolve_import ) ,
492534 }
493535 }
494536}
495537
496538#[ derive( Debug , Default ) ]
497- #[ napi( object) ]
539+ #[ napi( object, object_to_js = false ) ]
498540pub struct RawCssModuleParserOptions {
499541 pub named_exports : Option < bool > ,
500542 pub url : Option < bool > ,
543+ #[ napi(
544+ ts_type = "boolean | ((args: [string, string | undefined, string, string | undefined, string | undefined]) => boolean)"
545+ ) ]
546+ pub resolve_import : Option < Either < bool , RawCssImportFn > > ,
501547}
502548
503549impl From < RawCssModuleParserOptions > for CssModuleParserOptions {
504550 fn from ( value : RawCssModuleParserOptions ) -> Self {
505551 Self {
506552 named_exports : value. named_exports ,
507553 url : value. url ,
554+ resolve_import : convert_import_option ( value. resolve_import ) ,
508555 }
509556 }
510557}
0 commit comments