1
1
use swc_common:: DUMMY_SP ;
2
2
use swc_core:: ecma:: {
3
3
ast:: * ,
4
+ atoms:: JsWord ,
4
5
visit:: { noop_visit_mut_type, VisitMut , VisitMutWith } ,
5
6
} ;
6
7
7
8
#[ derive( Default ) ]
8
9
pub struct InjectAcceptVisitor {
9
10
class_names : Vec < Ident > ,
11
+ import_paths : Vec < JsWord > ,
10
12
}
11
13
12
- fn runtime_accept ( class_names : & [ Ident ] ) -> ModuleItem {
14
+ fn runtime_accept ( class_names : & Vec < Ident > , import_paths : & Vec < JsWord > ) -> ModuleItem {
13
15
ModuleItem :: Stmt ( Stmt :: Expr ( ExprStmt {
14
16
span : DUMMY_SP ,
15
17
expr : Box :: new ( Expr :: Call ( CallExpr {
@@ -27,13 +29,66 @@ fn runtime_accept(class_names: &[Ident]) -> ModuleItem {
27
29
optional : false ,
28
30
} ) ,
29
31
} ) ) ) ,
30
- args : class_names
31
- . iter ( )
32
- . map ( |class_name| ExprOrSpread {
32
+ args : vec ! [
33
+ ExprOrSpread {
33
34
spread: None ,
34
- expr : Box :: new ( Expr :: Ident ( class_name. clone ( ) ) ) ,
35
- } )
36
- . collect ( ) ,
35
+ expr: Box :: new( Expr :: Ident ( Ident {
36
+ span: DUMMY_SP ,
37
+ sym: "module" . into( ) ,
38
+ optional: false ,
39
+ } ) ) ,
40
+ } ,
41
+ ExprOrSpread {
42
+ spread: None ,
43
+ expr: Box :: new( Expr :: Object ( ObjectLit {
44
+ span: DUMMY_SP ,
45
+ props: vec![
46
+ PropOrSpread :: Prop ( Box :: new( Prop :: KeyValue ( KeyValueProp {
47
+ key: PropName :: Ident ( Ident {
48
+ span: DUMMY_SP ,
49
+ sym: "klasses" . into( ) ,
50
+ optional: false ,
51
+ } ) ,
52
+ value: Box :: new( Expr :: Array ( ArrayLit {
53
+ span: DUMMY_SP ,
54
+ elems: class_names
55
+ . iter( )
56
+ . map( |class_name| {
57
+ Some ( ExprOrSpread {
58
+ spread: None ,
59
+ expr: Box :: new( Expr :: Ident ( class_name. clone( ) ) ) ,
60
+ } )
61
+ } )
62
+ . collect( ) ,
63
+ } ) ) ,
64
+ } ) ) ) ,
65
+ PropOrSpread :: Prop ( Box :: new( Prop :: KeyValue ( KeyValueProp {
66
+ key: PropName :: Ident ( Ident {
67
+ span: DUMMY_SP ,
68
+ sym: "dependencies" . into( ) ,
69
+ optional: false ,
70
+ } ) ,
71
+ value: Box :: new( Expr :: Array ( ArrayLit {
72
+ span: DUMMY_SP ,
73
+ elems: import_paths
74
+ . iter( )
75
+ . map( |import_path| {
76
+ Some ( ExprOrSpread {
77
+ spread: None ,
78
+ expr: Box :: new( Expr :: Lit ( Lit :: Str ( Str {
79
+ span: DUMMY_SP ,
80
+ value: import_path. clone( ) ,
81
+ raw: None ,
82
+ } ) ) ) ,
83
+ } )
84
+ } )
85
+ . collect( ) ,
86
+ } ) ) ,
87
+ } ) ) ) ,
88
+ ] ,
89
+ } ) ) ,
90
+ } ,
91
+ ] ,
37
92
type_args : None ,
38
93
} ) ) ,
39
94
} ) )
@@ -43,13 +98,13 @@ impl VisitMut for InjectAcceptVisitor {
43
98
noop_visit_mut_type ! ( ) ;
44
99
45
100
fn visit_mut_module ( & mut self , n : & mut Module ) {
46
- self . class_names . push ( Ident {
47
- span : DUMMY_SP ,
48
- sym : "module" . into ( ) ,
49
- optional : false ,
50
- } ) ;
51
101
n. visit_mut_children_with ( self ) ;
52
- n. body . push ( runtime_accept ( & self . class_names ) ) ;
102
+ n. body
103
+ . push ( runtime_accept ( & self . class_names , & self . import_paths ) ) ;
104
+ }
105
+
106
+ fn visit_mut_import_decl ( & mut self , n : & mut ImportDecl ) {
107
+ self . import_paths . push ( n. src . value . clone ( ) ) ;
53
108
}
54
109
55
110
fn visit_mut_class_decl ( & mut self , n : & mut ClassDecl ) {
0 commit comments