@@ -13,10 +13,11 @@ use lightningcss::{
1313} ;
1414use swc_common:: { Span , DUMMY_SP } ;
1515use 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 ,
2021} ;
2122use swc_ecma_visit:: {
2223 noop_visit_mut_type, noop_visit_type, Visit , VisitAll , VisitAllWith , VisitMut , VisitMutWith ,
@@ -177,19 +178,12 @@ impl<'a> VisitAll for AstVisitor<'a> {
177178}
178179
179180pub struct ModuleMutVisitor < ' a > {
180- pub insert_module : Rc < RefCell < Module > > ,
181181 pub all_style : Rc < RefCell < HashMap < String , StyleDeclaration < ' a > > > > ,
182182}
183183
184184impl < ' 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 }
193187 }
194188}
195189
@@ -271,15 +265,29 @@ impl<'a> VisitMut for ModuleMutVisitor<'a> {
271265 if last_import_index != 0 {
272266 last_import_index += 1 ;
273267 }
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 ;
274288 module
275289 . body
276290 . 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- }
283291 }
284292}
285293
@@ -586,10 +594,15 @@ impl<'a> VisitMut for JSXMutVisitor<'a> {
586594 let fun_call_expr = Expr :: Call ( CallExpr {
587595 span : DUMMY_SP ,
588596 callee : Callee :: Expr ( Box :: new ( Expr :: Ident ( Ident :: new (
589- "__calc_style__ " . into ( ) ,
597+ "calcDynamicStyle " . into ( ) ,
590598 DUMMY_SP ,
591599 ) ) ) ) ,
592600 args : vec ! [
601+ ExprOrSpread :: from( Box :: new( Expr :: Ident ( Ident {
602+ span: DUMMY_SP ,
603+ sym: "__inner_style__" . into( ) ,
604+ optional: false ,
605+ } ) ) ) ,
593606 match class_attr_value {
594607 Some ( value) => ExprOrSpread :: from( Box :: new( value) ) ,
595608 None => ExprOrSpread :: from( Box :: new( Expr :: Lit ( Lit :: Null ( Null { span: DUMMY_SP } ) ) ) ) ,
0 commit comments