Skip to content

Commit

Permalink
chore: 🤖 tweak import.meta.env match
Browse files Browse the repository at this point in the history
  • Loading branch information
IWANABETHATGUY committed Aug 20, 2024
1 parent e364fc7 commit 3def020
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 12 additions & 2 deletions crates/oxc_minifier/src/plugins/replace_global_defines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,16 @@ impl<'a> ReplaceGlobalDefines<'a> {

let mut current_part_member_expression = Some(member);
let mut cur_part_name = &member.property.name;
let mut has_matched_part = false;
let mut is_full_match = true;
let mut i = meta_define.parts.len() - 1;
let mut has_matched_part = false;
loop {
let part = &meta_define.parts[i];
let matched = cur_part_name.as_str() == part;
if matched {
has_matched_part = true;
} else {
is_full_match = false;
// Considering import.meta.env.*
// ```js
// import.meta.env.test // should matched
Expand All @@ -270,7 +272,11 @@ impl<'a> ReplaceGlobalDefines<'a> {
Some(member)
}
Expression::MetaProperty(_) => {
return has_matched_part;
if meta_define.postfix_wildcard {
// `import.meta.env` should not match `import.meta.env.*`
return has_matched_part && !is_full_match;
}
return true;
}
Expression::Identifier(_) => {
return false;
Expand All @@ -281,6 +287,10 @@ impl<'a> ReplaceGlobalDefines<'a> {
return false;
};

// Considering `import.meta.env` -> `undefined`, for the first loop the i is already
// 0, if it did not match and still reach there, that means
// current_part_member_expression is still something, and possible to match in the
// further loop
if i == 0 && matched {
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn replace_global_definitions_dot_with_postfix_wildcard() {
let config =
ReplaceGlobalDefinesConfig::new(&[("import.meta.env.*", "undefined")]).unwrap();
test("import.meta.env.result", "undefined", config.clone());
test("import.meta.env", "undefined", config);
test("import.meta.env", "import.meta.env", config);
}
}

Expand Down

0 comments on commit 3def020

Please sign in to comment.