@@ -55,6 +55,80 @@ impl VisitMut for TransformVisitor {
5555 // We don't need to do anything besides imports here
5656 noop_visit_mut_type ! ( ) ;
5757
58+ // Can't use visit_mut_module_item for cases when we need to replace
59+ // the item with multiple statements
60+ fn visit_mut_module ( & mut self , module : & mut Module ) {
61+ let mut new_body = Vec :: with_capacity ( module. body . len ( ) ) ;
62+
63+ for mut item in module. body . drain ( ..) {
64+ self . visit_mut_module_item ( & mut item) ;
65+
66+ match & item {
67+ ModuleItem :: ModuleDecl ( ModuleDecl :: ExportDecl ( export_decl) ) => {
68+ let decl = export_decl. decl . clone ( ) ;
69+ let stmt_decl = ModuleItem :: Stmt ( Stmt :: Decl ( decl. clone ( ) ) ) ;
70+
71+ let mut ids = vec ! [ ] ;
72+
73+ match decl {
74+ Decl :: Var ( var_decl) => {
75+ for var_declarator in var_decl. decls . iter ( ) {
76+ if let Pat :: Ident ( BindingIdent { id, .. } ) = & var_declarator. name {
77+ ids. push ( id. clone ( ) ) ;
78+ }
79+ }
80+ }
81+ Decl :: Fn ( fn_decl) => {
82+ ids. push ( fn_decl. ident . clone ( ) ) ;
83+ }
84+ Decl :: Class ( class_decl) => {
85+ ids. push ( class_decl. ident . clone ( ) ) ;
86+ }
87+ _ => { }
88+ }
89+
90+ let props: Vec < PropOrSpread > = ids
91+ . iter ( )
92+ . map ( |ident| {
93+ PropOrSpread :: Prop ( Box :: new ( Prop :: KeyValue ( KeyValueProp {
94+ key : PropName :: Ident ( IdentName :: from ( ident. sym . clone ( ) ) ) ,
95+ value : Box :: new ( Expr :: Ident ( ident. clone ( ) ) ) ,
96+ } ) ) )
97+ } )
98+ . collect ( ) ;
99+
100+ let add_export_call = Expr :: Call ( CallExpr {
101+ span : DUMMY_SP ,
102+ callee : Callee :: Expr ( Box :: new ( Expr :: Ident ( Ident :: new (
103+ "addExport" . into ( ) ,
104+ DUMMY_SP ,
105+ SyntaxContext :: empty ( ) ,
106+ ) ) ) ) ,
107+ args : vec ! [ ExprOrSpread {
108+ spread: None ,
109+ expr: Box :: new( Expr :: Object ( ObjectLit {
110+ span: DUMMY_SP ,
111+ props,
112+ } ) ) ,
113+ } ] ,
114+ type_args : None ,
115+ ctxt : SyntaxContext :: empty ( ) ,
116+ } ) ;
117+
118+ let stmt_add_export = ModuleItem :: Stmt ( Stmt :: Expr ( ExprStmt {
119+ span : DUMMY_SP ,
120+ expr : Box :: new ( add_export_call) ,
121+ } ) ) ;
122+
123+ new_body. extend ( vec ! [ stmt_decl, stmt_add_export] ) ;
124+ }
125+ _ => new_body. push ( item) ,
126+ }
127+ }
128+
129+ module. body = new_body;
130+ }
131+
58132 fn visit_mut_module_item ( & mut self , item : & mut ModuleItem ) {
59133 if let ModuleItem :: ModuleDecl ( decl) = item {
60134 match decl {
0 commit comments