1
1
#![ deny( clippy:: all) ]
2
2
3
- use std:: sync:: Arc ;
3
+ use std:: { path :: PathBuf , sync:: Arc } ;
4
4
5
5
use farmfe_core:: {
6
6
config:: { config_regex:: ConfigRegex , Config } ,
7
7
context:: CompilationContext ,
8
8
error:: CompilationError ,
9
9
plugin:: { Plugin , PluginProcessModuleHookParam , PluginTransformHookResult } ,
10
10
swc_common:: { comments:: NoopComments , BytePos , Mark , SourceMap , DUMMY_SP } ,
11
- swc_ecma_ast,
11
+ swc_ecma_ast:: { self , Ident , Module } ,
12
12
swc_ecma_parser:: { EsConfig , Parser , StringInput , Syntax } ,
13
13
} ;
14
14
@@ -90,8 +90,12 @@ impl Plugin for FarmPluginReplaceDirname {
90
90
new_name : "newVarName" . to_string ( ) ,
91
91
} ;
92
92
let mut module = module;
93
- module. visit_mut_with ( & mut replacer) ;
94
- let cm = Arc :: new ( SourceMap :: default ( ) ) ;
93
+ // module.visit_mut_with(&mut replacer);
94
+ // let cm = Arc::new(SourceMap::default());
95
+ let ( cm, _) = create_swc_source_map ( Source {
96
+ path : PathBuf :: from ( & param. module_id . to_string ( ) ) ,
97
+ content : param. content . clone ( ) ,
98
+ } ) ;
95
99
let mut buf = vec ! [ ] ;
96
100
let writer = Box :: new ( JsWriter :: new ( cm. clone ( ) , "\n " , & mut buf, None ) ) ;
97
101
let mut emitter = Emitter {
@@ -104,11 +108,34 @@ impl Plugin for FarmPluginReplaceDirname {
104
108
comments : None ,
105
109
wr : writer,
106
110
} ;
107
-
108
- emitter. emit_module ( & module) . expect ( "Failed to emit module" ) ;
109
- let code = String :: from_utf8 ( buf) . expect ( "Failed to convert buffer to string" ) ;
110
- param. content = code. into ( ) ;
111
- println ! ( "param.content: {}" , param. content) ;
111
+ // println!("Emitting module {:#?}", module);
112
+ // emitter.emit_module(&module).expect("Failed to emit module");
113
+ // let code = String::from_utf8(buf).expect("Failed to convert buffer to string");
114
+ // param.content = Arc::new(code);
115
+ // println!("Module: {:?}", param.module_id.relative_path());
116
+ // let ast = &mut param.meta.as_script_mut().ast;
117
+ println ! ( "AST: {:#?}" , param. meta. as_script_mut( ) . ast) ;
118
+ println ! ( "module: {:#?}" , module) ;
119
+ // param.meta.as_script_mut().ast = module;
120
+ let ast = & mut param. meta . as_script_mut ( ) . ast ;
121
+ // println!("AST: {:#?}", ast);
122
+ // replace_lib_with_aaa(ast);
112
123
Ok ( Some ( ( ) ) )
113
124
}
114
125
}
126
+
127
+ pub fn replace_lib_with_aaa ( ast : & mut Module ) {
128
+ struct ReplaceLibVisitor ;
129
+
130
+ impl VisitMut for ReplaceLibVisitor {
131
+ fn visit_mut_ident ( & mut self , ident : & mut Ident ) {
132
+ println ! ( "Ident: {:?}" , ident. sym) ;
133
+ if ident. sym == * "a" {
134
+ println ! ( "Found a" ) ;
135
+ * ident = Ident :: new ( "bbbbbb" . into ( ) , DUMMY_SP ) ;
136
+ }
137
+ }
138
+ }
139
+ let mut visitor = ReplaceLibVisitor ;
140
+ ast. visit_mut_with ( & mut visitor) ;
141
+ }
0 commit comments