diff --git a/crates/preset_env_base/src/query.rs b/crates/preset_env_base/src/query.rs index fb2183af909e..e023a8970198 100644 --- a/crates/preset_env_base/src/query.rs +++ b/crates/preset_env_base/src/query.rs @@ -1,7 +1,7 @@ //! Module for `browserslist` queries. #![deny(clippy::all)] -use std::collections::HashMap; +use std::{collections::HashMap, path::PathBuf}; use anyhow::{Context, Error}; use dashmap::DashMap; @@ -96,8 +96,31 @@ impl Query { } } -pub fn targets_to_versions(v: Option) -> Result { +pub fn targets_to_versions(v: Option, path: Option) -> Result { match v { + #[cfg(not(target_arch = "wasm32"))] + None => { + let mut browserslist_opts = browserslist::Opts::new() + .mobile_to_desktop(true) + .ignore_unknown_versions(true) + .to_owned(); + if let Some(path) = path { + browserslist_opts.path( + path.clone() + .into_os_string() + .to_str() + .unwrap_or_else(|| panic!("Invalid path \"{:?}\"", path)), + ); + } + let distribs = browserslist::execute(&browserslist_opts) + .with_context(|| "failed to resolve browserslist query from browserslist config")?; + + let versions = + BrowserData::parse_versions(distribs).expect("failed to parse browser version"); + + Ok(versions) + } + #[cfg(target_arch = "wasm32")] None => Ok(Default::default()), Some(Targets::Versions(v)) => Ok(v), Some(Targets::Query(q)) => q diff --git a/crates/swc_css_prefixer/src/prefixer.rs b/crates/swc_css_prefixer/src/prefixer.rs index 292060b15161..47c359c94281 100644 --- a/crates/swc_css_prefixer/src/prefixer.rs +++ b/crates/swc_css_prefixer/src/prefixer.rs @@ -107,7 +107,7 @@ pub fn should_prefix(property: &str, target: Versions, default: bool) -> bool { } pub fn prefixer(options: Options) -> impl VisitMut { - let env: Versions = targets_to_versions(options.env).expect("failed to parse targets"); + let env: Versions = targets_to_versions(options.env, None).expect("failed to parse targets"); Prefixer { env, diff --git a/crates/swc_ecma_preset_env/src/lib.rs b/crates/swc_ecma_preset_env/src/lib.rs index cb19217b090d..19445c626e5d 100644 --- a/crates/swc_ecma_preset_env/src/lib.rs +++ b/crates/swc_ecma_preset_env/src/lib.rs @@ -46,7 +46,8 @@ where C: Comments + Clone, { let loose = c.loose; - let targets: Versions = targets_to_versions(c.targets).expect("failed to parse targets"); + let targets: Versions = + targets_to_versions(c.targets, c.path).expect("failed to parse targets"); let is_any_target = targets.is_any_target(); let (include, included_modules) = FeatureOrModule::split(c.include); @@ -602,8 +603,8 @@ pub struct Config { #[serde(default)] pub targets: Option, - #[serde(default = "default_path")] - pub path: PathBuf, + #[serde(default)] + pub path: Option, #[serde(default)] pub shipped_proposals: bool, @@ -615,14 +616,6 @@ pub struct Config { pub bugfixes: bool, } -fn default_path() -> PathBuf { - if cfg!(target_arch = "wasm32") { - Default::default() - } else { - std::env::current_dir().unwrap() - } -} - #[derive(Debug, Clone, Deserialize, FromVariant)] #[serde(untagged)] pub enum FeatureOrModule { diff --git a/crates/swc_ecma_preset_env/tests/test.rs b/crates/swc_ecma_preset_env/tests/test.rs index 1379699a01c7..8bfc662f3422 100644 --- a/crates/swc_ecma_preset_env/tests/test.rs +++ b/crates/swc_ecma_preset_env/tests/test.rs @@ -147,7 +147,7 @@ fn exec(c: PresetConfig, dir: PathBuf) -> Result<(), Error> { force_all_transforms: c.force_all_transforms, shipped_proposals: c.shipped_proposals, targets: c.targets, - path: std::env::current_dir().unwrap(), + path: std::env::current_dir().ok(), }, Default::default(), &mut Default::default(),