Skip to content

Commit da58f8a

Browse files
committed
chore: update swc
1 parent 0be9bb9 commit da58f8a

File tree

5 files changed

+38
-13
lines changed

5 files changed

+38
-13
lines changed

playground/dist/favicon.ico

-6 KB
Binary file not shown.

playground/dist/index.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

playground/dist/index.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

playground/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
console.log(lib)
1+
const a = 123;
2+
console.log(a)

src/lib.rs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#![deny(clippy::all)]
22

3-
use std::sync::Arc;
3+
use std::{path::PathBuf, sync::Arc};
44

55
use farmfe_core::{
66
config::{config_regex::ConfigRegex, Config},
77
context::CompilationContext,
88
error::CompilationError,
99
plugin::{Plugin, PluginProcessModuleHookParam, PluginTransformHookResult},
1010
swc_common::{comments::NoopComments, BytePos, Mark, SourceMap, DUMMY_SP},
11-
swc_ecma_ast,
11+
swc_ecma_ast::{self, Ident, Module},
1212
swc_ecma_parser::{EsConfig, Parser, StringInput, Syntax},
1313
};
1414

@@ -90,8 +90,12 @@ impl Plugin for FarmPluginReplaceDirname {
9090
new_name: "newVarName".to_string(),
9191
};
9292
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+
});
9599
let mut buf = vec![];
96100
let writer = Box::new(JsWriter::new(cm.clone(), "\n", &mut buf, None));
97101
let mut emitter = Emitter {
@@ -104,11 +108,34 @@ impl Plugin for FarmPluginReplaceDirname {
104108
comments: None,
105109
wr: writer,
106110
};
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);
112123
Ok(Some(()))
113124
}
114125
}
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

Comments
 (0)