Use browserslist by default if env is specified but targets are not specified

This commit is contained in:
강동윤 2019-12-28 23:31:58 +09:00
parent a8d7482294
commit 31c1bc8a8a
3 changed files with 42 additions and 30 deletions

View File

@ -1,5 +1,6 @@
{
"devDependencies": {
"browserslist": "=4.7.3"
}
},
"browserslist": ["defaults"]
}

View File

@ -388,8 +388,6 @@ impl BrowserData<Option<Version>> {
}
}
println!("Versions: {:?}", data);
Ok(data)
}
}
@ -426,7 +424,7 @@ pub struct Config {
#[serde(default)]
pub core_js: Option<Version>,
#[serde(default)]
#[serde(default = "default_targets")]
pub targets: Option<Targets>,
#[serde(default)]
@ -436,6 +434,10 @@ pub struct Config {
pub force_all_transforms: bool,
}
fn default_targets() -> Option<Targets> {
Some(Targets::Query(Query::Single("".into())))
}
#[derive(Debug, Clone, Deserialize, FromVariant)]
#[serde(untagged)]
pub enum FeatureOrModule {
@ -498,29 +500,24 @@ impl Query {
T: AsRef<str> + Serialize,
{
let output = {
if s.len() == 0 {
b"[]".to_vec()
} else {
let output = Command::new("node")
.arg("-e")
.arg(include_str!("query.js"))
.arg(serde_json::to_string(&s).expect("failed to serialize with serde"))
.output()
.expect("failed to collect output");
let output = Command::new("node")
.arg("-e")
.arg(include_str!("query.js"))
.arg(serde_json::to_string(&s).expect("failed to serialize with serde"))
.output()
.expect("failed to collect output");
if !output.status.success() {
println!(
"{}\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr),
);
println!("query.js: Status {:?}", output.status,);
return Err(());
}
output.stdout
println!(
"{}\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr),
);
if !output.status.success() {
println!("query.js: Status {:?}", output.status,);
return Err(());
}
output.stdout
};
let browsers: Vec<String> =
@ -556,7 +553,7 @@ impl TryFrom<Option<Targets>> for Versions {
fn try_from(v: Option<Targets>) -> Result<Self, Self::Error> {
match v {
None => Ok(Versions::default()),
None => Ok(Default::default()),
Some(Targets::Versions(v)) => Ok(v),
Some(Targets::Query(q)) => q.exec(),
Some(Targets::HashMap(mut map)) => {
@ -583,3 +580,17 @@ impl TryFrom<Option<Targets>> for Versions {
}
}
}
#[cfg(test)]
mod tests {
use super::Query;
#[test]
fn test_empty() {
let res = Query::Single("".into()).exec().unwrap();
assert!(
!res.is_any_target(),
"empty query should return non-empty result"
);
}
}

View File

@ -3,11 +3,11 @@ let target = JSON.parse(process.argv[1]);
target = target.browsers ? target.browsers : target;
target = Array.isArray(target) ? target : (typeof target === 'string' ? [target] : Object.keys(target).map((k) => `${k} ${target[k]}`));
target = target.filter(v => !v.startsWith('esmodules'));
target = target.filter(v => !v.startsWith('esmodules') && !!v);
// console.log('Target: ', target);
const browsers = browserslist(target, {
const browsers = browserslist(target && target.length ? target : undefined, {
mobileToDesktop: true,
})
console.log(JSON.stringify(browsers))
});
console.log(JSON.stringify(browsers));