mirror of
https://github.com/swc-project/swc.git
synced 2024-10-05 12:49:21 +03:00
feat(es/minifier): Add pristine_globals
(#4480)
This commit is contained in:
parent
48b427e403
commit
09ba3c0e68
@ -806,7 +806,7 @@ impl Pure<'_> {
|
||||
_ => {}
|
||||
}
|
||||
|
||||
if self.options.side_effects {
|
||||
if self.options.side_effects && self.options.pristine_globals {
|
||||
if let Expr::New(NewExpr { callee, args, .. }) = e {
|
||||
if let Expr::Ident(i) = &**callee {
|
||||
match &*i.sym {
|
||||
@ -830,6 +830,7 @@ impl Pure<'_> {
|
||||
}
|
||||
|
||||
// Remove pure member expressions.
|
||||
if self.options.pristine_globals {
|
||||
if let Expr::Member(MemberExpr { obj, prop, .. }) = e {
|
||||
if let Expr::Ident(obj) = &**obj {
|
||||
if obj.span.ctxt.outer() == self.marks.unresolved_mark {
|
||||
@ -843,6 +844,7 @@ impl Pure<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, Copy)]
|
||||
pub(super) struct DropOpts {
|
||||
|
@ -311,6 +311,12 @@ pub struct CompressOptions {
|
||||
|
||||
#[serde(default = "true_by_default")]
|
||||
pub const_to_let: bool,
|
||||
|
||||
/// If you modified globals, set this to false.
|
||||
///
|
||||
/// Defaults to true.
|
||||
#[serde(default = "true_by_default")]
|
||||
pub pristine_globals: bool,
|
||||
}
|
||||
|
||||
impl CompressOptions {
|
||||
|
@ -242,6 +242,9 @@ pub struct TerserCompressorOptions {
|
||||
|
||||
#[serde(default)]
|
||||
pub const_to_let: Option<bool>,
|
||||
|
||||
#[serde(default)]
|
||||
pub pristine_globals: Option<bool>,
|
||||
}
|
||||
|
||||
impl Default for TerserCompressorOptions {
|
||||
@ -388,6 +391,7 @@ impl TerserCompressorOptions {
|
||||
unsafe_undefined: self.unsafe_undefined,
|
||||
unused: self.unused.unwrap_or(self.defaults),
|
||||
const_to_let: self.const_to_let.unwrap_or(self.defaults),
|
||||
pristine_globals: self.pristine_globals.unwrap_or(self.defaults),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -126,6 +126,7 @@ fn parse_compressor_config(cm: Lrc<SourceMap>, s: &str) -> (bool, CompressOption
|
||||
|
||||
c.defaults = opts.defaults;
|
||||
c.const_to_let = Some(false);
|
||||
c.pristine_globals = Some(true);
|
||||
c.passes = opts.passes;
|
||||
|
||||
(c.module, c.into_config(cm))
|
||||
|
Loading…
Reference in New Issue
Block a user