Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(preset-env): Consider browserslist config if env.target not configured #8921

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions crates/preset_env_base/src/query.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -96,8 +96,31 @@ impl Query {
}
}

pub fn targets_to_versions(v: Option<Targets>) -> 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 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
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_css_prefixer/src/prefixer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 4 additions & 11 deletions crates/swc_ecma_preset_env/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -602,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 @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_preset_env/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Loading