mod disable;
mod memory;
mod mixed;
pub mod persistent;
use std::{fmt::Debug, sync::Arc};
use rspack_fs::{IntermediateFileSystem, ReadableFileSystem};
use self::{
disable::DisableCache, memory::MemoryCache, mixed::MixedCache, persistent::PersistentCache,
};
use crate::{CacheOptions, Compilation, CompilerOptions};
#[async_trait::async_trait]
pub trait Cache: Debug + Send + Sync {
async fn before_compile(&mut self, _compilation: &mut Compilation) -> bool {
false
}
async fn after_compile(&mut self, _compilation: &Compilation) {}
async fn before_build_module_graph(&mut self, _compilation: &mut Compilation) {}
async fn after_build_module_graph(&mut self, _compilation: &Compilation) {}
async fn before_finish_modules(&mut self, _compilation: &mut Compilation) {}
async fn after_finish_modules(&self, _compilation: &Compilation) {}
async fn before_optimize_dependencies(&mut self, _compilation: &mut Compilation) {}
async fn after_optimize_dependencies(&self, _compilation: &Compilation) {}
async fn before_build_chunk_graph(&mut self, _compilation: &mut Compilation) {}
async fn after_build_chunk_graph(&mut self, _compilation: &mut Compilation) {}
async fn before_optimize_chunk_modules(&mut self, _compilation: &mut Compilation) {}
async fn after_optimize_chunk_modules(&self, _compilation: &Compilation) {}
async fn before_module_ids(&mut self, _compilation: &mut Compilation) {}
async fn after_module_ids(&self, _compilation: &Compilation) {}
async fn before_chunk_ids(&mut self, _compilation: &mut Compilation) {}
async fn after_chunk_ids(&self, _compilation: &Compilation) {}
async fn before_modules_hashes(&mut self, _compilation: &mut Compilation) {}
async fn after_modules_hashes(&self, _compilation: &Compilation) {}
async fn before_modules_codegen(&mut self, _compilation: &mut Compilation) {}
async fn after_modules_codegen(&self, _compilation: &Compilation) {}
async fn before_modules_runtime_requirements(&mut self, _compilation: &mut Compilation) {}
async fn after_modules_runtime_requirements(&self, _compilation: &Compilation) {}
async fn before_chunks_runtime_requirements(&mut self, _compilation: &mut Compilation) {}
async fn after_chunks_runtime_requirements(&self, _compilation: &Compilation) {}
async fn before_chunks_hashes(&mut self, _compilation: &mut Compilation) {}
async fn after_chunks_hashes(&self, _compilation: &Compilation) {}
async fn before_chunk_asset(&mut self, _compilation: &mut Compilation) {}
async fn after_chunk_asset(&self, _compilation: &Compilation) {}
async fn before_process_assets(&mut self, _compilation: &mut Compilation) {}
async fn after_process_assets(&mut self, _compilation: &Compilation) {}
async fn before_emit_assets(&mut self, _compilation: &mut Compilation) {}
async fn after_emit_assets(&self, _compilation: &Compilation) {}
fn store_old_compilation(&mut self, _compilation: Box<Compilation>) {}
async fn close(&self) {}
}
pub fn new_cache(
compiler_path: &str,
compiler_option: Arc<CompilerOptions>,
input_filesystem: Arc<dyn ReadableFileSystem>,
intermediate_filesystem: Arc<dyn IntermediateFileSystem>,
) -> Box<dyn Cache> {
match &compiler_option.cache {
CacheOptions::Disabled => Box::new(DisableCache),
CacheOptions::Memory { .. } => Box::<MemoryCache>::default(),
CacheOptions::Persistent(option) => {
let persistent = PersistentCache::new(
compiler_path,
option,
compiler_option.clone(),
input_filesystem,
intermediate_filesystem,
);
Box::new(MixedCache::new(persistent))
}
}
}