Skip to content

Commit

Permalink
feat(minfier): add CompressOptions::target (#8179)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 29, 2024
1 parent ad146bb commit 41ddf60
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 66 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions crates/oxc_minifier/src/options.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
use oxc_syntax::es_target::ESTarget;

#[derive(Debug, Clone, Copy)]
pub struct CompressOptions {
/// Enable features that are targeted above.
///
/// e.g.
///
/// * catch optional binding when >= es2019
pub target: ESTarget,

/// Remove `debugger;` statements.
///
/// Default `true`
Expand All @@ -20,10 +29,10 @@ impl Default for CompressOptions {

impl CompressOptions {
pub fn all_true() -> Self {
Self { drop_debugger: true, drop_console: true }
Self { target: ESTarget::ESNext, drop_debugger: true, drop_console: true }
}

pub fn all_false() -> Self {
Self { drop_debugger: false, drop_console: false }
Self { target: ESTarget::ESNext, drop_debugger: false, drop_console: false }
}
}
1 change: 1 addition & 0 deletions crates/oxc_syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ oxc_span = { workspace = true }

assert-unchecked = { workspace = true }
bitflags = { workspace = true }
cow-utils = { workspace = true }
nonmax = { workspace = true }
phf = { workspace = true, features = ["macros"] }
rustc-hash = { workspace = true }
Expand Down
68 changes: 68 additions & 0 deletions crates/oxc_syntax/src/es_target.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//! ECMAScript Target
use std::{fmt, str::FromStr};

use cow_utils::CowUtils;

/// ECMAScript Target
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
#[allow(missing_docs)]
pub enum ESTarget {
ES5,
ES2015,
ES2016,
ES2017,
ES2018,
ES2019,
ES2020,
ES2021,
ES2022,
ES2023,
ES2024,
ES2025,
#[default]
ESNext,
}

impl FromStr for ESTarget {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.cow_to_lowercase().as_ref() {
"es5" => Ok(Self::ES5),
"es6" | "es2015" => Ok(Self::ES2015),
"es2016" => Ok(Self::ES2016),
"es2017" => Ok(Self::ES2017),
"es2018" => Ok(Self::ES2018),
"es2019" => Ok(Self::ES2019),
"es2020" => Ok(Self::ES2020),
"es2021" => Ok(Self::ES2021),
"es2022" => Ok(Self::ES2022),
"es2023" => Ok(Self::ES2023),
"es2024" => Ok(Self::ES2024),
"es2025" => Ok(Self::ES2025),
"esnext" => Ok(Self::ESNext),
_ => Err(format!("Invalid target \"{s}\".")),
}
}
}

impl fmt::Display for ESTarget {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Self::ES5 => "es5",
Self::ES2015 => "es2015",
Self::ES2016 => "es2016",
Self::ES2017 => "es2017",
Self::ES2018 => "es2018",
Self::ES2019 => "es2019",
Self::ES2020 => "es2020",
Self::ES2021 => "es2021",
Self::ES2022 => "es2022",
Self::ES2023 => "es2023",
Self::ES2024 => "es2024",
Self::ES2025 => "es2025",
Self::ESNext => "esnext",
};
write!(f, "{s}",)
}
}
1 change: 1 addition & 0 deletions crates/oxc_syntax/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Common code for JavaScript Syntax
#![warn(missing_docs)]
pub mod class;
pub mod es_target;
pub mod identifier;
pub mod keyword;
pub mod module_record;
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_transformer/src/options/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ impl EnvOptions {
///
/// * When the query failed to parse.
pub fn from_target_list<S: AsRef<str>>(list: &[S]) -> Result<Self, String> {
use crate::options::es_target::ESVersion;
let mut es_target = None;
let mut engine_targets = EngineTargets::default();

Expand Down
69 changes: 5 additions & 64 deletions crates/oxc_transformer/src/options/es_target.rs
Original file line number Diff line number Diff line change
@@ -1,72 +1,13 @@
use std::{fmt, str::FromStr};

use browserslist::Version;
use cow_utils::CowUtils;

#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
pub enum ESTarget {
ES5,
ES2015,
ES2016,
ES2017,
ES2018,
ES2019,
ES2020,
ES2021,
ES2022,
ES2023,
ES2024,
ES2025,
#[default]
ESNext,
}

impl FromStr for ESTarget {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.cow_to_lowercase().as_ref() {
"es5" => Ok(Self::ES5),
"es6" | "es2015" => Ok(Self::ES2015),
"es2016" => Ok(Self::ES2016),
"es2017" => Ok(Self::ES2017),
"es2018" => Ok(Self::ES2018),
"es2019" => Ok(Self::ES2019),
"es2020" => Ok(Self::ES2020),
"es2021" => Ok(Self::ES2021),
"es2022" => Ok(Self::ES2022),
"es2023" => Ok(Self::ES2023),
"es2024" => Ok(Self::ES2024),
"es2025" => Ok(Self::ES2025),
"esnext" => Ok(Self::ESNext),
_ => Err(format!("Invalid target \"{s}\".")),
}
}
}
pub use oxc_syntax::es_target::ESTarget;

impl fmt::Display for ESTarget {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Self::ES5 => "es5",
Self::ES2015 => "es2015",
Self::ES2016 => "es2016",
Self::ES2017 => "es2017",
Self::ES2018 => "es2018",
Self::ES2019 => "es2019",
Self::ES2020 => "es2020",
Self::ES2021 => "es2021",
Self::ES2022 => "es2022",
Self::ES2023 => "es2023",
Self::ES2024 => "es2024",
Self::ES2025 => "es2025",
Self::ESNext => "esnext",
};
write!(f, "{s}",)
}
pub trait ESVersion {
fn version(&self) -> Version;
}

impl ESTarget {
pub fn version(&self) -> Version {
impl ESVersion for ESTarget {
fn version(&self) -> Version {
match self {
Self::ES5 => Version(5, 0, 0),
Self::ES2015 => Version(2015, 0, 0),
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_transformer/src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ impl TransformOptions {

impl From<ESTarget> for TransformOptions {
fn from(target: ESTarget) -> Self {
use crate::options::es_target::ESVersion;
let mut engine_targets = EngineTargets::default();
engine_targets.insert(Engine::Es, target.version());
let mut env = EnvOptions::from(engine_targets);
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ impl Oxc {
CompressOptions {
drop_console: compress_options.drop_console,
drop_debugger: compress_options.drop_debugger,
..CompressOptions::all_false()
}
} else {
CompressOptions::all_false()
Expand Down

0 comments on commit 41ddf60

Please sign in to comment.