Skip to content

Commit

Permalink
fix: 当用到 __inner_style__ 时才插入
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Oct 24, 2023
1 parent 0f3111b commit c10cffc
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use swc_ecma_ast::{
};
use swc_ecma_visit::{
noop_visit_mut_type, noop_visit_type, Visit, VisitAll, VisitAllWith, VisitMut, VisitMutWith,
VisitWith,
};

use crate::{
Expand Down Expand Up @@ -47,6 +48,20 @@ impl Hash for SpanKey {

pub type JSXRecord = HashMap<SpanKey, Element>;

struct VarChecker {
found: bool,
}

impl Visit for VarChecker {
noop_visit_type!();

fn visit_ident(&mut self, ident: &Ident) {
if ident.sym == "__inner_style__" {
self.found = true;
}
}
}

pub struct CollectVisitor {
pub taro_components: Vec<String>,
}
Expand Down Expand Up @@ -276,9 +291,15 @@ impl VisitMut for ModuleMutVisitor {
})),
);
last_import_index += 1;
module
.body
.insert(last_import_index, ModuleItem::Stmt(inner_style_stmt));
let mut var_checker = VarChecker { found: false };
module.visit_with(&mut var_checker);
println!("var_checker.found: {}", var_checker.found);
if var_checker.found {
// 插入代码 const __inner_style__ = calcDynamicStyle(__inner_style__)
module
.body
.insert(last_import_index, ModuleItem::Stmt(inner_style_stmt));
}
}
}

Expand Down

0 comments on commit c10cffc

Please # to comment.