From 31c1bc8a8ab4ccd2f7e1b63ccb2bb87329df1389 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 28 Dec 2019 23:31:58 +0900 Subject: [PATCH] Use browserslist by default if env is specified but targets are not specified --- ecmascript/preset_env/package.json | 3 +- ecmascript/preset_env/src/lib.rs | 61 ++++++++++++++++++------------ ecmascript/preset_env/src/query.js | 8 ++-- 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/ecmascript/preset_env/package.json b/ecmascript/preset_env/package.json index 5b69cc2da6c..ea107de5c9d 100644 --- a/ecmascript/preset_env/package.json +++ b/ecmascript/preset_env/package.json @@ -1,5 +1,6 @@ { "devDependencies": { "browserslist": "=4.7.3" - } + }, + "browserslist": ["defaults"] } diff --git a/ecmascript/preset_env/src/lib.rs b/ecmascript/preset_env/src/lib.rs index acf6bb5731b..e7bc30a64ba 100644 --- a/ecmascript/preset_env/src/lib.rs +++ b/ecmascript/preset_env/src/lib.rs @@ -388,8 +388,6 @@ impl BrowserData> { } } - println!("Versions: {:?}", data); - Ok(data) } } @@ -426,7 +424,7 @@ pub struct Config { #[serde(default)] pub core_js: Option, - #[serde(default)] + #[serde(default = "default_targets")] pub targets: Option, #[serde(default)] @@ -436,6 +434,10 @@ pub struct Config { pub force_all_transforms: bool, } +fn default_targets() -> Option { + Some(Targets::Query(Query::Single("".into()))) +} + #[derive(Debug, Clone, Deserialize, FromVariant)] #[serde(untagged)] pub enum FeatureOrModule { @@ -498,29 +500,24 @@ impl Query { T: AsRef + 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 = @@ -556,7 +553,7 @@ impl TryFrom> for Versions { fn try_from(v: Option) -> Result { 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> 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" + ); + } +} diff --git a/ecmascript/preset_env/src/query.js b/ecmascript/preset_env/src/query.js index 71025b8d535..c2c3f7d598c 100644 --- a/ecmascript/preset_env/src/query.js +++ b/ecmascript/preset_env/src/query.js @@ -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)) \ No newline at end of file +}); +console.log(JSON.stringify(browsers)); \ No newline at end of file