swc_common/plugin/
metadata.rsuse std::env;
use once_cell::sync::Lazy;
use crate::collections::AHashMap;
#[derive(Copy, Clone)]
pub enum TransformPluginMetadataContextKind {
Filename = 1,
Env = 2,
Cwd = 3,
}
impl From<u32> for TransformPluginMetadataContextKind {
fn from(key: u32) -> TransformPluginMetadataContextKind {
match key {
1 => TransformPluginMetadataContextKind::Filename,
2 => TransformPluginMetadataContextKind::Env,
3 => TransformPluginMetadataContextKind::Cwd,
_ => panic!("Invalid TransformPluginMetadataContextKind key"),
}
}
}
pub struct TransformPluginMetadataContext {
pub filename: Option<String>,
pub env: String,
pub cwd: Option<String>,
pub experimental: AHashMap<String, String>,
}
impl TransformPluginMetadataContext {
pub fn new(
filename: Option<String>,
env: String,
experimental: Option<AHashMap<String, String>>,
) -> Self {
static CWD: Lazy<Option<String>> = Lazy::new(|| {
env::current_dir()
.map(|cwd| cwd.as_path().to_string_lossy().to_string())
.ok()
});
TransformPluginMetadataContext {
filename,
env,
cwd: CWD.clone(),
experimental: experimental.unwrap_or_default(),
}
}
pub fn get(&self, key: &TransformPluginMetadataContextKind) -> Option<String> {
match key {
TransformPluginMetadataContextKind::Filename => self.filename.clone(),
TransformPluginMetadataContextKind::Env => Some(self.env.clone()),
TransformPluginMetadataContextKind::Cwd => self.cwd.clone(),
}
}
}