Skip to content

Commit 359a7f0

Browse files
committed
chore: finish plugin
1 parent 45d50a8 commit 359a7f0

File tree

6 files changed

+56
-194
lines changed

6 files changed

+56
-194
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "farm-plugin-replace-dirname",
3-
"version": "0.1.7",
3+
"version": "0.2.0",
44
"main": "scripts/index.js",
55
"types": "scripts/index.d.ts",
66
"type": "module",
@@ -30,7 +30,8 @@
3030
"./package.json": "./package.json"
3131
},
3232
"scripts": {
33-
"dev": "cargo watch -w src -s 'scripts/watch.sh'",
33+
"dev:mac": "cargo watch -w src -s 'scripts/watch.sh'",
34+
"dev:win": "cargo watch -w src -s 'cmd /C scripts\\watch.bat'",
3435
"bump": "npx changeset version",
3536
"play": "cd playground && farm",
3637
"build:play": "cd playground && farm build",

playground/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
const bbbbbb = 123132132132
22
console.log('Hello World', bbbbbb);
33
console.log(__dirname)
4+
console.log(__filename);
5+
console.log(import.meta.url);

scripts/watch.bat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@echo off
2+
echo Running watch script...
3+
npm run build

src/base.rs

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

src/lib.rs

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use farmfe_core::{
1212
error::CompilationError,
1313
plugin::{Plugin, PluginProcessModuleHookParam, PluginTransformHookResult},
1414
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},
1616
swc_ecma_parser::{EsConfig, Parser, StringInput, Syntax},
1717
};
1818

@@ -45,6 +45,7 @@ impl Default for ReplaceDirnameOptions {
4545
}
4646
}
4747
}
48+
4849
impl FarmPluginReplaceDirname {
4950
fn new(config: &Config, options: String) -> Self {
5051
let options: ReplaceDirnameOptions = serde_json::from_str(&options).unwrap_or_default();
@@ -66,54 +67,76 @@ impl Plugin for FarmPluginReplaceDirname {
6667
if !filter.execute(&param.module_id.relative_path()) {
6768
return Ok(None);
6869
}
69-
let absolute_path = env::current_dir()
70+
71+
let file_path = env::current_dir()
7072
.unwrap()
7173
.join(param.module_id.relative_path());
7274

73-
let dir_path: &str = Path::new(&absolute_path)
75+
let dir_path: &str = Path::new(&file_path)
7476
.parent()
7577
.map_or("", |p| p.to_str().unwrap_or(""));
7678

7779
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());
7981
Ok(Some(()))
8082
}
8183
}
8284

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) {
9486
struct ReplaceLibVisitor<'a> {
9587
dir_path: &'a str,
96-
absolute_path: &'a str,
88+
file_path: &'a str,
9789
}
9890

9991
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+
}
104127
}
105-
"__filename" => {
106-
*ident = Ident::new(format!("\"{}\"", self.absolute_path).into(), DUMMY_SP);
128+
_ => {
129+
// 递归访问子节点
130+
expr.visit_mut_children_with(self);
107131
}
108-
_ => {}
109132
}
110133
}
111134
}
112135

113136
let mut visitor = ReplaceLibVisitor {
114137
dir_path,
115-
absolute_path: absolute_path.to_str().unwrap(),
138+
file_path,
116139
};
117140

118141
ast.visit_mut_with(&mut visitor);
119-
}
142+
}

src/swc.rs

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

0 commit comments

Comments
 (0)