Skip to content

Commit

Permalink
fix(es/minifier): Make Finalizer inline literals (#8285)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8284
  • Loading branch information
Austaras authored Nov 15, 2023
1 parent b8c887f commit 73fec94
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions crates/swc_ecma_minifier/src/compress/optimize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ impl Vars {
{
let mut changed = false;
if !self.simple_functions.is_empty()
|| !self.lits.is_empty()
|| !self.lits_for_cmp.is_empty()
|| !self.lits_for_array_access.is_empty()
|| !self.removed.is_empty()
Expand All @@ -275,6 +276,7 @@ impl Vars {
simple_functions: &self.simple_functions,
lits_for_cmp: &self.lits_for_cmp,
lits_for_array_access: &self.lits_for_array_access,
lits: &self.lits,
vars_to_remove: &self.removed,
changed: false,
};
Expand Down
11 changes: 11 additions & 0 deletions crates/swc_ecma_minifier/src/compress/optimize/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ pub(crate) fn is_valid_for_lhs(e: &Expr) -> bool {
#[derive(Clone, Copy)]
pub(crate) struct Finalizer<'a> {
pub simple_functions: &'a FxHashMap<Id, Box<Expr>>,
pub lits: &'a FxHashMap<Id, Box<Expr>>,
pub lits_for_cmp: &'a FxHashMap<Id, Box<Expr>>,
pub lits_for_array_access: &'a FxHashMap<Id, Box<Expr>>,

Expand Down Expand Up @@ -378,6 +379,16 @@ impl VisitMut for Finalizer<'_> {
});
}

fn visit_mut_expr(&mut self, n: &mut Expr) {
if let Expr::Ident(i) = n {
if let Some(expr) = self.lits.get(&i.to_id()) {
*n = *expr.clone();
}
} else {
n.visit_mut_children_with(self);
}
}

fn visit_mut_stmts(&mut self, n: &mut Vec<Stmt>) {
self.maybe_par(*HEAVY_TASK_PARALLELS, n, |v, n| {
n.visit_mut_with(v);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"hoist_funs": true,
"defaults": true,
"toplevel": true,
"passes": 0
}
36 changes: 36 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/8284/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
(function (global, factory) {
typeof exports === "object" && typeof module !== "undefined"
? factory(exports)
: typeof define === "function" && define.amd
? define(["exports"], factory)
: ((global =
typeof globalThis !== "undefined" ? globalThis : global || self),
factory((global.Sentry = {})));
})(this, function (exports) {
"use strict";
function isGlobalObj(obj) {
return obj && obj.Math == Math ? obj : undefined;
}
const GLOBAL_OBJ =
(typeof globalThis == "object" && isGlobalObj(globalThis)) ||
// eslint-disable-next-line no-restricted-globals
isGlobalObj(globalThis) ||
(typeof self == "object" && isGlobalObj(self)) ||
(typeof global == "object" && isGlobalObj(global)) ||
(function () {
return this;
})() ||
{};
function getGlobalObject() {
return GLOBAL_OBJ;
}
const WINDOW$6 = getGlobalObject();
function supportsFetch() {
if (!("fetch" in WINDOW$6)) {
return false;
}
return true;
}

console.log(supportsFetch());
});
12 changes: 12 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/8284/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var global1, factory;
global1 = this, factory = function(exports1) {
function isGlobalObj(obj) {
return obj && obj.Math == Math ? obj : void 0;
}
"use strict";
console.log("fetch" in ("object" == typeof globalThis && isGlobalObj(globalThis) || isGlobalObj(globalThis) || "object" == typeof self && isGlobalObj(self) || "object" == typeof global && isGlobalObj(global) || function() {
return this;
}() || {}));
}, "object" == typeof exports && "undefined" != typeof module ? factory(exports) : "function" == typeof define && define.amd ? define([
"exports"
], factory) : factory((global1 = "undefined" != typeof globalThis ? globalThis : global1 || self).Sentry = {});

0 comments on commit 73fec94

Please sign in to comment.