Skip to content

Commit

Permalink
feat(preset-env): Let browserslist-rs handle default config path
Browse files Browse the repository at this point in the history
  • Loading branch information
egfx-notifications committed May 6, 2024
1 parent 75d2ff4 commit 25eb315
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
28 changes: 15 additions & 13 deletions crates/preset_env_base/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,24 @@ impl Query {
}
}

pub fn targets_to_versions(v: Option<Targets>, path: PathBuf) -> Result<Versions, Error> {
pub fn targets_to_versions(v: Option<Targets>, path: Option<PathBuf>) -> Result<Versions, Error> {
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");
Expand Down
12 changes: 2 additions & 10 deletions crates/swc_ecma_preset_env/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,8 +603,8 @@ pub struct Config {
#[serde(default)]
pub targets: Option<Targets>,

#[serde(default = "default_path")]
pub path: PathBuf,
#[serde(default)]
pub path: Option<PathBuf>,

#[serde(default)]
pub shipped_proposals: bool,
Expand All @@ -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 {
Expand Down

0 comments on commit 25eb315

Please sign in to comment.