diff --git a/Cargo.lock b/Cargo.lock index 3f5250cd2e4..2ecfd764064 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2324,7 +2324,7 @@ checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c" [[package]] name = "swc" -version = "0.72.2" +version = "0.73.0" dependencies = [ "ahash", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index 6297017e25a..99cef974f36 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ include = ["Cargo.toml", "src/**/*.rs"] license = "Apache-2.0/MIT" name = "swc" repository = "https://github.com/swc-project/swc.git" -version = "0.72.2" +version = "0.73.0" [lib] name = "swc" diff --git a/deny.toml b/deny.toml index e38122103e6..3eb259a4f0c 100644 --- a/deny.toml +++ b/deny.toml @@ -50,6 +50,7 @@ notice = "warn" ignore = [ #"RUSTSEC-0000-0000", "RUSTSEC-2020-0071", + "RUSTSEC-2020-0159", ] # Threshold for security vulnerabilities, any vulnerability with a CVSS score # lower than the range specified will be ignored. Note that ignored advisories diff --git a/node/bundler/src/loaders/swc.rs b/node/bundler/src/loaders/swc.rs index e51e4845e27..7a2bb121a2e 100644 --- a/node/bundler/src/loaders/swc.rs +++ b/node/bundler/src/loaders/swc.rs @@ -3,7 +3,7 @@ use anyhow::{bail, Context, Error}; use helpers::Helpers; use std::{collections::HashMap, env, sync::Arc}; use swc::{ - config::{InputSourceMap, JscConfig, TransformConfig}, + config::{GlobalInliningPassEnvs, InputSourceMap, JscConfig, TransformConfig}, try_with_handler, }; use swc_atoms::JsWord; @@ -46,13 +46,16 @@ impl SwcLoader { .and_then(|g| Some(g.envs.clone())) .unwrap_or_default(); - let envs_map: AHashMap<_, _> = envs - .into_iter() - .map(|name| { - let value = env::var(&name).ok(); - (name, value.unwrap_or_default()) - }) - .collect(); + let envs_map: AHashMap<_, _> = match envs { + GlobalInliningPassEnvs::List(envs) => envs + .into_iter() + .map(|name| { + let value = env::var(&name).ok(); + (name, value.unwrap_or_default()) + }) + .collect(), + GlobalInliningPassEnvs::Map(m) => m, + }; for (k, v) in envs_map { m.insert( diff --git a/src/config/mod.rs b/src/config/mod.rs index 9c1b91c3dcb..55034ea99d5 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -846,7 +846,7 @@ impl Merge for JscExperimental { fn merge(&mut self, _from: &Self) {} } -/// `paths` sectiob of `tsconfig.json`. +/// `paths` section of `tsconfig.json`. pub type Paths = AHashMap>; pub(crate) type CompiledPaths = Vec<(String, Vec)>; @@ -988,15 +988,24 @@ fn default_jsonify_min_cost() -> usize { pub struct GlobalPassOption { #[serde(default)] pub vars: AHashMap, - #[serde(default = "default_envs")] - pub envs: AHashSet, + #[serde(default)] + pub envs: GlobalInliningPassEnvs, } -fn default_envs() -> AHashSet { - let mut v = HashSet::default(); - v.insert(String::from("NODE_ENV")); - v.insert(String::from("SWC_ENV")); - v +#[derive(Debug, Clone, Serialize, Deserialize)] +pub enum GlobalInliningPassEnvs { + List(AHashSet), + Map(AHashMap), +} + +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 { @@ -1056,22 +1065,49 @@ impl GlobalPassOption { let env_map = if cfg!(target_arch = "wasm32") { Arc::new(Default::default()) } else { - static CACHE: Lazy, ValuesMap, ahash::RandomState>> = - Lazy::new(|| Default::default()); + match &self.envs { + GlobalInliningPassEnvs::List(env_list) => { + static CACHE: Lazy, ValuesMap, ahash::RandomState>> = + Lazy::new(|| Default::default()); - let cache_key = self.envs.iter().cloned().collect::>(); - if let Some(v) = CACHE.get(&cache_key).as_deref().cloned() { - v - } else { - let envs = self.envs; - let map = mk_map( - cm, - handler, - env::vars().filter(|(k, _)| envs.contains(&*k)), - true, - ); - CACHE.insert(cache_key, map.clone()); - map + let cache_key = env_list.iter().cloned().collect::>(); + if let Some(v) = CACHE.get(&cache_key).as_deref().cloned() { + v + } else { + let map = mk_map( + cm, + handler, + env::vars().filter(|(k, _)| env_list.contains(&*k)), + true, + ); + CACHE.insert(cache_key, map.clone()); + map + } + } + + GlobalInliningPassEnvs::Map(map) => { + static CACHE: Lazy< + DashMap, ValuesMap, ahash::RandomState>, + > = Lazy::new(|| Default::default()); + + let cache_key = self + .vars + .iter() + .map(|(k, v)| (k.clone(), v.clone())) + .collect::>(); + 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 + } + } } };