feat(swc): Accept map for envs (#2467)

swc:
 - Support specifying map to global inlining pass.
This commit is contained in:
Donny/강동윤 2021-10-19 14:13:21 +09:00 committed by GitHub
parent 98a18e306a
commit 28f2c7ae5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 73 additions and 33 deletions

2
Cargo.lock generated
View File

@ -2324,7 +2324,7 @@ checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c"
[[package]] [[package]]
name = "swc" name = "swc"
version = "0.72.2" version = "0.73.0"
dependencies = [ dependencies = [
"ahash", "ahash",
"anyhow", "anyhow",

View File

@ -21,7 +21,7 @@ include = ["Cargo.toml", "src/**/*.rs"]
license = "Apache-2.0/MIT" license = "Apache-2.0/MIT"
name = "swc" name = "swc"
repository = "https://github.com/swc-project/swc.git" repository = "https://github.com/swc-project/swc.git"
version = "0.72.2" version = "0.73.0"
[lib] [lib]
name = "swc" name = "swc"

View File

@ -50,6 +50,7 @@ notice = "warn"
ignore = [ ignore = [
#"RUSTSEC-0000-0000", #"RUSTSEC-0000-0000",
"RUSTSEC-2020-0071", "RUSTSEC-2020-0071",
"RUSTSEC-2020-0159",
] ]
# Threshold for security vulnerabilities, any vulnerability with a CVSS score # Threshold for security vulnerabilities, any vulnerability with a CVSS score
# lower than the range specified will be ignored. Note that ignored advisories # lower than the range specified will be ignored. Note that ignored advisories

View File

@ -3,7 +3,7 @@ use anyhow::{bail, Context, Error};
use helpers::Helpers; use helpers::Helpers;
use std::{collections::HashMap, env, sync::Arc}; use std::{collections::HashMap, env, sync::Arc};
use swc::{ use swc::{
config::{InputSourceMap, JscConfig, TransformConfig}, config::{GlobalInliningPassEnvs, InputSourceMap, JscConfig, TransformConfig},
try_with_handler, try_with_handler,
}; };
use swc_atoms::JsWord; use swc_atoms::JsWord;
@ -46,13 +46,16 @@ impl SwcLoader {
.and_then(|g| Some(g.envs.clone())) .and_then(|g| Some(g.envs.clone()))
.unwrap_or_default(); .unwrap_or_default();
let envs_map: AHashMap<_, _> = envs let envs_map: AHashMap<_, _> = match envs {
.into_iter() GlobalInliningPassEnvs::List(envs) => envs
.map(|name| { .into_iter()
let value = env::var(&name).ok(); .map(|name| {
(name, value.unwrap_or_default()) let value = env::var(&name).ok();
}) (name, value.unwrap_or_default())
.collect(); })
.collect(),
GlobalInliningPassEnvs::Map(m) => m,
};
for (k, v) in envs_map { for (k, v) in envs_map {
m.insert( m.insert(

View File

@ -846,7 +846,7 @@ impl Merge for JscExperimental {
fn merge(&mut self, _from: &Self) {} fn merge(&mut self, _from: &Self) {}
} }
/// `paths` sectiob of `tsconfig.json`. /// `paths` section of `tsconfig.json`.
pub type Paths = AHashMap<String, Vec<String>>; pub type Paths = AHashMap<String, Vec<String>>;
pub(crate) type CompiledPaths = Vec<(String, Vec<String>)>; pub(crate) type CompiledPaths = Vec<(String, Vec<String>)>;
@ -988,15 +988,24 @@ fn default_jsonify_min_cost() -> usize {
pub struct GlobalPassOption { pub struct GlobalPassOption {
#[serde(default)] #[serde(default)]
pub vars: AHashMap<String, String>, pub vars: AHashMap<String, String>,
#[serde(default = "default_envs")] #[serde(default)]
pub envs: AHashSet<String>, pub envs: GlobalInliningPassEnvs,
} }
fn default_envs() -> AHashSet<String> { #[derive(Debug, Clone, Serialize, Deserialize)]
let mut v = HashSet::default(); pub enum GlobalInliningPassEnvs {
v.insert(String::from("NODE_ENV")); List(AHashSet<String>),
v.insert(String::from("SWC_ENV")); Map(AHashMap<String, String>),
v }
impl Default for GlobalInliningPassEnvs {
fn default() -> Self {
let mut v = HashSet::default();
v.insert(String::from("NODE_ENV"));
v.insert(String::from("SWC_ENV"));
GlobalInliningPassEnvs::List(v)
}
} }
impl GlobalPassOption { impl GlobalPassOption {
@ -1056,22 +1065,49 @@ impl GlobalPassOption {
let env_map = if cfg!(target_arch = "wasm32") { let env_map = if cfg!(target_arch = "wasm32") {
Arc::new(Default::default()) Arc::new(Default::default())
} else { } else {
static CACHE: Lazy<DashMap<Vec<String>, ValuesMap, ahash::RandomState>> = match &self.envs {
Lazy::new(|| Default::default()); GlobalInliningPassEnvs::List(env_list) => {
static CACHE: Lazy<DashMap<Vec<String>, ValuesMap, ahash::RandomState>> =
Lazy::new(|| Default::default());
let cache_key = self.envs.iter().cloned().collect::<Vec<_>>(); let cache_key = env_list.iter().cloned().collect::<Vec<_>>();
if let Some(v) = CACHE.get(&cache_key).as_deref().cloned() { if let Some(v) = CACHE.get(&cache_key).as_deref().cloned() {
v v
} else { } else {
let envs = self.envs; let map = mk_map(
let map = mk_map( cm,
cm, handler,
handler, env::vars().filter(|(k, _)| env_list.contains(&*k)),
env::vars().filter(|(k, _)| envs.contains(&*k)), true,
true, );
); CACHE.insert(cache_key, map.clone());
CACHE.insert(cache_key, map.clone()); map
map }
}
GlobalInliningPassEnvs::Map(map) => {
static CACHE: Lazy<
DashMap<Vec<(String, String)>, ValuesMap, ahash::RandomState>,
> = Lazy::new(|| Default::default());
let cache_key = self
.vars
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect::<Vec<_>>();
if let Some(v) = CACHE.get(&cache_key) {
(*v).clone()
} else {
let map = mk_map(
cm,
handler,
map.into_iter().map(|(k, v)| (k.clone(), v.clone())),
false,
);
CACHE.insert(cache_key, map.clone());
map
}
}
} }
}; };