@@ -13,10 +13,11 @@ use lightningcss::{
13
13
} ;
14
14
use swc_common:: { Span , DUMMY_SP } ;
15
15
use swc_ecma_ast:: {
16
- BindingIdent , CallExpr , Callee , Decl , Expr , ExprOrSpread , Ident , ImportDecl , ImportSpecifier ,
17
- JSXAttr , JSXAttrName , JSXAttrOrSpread , JSXAttrValue , JSXElement , JSXElementName , JSXExpr ,
18
- JSXExprContainer , JSXFragment , KeyValueProp , Lit , Module , ModuleDecl , ModuleItem , Null ,
19
- ObjectLit , Pat , Prop , PropName , PropOrSpread , Stmt , Str , VarDecl , VarDeclKind , VarDeclarator ,
16
+ BindingIdent , CallExpr , Callee , Decl , Expr , ExprOrSpread , Ident , ImportDecl ,
17
+ ImportNamedSpecifier , ImportSpecifier , JSXAttr , JSXAttrName , JSXAttrOrSpread , JSXAttrValue ,
18
+ JSXElement , JSXElementName , JSXExpr , JSXExprContainer , JSXFragment , KeyValueProp , Lit , Module ,
19
+ ModuleDecl , ModuleExportName , ModuleItem , Null , ObjectLit , Pat , Prop , PropName , PropOrSpread ,
20
+ Stmt , Str , VarDecl , VarDeclKind , VarDeclarator ,
20
21
} ;
21
22
use swc_ecma_visit:: {
22
23
noop_visit_mut_type, noop_visit_type, Visit , VisitAll , VisitAllWith , VisitMut , VisitMutWith ,
@@ -177,19 +178,12 @@ impl<'a> VisitAll for AstVisitor<'a> {
177
178
}
178
179
179
180
pub struct ModuleMutVisitor < ' a > {
180
- pub insert_module : Rc < RefCell < Module > > ,
181
181
pub all_style : Rc < RefCell < HashMap < String , StyleDeclaration < ' a > > > > ,
182
182
}
183
183
184
184
impl < ' a > ModuleMutVisitor < ' a > {
185
- pub fn new (
186
- all_style : Rc < RefCell < HashMap < String , StyleDeclaration < ' a > > > > ,
187
- insert_module : Rc < RefCell < Module > > ,
188
- ) -> Self {
189
- ModuleMutVisitor {
190
- all_style,
191
- insert_module,
192
- }
185
+ pub fn new ( all_style : Rc < RefCell < HashMap < String , StyleDeclaration < ' a > > > > ) -> Self {
186
+ ModuleMutVisitor { all_style }
193
187
}
194
188
}
195
189
@@ -271,15 +265,29 @@ impl<'a> VisitMut for ModuleMutVisitor<'a> {
271
265
if last_import_index != 0 {
272
266
last_import_index += 1 ;
273
267
}
268
+ // 插入代码 import { calcDynamicStyle } from '@tarojs/runtime'
269
+ module. body . insert (
270
+ last_import_index,
271
+ ModuleItem :: ModuleDecl ( ModuleDecl :: Import ( ImportDecl {
272
+ span : DUMMY_SP ,
273
+ specifiers : vec ! [ ImportSpecifier :: Named ( ImportNamedSpecifier {
274
+ span: DUMMY_SP ,
275
+ local: Ident :: new( "calcDynamicStyle" . into( ) , DUMMY_SP ) ,
276
+ imported: Some ( ModuleExportName :: Ident ( Ident :: new(
277
+ "calcDynamicStyle" . into( ) ,
278
+ DUMMY_SP ,
279
+ ) ) ) ,
280
+ is_type_only: false ,
281
+ } ) ] ,
282
+ src : Box :: new ( Str :: from ( "@tarojs/runtime" ) ) ,
283
+ type_only : false ,
284
+ with : None ,
285
+ } ) ) ,
286
+ ) ;
287
+ last_import_index += 1 ;
274
288
module
275
289
. body
276
290
. insert ( last_import_index, ModuleItem :: Stmt ( inner_style_stmt) ) ;
277
- for item in self . insert_module . borrow ( ) . body . iter ( ) {
278
- if last_import_index != 0 {
279
- last_import_index += 1 ;
280
- }
281
- module. body . insert ( last_import_index, item. clone ( ) ) ;
282
- }
283
291
}
284
292
}
285
293
@@ -586,10 +594,15 @@ impl<'a> VisitMut for JSXMutVisitor<'a> {
586
594
let fun_call_expr = Expr :: Call ( CallExpr {
587
595
span : DUMMY_SP ,
588
596
callee : Callee :: Expr ( Box :: new ( Expr :: Ident ( Ident :: new (
589
- "__calc_style__ " . into ( ) ,
597
+ "calcDynamicStyle " . into ( ) ,
590
598
DUMMY_SP ,
591
599
) ) ) ) ,
592
600
args : vec ! [
601
+ ExprOrSpread :: from( Box :: new( Expr :: Ident ( Ident {
602
+ span: DUMMY_SP ,
603
+ sym: "__inner_style__" . into( ) ,
604
+ optional: false ,
605
+ } ) ) ) ,
593
606
match class_attr_value {
594
607
Some ( value) => ExprOrSpread :: from( Box :: new( value) ) ,
595
608
None => ExprOrSpread :: from( Box :: new( Expr :: Lit ( Lit :: Null ( Null { span: DUMMY_SP } ) ) ) ) ,
0 commit comments