@@ -12,7 +12,7 @@ use farmfe_core::{
12
12
error:: CompilationError ,
13
13
plugin:: { Plugin , PluginProcessModuleHookParam , PluginTransformHookResult } ,
14
14
swc_common:: { comments:: NoopComments , BytePos , Mark , SourceMap , DUMMY_SP } ,
15
- swc_ecma_ast:: { self , Ident , Module } ,
15
+ swc_ecma_ast:: { self , Expr , Ident , Lit , MemberExpr , MemberProp , Module , Str } ,
16
16
swc_ecma_parser:: { EsConfig , Parser , StringInput , Syntax } ,
17
17
} ;
18
18
@@ -45,6 +45,7 @@ impl Default for ReplaceDirnameOptions {
45
45
}
46
46
}
47
47
}
48
+
48
49
impl FarmPluginReplaceDirname {
49
50
fn new ( config : & Config , options : String ) -> Self {
50
51
let options: ReplaceDirnameOptions = serde_json:: from_str ( & options) . unwrap_or_default ( ) ;
@@ -66,54 +67,76 @@ impl Plugin for FarmPluginReplaceDirname {
66
67
if !filter. execute ( & param. module_id . relative_path ( ) ) {
67
68
return Ok ( None ) ;
68
69
}
69
- let absolute_path = env:: current_dir ( )
70
+
71
+ let file_path = env:: current_dir ( )
70
72
. unwrap ( )
71
73
. join ( param. module_id . relative_path ( ) ) ;
72
74
73
- let dir_path: & str = Path :: new ( & absolute_path )
75
+ let dir_path: & str = Path :: new ( & file_path )
74
76
. parent ( )
75
77
. map_or ( "" , |p| p. to_str ( ) . unwrap_or ( "" ) ) ;
76
78
77
79
let ast = & mut param. meta . as_script_mut ( ) . ast ;
78
- replace_dirname_with_ast ( param ) ;
80
+ replace_dirname_with_ast ( ast , dir_path , file_path . to_str ( ) . unwrap ( ) ) ;
79
81
Ok ( Some ( ( ) ) )
80
82
}
81
83
}
82
84
83
- pub fn replace_dirname_with_ast ( param : & mut PluginProcessModuleHookParam ) {
84
- let absolute_path = env:: current_dir ( )
85
- . unwrap ( )
86
- . join ( param. module_id . relative_path ( ) ) ;
87
-
88
- let dir_path: & str = Path :: new ( & absolute_path)
89
- . parent ( )
90
- . map_or ( "" , |p| p. to_str ( ) . unwrap_or ( "" ) ) ;
91
-
92
- let ast = & mut param. meta . as_script_mut ( ) . ast ;
93
-
85
+ pub fn replace_dirname_with_ast ( ast : & mut Module , dir_path : & str , file_path : & str ) {
94
86
struct ReplaceLibVisitor < ' a > {
95
87
dir_path : & ' a str ,
96
- absolute_path : & ' a str ,
88
+ file_path : & ' a str ,
97
89
}
98
90
99
91
impl < ' a > VisitMut for ReplaceLibVisitor < ' a > {
100
- fn visit_mut_ident ( & mut self , ident : & mut Ident ) {
101
- match & * ident. sym {
102
- "__dirname" => {
103
- * ident = Ident :: new ( format ! ( "\" {}\" " , self . dir_path) . into ( ) , DUMMY_SP ) ;
92
+ fn visit_mut_expr ( & mut self , expr : & mut Expr ) {
93
+ match expr {
94
+ Expr :: Ident ( ident) => {
95
+ match & * ident. sym {
96
+ "__dirname" => {
97
+ * expr = Expr :: Lit ( Lit :: Str ( Str {
98
+ value : self . dir_path . into ( ) ,
99
+ span : DUMMY_SP ,
100
+ raw : None ,
101
+ } ) ) ;
102
+ }
103
+ "__filename" => {
104
+ * expr = Expr :: Lit ( Lit :: Str ( Str {
105
+ value : self . file_path . into ( ) ,
106
+ span : DUMMY_SP ,
107
+ raw : None ,
108
+ } ) ) ;
109
+ }
110
+ _ => { }
111
+ }
112
+ }
113
+ Expr :: Member ( MemberExpr { obj, prop, .. } ) => {
114
+ if let Expr :: MetaProp ( meta_prop) = & * * obj {
115
+ if meta_prop. kind == swc_ecma_ast:: MetaPropKind :: ImportMeta {
116
+ if let MemberProp :: Ident ( ident) = & prop {
117
+ if ident. sym == "url" {
118
+ * expr = Expr :: Lit ( Lit :: Str ( Str {
119
+ value : self . file_path . into ( ) ,
120
+ span : DUMMY_SP ,
121
+ raw : None ,
122
+ } ) ) ;
123
+ }
124
+ }
125
+ }
126
+ }
104
127
}
105
- "__filename" => {
106
- * ident = Ident :: new ( format ! ( "\" {}\" " , self . absolute_path) . into ( ) , DUMMY_SP ) ;
128
+ _ => {
129
+ // 递归访问子节点
130
+ expr. visit_mut_children_with ( self ) ;
107
131
}
108
- _ => { }
109
132
}
110
133
}
111
134
}
112
135
113
136
let mut visitor = ReplaceLibVisitor {
114
137
dir_path,
115
- absolute_path : absolute_path . to_str ( ) . unwrap ( ) ,
138
+ file_path ,
116
139
} ;
117
140
118
141
ast. visit_mut_with ( & mut visitor) ;
119
- }
142
+ }
0 commit comments