Skip to content

Commit

Permalink
perf(minifier): InjectGlobalVariables only add to `replaced_dot_def…
Browse files Browse the repository at this point in the history
…ines` once for each (#4803)

`InjectGlobalVariables` minifier plugin was previously adding an entry to `replaced_dot_defines` each time it made a replacement, resulting in many duplicate entries if a single dot define gets replaced multiple times. Only add once for each dot define.
  • Loading branch information
overlookmotel committed Aug 10, 2024
1 parent 35f2742 commit 504ac0b
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions crates/oxc_minifier/src/plugins/inject_global_variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,18 @@ impl<'a> InjectGlobalVariables<'a> {
if let Expression::StaticMemberExpression(member) = expr {
for DotDefineState { dot_define, value_atom } in &mut self.dot_defines {
if ReplaceGlobalDefines::is_dot_define(dot_define, member) {
// Create `Atom` for replacement lazily on first replacement
// If this is first replacement made for this dot define,
// create `Atom` for replacement, and record in `replaced_dot_defines`
if value_atom.is_none() {
*value_atom = Some(self.ast.atom(dot_define.value.as_str()));

self.replaced_dot_defines
.push((dot_define.parts[0].clone(), dot_define.value.clone()));
}
let value_atom = value_atom.as_ref().unwrap().clone();

let value = self.ast.expression_identifier_reference(SPAN, value_atom);
*expr = value;
self.replaced_dot_defines
.push((dot_define.parts[0].clone(), dot_define.value.clone()));
break;
}
}
Expand Down

0 comments on commit 504ac0b

Please sign in to comment.