diff --git a/crates/oxc_transformer/src/env/mod.rs b/crates/oxc_transformer/src/env/mod.rs index 7c7176e3426a84..73f79ff274656f 100644 --- a/crates/oxc_transformer/src/env/mod.rs +++ b/crates/oxc_transformer/src/env/mod.rs @@ -2,4 +2,8 @@ mod data; mod options; mod targets; -pub use self::{data::can_enable_plugin, options::EnvOptions, targets::Versions}; +pub use self::{ + data::can_enable_plugin, + options::EnvOptions, + targets::{Targets, Versions}, +}; diff --git a/crates/oxc_transformer/src/env/options.rs b/crates/oxc_transformer/src/env/options.rs index dfb97e9ee0d779..b2d77355ec3bf9 100644 --- a/crates/oxc_transformer/src/env/options.rs +++ b/crates/oxc_transformer/src/env/options.rs @@ -12,7 +12,7 @@ fn default_as_true() -> bool { #[serde(default, rename_all = "camelCase", deny_unknown_fields)] pub struct EnvOptions { #[serde(default)] - targets: Targets, + pub targets: Targets, #[serde(default = "default_as_true")] pub bugfixes: bool, diff --git a/crates/oxc_transformer/src/env/targets/mod.rs b/crates/oxc_transformer/src/env/targets/mod.rs index 8a2eca93ebaabb..5f469a8fe9a463 100644 --- a/crates/oxc_transformer/src/env/targets/mod.rs +++ b/crates/oxc_transformer/src/env/targets/mod.rs @@ -12,6 +12,7 @@ use serde::Deserialize; use self::version::Version; +pub use self::query::Targets; pub mod query; pub mod version; diff --git a/crates/oxc_transformer/src/env/targets/query.rs b/crates/oxc_transformer/src/env/targets/query.rs index 642503ec569c0d..5e2a2168cd33c5 100644 --- a/crates/oxc_transformer/src/env/targets/query.rs +++ b/crates/oxc_transformer/src/env/targets/query.rs @@ -29,6 +29,13 @@ impl Default for Targets { } impl Targets { + pub fn new_with_query(query: &str) -> Self { + Targets::Query(Query::Single(query.into())) + } + + /// # Errors + /// + /// If `targets` is not supported, this method returns an error. pub fn get_targets(self) -> Result { match self { Targets::Versions(v) => Ok(v), diff --git a/crates/oxc_transformer/src/lib.rs b/crates/oxc_transformer/src/lib.rs index cbde6cfa7a6f6d..e2c7fc5e3554d9 100644 --- a/crates/oxc_transformer/src/lib.rs +++ b/crates/oxc_transformer/src/lib.rs @@ -44,7 +44,7 @@ use oxc_traverse::{traverse_mut, Traverse, TraverseCtx}; pub use crate::{ compiler_assumptions::CompilerAssumptions, - env::EnvOptions, + env::{EnvOptions, Targets}, es2015::{ArrowFunctionsOptions, ES2015Options}, options::{BabelOptions, TransformOptions}, react::{ReactJsxRuntime, ReactOptions, ReactRefreshOptions}, diff --git a/tasks/benchmark/benches/transformer.rs b/tasks/benchmark/benches/transformer.rs index 124b6b525104c5..56e809c3d9a173 100644 --- a/tasks/benchmark/benches/transformer.rs +++ b/tasks/benchmark/benches/transformer.rs @@ -6,7 +6,7 @@ use oxc_parser::{Parser, ParserReturn}; use oxc_semantic::SemanticBuilder; use oxc_span::SourceType; use oxc_tasks_common::TestFiles; -use oxc_transformer::{TransformOptions, Transformer}; +use oxc_transformer::{EnvOptions, Targets, TransformOptions, Transformer}; fn bench_transformer(criterion: &mut Criterion) { let mut group = criterion.benchmark_group("transformer"); @@ -41,7 +41,13 @@ fn bench_transformer(criterion: &mut Criterion) { let trivias_copy = trivias.clone(); runner.run(|| { - let transform_options = TransformOptions::default(); + let env_options = EnvOptions { + // >= ES2016 + targets: Targets::new_with_query("chrome 51"), + ..Default::default() + }; + let transform_options = + TransformOptions::from_preset_env(&env_options).unwrap(); let ret = Transformer::new( &allocator, Path::new(&file.file_name),