From bd119e6634d4b017982d4a02ebd07fa867c4a9fd Mon Sep 17 00:00:00 2001 From: Michael Nicholls Date: Wed, 3 Feb 2021 04:27:11 +0000 Subject: [PATCH] feat(es/preset-env): Custom config path (#1374) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit swc_ecma_preset_env: - Support custom path. Co-authored-by: 강동윤 --- .../fixture/deno-8574/output/entry.inlined.ts | 2 +- .../tests/fixture/deno-8574/output/entry.ts | 2 +- ecmascript/preset_env/src/lib.rs | 70 ++++++++++--------- ecmascript/preset_env/tests/test.rs | 1 + node-swc/src/types.ts | 2 + 5 files changed, 43 insertions(+), 34 deletions(-) diff --git a/bundler/tests/fixture/deno-8574/output/entry.inlined.ts b/bundler/tests/fixture/deno-8574/output/entry.inlined.ts index 5ac982db102..a2789a9a444 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(); } diff --git a/bundler/tests/fixture/deno-8574/output/entry.ts b/bundler/tests/fixture/deno-8574/output/entry.ts index 7ccfa1a5403..521e55fbd4d 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(); } diff --git a/ecmascript/preset_env/src/lib.rs b/ecmascript/preset_env/src/lib.rs index 3b3cb0b6dea..912fb9d3fb5 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 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!(), + }); - 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!(), + }); - 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); - } + 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 e59c3a6054a..73bef60cc09 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 420c5287d5a..f5323a0b7a4 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; /**