Skip to content

Commit c10cffc

Browse files
committed
fix: 当用到 __inner_style__ 时才插入
1 parent 0f3111b commit c10cffc

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/visitor.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use swc_ecma_ast::{
1616
};
1717
use swc_ecma_visit::{
1818
noop_visit_mut_type, noop_visit_type, Visit, VisitAll, VisitAllWith, VisitMut, VisitMutWith,
19+
VisitWith,
1920
};
2021

2122
use crate::{
@@ -47,6 +48,20 @@ impl Hash for SpanKey {
4748

4849
pub type JSXRecord = HashMap<SpanKey, Element>;
4950

51+
struct VarChecker {
52+
found: bool,
53+
}
54+
55+
impl Visit for VarChecker {
56+
noop_visit_type!();
57+
58+
fn visit_ident(&mut self, ident: &Ident) {
59+
if ident.sym == "__inner_style__" {
60+
self.found = true;
61+
}
62+
}
63+
}
64+
5065
pub struct CollectVisitor {
5166
pub taro_components: Vec<String>,
5267
}
@@ -276,9 +291,15 @@ impl VisitMut for ModuleMutVisitor {
276291
})),
277292
);
278293
last_import_index += 1;
279-
module
280-
.body
281-
.insert(last_import_index, ModuleItem::Stmt(inner_style_stmt));
294+
let mut var_checker = VarChecker { found: false };
295+
module.visit_with(&mut var_checker);
296+
println!("var_checker.found: {}", var_checker.found);
297+
if var_checker.found {
298+
// 插入代码 const __inner_style__ = calcDynamicStyle(__inner_style__)
299+
module
300+
.body
301+
.insert(last_import_index, ModuleItem::Stmt(inner_style_stmt));
302+
}
282303
}
283304
}
284305

0 commit comments

Comments
 (0)