Skip to content

Commit

Permalink
feat(es/preset-env): Custom config path (#1374)
Browse files Browse the repository at this point in the history
swc_ecma_preset_env:
 - Support custom path.

Co-authored-by: 강동윤 <kdy1@dudy.kr>
  • Loading branch information
mcnicholls and kdy1 authored Feb 3, 2021
1 parent bfde9a1 commit bd119e6
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 36 deletions.
2 changes: 1 addition & 1 deletion bundler/tests/fixture/deno-8574/output/entry.inlined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion bundler/tests/fixture/deno-8574/output/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
74 changes: 40 additions & 34 deletions ecmascript/preset_env/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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);
Expand Down Expand Up @@ -427,6 +428,9 @@ pub struct Config {
#[serde(default = "default_targets")]
pub targets: Option<Targets>,

#[serde(default = "default_path")]
pub path: PathBuf,

#[serde(default)]
pub shipped_proposals: bool,

Expand All @@ -438,6 +442,10 @@ fn default_targets() -> Option<Targets> {
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 {
Expand Down Expand Up @@ -494,13 +502,14 @@ pub enum Query {
type QueryResult = Result<Versions, ()>;

impl Query {
fn exec(&self) -> QueryResult {
fn query<T>(s: &[T]) -> QueryResult
fn exec(&self, path: &Path) -> QueryResult {
fn query<T>(s: &[T], path: &Path) -> QueryResult
where
T: AsRef<str> + 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"))
Expand Down Expand Up @@ -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);
Expand All @@ -547,36 +556,32 @@ impl Query {
}
}

impl TryFrom<Option<Targets>> for Versions {
type Error = ();

fn try_from(v: Option<Targets>) -> Result<Self, Self::Error> {
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<Targets>, path: &Path) -> Result<Versions, ()> {
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<Targets>: {:?}", v),

unimplemented!("Targets: {:?}", map)
}
_ => unimplemented!("Option<Targets>: {:?}", v),
}
}

Expand All @@ -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"
Expand Down
1 change: 1 addition & 0 deletions ecmascript/preset_env/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
);

Expand Down
2 changes: 2 additions & 0 deletions node-swc/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ export interface EnvConfig {

targets?: any;

path?: string;

shippedProposals?: boolean;

/**
Expand Down

0 comments on commit bd119e6

Please sign in to comment.