mirror of
https://github.com/swc-project/swc.git
synced 2024-12-26 23:27:56 +03:00
feat(swc): Accept map for envs
(#2467)
swc: - Support specifying map to global inlining pass.
This commit is contained in:
parent
98a18e306a
commit
28f2c7ae5d
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -2324,7 +2324,7 @@ checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c"
|
||||
|
||||
[[package]]
|
||||
name = "swc"
|
||||
version = "0.72.2"
|
||||
version = "0.73.0"
|
||||
dependencies = [
|
||||
"ahash",
|
||||
"anyhow",
|
||||
|
@ -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"
|
||||
|
@ -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
|
||||
|
@ -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(
|
||||
|
@ -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<String, Vec<String>>;
|
||||
pub(crate) type CompiledPaths = Vec<(String, Vec<String>)>;
|
||||
|
||||
@ -988,15 +988,24 @@ fn default_jsonify_min_cost() -> usize {
|
||||
pub struct GlobalPassOption {
|
||||
#[serde(default)]
|
||||
pub vars: AHashMap<String, String>,
|
||||
#[serde(default = "default_envs")]
|
||||
pub envs: AHashSet<String>,
|
||||
#[serde(default)]
|
||||
pub envs: GlobalInliningPassEnvs,
|
||||
}
|
||||
|
||||
fn default_envs() -> AHashSet<String> {
|
||||
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<String>),
|
||||
Map(AHashMap<String, String>),
|
||||
}
|
||||
|
||||
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<DashMap<Vec<String>, ValuesMap, ahash::RandomState>> =
|
||||
Lazy::new(|| Default::default());
|
||||
match &self.envs {
|
||||
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<_>>();
|
||||
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::<Vec<_>>();
|
||||
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<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
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user