From 364459e5d5366238887c16cbe1bd8783c60c7e82 Mon Sep 17 00:00:00 2001 From: Florian Schwalm Date: Mon, 6 May 2024 12:08:07 +0200 Subject: [PATCH 1/4] fix(preset-env): Load browserslist config if no swc target configured --- crates/preset_env_base/src/query.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/crates/preset_env_base/src/query.rs b/crates/preset_env_base/src/query.rs index fb2183af909e..d6d5f2aa167c 100644 --- a/crates/preset_env_base/src/query.rs +++ b/crates/preset_env_base/src/query.rs @@ -98,6 +98,21 @@ impl Query { pub fn targets_to_versions(v: Option) -> Result { match v { + #[cfg(not(target_arch = "wasm32"))] + None => { + let distribs = browserslist::execute( + browserslist::Opts::new() + .mobile_to_desktop(true) + .ignore_unknown_versions(true), + ) + .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 From 75d2ff4cf1bde22f4fc895287721cb1c9e59eb3f Mon Sep 17 00:00:00 2001 From: Florian Schwalm Date: Mon, 6 May 2024 12:42:38 +0200 Subject: [PATCH 2/4] fix(preset-env): Pass through path from swc env config to browserslist Defaults to empty PathBuf for wasm32 target, but does not seem to be used anyway --- crates/preset_env_base/src/query.rs | 12 +++++++++--- crates/swc_ecma_preset_env/src/lib.rs | 3 ++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/crates/preset_env_base/src/query.rs b/crates/preset_env_base/src/query.rs index d6d5f2aa167c..3639ed524158 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,14 +96,20 @@ impl Query { } } -pub fn targets_to_versions(v: Option) -> Result { +pub fn targets_to_versions(v: Option, path: PathBuf) -> Result { match v { #[cfg(not(target_arch = "wasm32"))] None => { let distribs = browserslist::execute( browserslist::Opts::new() .mobile_to_desktop(true) - .ignore_unknown_versions(true), + .ignore_unknown_versions(true) + .path( + path.clone() + .into_os_string() + .to_str() + .unwrap_or_else(|| panic!("Invalid path \"{:?}\"", path)), + ), ) .with_context(|| "failed to resolve browserslist query from browserslist config")?; diff --git a/crates/swc_ecma_preset_env/src/lib.rs b/crates/swc_ecma_preset_env/src/lib.rs index cb19217b090d..401353c77e6e 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); From 45ece22de08072e108fc003510eda72138a55c30 Mon Sep 17 00:00:00 2001 From: Florian Schwalm Date: Mon, 6 May 2024 13:12:52 +0200 Subject: [PATCH 3/4] feat(preset-env): Let browserslist-rs handle default config path --- crates/preset_env_base/src/query.rs | 28 +++++++++++++----------- crates/swc_ecma_preset_env/src/lib.rs | 12 ++-------- crates/swc_ecma_preset_env/tests/test.rs | 2 +- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/crates/preset_env_base/src/query.rs b/crates/preset_env_base/src/query.rs index 3639ed524158..e023a8970198 100644 --- a/crates/preset_env_base/src/query.rs +++ b/crates/preset_env_base/src/query.rs @@ -96,22 +96,24 @@ impl Query { } } -pub fn targets_to_versions(v: Option, path: PathBuf) -> Result { +pub fn targets_to_versions(v: Option, path: Option) -> Result { match v { #[cfg(not(target_arch = "wasm32"))] None => { - let distribs = browserslist::execute( - browserslist::Opts::new() - .mobile_to_desktop(true) - .ignore_unknown_versions(true) - .path( - path.clone() - .into_os_string() - .to_str() - .unwrap_or_else(|| panic!("Invalid path \"{:?}\"", path)), - ), - ) - .with_context(|| "failed to resolve browserslist query from browserslist config")?; + 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"); diff --git a/crates/swc_ecma_preset_env/src/lib.rs b/crates/swc_ecma_preset_env/src/lib.rs index 401353c77e6e..19445c626e5d 100644 --- a/crates/swc_ecma_preset_env/src/lib.rs +++ b/crates/swc_ecma_preset_env/src/lib.rs @@ -603,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, @@ -616,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(), From 7a7aaf8f76976f054ddefa4c080c65b65f163ef1 Mon Sep 17 00:00:00 2001 From: Florian Schwalm Date: Mon, 6 May 2024 15:39:34 +0200 Subject: [PATCH 4/4] fix(preset-env): Ignore env.path for swc_css_prefixer Maybe reconsider this later if this is functionality we want --- crates/swc_css_prefixer/src/prefixer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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,