swc_bundler/bundler/
scope.rsuse std::sync::atomic::{AtomicBool, Ordering};
use swc_common::{sync::Lrc, FileName};
use super::load::TransformedModule;
use crate::{
id::{Id, ModuleId, ModuleIdGenerator},
util::CloneMap,
};
#[derive(Debug, Default)]
pub(super) struct Scope {
pub module_id_gen: ModuleIdGenerator,
loaded_modules: CloneMap<ModuleId, ()>,
transformed_modules: CloneMap<ModuleId, TransformedModule>,
accessed_with_computed_key: CloneMap<ModuleId, Lrc<AtomicBool>>,
is_cjs: CloneMap<ModuleId, Lrc<AtomicBool>>,
}
impl Scope {
pub fn mark_as_loaded(&self, id: ModuleId) {
self.loaded_modules.insert(id, ());
}
pub fn store_module(&self, info: TransformedModule) {
self.transformed_modules.insert(info.id, info);
}
pub fn get_module_by_path(&self, file_name: &FileName) -> Option<TransformedModule> {
let (id, _, _) = self.module_id_gen.gen(file_name);
self.get_module(id)
}
pub fn get_module(&self, id: ModuleId) -> Option<TransformedModule> {
self.transformed_modules.get(&id)
}
pub fn is_cjs(&self, id: ModuleId) -> bool {
if let Some(v) = self.is_cjs.get(&id) {
v.load(Ordering::SeqCst)
} else {
false
}
}
pub fn mark_as_cjs(&self, id: ModuleId) {
if let Some(v) = self.is_cjs.get(&id) {
v.store(true, Ordering::SeqCst);
return;
}
self.is_cjs.insert(id, Lrc::new(AtomicBool::from(true)));
}
pub fn mark_as_wrapping_required(&self, id: ModuleId) {
if let Some(v) = self.accessed_with_computed_key.get(&id) {
v.store(true, Ordering::SeqCst);
return;
}
self.accessed_with_computed_key
.insert(id, Lrc::new(AtomicBool::from(true)));
}
pub fn should_be_wrapped_with_a_fn(&self, id: ModuleId) -> bool {
if let Some(v) = self.accessed_with_computed_key.get(&id) {
v.load(Ordering::SeqCst)
} else {
false
}
}
pub fn wrapped_esm_id(&self, id: ModuleId) -> Option<Id> {
if !self.should_be_wrapped_with_a_fn(id) {
return None;
}
let info = self.get_module(id)?;
Some(Id::new("mod".into(), info.export_ctxt()))
}
}