feat(es/minifier): Add pristine_globals (#4480)

This commit is contained in:
Donny/강동윤 2022-04-29 17:40:16 +09:00 committed by GitHub
parent 48b427e403
commit 09ba3c0e68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 8 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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),
}
}
}

View File

@ -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))