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 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions .changeset/tidy-pots-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
preset_env_base: major
---

fix(preset-env): Consider browserslist config if env.target not configured
31 changes: 29 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, sync::Arc};
use std::{collections::HashMap, path::PathBuf, sync::Arc};

use anyhow::{Context, Error};
use dashmap::DashMap;
Expand Down Expand Up @@ -96,8 +96,35 @@ impl Query {
}
}

pub fn targets_to_versions(v: Option<Targets>) -> Result<Arc<Versions>, Error> {
pub fn targets_to_versions(
v: Option<Targets>,
path: Option<PathBuf>,
) -> Result<Arc<Versions>, Error> {
match v {
#[cfg(not(target_arch = "wasm32"))]
None => {
let mut browserslist_opts = browserslist::Opts {
mobile_to_desktop: true,
ignore_unknown_versions: true,
..Default::default()
};
if let Some(path) = path {
browserslist_opts.path = path
.clone()
.into_os_string()
.into_string()
.unwrap_or_else(|_| panic!("Invalid path \"{:?}\"", path))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not panic!() here. Can you return an instance of anyhow::Error instead?

.into();
}
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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. We don't have to panic.


Ok(Arc::new(versions))
}
#[cfg(target_arch = "wasm32")]
None => Ok(Default::default()),
Some(Targets::Versions(v)) => Ok(Arc::new(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 @@ -111,7 +111,7 @@ pub fn should_prefix(property: &str, target: &Versions, default: bool) -> bool {
}

pub fn prefixer(options: Options) -> impl VisitMut {
let env = targets_to_versions(options.env).expect("failed to parse targets");
let env = targets_to_versions(options.env, None).expect("failed to parse targets");

Prefixer {
env,
Expand Down
14 changes: 3 additions & 11 deletions crates/swc_ecma_preset_env/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ where
C: Comments + Clone,
{
let loose = c.loose;
let targets = targets_to_versions(c.targets).expect("failed to parse targets");
let targets = 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 @@ -596,8 +596,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 @@ -609,14 +609,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