@@ -3,10 +3,10 @@ mod naming;
33use serde:: Deserialize ;
44use std:: collections:: { HashMap , HashSet } ;
55use swc_core:: {
6- common:: { DUMMY_SP , SyntaxContext , errors :: HANDLER } ,
6+ common:: { errors :: HANDLER , SyntaxContext , DUMMY_SP } ,
77 ecma:: {
88 ast:: * ,
9- visit:: { VisitMut , VisitMutWith , noop_visit_mut_type } ,
9+ visit:: { noop_visit_mut_type , VisitMut , VisitMutWith } ,
1010 } ,
1111} ;
1212
@@ -2230,6 +2230,45 @@ impl StepTransform {
22302230 } ) )
22312231 }
22322232
2233+ // Create a registration call statement: registerSerializationClass("class//...", ClassName)
2234+ // Used in workflow mode and client mode to register classes for serialization
2235+ fn create_class_serialization_registration ( & self , class_name : & str ) -> Stmt {
2236+ let class_id = naming:: format_name ( "class" , & self . filename , class_name) ;
2237+ Stmt :: Expr ( ExprStmt {
2238+ span : DUMMY_SP ,
2239+ expr : Box :: new ( Expr :: Call ( CallExpr {
2240+ span : DUMMY_SP ,
2241+ ctxt : SyntaxContext :: empty ( ) ,
2242+ callee : Callee :: Expr ( Box :: new ( Expr :: Ident ( Ident :: new (
2243+ "registerSerializationClass" . into ( ) ,
2244+ DUMMY_SP ,
2245+ SyntaxContext :: empty ( ) ,
2246+ ) ) ) ) ,
2247+ args : vec ! [
2248+ // First argument: class ID
2249+ ExprOrSpread {
2250+ spread: None ,
2251+ expr: Box :: new( Expr :: Lit ( Lit :: Str ( Str {
2252+ span: DUMMY_SP ,
2253+ value: class_id. into( ) ,
2254+ raw: None ,
2255+ } ) ) ) ,
2256+ } ,
2257+ // Second argument: ClassName
2258+ ExprOrSpread {
2259+ spread: None ,
2260+ expr: Box :: new( Expr :: Ident ( Ident :: new(
2261+ class_name. into( ) ,
2262+ DUMMY_SP ,
2263+ SyntaxContext :: empty( ) ,
2264+ ) ) ) ,
2265+ } ,
2266+ ] ,
2267+ type_args : None ,
2268+ } ) ) ,
2269+ } )
2270+ }
2271+
22332272 // Create a proxy reference: globalThis[Symbol.for("WORKFLOW_USE_STEP")]("step_id", closure_fn) (workflow mode)
22342273 fn create_step_proxy_reference ( & self , step_id : & str , closure_vars : & [ String ] ) -> Expr {
22352274 let mut args = vec ! [ ExprOrSpread {
@@ -3239,7 +3278,13 @@ impl VisitMut for StepTransform {
32393278 }
32403279 }
32413280 TransformMode :: Client => {
3242- // No imports needed for client mode since step functions are not transformed
3281+ // In client mode, we still need class serialization registration
3282+ // so that classes can be serialized when passed to start(workflow)
3283+ let needs_class_serialization =
3284+ !self . classes_needing_serialization . is_empty ( ) ;
3285+ if needs_class_serialization {
3286+ imports_to_add. push ( self . create_class_serialization_import ( ) ) ;
3287+ }
32433288 }
32443289 }
32453290
@@ -3710,43 +3755,22 @@ impl VisitMut for StepTransform {
37103755 self . classes_needing_serialization . drain ( ) . collect ( ) ;
37113756 sorted_classes. sort ( ) ;
37123757 for class_name in sorted_classes {
3713- // Generate class ID: class//filename//ClassName
3714- let class_id = naming:: format_name ( "class" , & self . filename , & class_name) ;
3758+ let registration_call =
3759+ self . create_class_serialization_registration ( & class_name) ;
3760+ module. body . push ( ModuleItem :: Stmt ( registration_call) ) ;
3761+ }
3762+ }
37153763
3716- // Create: registerSerializationClass("class//...", ClassName)
3717- let registration_call = Stmt :: Expr ( ExprStmt {
3718- span : DUMMY_SP ,
3719- expr : Box :: new ( Expr :: Call ( CallExpr {
3720- span : DUMMY_SP ,
3721- ctxt : SyntaxContext :: empty ( ) ,
3722- callee : Callee :: Expr ( Box :: new ( Expr :: Ident ( Ident :: new (
3723- "registerSerializationClass" . into ( ) ,
3724- DUMMY_SP ,
3725- SyntaxContext :: empty ( ) ,
3726- ) ) ) ) ,
3727- args : vec ! [
3728- // First argument: class ID
3729- ExprOrSpread {
3730- spread: None ,
3731- expr: Box :: new( Expr :: Lit ( Lit :: Str ( Str {
3732- span: DUMMY_SP ,
3733- value: class_id. into( ) ,
3734- raw: None ,
3735- } ) ) ) ,
3736- } ,
3737- // Second argument: ClassName
3738- ExprOrSpread {
3739- spread: None ,
3740- expr: Box :: new( Expr :: Ident ( Ident :: new(
3741- class_name. into( ) ,
3742- DUMMY_SP ,
3743- SyntaxContext :: empty( ) ,
3744- ) ) ) ,
3745- } ,
3746- ] ,
3747- type_args : None ,
3748- } ) ) ,
3749- } ) ;
3764+ // Add class serialization registrations for client mode
3765+ // In client mode, we need classes to be registered so that serialization works
3766+ // when passing class instances to start(workflow)
3767+ if matches ! ( self . mode, TransformMode :: Client ) {
3768+ let mut sorted_classes: Vec < _ > =
3769+ self . classes_needing_serialization . drain ( ) . collect ( ) ;
3770+ sorted_classes. sort ( ) ;
3771+ for class_name in sorted_classes {
3772+ let registration_call =
3773+ self . create_class_serialization_registration ( & class_name) ;
37503774 module. body . push ( ModuleItem :: Stmt ( registration_call) ) ;
37513775 }
37523776 }
@@ -3935,7 +3959,13 @@ impl VisitMut for StepTransform {
39353959 }
39363960 }
39373961 TransformMode :: Client => {
3938- // No imports needed for workflow mode
3962+ // In client mode, we still need class serialization registration
3963+ // so that classes can be serialized when passed to start(workflow)
3964+ let needs_class_serialization =
3965+ !self . classes_needing_serialization . is_empty ( ) ;
3966+ if needs_class_serialization {
3967+ module_items. push ( self . create_class_serialization_import ( ) ) ;
3968+ }
39393969 }
39403970 }
39413971
@@ -3951,6 +3981,18 @@ impl VisitMut for StepTransform {
39513981 }
39523982 }
39533983
3984+ // Add class serialization registrations for client mode (Script case)
3985+ if matches ! ( self . mode, TransformMode :: Client ) {
3986+ let mut sorted_classes: Vec < _ > =
3987+ self . classes_needing_serialization . drain ( ) . collect ( ) ;
3988+ sorted_classes. sort ( ) ;
3989+ for class_name in sorted_classes {
3990+ let registration_call =
3991+ self . create_class_serialization_registration ( & class_name) ;
3992+ module_items. push ( ModuleItem :: Stmt ( registration_call) ) ;
3993+ }
3994+ }
3995+
39543996 // Note: workflowId assignments are now handled in visit_mut_module_items
39553997
39563998 // Add metadata comment at the beginning of the module
0 commit comments