mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
feat(minfier): add CompressOptions::target (#8179)
This commit is contained in:
parent
ad146bbb90
commit
41ddf60ab0
9 changed files with 91 additions and 67 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -1987,6 +1987,7 @@ version = "0.44.0"
|
|||
dependencies = [
|
||||
"assert-unchecked",
|
||||
"bitflags 2.6.0",
|
||||
"cow-utils",
|
||||
"nonmax",
|
||||
"oxc_allocator",
|
||||
"oxc_ast_macros",
|
||||
|
|
|
|||
|
|
@ -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`
|
||||
|
|
@ -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 }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 }
|
||||
|
|
|
|||
68
crates/oxc_syntax/src/es_target.rs
Normal file
68
crates/oxc_syntax/src/es_target.rs
Normal file
|
|
@ -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,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;
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
pub use oxc_syntax::es_target::ESTarget;
|
||||
|
||||
pub trait ESVersion {
|
||||
fn version(&self) -> Version;
|
||||
}
|
||||
|
||||
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}",)
|
||||
}
|
||||
}
|
||||
|
||||
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),
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Reference in a new issue