From 3def02079527a9bc31d56a0fe8ce64245562a086 Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY Date: Tue, 20 Aug 2024 23:50:54 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=A4=96=20tweak=20import.meta.env?= =?UTF-8?q?=20match?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/plugins/replace_global_defines.rs | 14 ++++++++++++-- .../tests/plugins/replace_global_defines.rs | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/crates/oxc_minifier/src/plugins/replace_global_defines.rs b/crates/oxc_minifier/src/plugins/replace_global_defines.rs index 1d59933f147e13..3616f281bfa374 100644 --- a/crates/oxc_minifier/src/plugins/replace_global_defines.rs +++ b/crates/oxc_minifier/src/plugins/replace_global_defines.rs @@ -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 @@ -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; @@ -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; } diff --git a/crates/oxc_minifier/tests/plugins/replace_global_defines.rs b/crates/oxc_minifier/tests/plugins/replace_global_defines.rs index 902e3e6b0e9c53..b87cafbaae9200 100644 --- a/crates/oxc_minifier/tests/plugins/replace_global_defines.rs +++ b/crates/oxc_minifier/tests/plugins/replace_global_defines.rs @@ -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); } }