Skip to content

Commit

Permalink
fix: complicated define env replace
Browse files Browse the repository at this point in the history
  • Loading branch information
xusd320 committed Aug 21, 2024
1 parent efd484a commit 885b592
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 74 deletions.
59 changes: 30 additions & 29 deletions crates/mako/src/visitors/env_replacer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,21 @@ impl VisitMut for EnvReplacer {
}

match expr {
Expr::Member(MemberExpr {
obj,
prop: MemberProp::Ident(Ident { sym, .. }),
..
}) => {
let mut member_visit_path = sym.to_string();
Expr::Member(MemberExpr { obj, prop, .. }) => {
let mut member_visit_path = match prop {
MemberProp::Ident(Ident { sym, .. }) => sym.to_string(),
MemberProp::Computed(ComputedPropName {
expr: expr_computed,
..
}) => match expr_computed.as_ref() {
Expr::Lit(Lit::Str(Str { value, .. })) => value.to_string(),

Expr::Lit(Lit::Num(Number { value, .. })) => value.to_string(),
_ => return,
},
_ => return,
};

let mut current_member_obj = obj.as_ref();

while let Expr::Member(MemberExpr { obj, prop, .. }) = current_member_obj {
Expand All @@ -70,10 +79,10 @@ impl VisitMut for EnvReplacer {
member_visit_path.push('.');
member_visit_path.push_str(&value.to_string());
}
_ => (),
_ => return,
}
}
_ => {}
_ => return,
}
current_member_obj = obj.as_ref();
}
Expand All @@ -92,28 +101,7 @@ impl VisitMut for EnvReplacer {
*expr = env
}
}
Expr::Member(MemberExpr {
obj: box Expr::Ident(Ident { sym, .. }),
prop:
MemberProp::Computed(ComputedPropName {
expr: expr_computed,
..
}),
..
}) => match expr_computed.as_ref() {
Expr::Lit(Lit::Str(Str { value, .. })) => {
if let Some(env) = self.get_define_env(&format!("{}.{}", sym, value)) {
*expr = env
}
}

Expr::Lit(Lit::Num(Number { value, .. })) => {
if let Some(env) = self.get_define_env(&format!("{}.{}", sym, value)) {
*expr = env
}
}
_ => (),
},
Expr::Ident(Ident { sym, .. }) => {
if let Some(env) = self.get_define_env(sym.as_ref()) {
*expr = env
Expand Down Expand Up @@ -374,6 +362,19 @@ log([
);
}

#[test]
fn test_complicated() {
assert_eq!(
run(
r#"log(A.v["v"])"#,
hashmap! {
"A.v.v".to_string() => json!(1)
}
),
r#"log(1);"#
);
}

fn run(js_code: &str, envs: HashMap<String, Value>) -> String {
let mut test_utils = TestUtils::gen_js_ast(js_code);
let envs = build_env_map(envs, &test_utils.context).unwrap();
Expand Down
51 changes: 6 additions & 45 deletions crates/mako/src/visitors/import_meta_env_replacer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use swc_core::ecma::ast::{
PropOrSpread,
};
use swc_core::ecma::atoms::js_word;
use swc_core::ecma::utils::{quote_ident, quote_str};
use swc_core::ecma::utils::{quote_ident, quote_str, ExprFactory};
use swc_core::ecma::visit::{VisitMut, VisitMutWith};

#[derive(Debug)]
Expand All @@ -19,46 +19,6 @@ impl ImportMetaEnvReplacer {
}

impl VisitMut for ImportMetaEnvReplacer {
fn visit_mut_member_expr(&mut self, member_expr: &mut MemberExpr) {
match member_expr {
MemberExpr {
obj:
box Expr::Member(MemberExpr {
obj:
box Expr::MetaProp(MetaPropExpr {
kind: MetaPropKind::ImportMeta,
..
}),
prop:
MemberProp::Ident(Ident {
sym: js_word!("env"),
..
}),
..
}),
..
} => {
// replace import.meta.env.MODE with "({ MODE: 'production' }).MODE"
*member_expr.obj = Expr::Paren(swc_core::ecma::ast::ParenExpr {
span: DUMMY_SP,
expr: ObjectLit {
props: vec![PropOrSpread::Prop(
Prop::KeyValue(KeyValueProp {
key: quote_ident!("MODE").into(),
value: quote_str!(self.mode.clone()).into(),
})
.into(),
)],
span: DUMMY_SP,
}
.into(),
});
}

_ => member_expr.visit_mut_children_with(self),
}
}

fn visit_mut_expr(&mut self, expr: &mut Expr) {
match expr {
Expr::Member(MemberExpr {
Expand All @@ -75,7 +35,7 @@ impl VisitMut for ImportMetaEnvReplacer {
..
}) => {
// replace import.meta.env with "({ MODE: 'production' })"
*expr = Expr::Object(ObjectLit {
*expr = ObjectLit {
props: vec![PropOrSpread::Prop(
Prop::KeyValue(KeyValueProp {
key: quote_ident!("MODE").into(),
Expand All @@ -84,7 +44,8 @@ impl VisitMut for ImportMetaEnvReplacer {
.into(),
)],
span: DUMMY_SP,
});
}
.wrap_with_paren();
}
_ => expr.visit_mut_children_with(self),
}
Expand All @@ -106,9 +67,9 @@ mod tests {
run(
r#"typeof import.meta.env === "object" ? import.meta.env.MODE : process.env.NODE_ENV"#
),
r#"typeof {
r#"typeof ({
MODE: "development"
} === "object" ? ({
}) === "object" ? ({
MODE: "development"
}).MODE : process.env.NODE_ENV;"#
);
Expand Down

0 comments on commit 885b592

Please sign in to comment.