From e2ff70128d491ead453580b228cdf6950ce4b732 Mon Sep 17 00:00:00 2001 From: Michael Nicholls Date: Tue, 2 Feb 2021 11:55:38 +0000 Subject: [PATCH 1/3] Allow preset-env to take a config path --- ecmascript/preset_env/src/lib.rs | 74 ++++++++++++++++------------- ecmascript/preset_env/tests/test.rs | 1 + node-swc/src/types.ts | 2 + 3 files changed, 43 insertions(+), 34 deletions(-) diff --git a/ecmascript/preset_env/src/lib.rs b/ecmascript/preset_env/src/lib.rs index 3b3cb0b6dea9..912fb9d3fb5e 100644 --- a/ecmascript/preset_env/src/lib.rs +++ b/ecmascript/preset_env/src/lib.rs @@ -7,7 +7,7 @@ use once_cell::sync::Lazy; use serde::{Deserialize, Serialize}; use st_map::StaticMap; use std::{ - convert::{TryFrom, TryInto}, + path::{Path, PathBuf}, process::Command, }; use swc_atoms::{js_word, JsWord}; @@ -30,7 +30,8 @@ mod version; pub fn preset_env(global_mark: Mark, c: Config) -> impl Fold { let loose = c.loose; - let targets: Versions = c.targets.try_into().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); @@ -427,6 +428,9 @@ pub struct Config { #[serde(default = "default_targets")] pub targets: Option, + #[serde(default = "default_path")] + pub path: PathBuf, + #[serde(default)] pub shipped_proposals: bool, @@ -438,6 +442,10 @@ fn default_targets() -> Option { Some(Targets::Query(Query::Single("".into()))) } +fn default_path() -> PathBuf { + std::env::current_dir().unwrap() +} + #[derive(Debug, Clone, Deserialize, FromVariant)] #[serde(untagged)] pub enum FeatureOrModule { @@ -494,13 +502,14 @@ pub enum Query { type QueryResult = Result; impl Query { - fn exec(&self) -> QueryResult { - fn query(s: &[T]) -> QueryResult + fn exec(&self, path: &Path) -> QueryResult { + fn query(s: &[T], path: &Path) -> QueryResult where T: AsRef + Serialize, { let output = { let output = Command::new("node") + .current_dir(path) .arg("-e") .arg(include_str!("query.js")) .arg(serde_json::to_string(&s).expect("failed to serialize with serde")) @@ -537,8 +546,8 @@ impl Query { } let result = match *self { - Query::Single(ref s) => query(&[s]), - Query::Multiple(ref s) => query(&s), + Query::Single(ref s) => query(&[s], path), + Query::Multiple(ref s) => query(&s, path), }; CACHE.insert(self.clone(), result); @@ -547,36 +556,32 @@ impl Query { } } -impl TryFrom> for Versions { - type Error = (); - - fn try_from(v: Option) -> Result { - match v { - None => Ok(Default::default()), - Some(Targets::Versions(v)) => Ok(v), - Some(Targets::Query(q)) => q.exec(), - Some(Targets::HashMap(mut map)) => { - let q = map.remove("browsers").map(|q| match q { - QueryOrVersion::Query(q) => q.exec().expect("failed to run query"), - _ => unreachable!(), - }); - - let node = map.remove("node").map(|q| match q { - QueryOrVersion::Version(v) => v, - QueryOrVersion::Query(..) => unreachable!(), - }); - - if map.is_empty() { - if let Some(mut q) = q { - q.node = node; - return Ok(q); - } +fn targets_to_versions(v: Option, path: &Path) -> Result { + match v { + None => Ok(Default::default()), + Some(Targets::Versions(v)) => Ok(v), + Some(Targets::Query(q)) => q.exec(path), + Some(Targets::HashMap(mut map)) => { + let q = map.remove("browsers").map(|q| match q { + QueryOrVersion::Query(q) => q.exec(path).expect("failed to run query"), + _ => unreachable!(), + }); + + let node = map.remove("node").map(|q| match q { + QueryOrVersion::Version(v) => v, + QueryOrVersion::Query(..) => unreachable!(), + }); + + if map.is_empty() { + if let Some(mut q) = q { + q.node = node; + return Ok(q); } - - unimplemented!("Targets: {:?}", map) } - _ => unimplemented!("Option: {:?}", v), + + unimplemented!("Targets: {:?}", map) } + _ => unimplemented!("Option: {:?}", v), } } @@ -586,7 +591,8 @@ mod tests { #[test] fn test_empty() { - let res = Query::Single("".into()).exec().unwrap(); + let path = std::env::current_dir().unwrap(); + let res = Query::Single("".into()).exec(&path).unwrap(); assert!( !res.is_any_target(), "empty query should return non-empty result" diff --git a/ecmascript/preset_env/tests/test.rs b/ecmascript/preset_env/tests/test.rs index e59c3a6054a1..73bef60cc096 100644 --- a/ecmascript/preset_env/tests/test.rs +++ b/ecmascript/preset_env/tests/test.rs @@ -199,6 +199,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(), }, ); diff --git a/node-swc/src/types.ts b/node-swc/src/types.ts index 420c5287d5a9..f5323a0b7a43 100644 --- a/node-swc/src/types.ts +++ b/node-swc/src/types.ts @@ -245,6 +245,8 @@ export interface EnvConfig { targets?: any; + path?: string; + shippedProposals?: boolean; /** From 166aa58c9d9a06216bc821190b43df08a9e4db25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 3 Feb 2021 12:45:42 +0900 Subject: [PATCH 2/3] Fix test --- bundler/tests/fixture/deno-8574/output/entry.inlined.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bundler/tests/fixture/deno-8574/output/entry.inlined.ts b/bundler/tests/fixture/deno-8574/output/entry.inlined.ts index 5ac982db102b..a2789a9a4442 100644 --- a/bundler/tests/fixture/deno-8574/output/entry.inlined.ts +++ b/bundler/tests/fixture/deno-8574/output/entry.inlined.ts @@ -660,7 +660,7 @@ var getGlobal = function() { }; var global = getGlobal(); var nodeFetch = global.fetch.bind(global); -const VERSION1 = "5.4.13"; +const VERSION1 = "5.4.14"; function getBufferResponse(response) { return response.arrayBuffer(); } From b0d4dc49d46496abeba18a66623ea3a4bdbaa6e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 3 Feb 2021 12:46:14 +0900 Subject: [PATCH 3/3] Fix test --- bundler/tests/fixture/deno-8574/output/entry.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bundler/tests/fixture/deno-8574/output/entry.ts b/bundler/tests/fixture/deno-8574/output/entry.ts index 7ccfa1a54034..521e55fbd4d1 100644 --- a/bundler/tests/fixture/deno-8574/output/entry.ts +++ b/bundler/tests/fixture/deno-8574/output/entry.ts @@ -678,7 +678,7 @@ var getGlobal = function() { }; var global = getGlobal(); var nodeFetch = global.fetch.bind(global); -const VERSION1 = "5.4.13"; +const VERSION1 = "5.4.14"; function getBufferResponse(response) { return response.arrayBuffer(); }