diff --git a/.cargo/config.toml b/.cargo/config.toml index 7065f628e4a..28198e936c6 100755 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -27,7 +27,8 @@ rustflags = [ "-Wclippy::empty_structs_with_brackets", "-Wclippy::rc_buffer", "-Wclippy::rc_mutex", - "-Wclippy::same_name_method", + # https://github.com/bitflags/bitflags/issues/424 + # "-Wclippy::same_name_method", "-Aclippy::default_constructed_unit_structs", "--cfg", "tokio_unstable" diff --git a/Cargo.lock b/Cargo.lock index 6bfd3308e0a..ac54874d177 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3783,6 +3783,7 @@ dependencies = [ "napi-derive", "pollster", "rspack_binding_values", + "rspack_cacheable", "rspack_collections", "rspack_core", "rspack_error", @@ -3883,6 +3884,7 @@ dependencies = [ "rspack_resolver", "rspack_sources", "serde_json", + "smol_str", "swc_core", "ustr-fxhash", ] @@ -3913,6 +3915,7 @@ dependencies = [ "hashlink", "indexmap 2.7.0", "rayon", + "rspack_cacheable", "serde", "ustr-fxhash", ] @@ -3992,6 +3995,7 @@ dependencies = [ "miette 5.10.0", "once_cell", "owo-colors 3.5.0", + "rspack_cacheable", "rspack_collections", "rspack_paths", "swc_core", @@ -4040,6 +4044,7 @@ version = "0.2.0" dependencies = [ "data-encoding", "md4", + "rspack_cacheable", "smol_str", "xxhash-rust", ] @@ -4079,6 +4084,7 @@ dependencies = [ "derivative", "lightningcss", "parcel_sourcemap", + "rspack_cacheable", "rspack_core", "rspack_error", "rspack_loader_runner", @@ -4091,6 +4097,7 @@ name = "rspack_loader_preact_refresh" version = "0.2.0" dependencies = [ "async-trait", + "rspack_cacheable", "rspack_core", "rspack_error", "rspack_loader_runner", @@ -4101,6 +4108,7 @@ name = "rspack_loader_react_refresh" version = "0.2.0" dependencies = [ "async-trait", + "rspack_cacheable", "rspack_core", "rspack_error", "rspack_loader_runner", @@ -4115,6 +4123,7 @@ dependencies = [ "derivative", "once_cell", "regex", + "rspack_cacheable", "rspack_collections", "rspack_error", "rspack_fs", @@ -4136,6 +4145,7 @@ dependencies = [ "either", "jsonc-parser 0.26.2", "rspack_ast", + "rspack_cacheable", "rspack_core", "rspack_error", "rspack_loader_runner", @@ -4156,6 +4166,7 @@ name = "rspack_loader_testing" version = "0.2.0" dependencies = [ "async-trait", + "rspack_cacheable", "rspack_core", "rspack_error", "rspack_loader_runner", @@ -4175,6 +4186,7 @@ dependencies = [ name = "rspack_macros_test" version = "0.2.0" dependencies = [ + "rspack_cacheable", "rspack_collections", "rspack_core", "rspack_error", @@ -4240,6 +4252,7 @@ name = "rspack_paths" version = "0.2.0" dependencies = [ "camino", + "rspack_cacheable", ] [[package]] @@ -4250,6 +4263,7 @@ dependencies = [ "mime_guess", "rayon", "rspack_base64", + "rspack_cacheable", "rspack_core", "rspack_error", "rspack_hash", @@ -4319,10 +4333,11 @@ dependencies = [ "cow-utils", "css-module-lexer", "heck 0.5.0", - "indexmap 1.9.3", + "indexmap 2.7.0", "once_cell", "rayon", "regex", + "rspack_cacheable", "rspack_collections", "rspack_core", "rspack_error", @@ -4366,6 +4381,7 @@ name = "rspack_plugin_dll" version = "0.2.0" dependencies = [ "async-trait", + "rspack_cacheable", "rspack_collections", "rspack_core", "rspack_error", @@ -4433,6 +4449,7 @@ dependencies = [ "async-trait", "cow-utils", "regex", + "rspack_cacheable", "rspack_collections", "rspack_core", "rspack_error", @@ -4456,6 +4473,7 @@ version = "0.2.0" dependencies = [ "async-trait", "cow-utils", + "rspack_cacheable", "rspack_collections", "rspack_core", "rspack_error", @@ -4531,6 +4549,7 @@ dependencies = [ "regex", "ropey", "rspack_ast", + "rspack_cacheable", "rspack_collections", "rspack_core", "rspack_error", @@ -4557,6 +4576,7 @@ dependencies = [ "cow-utils", "json", "ropey", + "rspack_cacheable", "rspack_core", "rspack_error", "rspack_util", @@ -4568,6 +4588,7 @@ version = "0.2.0" dependencies = [ "async-trait", "cow-utils", + "rspack_cacheable", "rspack_collections", "rspack_core", "rspack_error", @@ -4586,6 +4607,7 @@ version = "0.2.0" dependencies = [ "async-trait", "regex", + "rspack_cacheable", "rspack_collections", "rspack_core", "rspack_error", @@ -4646,6 +4668,7 @@ dependencies = [ "hashlink", "itertools 0.13.0", "regex", + "rspack_cacheable", "rspack_collections", "rspack_core", "rspack_error", @@ -4731,6 +4754,7 @@ dependencies = [ "derivative", "indexmap 2.7.0", "itertools 0.13.0", + "rspack_cacheable", "rspack_collections", "rspack_core", "rspack_error", @@ -4853,6 +4877,7 @@ dependencies = [ "dashmap 6.1.0", "indexmap 2.7.0", "rayon", + "rspack_cacheable", "rspack_collections", "rspack_core", "rspack_error", @@ -4890,6 +4915,7 @@ dependencies = [ "napi", "regex-syntax 0.8.5", "regress", + "rspack_cacheable", "rspack_error", "swc_core", ] @@ -4964,6 +4990,7 @@ dependencies = [ "itoa", "regex", "ropey", + "rspack_cacheable", "rspack_regex", "rustc-hash 2.1.0", "serde", diff --git a/Cargo.toml b/Cargo.toml index affa51f803d..23dd2f61238 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -71,6 +71,7 @@ rustc-hash = { version = "2.1.0" } serde = { version = "1.0.215" } serde_json = { version = "1.0.133" } simd-json = { version = "0.14.0-rc.2" } +smol_str = { version = "0.3.0" } stacker = { version = "0.1.17" } sugar_path = { version = "1.2.0", features = ["cached_current_dir"] } syn = { version = "2.0.90" } diff --git a/crates/rspack_binding_options/Cargo.toml b/crates/rspack_binding_options/Cargo.toml index 688f4b1632f..2ed2b797736 100644 --- a/crates/rspack_binding_options/Cargo.toml +++ b/crates/rspack_binding_options/Cargo.toml @@ -21,6 +21,7 @@ napi = { workspace = true, features = ["async" napi-derive = { workspace = true } pollster = { workspace = true } rspack_binding_values = { workspace = true } +rspack_cacheable = { workspace = true } rspack_collections = { workspace = true } rspack_core = { workspace = true } rspack_error = { workspace = true } diff --git a/crates/rspack_binding_options/src/plugins/js_loader/resolver.rs b/crates/rspack_binding_options/src/plugins/js_loader/resolver.rs index d08f37f1cea..558b8c91fad 100644 --- a/crates/rspack_binding_options/src/plugins/js_loader/resolver.rs +++ b/crates/rspack_binding_options/src/plugins/js_loader/resolver.rs @@ -3,6 +3,7 @@ use std::{ sync::{Arc, LazyLock}, }; +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_collections::{Identifiable, Identifier}; use rspack_core::{ BoxLoader, Context, Loader, ModuleRuleUseLoader, NormalModuleFactoryResolveLoader, ResolveResult, @@ -24,9 +25,11 @@ use tokio::sync::RwLock; use super::{JsLoaderRspackPlugin, JsLoaderRspackPluginInner}; +#[cacheable] #[derive(Debug)] pub struct JsLoader(pub Identifier); +#[cacheable_dyn] impl Loader for JsLoader {} impl Identifiable for JsLoader { @@ -60,14 +63,15 @@ pub async fn get_builtin_loader(builtin: &str, options: Option<&str>) -> Result< } let loader = Arc::new( - rspack_loader_swc::SwcLoader::new(serde_json::from_str(options.as_ref()).map_err(|e| { - serde_error_to_miette( - e, - options.clone(), - "failed to parse builtin:swc-loader options", - ) - })?) - .with_identifier(builtin.into()), + rspack_loader_swc::SwcLoader::new(options.as_ref()) + .map_err(|e| { + serde_error_to_miette( + e, + options.clone(), + "failed to parse builtin:swc-loader options", + ) + })? + .with_identifier(builtin.into()), ); SWC_LOADER_CACHE.write().await.insert( diff --git a/crates/rspack_cacheable/Cargo.toml b/crates/rspack_cacheable/Cargo.toml index 6c70f4811d2..9dee325c149 100644 --- a/crates/rspack_cacheable/Cargo.toml +++ b/crates/rspack_cacheable/Cargo.toml @@ -23,5 +23,6 @@ rspack_macros = { workspace = true } rspack_resolver = { workspace = true } rspack_sources = { workspace = true } serde_json = { workspace = true } +smol_str = { workspace = true } swc_core = { workspace = true, features = ["ecma_ast"] } ustr = { workspace = true } diff --git a/crates/rspack_cacheable/src/with/as_preset/mod.rs b/crates/rspack_cacheable/src/with/as_preset/mod.rs index c26358bd941..1c6165ca4dd 100644 --- a/crates/rspack_cacheable/src/with/as_preset/mod.rs +++ b/crates/rspack_cacheable/src/with/as_preset/mod.rs @@ -4,6 +4,7 @@ mod lightningcss; mod rspack_resolver; mod rspack_sources; mod serde_json; +mod smol_str; mod swc; mod ustr; diff --git a/crates/rspack_cacheable/src/with/as_preset/smol_str.rs b/crates/rspack_cacheable/src/with/as_preset/smol_str.rs new file mode 100644 index 00000000000..4c7c49ad289 --- /dev/null +++ b/crates/rspack_cacheable/src/with/as_preset/smol_str.rs @@ -0,0 +1,41 @@ +use rkyv::{ + rancor::{Fallible, Source}, + ser::Writer, + string::{ArchivedString, StringResolver}, + with::{ArchiveWith, DeserializeWith, SerializeWith}, + Place, +}; +use smol_str::SmolStr; + +use super::AsPreset; + +impl ArchiveWith for AsPreset { + type Archived = ArchivedString; + type Resolver = StringResolver; + + #[inline] + fn resolve_with(field: &SmolStr, resolver: Self::Resolver, out: Place) { + ArchivedString::resolve_from_str(field.as_str(), resolver, out); + } +} + +impl SerializeWith for AsPreset +where + S: ?Sized + Fallible + Writer, + S::Error: Source, +{ + #[inline] + fn serialize_with(field: &SmolStr, serializer: &mut S) -> Result { + ArchivedString::serialize_from_str(field.as_str(), serializer) + } +} + +impl DeserializeWith for AsPreset +where + D: ?Sized + Fallible, +{ + #[inline] + fn deserialize_with(field: &ArchivedString, _: &mut D) -> Result { + Ok(SmolStr::from(field.as_str())) + } +} diff --git a/crates/rspack_cacheable/src/with/as_ref_str.rs b/crates/rspack_cacheable/src/with/as_ref_str.rs index ec1d1abaf78..912ac2b79bc 100644 --- a/crates/rspack_cacheable/src/with/as_ref_str.rs +++ b/crates/rspack_cacheable/src/with/as_ref_str.rs @@ -87,3 +87,16 @@ impl AsRefStrConverter for Arc { } } } + +// for Box +impl AsRefStrConverter for Box { + fn as_str(&self) -> &str { + self + } + fn from_str(s: &str) -> Self + where + Self: Sized, + { + s.into() + } +} diff --git a/crates/rspack_cacheable/src/with/as_string.rs b/crates/rspack_cacheable/src/with/as_string.rs index a7003758966..3cc80197c37 100644 --- a/crates/rspack_cacheable/src/with/as_string.rs +++ b/crates/rspack_cacheable/src/with/as_string.rs @@ -73,16 +73,3 @@ impl AsStringConverter for PathBuf { Ok(PathBuf::from(s)) } } - -// for Box -impl AsStringConverter for Box { - fn to_string(&self) -> Result { - Ok(str::to_string(self)) - } - fn from_str(s: &str) -> Result - where - Self: Sized, - { - Ok(s.into()) - } -} diff --git a/crates/rspack_collections/Cargo.toml b/crates/rspack_collections/Cargo.toml index b98e18ed25f..a1a5b3df41b 100644 --- a/crates/rspack_collections/Cargo.toml +++ b/crates/rspack_collections/Cargo.toml @@ -7,10 +7,12 @@ license = "MIT" name = "rspack_collections" repository.workspace = true version = "0.2.0" + [dependencies] -dashmap = { workspace = true } -hashlink = { workspace = true } -indexmap = { workspace = true } -rayon = { workspace = true } -serde = { workspace = true, features = ["derive"] } -ustr = { workspace = true, features = ["serde"] } +dashmap = { workspace = true } +hashlink = { workspace = true } +indexmap = { workspace = true } +rayon = { workspace = true } +rspack_cacheable = { workspace = true } +serde = { workspace = true, features = ["derive"] } +ustr = { workspace = true, features = ["serde"] } diff --git a/crates/rspack_collections/src/identifier.rs b/crates/rspack_collections/src/identifier.rs index 64b01a9d751..91d989f1e0c 100644 --- a/crates/rspack_collections/src/identifier.rs +++ b/crates/rspack_collections/src/identifier.rs @@ -5,6 +5,7 @@ use std::{convert::From, fmt, ops::Deref}; use dashmap::{DashMap, DashSet}; use hashlink::{LinkedHashMap, LinkedHashSet}; use indexmap::{IndexMap, IndexSet}; +use rspack_cacheable::{cacheable, with::AsPreset}; use serde::Serialize; use ustr::Ustr; @@ -29,8 +30,9 @@ pub type IdentifierIndexSet = IndexSet>; pub type IdentifierLinkedSet = LinkedHashSet>; +#[cacheable(hashable)] #[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize)] -pub struct Identifier(Ustr); +pub struct Identifier(#[cacheable(with=AsPreset)] Ustr); impl Deref for Identifier { type Target = Ustr; diff --git a/crates/rspack_collections/src/ukey.rs b/crates/rspack_collections/src/ukey.rs index 224bc1c17bf..57eb5ab4203 100644 --- a/crates/rspack_collections/src/ukey.rs +++ b/crates/rspack_collections/src/ukey.rs @@ -31,6 +31,7 @@ pub trait ItemUkey { } /// Ukey stands for Unique key +#[rspack_cacheable::cacheable] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] pub struct Ukey(u32); diff --git a/crates/rspack_core/src/cache/disable.rs b/crates/rspack_core/src/cache/disable.rs index dfac1117ed5..fb4ae2d825e 100644 --- a/crates/rspack_core/src/cache/disable.rs +++ b/crates/rspack_core/src/cache/disable.rs @@ -1,4 +1,5 @@ use super::Cache; +use crate::make::MakeArtifact; /// Disable cache implementation /// @@ -6,4 +7,8 @@ use super::Cache; #[derive(Debug)] pub struct DisableCache; -impl Cache for DisableCache {} +impl Cache for DisableCache { + fn before_make(&self, make_artifact: &mut MakeArtifact) { + *make_artifact = Default::default(); + } +} diff --git a/crates/rspack_core/src/cache/mod.rs b/crates/rspack_core/src/cache/mod.rs index 8a36d571faa..5e481cee4d0 100644 --- a/crates/rspack_core/src/cache/mod.rs +++ b/crates/rspack_core/src/cache/mod.rs @@ -7,7 +7,7 @@ use std::{fmt::Debug, sync::Arc}; use rspack_fs::FileSystem; use self::{disable::DisableCache, memory::MemoryCache, persistent::PersistentCache}; -use crate::{Compilation, CompilerOptions, ExperimentCacheOptions}; +use crate::{make::MakeArtifact, Compilation, CompilerOptions, ExperimentCacheOptions}; /// Cache trait /// @@ -24,12 +24,22 @@ use crate::{Compilation, CompilerOptions, ExperimentCacheOptions}; pub trait Cache: Debug + Send + Sync { fn before_compile(&self, _compilation: &mut Compilation) {} fn after_compile(&self, _compilation: &Compilation) {} + + fn before_make(&self, _make_artifact: &mut MakeArtifact) {} + fn after_make(&self, _make_artifact: &MakeArtifact) {} } -pub fn new_cache(compiler_option: Arc, fs: Arc) -> Arc { +pub fn new_cache( + compiler_option: Arc, + input_filesystem: Arc, +) -> Arc { match &compiler_option.experiments.cache { ExperimentCacheOptions::Disabled => Arc::new(DisableCache), ExperimentCacheOptions::Memory => Arc::new(MemoryCache), - ExperimentCacheOptions::Persistent(option) => Arc::new(PersistentCache::new(option, fs)), + ExperimentCacheOptions::Persistent(option) => Arc::new(PersistentCache::new( + option, + input_filesystem, + compiler_option.clone(), + )), } } diff --git a/crates/rspack_core/src/cache/persistent/cacheable_context.rs b/crates/rspack_core/src/cache/persistent/cacheable_context.rs new file mode 100644 index 00000000000..f9306fae26b --- /dev/null +++ b/crates/rspack_core/src/cache/persistent/cacheable_context.rs @@ -0,0 +1,27 @@ +use std::{any::Any, sync::Arc}; + +use rspack_cacheable::{cacheable, with::AsConverter, DeserializeError, SerializeError}; +use rspack_fs::FileSystem; + +use crate::CompilerOptions; + +#[derive(Debug)] +pub struct CacheableContext { + pub options: Arc, + pub input_filesystem: Arc, +} + +#[cacheable] +pub struct FromContext; + +impl AsConverter> for FromContext { + fn serialize(_data: &Arc, _ctx: &dyn Any) -> Result { + Ok(FromContext) + } + fn deserialize(self, ctx: &dyn Any) -> Result, DeserializeError> { + let Some(ctx) = ctx.downcast_ref::() else { + return Err(DeserializeError::NoContext); + }; + Ok(ctx.options.clone()) + } +} diff --git a/crates/rspack_core/src/cache/persistent/mod.rs b/crates/rspack_core/src/cache/persistent/mod.rs index e5f4b1de7d2..4553fcb9bbf 100644 --- a/crates/rspack_core/src/cache/persistent/mod.rs +++ b/crates/rspack_core/src/cache/persistent/mod.rs @@ -1,8 +1,11 @@ +mod cacheable_context; +mod occasion; pub mod snapshot; pub mod storage; - use std::sync::Arc; +pub use cacheable_context::{CacheableContext, FromContext}; +use occasion::MakeOccasion; use rspack_fs::FileSystem; use rspack_paths::ArcPath; use rustc_hash::FxHashSet as HashSet; @@ -12,7 +15,7 @@ use self::{ storage::{MemoryStorage, Storage, StorageOptions}, }; use super::Cache; -use crate::Compilation; +use crate::{make::MakeArtifact, Compilation, CompilerOptions}; #[derive(Debug, Clone)] pub struct PersistentCacheOptions { @@ -25,14 +28,25 @@ pub struct PersistentCacheOptions { pub struct PersistentCache { storage: Arc, snapshot: Snapshot, + make_occasion: MakeOccasion, } impl PersistentCache { - pub fn new(option: &PersistentCacheOptions, fs: Arc) -> Self { + pub fn new( + option: &PersistentCacheOptions, + input_filesystem: Arc, + compiler_options: Arc, + ) -> Self { let storage = Arc::new(MemoryStorage::default()); + let context = Arc::new(CacheableContext { + options: compiler_options, + input_filesystem: input_filesystem.clone(), + }); + let make_occasion = MakeOccasion::new(storage.clone(), context); Self { - snapshot: Snapshot::new(option.snapshot.clone(), fs, storage.clone()), + snapshot: Snapshot::new(option.snapshot.clone(), input_filesystem, storage.clone()), storage, + make_occasion, } } } @@ -80,4 +94,16 @@ impl Cache for PersistentCache { self.storage.idle(); } + + fn before_make(&self, make_artifact: &mut MakeArtifact) { + if !make_artifact.initialized { + if let Ok(artifact) = self.make_occasion.recovery() { + *make_artifact = artifact; + } + } + } + + fn after_make(&self, make_artifact: &MakeArtifact) { + self.make_occasion.save(make_artifact); + } } diff --git a/crates/rspack_core/src/cache/persistent/occasion/make/dependencies.rs b/crates/rspack_core/src/cache/persistent/occasion/make/dependencies.rs new file mode 100644 index 00000000000..ecb62309c58 --- /dev/null +++ b/crates/rspack_core/src/cache/persistent/occasion/make/dependencies.rs @@ -0,0 +1,167 @@ +use std::sync::{Arc, Mutex}; + +use rayon::prelude::*; +use rspack_cacheable::{ + cacheable, from_bytes, to_bytes, with::Inline, DeserializeError, SerializeError, +}; +use rspack_paths::ArcPath; +use rustc_hash::FxHashMap as HashMap; + +use super::Storage; +use crate::FileCounter; + +const SCOPE: &str = "occasion::make::dependencies"; + +/// Dependency type +#[cacheable] +enum DepType { + /// file_dependencies + File, + /// context_dependencies + Context, + /// missing_dependencies + Missing, + /// build_dependencies + Build, +} + +/// The key struct of current storage scope +#[cacheable] +struct Dependency { + r#type: DepType, + path: ArcPath, +} + +#[cacheable(as=Dependency)] +struct DependencyRef<'a> { + r#type: DepType, + #[cacheable(with=Inline)] + path: &'a ArcPath, +} + +pub fn save_dependencies_info( + file_dependencies: &FileCounter, + context_dependencies: &FileCounter, + missing_dependencies: &FileCounter, + build_dependencies: &FileCounter, + storage: &Arc, +) -> Result<(), SerializeError> { + let f = file_dependencies + .updated_files_count_info() + .map(|(path, count)| { + ( + DependencyRef { + r#type: DepType::File, + path, + }, + count, + ) + }); + + let c = context_dependencies + .updated_files_count_info() + .map(|(path, count)| { + ( + DependencyRef { + r#type: DepType::Context, + path, + }, + count, + ) + }); + + let m = missing_dependencies + .updated_files_count_info() + .map(|(path, count)| { + ( + DependencyRef { + r#type: DepType::Missing, + path, + }, + count, + ) + }); + + let b = build_dependencies + .updated_files_count_info() + .map(|(path, count)| { + ( + DependencyRef { + r#type: DepType::Build, + path, + }, + count, + ) + }); + + f.chain(c) + .chain(m) + .chain(b) + .par_bridge() + .try_for_each(|(dep_ref, count)| { + let dep_ref = to_bytes(&dep_ref, &())?; + if count == 0 { + storage.remove(SCOPE, &dep_ref); + } else { + storage.set(SCOPE, dep_ref, count.to_ne_bytes().to_vec()); + } + Ok(()) + }) +} + +pub fn recovery_dependencies_info( + storage: &Arc, +) -> Result<(FileCounter, FileCounter, FileCounter, FileCounter), DeserializeError> { + let file_dep = Mutex::new(HashMap::default()); + let context_dep = Mutex::new(HashMap::default()); + let missing_dep = Mutex::new(HashMap::default()); + let build_dep = Mutex::new(HashMap::default()); + storage + .get_all(SCOPE) + .into_par_iter() + .try_for_each(|(k, v)| { + let count = usize::from_ne_bytes( + v.try_into() + .map_err(|_| DeserializeError::MessageError("deserialize count failed"))?, + ); + let Dependency { r#type, path } = from_bytes(&k, &())?; + match r#type { + DepType::File => file_dep + .lock() + .expect("should get file dep") + .insert(path, count), + DepType::Context => context_dep + .lock() + .expect("should get context dep") + .insert(path, count), + DepType::Missing => missing_dep + .lock() + .expect("should get missing dep") + .insert(path, count), + DepType::Build => build_dep + .lock() + .expect("should get build dep") + .insert(path, count), + }; + Ok(()) + })?; + + Ok(( + FileCounter::new(file_dep.into_inner().expect("into_inner should be success")), + FileCounter::new( + context_dep + .into_inner() + .expect("into_inner should be success"), + ), + FileCounter::new( + missing_dep + .into_inner() + .expect("into_inner should be success"), + ), + FileCounter::new( + build_dep + .into_inner() + .expect("into_inner should be success"), + ), + )) +} diff --git a/crates/rspack_core/src/cache/persistent/occasion/make/meta.rs b/crates/rspack_core/src/cache/persistent/occasion/make/meta.rs new file mode 100644 index 00000000000..8eb03d1b148 --- /dev/null +++ b/crates/rspack_core/src/cache/persistent/occasion/make/meta.rs @@ -0,0 +1,59 @@ +use std::sync::{atomic::Ordering::Relaxed, Arc}; + +use rspack_cacheable::{ + cacheable, from_bytes, to_bytes, with::Inline, DeserializeError, SerializeError, +}; +use rspack_collections::IdentifierSet; +use rustc_hash::FxHashSet as HashSet; + +use super::Storage; +use crate::{BuildDependency, DEPENDENCY_ID}; + +const SCOPE: &str = "occasion::make::meta"; + +/// The value struct of current storage scope +#[cacheable] +pub struct Meta { + pub make_failed_dependencies: HashSet, + pub make_failed_module: IdentifierSet, + // Ignore entry_dependencies, compile will regenerate it. + // pub entry_dependencies: HashSet, + pub next_dependencies_id: u32, +} + +#[cacheable(as=Meta)] +pub struct MetaRef<'a> { + #[cacheable(with=Inline)] + pub make_failed_dependencies: &'a HashSet, + #[cacheable(with=Inline)] + pub make_failed_module: &'a IdentifierSet, + pub next_dependencies_id: u32, +} + +pub fn save_meta( + make_failed_dependencies: &HashSet, + make_failed_module: &IdentifierSet, + storage: &Arc, +) -> Result<(), SerializeError> { + let meta = MetaRef { + make_failed_dependencies, + make_failed_module, + next_dependencies_id: DEPENDENCY_ID.load(Relaxed), + }; + storage.set(SCOPE, "default".as_bytes().to_vec(), to_bytes(&meta, &())?); + Ok(()) +} + +pub fn recovery_meta( + storage: &Arc, +) -> Result<(HashSet, IdentifierSet), DeserializeError> { + let Some((_, value)) = storage.get_all(SCOPE).pop() else { + return Err(DeserializeError::MessageError("can not get meta data")); + }; + let meta: Meta = from_bytes(&value, &())?; + // TODO make dependency id to string like module id + if DEPENDENCY_ID.load(Relaxed) < meta.next_dependencies_id { + DEPENDENCY_ID.store(meta.next_dependencies_id, Relaxed); + } + Ok((meta.make_failed_dependencies, meta.make_failed_module)) +} diff --git a/crates/rspack_core/src/cache/persistent/occasion/make/mod.rs b/crates/rspack_core/src/cache/persistent/occasion/make/mod.rs new file mode 100644 index 00000000000..55ed6340dd9 --- /dev/null +++ b/crates/rspack_core/src/cache/persistent/occasion/make/mod.rs @@ -0,0 +1,90 @@ +mod dependencies; +mod meta; +mod module_graph; + +use std::sync::Arc; + +use rspack_cacheable::DeserializeError; + +use super::super::{cacheable_context::CacheableContext, Storage}; +use crate::make::MakeArtifact; + +#[derive(Debug)] +pub struct MakeOccasion { + context: Arc, + storage: Arc, +} + +impl MakeOccasion { + pub fn new(storage: Arc, context: Arc) -> Self { + Self { storage, context } + } + + #[tracing::instrument(name = "MakeOccasion::save", skip_all)] + pub fn save(&self, artifact: &MakeArtifact) { + let MakeArtifact { + // write all of field here to avoid forget to update occasion when add new fields + // for dependencies + file_dependencies, + context_dependencies, + missing_dependencies, + build_dependencies, + // for module graph + module_graph_partial, + revoked_modules, + built_modules, + // for meta + make_failed_dependencies, + make_failed_module, + // skip + entry_dependencies: _, + initialized: _, + has_module_graph_change: _, + diagnostics: _, + } = artifact; + dependencies::save_dependencies_info( + file_dependencies, + context_dependencies, + missing_dependencies, + build_dependencies, + &self.storage, + ) + .expect("should save dependencies success"); + + module_graph::save_module_graph( + module_graph_partial, + revoked_modules, + built_modules, + &self.storage, + &self.context, + ); + + meta::save_meta(make_failed_dependencies, make_failed_module, &self.storage) + .expect("should save make meta"); + } + + #[tracing::instrument(name = "MakeOccasion::recovery", skip_all)] + pub fn recovery(&self) -> Result { + let mut artifact = MakeArtifact::default(); + + let (file_dependencies, context_dependencies, missing_dependencies, build_dependencies) = + dependencies::recovery_dependencies_info(&self.storage)?; + artifact.file_dependencies = file_dependencies; + artifact.context_dependencies = context_dependencies; + artifact.missing_dependencies = missing_dependencies; + artifact.build_dependencies = build_dependencies; + + let (make_failed_dependencies, make_failed_module) = meta::recovery_meta(&self.storage)?; + artifact.make_failed_dependencies = make_failed_dependencies; + artifact.make_failed_module = make_failed_module; + + let (partial, make_failed_dependencies) = + module_graph::recovery_module_graph(&self.storage, &self.context)?; + artifact.module_graph_partial = partial; + artifact + .make_failed_dependencies + .extend(make_failed_dependencies); + + Ok(artifact) + } +} diff --git a/crates/rspack_core/src/cache/persistent/occasion/make/module_graph.rs b/crates/rspack_core/src/cache/persistent/occasion/make/module_graph.rs new file mode 100644 index 00000000000..5ac47e01110 --- /dev/null +++ b/crates/rspack_core/src/cache/persistent/occasion/make/module_graph.rs @@ -0,0 +1,185 @@ +use std::sync::Arc; + +use rayon::prelude::*; +use rspack_cacheable::{ + cacheable, from_bytes, to_bytes, + with::{AsOption, AsTuple2, AsVec, Inline}, + DeserializeError, SerializeError, +}; +use rspack_collections::IdentifierSet; +use rustc_hash::FxHashSet as HashSet; + +use super::Storage; +use crate::{ + cache::persistent::cacheable_context::CacheableContext, AsyncDependenciesBlock, + AsyncDependenciesBlockIdentifier, BoxDependency, BoxModule, BuildDependency, DependencyParents, + ExportInfoData, ExportsInfoData, ModuleGraph, ModuleGraphConnection, ModuleGraphModule, + ModuleGraphPartial, +}; + +const SCOPE: &str = "occasion::make::module_graph"; + +/// The value struct of current storage scope +#[cacheable] +struct Node { + pub mgm: ModuleGraphModule, + pub module: BoxModule, + // (dependency, parent_block) + // TODO remove parent block info after connection contains it + pub dependencies: Vec<(BoxDependency, Option)>, + pub connections: Vec, + pub blocks: Vec, +} + +#[cacheable(as=Node)] +struct NodeRef<'a> { + #[cacheable(with=Inline)] + pub mgm: &'a ModuleGraphModule, + #[cacheable(with=Inline)] + pub module: &'a BoxModule, + #[cacheable(with=AsVec>>)] + pub dependencies: Vec<( + &'a BoxDependency, + Option<&'a AsyncDependenciesBlockIdentifier>, + )>, + #[cacheable(with=AsVec)] + pub connections: Vec<&'a ModuleGraphConnection>, + #[cacheable(with=AsVec)] + pub blocks: Vec<&'a AsyncDependenciesBlock>, +} + +pub fn save_module_graph( + partial: &ModuleGraphPartial, + revoked_modules: &IdentifierSet, + built_modules: &IdentifierSet, + storage: &Arc, + context: &CacheableContext, +) { + let mg = ModuleGraph::new(vec![partial], None); + for identifier in revoked_modules { + storage.remove(SCOPE, identifier.as_bytes()); + } + + // save module_graph + let nodes = built_modules + .par_iter() + .filter_map(|identifier| { + let mgm = mg + .module_graph_module_by_identifier(identifier) + .expect("should have mgm"); + let module = mg + .module_by_identifier(identifier) + .expect("should have module"); + let blocks = module + .get_blocks() + .par_iter() + .map(|block_id| mg.block_by_id(block_id).expect("should have block")) + .collect::>(); + let dependencies = mgm + .all_dependencies + .par_iter() + .map(|dep_id| { + ( + mg.dependency_by_id(dep_id).expect("should have dependency"), + mg.get_parent_block(dep_id), + ) + }) + .collect::>(); + let connections = mgm + .outgoing_connections() + .par_iter() + .map(|dep_id| { + mg.connection_by_dependency_id(dep_id) + .expect("should have connection") + }) + .collect::>(); + let node = NodeRef { + mgm, + module, + dependencies, + connections, + blocks, + }; + match to_bytes(&node, context) { + Ok(bytes) => Some((identifier.as_bytes().to_vec(), bytes)), + Err(err) => { + if matches!(err, SerializeError::UnsupportedField) { + tracing::warn!("to bytes failed {:?}", err); + None + } else { + panic!("unexpected module graph serialize failed") + } + } + } + }) + .collect::>(); + + let saved = nodes.len(); + tracing::info!("save info {}/{}", saved, built_modules.len()); + + for (id, bytes) in nodes { + storage.set(SCOPE, id, bytes) + } +} + +pub fn recovery_module_graph( + storage: &Arc, + context: &CacheableContext, +) -> Result<(ModuleGraphPartial, HashSet), DeserializeError> { + let mut need_check_dep = vec![]; + let mut partial = ModuleGraphPartial::default(); + let mut mg = ModuleGraph::new(vec![], Some(&mut partial)); + for (_, v) in storage.get_all(SCOPE) { + let mut node: Node = + from_bytes(&v, context).expect("unexpected module graph deserialize failed"); + for (dep, parent_block) in node.dependencies { + mg.set_parents( + *dep.id(), + DependencyParents { + block: parent_block, + module: node.module.identifier(), + }, + ); + mg.add_dependency(dep); + } + for con in node.connections { + need_check_dep.push((con.dependency_id, *con.module_identifier())); + mg.cache_recovery_connection(con); + } + for block in node.blocks { + mg.add_block(Box::new(block)); + } + // recovery exports/export info + let other_exports_info = ExportInfoData::new(None, None); + let side_effects_only_info = ExportInfoData::new(Some("*side effects only*".into()), None); + let exports_info = ExportsInfoData::new(other_exports_info.id(), side_effects_only_info.id()); + node.mgm.exports = exports_info.id(); + mg.set_exports_info(exports_info.id(), exports_info); + mg.set_export_info(side_effects_only_info.id(), side_effects_only_info); + mg.set_export_info(other_exports_info.id(), other_exports_info); + + mg.add_module_graph_module(node.mgm); + mg.add_module(node.module); + } + // recovery incoming connections + for (con_id, module_identifier) in &need_check_dep { + if let Some(mgm) = mg.module_graph_module_by_identifier_mut(module_identifier) { + mgm.add_incoming_connection(*con_id); + } + } + + tracing::info!("recovery {} module", mg.modules().len()); + let make_failed_dependencies = need_check_dep + .iter() + .filter_map(|(dep_id, module_identifier)| { + let module_exist = mg.module_by_identifier(module_identifier).is_some(); + if module_exist { + None + } else { + mg.revoke_connection(dep_id, false) + } + }) + .collect::>(); + tracing::info!("recovery failed {} deps", make_failed_dependencies.len()); + Ok((partial, make_failed_dependencies)) +} diff --git a/crates/rspack_core/src/cache/persistent/occasion/mod.rs b/crates/rspack_core/src/cache/persistent/occasion/mod.rs new file mode 100644 index 00000000000..60f071f48ed --- /dev/null +++ b/crates/rspack_core/src/cache/persistent/occasion/mod.rs @@ -0,0 +1,3 @@ +mod make; + +pub use make::MakeOccasion; diff --git a/crates/rspack_core/src/cache/persistent/snapshot/mod.rs b/crates/rspack_core/src/cache/persistent/snapshot/mod.rs index fec74767925..c25911970e3 100644 --- a/crates/rspack_core/src/cache/persistent/snapshot/mod.rs +++ b/crates/rspack_core/src/cache/persistent/snapshot/mod.rs @@ -29,6 +29,7 @@ pub struct Snapshot { storage: Arc, } +// TODO remove all of `.expect()` to return error impl Snapshot { pub fn new(options: SnapshotOptions, fs: Arc, storage: Arc) -> Self { Self { diff --git a/crates/rspack_core/src/chunk_group.rs b/crates/rspack_core/src/chunk_group.rs index 65a2406f75b..4c99c27abaf 100644 --- a/crates/rspack_core/src/chunk_group.rs +++ b/crates/rspack_core/src/chunk_group.rs @@ -2,6 +2,7 @@ use std::cmp::Ordering; use std::fmt::{self, Display}; use itertools::Itertools; +use rspack_cacheable::cacheable; use rspack_collections::IdentifierMap; use rspack_collections::{DatabaseItem, UkeySet}; use rspack_error::{error, Result}; @@ -400,6 +401,7 @@ impl ChunkGroupKind { } } +#[cacheable] #[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] pub enum EntryRuntime { String(String), @@ -429,7 +431,7 @@ impl EntryRuntime { } // pub type EntryRuntime = String; - +#[cacheable] #[derive(Debug, Default, Clone, Hash, PartialEq, Eq)] pub struct EntryOptions { pub name: Option, @@ -500,6 +502,7 @@ impl Display for ChunkGroupOrderKey { } } +#[cacheable] #[derive(Debug, Default, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct ChunkGroupOptions { pub name: Option, @@ -528,6 +531,7 @@ impl ChunkGroupOptions { } } +#[cacheable] #[derive(Debug, Clone, Hash, PartialEq, Eq)] pub enum GroupOptions { Entrypoint(Box), diff --git a/crates/rspack_core/src/compiler/compilation.rs b/crates/rspack_core/src/compiler/compilation.rs index 9d594c5cc05..0e8c651b490 100644 --- a/crates/rspack_core/src/compiler/compilation.rs +++ b/crates/rspack_core/src/compiler/compilation.rs @@ -9,6 +9,7 @@ use dashmap::DashSet; use indexmap::{IndexMap, IndexSet}; use itertools::Itertools; use rayon::prelude::*; +use rspack_cacheable::cacheable; use rspack_collections::{ DatabaseItem, Identifiable, IdentifierDashMap, IdentifierMap, IdentifierSet, UkeyMap, UkeySet, }; @@ -119,6 +120,7 @@ pub struct CompilationHooks { pub after_seal: CompilationAfterSealHook, } +#[cacheable] #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd)] pub struct CompilationId(u32); @@ -191,7 +193,6 @@ pub struct Compilation { pub chunk_hashes_results: UkeyMap, // artifact for create_chunk_assets pub chunk_render_results: UkeyMap, - pub code_generated_modules: IdentifierSet, pub build_time_executed_modules: IdentifierSet, pub cache: Arc, @@ -215,7 +216,7 @@ pub struct Compilation { pub modified_files: HashSet, pub removed_files: HashSet, - make_artifact: MakeArtifact, + pub make_artifact: MakeArtifact, pub input_filesystem: Arc, } @@ -1176,7 +1177,6 @@ impl Compilation { // take make diagnostics let diagnostics = self.make_artifact.take_diagnostics(); self.extend_diagnostics(diagnostics); - Ok(()) } diff --git a/crates/rspack_core/src/compiler/make/cutout/has_module_graph_change.rs b/crates/rspack_core/src/compiler/make/cutout/has_module_graph_change.rs index b2eb85cba73..750e3260e19 100644 --- a/crates/rspack_core/src/compiler/make/cutout/has_module_graph_change.rs +++ b/crates/rspack_core/src/compiler/make/cutout/has_module_graph_change.rs @@ -143,6 +143,7 @@ mod t { use std::borrow::Cow; use itertools::Itertools; + use rspack_cacheable::{cacheable, cacheable_dyn, with::Skip}; use rspack_collections::Identifiable; use rspack_error::{impl_empty_diagnosable_trait, Diagnostic, Result}; use rspack_macros::impl_source_map_config; @@ -157,8 +158,10 @@ mod t { ModuleType, RuntimeSpec, SourceType, }; + #[cacheable] #[derive(Debug, Clone)] struct TestDep { + #[cacheable(with=Skip)] ids: Vec<&'static str>, id: DependencyId, } @@ -174,6 +177,7 @@ mod t { impl AsContextDependency for TestDep {} + #[cacheable_dyn] impl Dependency for TestDep { fn dependency_type(&self) -> &crate::DependencyType { &crate::DependencyType::EsmImportSpecifier @@ -196,6 +200,7 @@ mod t { } } + #[cacheable_dyn] impl DependencyTemplate for TestDep { fn apply( &self, @@ -219,14 +224,16 @@ mod t { } } + #[cacheable_dyn] impl ModuleDependency for TestDep { fn request(&self) -> &str { "" } } - #[derive(Debug, Hash, Eq, PartialEq, PartialOrd, Ord)] #[impl_source_map_config] + #[cacheable] + #[derive(Debug, Hash, Eq, PartialEq, PartialOrd, Ord)] struct TestModule { pub(crate) id: ModuleIdentifier, deps: Vec, @@ -272,6 +279,7 @@ mod t { impl_empty_diagnosable_trait!(TestModule); + #[cacheable_dyn] impl Module for TestModule { fn module_type(&self) -> &ModuleType { todo!() diff --git a/crates/rspack_core/src/compiler/make/mod.rs b/crates/rspack_core/src/compiler/make/mod.rs index e85d9e8c5a7..53c7acbef5b 100644 --- a/crates/rspack_core/src/compiler/make/mod.rs +++ b/crates/rspack_core/src/compiler/make/mod.rs @@ -20,12 +20,16 @@ pub struct MakeArtifact { pub has_module_graph_change: bool, pub built_modules: IdentifierSet, pub revoked_modules: IdentifierSet, + // Field to mark whether artifact has been initialized. + // Only Default::default() is false, `update_module_graph` will set this field to true + // Persistent cache will update MakeArtifact when this is false. + pub initialized: bool, // data pub make_failed_dependencies: HashSet, pub make_failed_module: IdentifierSet, pub module_graph_partial: ModuleGraphPartial, - entry_dependencies: HashSet, + pub entry_dependencies: HashSet, pub file_dependencies: FileCounter, pub context_dependencies: FileCounter, pub missing_dependencies: FileCounter, @@ -142,6 +146,7 @@ pub async fn update_module_graph( mut artifact: MakeArtifact, params: Vec, ) -> Result { + artifact.initialized = true; let mut cutout = Cutout::default(); let build_dependencies = cutout.cutout_artifact(&mut artifact, params); artifact = repair(compilation, artifact, build_dependencies).await?; diff --git a/crates/rspack_core/src/compiler/mod.rs b/crates/rspack_core/src/compiler/mod.rs index a4e67e126d8..cfebedc221d 100644 --- a/crates/rspack_core/src/compiler/mod.rs +++ b/crates/rspack_core/src/compiler/mod.rs @@ -1,6 +1,6 @@ mod compilation; mod hmr; -mod make; +pub mod make; mod module_executor; use std::sync::Arc; @@ -206,6 +206,7 @@ impl Compiler { let logger = self.compilation.get_logger("rspack.Compiler"); let make_start = logger.time("make"); let make_hook_start = logger.time("make hook"); + self.cache.before_make(&mut self.compilation.make_artifact); if let Some(e) = self .plugin_driver .compiler_hooks @@ -231,6 +232,7 @@ impl Compiler { let start = logger.time("finish compilation"); self.compilation.finish(self.plugin_driver.clone()).await?; + self.cache.after_make(&self.compilation.make_artifact); logger.time_end(start); let start = logger.time("seal compilation"); self.compilation.seal(self.plugin_driver.clone()).await?; diff --git a/crates/rspack_core/src/concatenated_module.rs b/crates/rspack_core/src/concatenated_module.rs index bde515f202e..e74154686a4 100644 --- a/crates/rspack_core/src/concatenated_module.rs +++ b/crates/rspack_core/src/concatenated_module.rs @@ -11,6 +11,10 @@ use indexmap::IndexMap; use rayon::prelude::*; use regex::Regex; use rspack_ast::javascript::Ast; +use rspack_cacheable::{ + cacheable, cacheable_dyn, + with::{AsMap, Skip}, +}; use rspack_collections::{ Identifiable, Identifier, IdentifierIndexMap, IdentifierIndexSet, IdentifierMap, IdentifierSet, }; @@ -58,6 +62,7 @@ pub struct ConcatenatedModuleHooks { pub exports_definitions: ConcatenatedModuleExportsDefinitionsHook, } +#[cacheable] #[derive(Debug)] pub struct RootModuleContext { pub id: ModuleIdentifier, @@ -107,6 +112,7 @@ pub enum BindingType { Symbol, } +#[cacheable] #[derive(Debug, Clone)] pub struct ConcatenatedInnerModule { pub id: ModuleIdentifier, @@ -211,7 +217,7 @@ pub struct ConnectionWithRuntimeCondition<'a> { #[derive(Debug, Clone)] pub enum ModuleInfo { External(ExternalModuleInfo), - Concatenated(ConcatenatedModuleInfo), + Concatenated(Box), } impl ModuleInfo { @@ -349,6 +355,7 @@ impl ModuleInfo { } #[impl_source_map_config] +#[cacheable] #[derive(Debug)] pub struct ConcatenatedModule { id: ModuleIdentifier, @@ -359,9 +366,9 @@ pub struct ConcatenatedModule { blocks: Vec, dependencies: Vec, - + #[cacheable(with=AsMap)] cached_source_sizes: DashMap>, - + #[cacheable(with=Skip)] diagnostics: Mutex>, build_info: Option, } @@ -462,6 +469,7 @@ impl DependenciesBlock for ConcatenatedModule { } } +#[cacheable_dyn] #[async_trait::async_trait] impl Module for ConcatenatedModule { fn module_type(&self) -> &ModuleType { @@ -1467,7 +1475,7 @@ impl ConcatenatedModule { module: module_id, ..Default::default() }; - vac.insert(ModuleInfo::Concatenated(info)); + vac.insert(ModuleInfo::Concatenated(Box::new(info))); list.push(module_id); } ConcatenationEntry::External(e) => { @@ -1689,7 +1697,7 @@ impl ConcatenatedModule { info: ModuleInfo, runtime: Option<&RuntimeSpec>, ) -> Result { - if let ModuleInfo::Concatenated(info) = info { + if let ModuleInfo::Concatenated(box info) = info { let module_id = info.module; let concatenation_scope = ConcatenationScope::new(module_info_map, info); @@ -1776,7 +1784,7 @@ impl ConcatenatedModule { module_info.internal_source = Some(source); module_info.source = Some(result_source); module_info.chunk_init_fragments = chunk_init_fragments; - Ok(ModuleInfo::Concatenated(module_info)) + Ok(ModuleInfo::Concatenated(Box::new(module_info))) } else { Ok(info) } diff --git a/crates/rspack_core/src/context_module.rs b/crates/rspack_core/src/context_module.rs index 08c69bd772d..8cdb3f60d0f 100644 --- a/crates/rspack_core/src/context_module.rs +++ b/crates/rspack_core/src/context_module.rs @@ -5,6 +5,10 @@ use cow_utils::CowUtils; use derivative::Derivative; use indoc::formatdoc; use itertools::Itertools; +use rspack_cacheable::{ + cacheable, cacheable_dyn, + with::{AsOption, AsPreset, AsVec, Unsupported}, +}; use rspack_collections::{Identifiable, Identifier, IdentifierMap}; use rspack_error::{impl_empty_diagnosable_trait, Diagnostic, Result}; use rspack_macros::impl_source_map_config; @@ -13,8 +17,7 @@ use rspack_regex::RspackRegex; use rspack_sources::{BoxSource, ConcatSource, RawStringSource, SourceExt}; use rspack_util::itoa; use rspack_util::{fx_hash::FxIndexMap, json_stringify, source_map::SourceMapKind}; -use rustc_hash::FxHashMap as HashMap; -use rustc_hash::FxHashSet as HashSet; +use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet}; use swc_core::atoms::Atom; use crate::{ @@ -32,6 +35,7 @@ use crate::{ static WEBPACK_CHUNK_NAME_INDEX_PLACEHOLDER: &str = "[index]"; static WEBPACK_CHUNK_NAME_REQUEST_PLACEHOLDER: &str = "[request]"; +#[cacheable] #[derive(Debug, PartialEq, Eq, Clone, Hash)] pub enum ContextMode { Sync, @@ -89,6 +93,7 @@ pub fn try_convert_str_to_context_mode(s: &str) -> Option { } } +#[cacheable] #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum ContextNameSpaceObject { Bool(bool), @@ -103,12 +108,14 @@ impl ContextNameSpaceObject { } } +#[cacheable] #[derive(Debug, Clone, Copy, Hash, PartialEq, PartialOrd, Ord, Eq)] pub enum ContextTypePrefix { Import, Normal, } +#[cacheable] #[derive(Debug, Clone)] pub struct ContextOptions { pub mode: ContextMode, @@ -124,13 +131,16 @@ pub struct ContextOptions { pub replaces: Vec<(String, u32, u32)>, pub start: u32, pub end: u32, + #[cacheable(with=AsOption>)] pub referenced_exports: Option>, pub attributes: Option, } +#[cacheable] #[derive(Debug, Clone)] pub struct ContextModuleOptions { pub addon: String, + #[cacheable(with=AsPreset)] pub resource: Utf8PathBuf, pub resource_query: String, pub resource_fragment: String, @@ -150,6 +160,7 @@ pub type ResolveContextModuleDependencies = Arc Result> + Send + Sync>; #[impl_source_map_config] +#[cacheable] #[derive(Derivative)] #[derivative(Debug)] pub struct ContextModule { @@ -161,6 +172,7 @@ pub struct ContextModule { build_info: Option, build_meta: Option, #[derivative(Debug = "ignore")] + #[cacheable(with=Unsupported)] resolve_dependencies: ResolveContextModuleDependencies, } @@ -814,6 +826,7 @@ impl DependenciesBlock for ContextModule { } } +#[cacheable_dyn] #[async_trait::async_trait] impl Module for ContextModule { impl_module_meta_info!(); diff --git a/crates/rspack_core/src/dependencies_block.rs b/crates/rspack_core/src/dependencies_block.rs index 2db0fab5473..faf7a44700f 100644 --- a/crates/rspack_core/src/dependencies_block.rs +++ b/crates/rspack_core/src/dependencies_block.rs @@ -1,5 +1,6 @@ use std::{borrow::Cow, hash::Hash}; +use rspack_cacheable::cacheable; use rspack_collections::Identifier; use rspack_error::{ miette::{self, Diagnostic}, @@ -44,6 +45,7 @@ pub fn dependencies_block_update_hash( } } +#[cacheable] #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd)] pub struct AsyncDependenciesBlockIdentifier(Identifier); @@ -65,6 +67,7 @@ impl From for AsyncDependenciesBlockIdentifier { } } +#[cacheable] #[derive(Debug, Clone)] pub struct AsyncDependenciesBlock { id: AsyncDependenciesBlockIdentifier, @@ -72,6 +75,7 @@ pub struct AsyncDependenciesBlock { // Vec> makes sense if T is a large type (see #3530, 1st comment). // #3530: https://github.com/rust-lang/rust-clippy/issues/3530 #[allow(clippy::vec_box)] + #[cacheable(omit_bounds)] blocks: Vec>, block_ids: Vec, dependency_ids: Vec, diff --git a/crates/rspack_core/src/dependency/cached_const_dependency.rs b/crates/rspack_core/src/dependency/cached_const_dependency.rs index 8feddacc74b..854e4456734 100644 --- a/crates/rspack_core/src/dependency/cached_const_dependency.rs +++ b/crates/rspack_core/src/dependency/cached_const_dependency.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_util::ext::DynHash; use crate::{ @@ -5,6 +6,7 @@ use crate::{ InitFragmentStage, NormalInitFragment, RuntimeSpec, TemplateContext, TemplateReplaceSource, }; +#[cacheable] #[derive(Debug, Clone)] pub struct CachedConstDependency { pub start: u32, @@ -24,6 +26,7 @@ impl CachedConstDependency { } } +#[cacheable_dyn] impl DependencyTemplate for CachedConstDependency { fn apply( &self, diff --git a/crates/rspack_core/src/dependency/const_dependency.rs b/crates/rspack_core/src/dependency/const_dependency.rs index 2141daf88a2..cdaf95ffd65 100644 --- a/crates/rspack_core/src/dependency/const_dependency.rs +++ b/crates/rspack_core/src/dependency/const_dependency.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn, with::AsRefStr}; use rspack_util::ext::DynHash; use crate::{ @@ -5,10 +6,12 @@ use crate::{ TemplateReplaceSource, }; +#[cacheable] #[derive(Debug, Clone)] pub struct ConstDependency { pub start: u32, pub end: u32, + #[cacheable(with=AsRefStr)] pub content: Box, pub runtime_requirements: Option, } @@ -29,6 +32,7 @@ impl ConstDependency { } } +#[cacheable_dyn] impl DependencyTemplate for ConstDependency { fn apply( &self, diff --git a/crates/rspack_core/src/dependency/context_element_dependency.rs b/crates/rspack_core/src/dependency/context_element_dependency.rs index 082f7357e8a..fdcc42c3e0e 100644 --- a/crates/rspack_core/src/dependency/context_element_dependency.rs +++ b/crates/rspack_core/src/dependency/context_element_dependency.rs @@ -1,4 +1,8 @@ use itertools::Itertools; +use rspack_cacheable::{ + cacheable, cacheable_dyn, + with::{AsOption, AsPreset, AsVec}, +}; use rspack_paths::Utf8Path; use rspack_util::json_stringify; use swc_core::ecma::atoms::Atom; @@ -13,6 +17,7 @@ use crate::{DependencyCategory, DependencyId, DependencyType}; use crate::{ExtendedReferencedExport, ModuleDependency}; use crate::{ModuleGraph, ReferencedExport, RuntimeSpec}; +#[cacheable] #[derive(Debug, Clone)] pub struct ContextElementDependency { pub id: DependencyId, @@ -24,6 +29,7 @@ pub struct ContextElementDependency { pub context: Context, pub layer: Option, pub resource_identifier: String, + #[cacheable(with=AsOption>)] pub referenced_exports: Option>, pub dependency_type: DependencyType, pub attributes: Option, @@ -43,6 +49,7 @@ impl ContextElementDependency { } } +#[cacheable_dyn] impl Dependency for ContextElementDependency { fn id(&self) -> &DependencyId { &self.id @@ -94,6 +101,7 @@ impl Dependency for ContextElementDependency { } } +#[cacheable_dyn] impl ModuleDependency for ContextElementDependency { fn request(&self) -> &str { &self.request diff --git a/crates/rspack_core/src/dependency/dependency_category.rs b/crates/rspack_core/src/dependency/dependency_category.rs index 5f0de90bcda..dbe577b8c1e 100644 --- a/crates/rspack_core/src/dependency/dependency_category.rs +++ b/crates/rspack_core/src/dependency/dependency_category.rs @@ -1,6 +1,7 @@ use std::fmt::{Debug, Display}; use std::hash::Hash; +#[rspack_cacheable::cacheable] #[derive(Default, Clone, Copy, PartialEq, Eq, Hash, Debug)] pub enum DependencyCategory { #[default] diff --git a/crates/rspack_core/src/dependency/dependency_id.rs b/crates/rspack_core/src/dependency/dependency_id.rs index 3663eb80902..b1ff2f88edb 100644 --- a/crates/rspack_core/src/dependency/dependency_id.rs +++ b/crates/rspack_core/src/dependency/dependency_id.rs @@ -1,11 +1,13 @@ use std::sync::atomic::AtomicU32; use std::sync::atomic::Ordering::Relaxed; +use rspack_cacheable::cacheable; use serde::Serialize; use swc_core::ecma::atoms::Atom; use crate::ModuleGraph; +#[cacheable(hashable)] #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize)] pub struct DependencyId(u32); diff --git a/crates/rspack_core/src/dependency/dependency_location.rs b/crates/rspack_core/src/dependency/dependency_location.rs index 46364906394..e6bf00a1a8e 100644 --- a/crates/rspack_core/src/dependency/dependency_location.rs +++ b/crates/rspack_core/src/dependency/dependency_location.rs @@ -4,9 +4,11 @@ use std::{ }; use derivative::Derivative; +use rspack_cacheable::cacheable; /// Represents a range in a dependency, typically used for tracking the span of code in a source file. /// It stores the start and end positions (as offsets) of the range, typically using base-0 indexing. +#[cacheable] #[derive(Derivative)] #[derivative(Debug, Clone, Hash)] pub struct DependencyRange { @@ -63,6 +65,7 @@ impl DependencyRange { /// Represents the real location of a dependency in a source file, including both start and optional end positions. /// These positions are described in terms of lines and columns in the source code. +#[cacheable] #[derive(Debug, Clone)] pub struct RealDependencyLocation { start: SourcePosition, @@ -98,6 +101,7 @@ impl fmt::Display for RealDependencyLocation { } /// Represents a synthetic dependency location, such as a generated dependency. +#[cacheable] #[derive(Debug, Clone)] pub struct SyntheticDependencyLocation { pub name: String, @@ -117,6 +121,7 @@ impl fmt::Display for SyntheticDependencyLocation { } } +#[cacheable] #[derive(Debug, Clone)] pub enum DependencyLocation { Real(RealDependencyLocation), @@ -134,6 +139,7 @@ impl fmt::Display for DependencyLocation { } /// Represents a position in the source file, including the line number and column number. +#[cacheable] #[derive(Debug, Clone, Copy)] pub struct SourcePosition { line: usize, diff --git a/crates/rspack_core/src/dependency/dependency_template.rs b/crates/rspack_core/src/dependency/dependency_template.rs index ec638bedbb0..69c818358a0 100644 --- a/crates/rspack_core/src/dependency/dependency_template.rs +++ b/crates/rspack_core/src/dependency/dependency_template.rs @@ -1,6 +1,7 @@ use std::fmt::Debug; use dyn_clone::{clone_trait_object, DynClone}; +use rspack_cacheable::cacheable_dyn; use rspack_sources::{BoxSource, ReplaceSource}; use rspack_util::ext::AsAny; @@ -42,6 +43,7 @@ pub type TemplateReplaceSource = ReplaceSource; clone_trait_object!(DependencyTemplate); // Align with https://github.com/webpack/webpack/blob/671ac29d462e75a10c3fdfc785a4c153e41e749e/lib/DependencyTemplate.js +#[cacheable_dyn] pub trait DependencyTemplate: Debug + DynClone + Sync + Send + AsDependency + AsAny { fn apply( &self, diff --git a/crates/rspack_core/src/dependency/dependency_trait.rs b/crates/rspack_core/src/dependency/dependency_trait.rs index ed3d067ad67..e5a48f496bf 100644 --- a/crates/rspack_core/src/dependency/dependency_trait.rs +++ b/crates/rspack_core/src/dependency/dependency_trait.rs @@ -1,6 +1,7 @@ use std::{any::Any, fmt::Debug}; use dyn_clone::{clone_trait_object, DynClone}; +use rspack_cacheable::cacheable_dyn; use rspack_collections::IdentifierSet; use rspack_error::Diagnostic; use rspack_util::ext::AsAny; @@ -27,6 +28,7 @@ pub enum AffectType { Transitive, } +#[cacheable_dyn] pub trait Dependency: AsDependencyTemplate + AsContextDependency diff --git a/crates/rspack_core/src/dependency/dependency_type.rs b/crates/rspack_core/src/dependency/dependency_type.rs index 7787f12de8d..9d89f93ec3b 100644 --- a/crates/rspack_core/src/dependency/dependency_type.rs +++ b/crates/rspack_core/src/dependency/dependency_type.rs @@ -1,9 +1,12 @@ use std::fmt::{Debug, Display}; +use rspack_macros::cacheable; + use crate::ContextTypePrefix; // Used to describe dependencies' types, see webpack's `type` getter in `Dependency` // Note: This is almost the same with the old `ResolveKind` +#[cacheable] #[derive(Default, Clone, Copy, PartialEq, Eq, Hash, Debug)] pub enum DependencyType { #[default] @@ -111,7 +114,7 @@ pub enum DependencyType { ModuleDecorator, DllEntry, DelegatedSource, - Custom(&'static str), + ExtractCSS, } impl DependencyType { @@ -166,7 +169,6 @@ impl DependencyType { DependencyType::WasmExportImported => "wasm export imported", DependencyType::StaticExports => "static exports", DependencyType::LoaderImport => "loader import", - DependencyType::Custom(ty) => ty, DependencyType::ExportInfoApi => "export info api", // TODO: mode DependencyType::ImportMetaContext => "import.meta context", @@ -184,6 +186,7 @@ impl DependencyType { DependencyType::LazyImport => "lazy import()", DependencyType::ModuleDecorator => "module decorator", DependencyType::DelegatedSource => "delegated source", + DependencyType::ExtractCSS => "extract css", } } } diff --git a/crates/rspack_core/src/dependency/entry.rs b/crates/rspack_core/src/dependency/entry.rs index 800b60616bb..b70f6eb8cbc 100644 --- a/crates/rspack_core/src/dependency/entry.rs +++ b/crates/rspack_core/src/dependency/entry.rs @@ -1,9 +1,12 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; + use super::AffectType; use crate::{ AsContextDependency, AsDependencyTemplate, Context, Dependency, DependencyCategory, DependencyId, DependencyType, ModuleDependency, ModuleLayer, }; +#[cacheable] #[derive(Debug, Hash, PartialEq, Eq, Clone)] pub struct EntryDependency { id: DependencyId, @@ -34,6 +37,7 @@ impl EntryDependency { } } +#[cacheable_dyn] impl Dependency for EntryDependency { fn id(&self) -> &DependencyId { &self.id @@ -60,6 +64,7 @@ impl Dependency for EntryDependency { } } +#[cacheable_dyn] impl ModuleDependency for EntryDependency { fn request(&self) -> &str { &self.request diff --git a/crates/rspack_core/src/dependency/loader_import.rs b/crates/rspack_core/src/dependency/loader_import.rs index 7ae2a64e81f..3482f95938f 100644 --- a/crates/rspack_core/src/dependency/loader_import.rs +++ b/crates/rspack_core/src/dependency/loader_import.rs @@ -1,9 +1,12 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; + use super::AffectType; use crate::{ AsContextDependency, AsDependencyTemplate, Context, Dependency, DependencyCategory, DependencyId, DependencyType, ModuleDependency, }; +#[cacheable] #[derive(Debug, Hash, PartialEq, Eq, Clone)] pub struct LoaderImportDependency { id: DependencyId, @@ -24,6 +27,7 @@ impl LoaderImportDependency { impl AsDependencyTemplate for LoaderImportDependency {} impl AsContextDependency for LoaderImportDependency {} +#[cacheable_dyn] impl Dependency for LoaderImportDependency { fn id(&self) -> &DependencyId { &self.id @@ -46,6 +50,7 @@ impl Dependency for LoaderImportDependency { } } +#[cacheable_dyn] impl ModuleDependency for LoaderImportDependency { fn request(&self) -> &str { &self.request diff --git a/crates/rspack_core/src/dependency/mod.rs b/crates/rspack_core/src/dependency/mod.rs index b855c197912..c5ddbb1af05 100644 --- a/crates/rspack_core/src/dependency/mod.rs +++ b/crates/rspack_core/src/dependency/mod.rs @@ -161,6 +161,7 @@ impl std::fmt::Debug for DependencyCondition { } } +#[rspack_cacheable::cacheable] #[derive(Debug, Clone, Serialize)] pub struct ImportAttributes(FxHashMap); diff --git a/crates/rspack_core/src/dependency/module_dependency.rs b/crates/rspack_core/src/dependency/module_dependency.rs index ce5c49003d4..e5c60fe8259 100644 --- a/crates/rspack_core/src/dependency/module_dependency.rs +++ b/crates/rspack_core/src/dependency/module_dependency.rs @@ -1,8 +1,10 @@ use dyn_clone::clone_trait_object; +use rspack_cacheable::cacheable_dyn; use super::Dependency; use crate::{DependencyCondition, ErrorSpan}; +#[cacheable_dyn] pub trait ModuleDependency: Dependency { fn request(&self) -> &str; diff --git a/crates/rspack_core/src/dependency/runtime_requirements_dependency.rs b/crates/rspack_core/src/dependency/runtime_requirements_dependency.rs index 70864325aef..4ce04097428 100644 --- a/crates/rspack_core/src/dependency/runtime_requirements_dependency.rs +++ b/crates/rspack_core/src/dependency/runtime_requirements_dependency.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_util::ext::DynHash; use crate::{ @@ -5,11 +6,13 @@ use crate::{ TemplateReplaceSource, }; +#[cacheable] #[derive(Debug, Eq, PartialEq, Clone, Hash)] pub struct RuntimeRequirementsDependency { pub runtime_requirements: RuntimeGlobals, } +#[cacheable_dyn] impl DependencyTemplate for RuntimeRequirementsDependency { fn apply( &self, diff --git a/crates/rspack_core/src/dependency/static_exports_dependency.rs b/crates/rspack_core/src/dependency/static_exports_dependency.rs index 38074af7ff9..a95bed9c889 100644 --- a/crates/rspack_core/src/dependency/static_exports_dependency.rs +++ b/crates/rspack_core/src/dependency/static_exports_dependency.rs @@ -1,3 +1,7 @@ +use rspack_cacheable::{ + cacheable, cacheable_dyn, + with::{AsPreset, AsVec}, +}; use swc_core::ecma::atoms::Atom; use super::AffectType; @@ -6,12 +10,14 @@ use crate::{ DependencyType, ExportNameOrSpec, ExportsOfExportsSpec, ExportsSpec, ModuleGraph, }; +#[cacheable] #[derive(Debug, Clone)] pub enum StaticExportsSpec { True, - Array(Vec), + Array(#[cacheable(with=AsVec)] Vec), } +#[cacheable] #[derive(Debug, Clone)] pub struct StaticExportsDependency { id: DependencyId, @@ -29,6 +35,7 @@ impl StaticExportsDependency { } } +#[cacheable_dyn] impl Dependency for StaticExportsDependency { fn id(&self) -> &DependencyId { &self.id diff --git a/crates/rspack_core/src/exports_info.rs b/crates/rspack_core/src/exports_info.rs index e2049c9705c..b051832bd2c 100644 --- a/crates/rspack_core/src/exports_info.rs +++ b/crates/rspack_core/src/exports_info.rs @@ -9,6 +9,10 @@ use std::sync::Arc; use either::Either; use itertools::Itertools; +use rspack_cacheable::{ + cacheable, + with::{AsPreset, AsVec}, +}; use rspack_collections::impl_item_ukey; use rspack_collections::Ukey; use rspack_collections::UkeySet; @@ -25,6 +29,7 @@ use crate::{ ModuleIdentifier, Nullable, RuntimeSpec, }; +#[cacheable] #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize)] pub struct ExportsInfo(Ukey); @@ -1958,9 +1963,10 @@ pub enum RuntimeUsageStateType { Used, } +#[cacheable] #[derive(Debug, Clone, PartialEq, Eq)] pub enum UsedByExports { - Set(HashSet), + Set(#[cacheable(with=AsVec)] HashSet), Bool(bool), } diff --git a/crates/rspack_core/src/external_module.rs b/crates/rspack_core/src/external_module.rs index 28e0757b761..e3a9e02add5 100644 --- a/crates/rspack_core/src/external_module.rs +++ b/crates/rspack_core/src/external_module.rs @@ -1,5 +1,6 @@ use std::{borrow::Cow, iter}; +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_collections::{Identifiable, Identifier}; use rspack_error::{error, impl_empty_diagnosable_trait, Diagnostic, Result}; use rspack_macros::impl_source_map_config; @@ -22,6 +23,7 @@ use crate::{ChunkGraph, ModuleGraph}; static EXTERNAL_MODULE_JS_SOURCE_TYPES: &[SourceType] = &[SourceType::JavaScript]; static EXTERNAL_MODULE_CSS_SOURCE_TYPES: &[SourceType] = &[SourceType::CssImport]; +#[cacheable] #[derive(Debug, Clone, Serialize)] #[serde(untagged)] pub enum ExternalRequest { @@ -29,6 +31,7 @@ pub enum ExternalRequest { Map(HashMap), } +#[cacheable] #[derive(Debug, Clone)] pub struct ExternalRequestValue { pub primary: String, @@ -164,6 +167,7 @@ fn resolve_external_type<'a>( } #[impl_source_map_config] +#[cacheable] #[derive(Debug)] pub struct ExternalModule { dependencies: Vec, @@ -179,6 +183,7 @@ pub struct ExternalModule { dependency_meta: DependencyMeta, } +#[cacheable] #[derive(Debug)] pub enum ExternalTypeEnum { Import, @@ -187,6 +192,7 @@ pub enum ExternalTypeEnum { pub type MetaExternalType = Option; +#[cacheable] #[derive(Debug)] pub struct DependencyMeta { pub external_type: MetaExternalType, @@ -444,6 +450,7 @@ impl DependenciesBlock for ExternalModule { } } +#[cacheable_dyn] #[async_trait::async_trait] impl Module for ExternalModule { impl_module_meta_info!(); diff --git a/crates/rspack_core/src/lib.rs b/crates/rspack_core/src/lib.rs index 76b0e982a62..a2331ed58b3 100644 --- a/crates/rspack_core/src/lib.rs +++ b/crates/rspack_core/src/lib.rs @@ -98,6 +98,7 @@ pub use resolver::*; pub mod concatenated_module; pub mod reserved_names; +use rspack_cacheable::{cacheable, with::AsPreset}; pub use rspack_loader_runner::{ get_scheme, parse_resource, AdditionalData, ResourceData, ResourceParsedData, Scheme, BUILTIN_LOADER_PREFIX, @@ -108,6 +109,7 @@ pub use rspack_sources; #[cfg(debug_assertions)] pub mod debug_info; +#[cacheable] #[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum SourceType { JavaScript, @@ -118,7 +120,7 @@ pub enum SourceType { Remote, ShareInit, ConsumeShared, - Custom(Ustr), + Custom(#[cacheable(with=AsPreset)] Ustr), #[default] Unknown, CssImport, @@ -162,6 +164,7 @@ impl From<&str> for SourceType { } } +#[cacheable] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum ModuleType { Json, @@ -183,7 +186,7 @@ pub enum ModuleType { ProvideShared, ConsumeShared, SelfReference, - Custom(Ustr), + Custom(#[cacheable(with=AsPreset)] Ustr), } impl ModuleType { diff --git a/crates/rspack_core/src/module.rs b/crates/rspack_core/src/module.rs index 4833bec9f20..df3d2ee7b5f 100644 --- a/crates/rspack_core/src/module.rs +++ b/crates/rspack_core/src/module.rs @@ -5,6 +5,11 @@ use std::{any::Any, borrow::Cow, fmt::Debug}; use async_trait::async_trait; use json::JsonValue; +use rspack_cacheable::with::AsPreset; +use rspack_cacheable::{ + cacheable, cacheable_dyn, + with::{AsOption, AsVec}, +}; use rspack_collections::{Identifiable, Identifier, IdentifierSet}; use rspack_error::{Diagnosable, Diagnostic, Result}; use rspack_fs::FileSystem; @@ -42,6 +47,7 @@ pub enum BuildExtraDataType { JavaScriptParserAndGenerator, } +#[cacheable] #[derive(Debug, Clone)] pub struct BuildInfo { /// Whether the result is cacheable, i.e shared between builds. @@ -52,10 +58,13 @@ pub struct BuildInfo { pub context_dependencies: HashSet, pub missing_dependencies: HashSet, pub build_dependencies: HashSet, + #[cacheable(with=AsVec)] pub esm_named_exports: HashSet, pub all_star_exports: Vec, pub need_create_require: bool, + #[cacheable(with=AsOption)] pub json_data: Option, + #[cacheable(with=AsOption>)] pub top_level_declarations: Option>, pub module_concatenation_bailout: Option, } @@ -80,6 +89,7 @@ impl Default for BuildInfo { } } +#[cacheable] #[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq, Serialize)] #[serde(rename_all = "camelCase")] pub enum BuildMetaExportsType { @@ -99,6 +109,7 @@ pub enum ExportsType { Dynamic, } +#[cacheable] #[derive(Debug, Default, Clone, Copy, Hash, Serialize)] #[serde(rename_all = "camelCase")] pub enum BuildMetaDefaultObject { @@ -114,6 +125,7 @@ pub enum BuildMetaDefaultObject { }, } +#[cacheable] #[derive(Debug, Default, Clone, Copy, Hash, Serialize)] #[serde(rename_all = "camelCase")] pub enum ModuleArgument { @@ -131,6 +143,7 @@ impl Display for ModuleArgument { } } +#[cacheable] #[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq, Serialize)] #[serde(rename_all = "camelCase")] pub enum ExportsArgument { @@ -148,6 +161,7 @@ impl Display for ExportsArgument { } } +#[cacheable] #[derive(Debug, Default, Clone, Hash, Serialize)] #[serde(rename_all = "camelCase")] pub struct BuildMeta { @@ -175,12 +189,15 @@ pub struct BuildResult { pub optimization_bailouts: Vec, } +#[cacheable] #[derive(Debug, Default, Clone)] pub struct FactoryMeta { pub side_effect_free: Option, } pub type ModuleIdentifier = Identifier; + +#[cacheable_dyn] #[async_trait] pub trait Module: Debug @@ -615,6 +632,7 @@ pub struct LibIdentOptions<'me> { mod test { use std::borrow::Cow; + use rspack_cacheable::cacheable; use rspack_collections::{Identifiable, Identifier}; use rspack_error::{Diagnosable, Diagnostic, Result}; use rspack_sources::Source; @@ -627,17 +645,19 @@ mod test { RuntimeSpec, SourceType, }; + #[cacheable] #[derive(Debug)] - struct RawModule(&'static str); + struct RawModule(String); + #[cacheable] #[derive(Debug)] - struct ExternalModule(&'static str); + struct ExternalModule(String); macro_rules! impl_noop_trait_module_type { ($ident: ident) => { impl Identifiable for $ident { fn identifier(&self) -> Identifier { - (stringify!($ident).to_owned() + self.0).into() + self.0.clone().into() } } @@ -665,6 +685,7 @@ mod test { } } + #[::rspack_cacheable::cacheable_dyn] #[::async_trait::async_trait] impl Module for $ident { fn module_type(&self) -> &ModuleType { @@ -688,7 +709,7 @@ mod test { } fn readable_identifier(&self, _context: &Context) -> Cow { - (stringify!($ident).to_owned() + self.0).into() + self.0.clone().into() } async fn build( @@ -762,8 +783,8 @@ mod test { #[test] fn should_downcast_successfully() { - let a: Box = ExternalModule("a").boxed(); - let b: Box = RawModule("a").boxed(); + let a: Box = ExternalModule(String::from("a")).boxed(); + let b: Box = RawModule(String::from("a")).boxed(); assert!(a.downcast_ref::().is_some()); assert!(b.downcast_ref::().is_some()); diff --git a/crates/rspack_core/src/module_factory.rs b/crates/rspack_core/src/module_factory.rs index 376df7f71a3..862da8f848e 100644 --- a/crates/rspack_core/src/module_factory.rs +++ b/crates/rspack_core/src/module_factory.rs @@ -1,18 +1,24 @@ use std::{fmt::Debug, sync::Arc}; +use rspack_cacheable::{ + cacheable, + with::{As, Skip}, +}; use rspack_error::{Diagnostic, Result}; use rspack_paths::ArcPath; use rustc_hash::FxHashSet as HashSet; use crate::{ - BoxDependency, BoxModule, CompilationId, CompilerOptions, Context, ModuleIdentifier, ModuleLayer, - Resolve, ResolverFactory, + cache::persistent::FromContext, BoxDependency, BoxModule, CompilationId, CompilerOptions, + Context, ModuleIdentifier, ModuleLayer, Resolve, ResolverFactory, }; +#[cacheable] #[derive(Debug, Clone)] pub struct ModuleFactoryCreateData { pub compilation_id: CompilationId, pub resolve_options: Option>, + #[cacheable(with=As)] pub options: Arc, pub context: Context, pub dependencies: Vec, @@ -24,6 +30,7 @@ pub struct ModuleFactoryCreateData { pub file_dependencies: HashSet, pub context_dependencies: HashSet, pub missing_dependencies: HashSet, + #[cacheable(with=Skip)] pub diagnostics: Vec, } diff --git a/crates/rspack_core/src/module_graph/connection.rs b/crates/rspack_core/src/module_graph/connection.rs index c86fca4c3a2..a12b5756cae 100644 --- a/crates/rspack_core/src/module_graph/connection.rs +++ b/crates/rspack_core/src/module_graph/connection.rs @@ -1,7 +1,10 @@ use std::hash::Hash; +use rspack_cacheable::cacheable; + use crate::{DependencyId, ModuleGraph, ModuleIdentifier, RuntimeSpec}; +#[cacheable] #[derive(Debug, Clone, Eq)] pub struct ModuleGraphConnection { pub dependency_id: DependencyId, @@ -93,6 +96,7 @@ impl ModuleGraphConnection { } } +#[cacheable] #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum ConnectionState { Bool(bool), diff --git a/crates/rspack_core/src/module_graph/mod.rs b/crates/rspack_core/src/module_graph/mod.rs index 13b7133acba..0ee3e6cf8e0 100644 --- a/crates/rspack_core/src/module_graph/mod.rs +++ b/crates/rspack_core/src/module_graph/mod.rs @@ -1089,4 +1089,26 @@ impl<'a> ModuleGraph<'a> { } }) } + + // todo remove it after module_graph_partial remove all of dependency_id_to_* + pub fn cache_recovery_connection(&mut self, connection: ModuleGraphConnection) { + let condition = self + .dependency_by_id(&connection.dependency_id) + .and_then(|d| d.as_module_dependency()) + .and_then(|dep| dep.get_condition()); + let Some(active_partial) = &mut self.active else { + panic!("should have active partial"); + }; + + // recovery condition + if let Some(condition) = condition { + active_partial + .connection_to_condition + .insert(connection.dependency_id, condition); + } + + active_partial + .connections + .insert(connection.dependency_id, Some(connection)); + } } diff --git a/crates/rspack_core/src/module_graph/module.rs b/crates/rspack_core/src/module_graph/module.rs index 43bf46b0784..bb6fee5c845 100644 --- a/crates/rspack_core/src/module_graph/module.rs +++ b/crates/rspack_core/src/module_graph/module.rs @@ -1,12 +1,17 @@ +use rspack_cacheable::{cacheable, with::Skip}; use rustc_hash::FxHashSet as HashSet; use crate::ExportsInfo; use crate::{DependencyId, ModuleIdentifier, ModuleIssuer, ModuleProfile}; +#[cacheable] #[derive(Debug, Clone)] pub struct ModuleGraphModule { // edges from module to module outgoing_connections: HashSet, + // incoming connections will regenerate by persistent cache recovery. + // we can cache it after all of module are cacheable + #[cacheable(with=Skip)] incoming_connections: HashSet, issuer: ModuleIssuer, @@ -19,6 +24,7 @@ pub struct ModuleGraphModule { pub(crate) pre_order_index: Option, pub post_order_index: Option, pub exports: ExportsInfo, + #[cacheable(with=Skip)] pub profile: Option>, pub depth: Option, pub optimization_bailout: Vec, diff --git a/crates/rspack_core/src/normal_module.rs b/crates/rspack_core/src/normal_module.rs index b5c5731c913..6f9f76734a4 100644 --- a/crates/rspack_core/src/normal_module.rs +++ b/crates/rspack_core/src/normal_module.rs @@ -11,6 +11,10 @@ use std::{ use dashmap::DashMap; use derivative::Derivative; +use rspack_cacheable::{ + cacheable, cacheable_dyn, + with::{AsMap, AsOption, AsPreset, Skip}, +}; use rspack_collections::{Identifiable, IdentifierSet}; use rspack_error::{error, Diagnosable, Diagnostic, DiagnosticExt, NodeError, Result, Severity}; use rspack_hash::{RspackHash, RspackHashDigest}; @@ -41,6 +45,7 @@ use crate::{ SourceType, }; +#[cacheable] #[derive(Debug, Clone)] pub enum ModuleIssuer { Unset, @@ -92,6 +97,7 @@ pub struct NormalModuleHooks { } #[impl_source_map_config] +#[cacheable] #[derive(Derivative)] #[derivative(Debug)] pub struct NormalModule { @@ -122,6 +128,7 @@ pub struct NormalModule { loaders: Vec, /// Original content of this module, will be available after module build + #[cacheable(with=AsOption)] original_source: Option, /// Built source of this module (passed with loaders) source: NormalModuleSource, @@ -135,7 +142,9 @@ pub struct NormalModule { #[allow(unused)] debug_id: usize, + #[cacheable(with=AsMap)] cached_source_sizes: DashMap>, + #[cacheable(with=Skip)] diagnostics: Mutex>, code_generation_dependencies: Option>>, @@ -148,10 +157,11 @@ pub struct NormalModule { last_successful_build_meta: BuildMeta, } +#[cacheable] #[derive(Debug, Clone)] pub enum NormalModuleSource { Unbuild, - BuiltSucceed(BoxSource), + BuiltSucceed(#[cacheable(with=AsPreset)] BoxSource), BuiltFailed(Diagnostic), } @@ -355,6 +365,7 @@ impl DependenciesBlock for NormalModule { } } +#[cacheable_dyn] #[async_trait::async_trait] impl Module for NormalModule { impl_module_meta_info!(); diff --git a/crates/rspack_core/src/normal_module_factory.rs b/crates/rspack_core/src/normal_module_factory.rs index 015449eeeff..60ff0f7e479 100644 --- a/crates/rspack_core/src/normal_module_factory.rs +++ b/crates/rspack_core/src/normal_module_factory.rs @@ -2,6 +2,7 @@ use std::sync::LazyLock; use std::{borrow::Cow, sync::Arc}; use regex::Regex; +use rspack_cacheable::cacheable; use rspack_error::{error, Result}; use rspack_hook::define_hook; use rspack_loader_runner::{get_scheme, Loader, Scheme}; @@ -845,6 +846,7 @@ impl NormalModuleFactory { /// `u32` is 4 bytes on 64bit machine, comparing to `usize` which is 8 bytes. /// ## Warning /// [ErrorSpan] start from zero, and `Span` of `swc` start from one. see https://swc-css.netlify.app/?code=eJzLzC3ILypRSFRIK8rPVVAvSS0u0csqVgcAZaoIKg +#[cacheable] #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Default, PartialOrd, Ord)] pub struct ErrorSpan { pub start: u32, diff --git a/crates/rspack_core/src/options/context.rs b/crates/rspack_core/src/options/context.rs index 09f27be7b4b..11a30b2d34b 100644 --- a/crates/rspack_core/src/options/context.rs +++ b/crates/rspack_core/src/options/context.rs @@ -1,13 +1,16 @@ use std::{fmt, ops::Deref, path::Path}; +use rspack_cacheable::{cacheable, with::AsPreset}; use rspack_loader_runner::ResourceData; use rspack_paths::{Utf8Path, Utf8PathBuf}; use rspack_util::atom::Atom; use crate::{contextify, parse_resource}; +#[cacheable] #[derive(Debug, Clone, Default, PartialEq, Eq, Hash)] pub struct Context { + #[cacheable(with=AsPreset)] inner: Atom, } diff --git a/crates/rspack_core/src/options/filename.rs b/crates/rspack_core/src/options/filename.rs index 3568f1cee93..1e8dee66161 100644 --- a/crates/rspack_core/src/options/filename.rs +++ b/crates/rspack_core/src/options/filename.rs @@ -7,6 +7,10 @@ use std::sync::LazyLock; use std::{borrow::Cow, convert::Infallible, ptr}; use regex::Regex; +use rspack_cacheable::{ + cacheable, + with::{AsPreset, Unsupported}, +}; use rspack_error::error; use rspack_macros::MergeFrom; use rspack_util::atom::Atom; @@ -35,10 +39,11 @@ pub static CONTENT_HASH_PLACEHOLDER: &str = "[contenthash]"; static DATA_URI_REGEX: LazyLock = LazyLock::new(|| Regex::new(r"^data:([^;,]+)").expect("Invalid regex")); +#[cacheable] #[derive(PartialEq, Debug, Hash, Eq, Clone, PartialOrd, Ord, MergeFrom)] enum FilenameKind { - Template(Atom), - Fn(F), + Template(#[cacheable(with=AsPreset)] Atom), + Fn(#[cacheable(with=Unsupported)] F), } /// Filename template string or function @@ -47,6 +52,7 @@ enum FilenameKind { /// implements `Hash` and `Eq`, and its error type is `rspack_error::Error`. /// /// Other possible function types are `NoFilenameFn` and `LocalJsFilenameFn` +#[cacheable] #[derive(PartialEq, Debug, Hash, Eq, Clone, PartialOrd, Ord)] pub struct Filename>(FilenameKind); diff --git a/crates/rspack_core/src/options/module.rs b/crates/rspack_core/src/options/module.rs index 4746e77ff0b..a8b1e70bf98 100644 --- a/crates/rspack_core/src/options/module.rs +++ b/crates/rspack_core/src/options/module.rs @@ -7,6 +7,7 @@ use async_recursion::async_recursion; use bitflags::bitflags; use derivative::Derivative; use futures::future::BoxFuture; +use rspack_cacheable::{cacheable, with::Unsupported}; use rspack_error::Result; use rspack_macros::MergeFrom; use rspack_regex::RspackRegex; @@ -30,6 +31,7 @@ impl ParserOptionsMap { } } +#[cacheable] #[derive(Debug, Clone, MergeFrom)] pub enum ParserOptions { Asset(AssetParserOptions), @@ -69,6 +71,7 @@ impl ParserOptions { ); } +#[cacheable] #[derive(Debug, Clone, Copy, MergeFrom)] pub enum DynamicImportMode { Lazy, @@ -92,6 +95,7 @@ impl From<&str> for DynamicImportMode { } } +#[cacheable] #[derive(Debug, Clone, Copy, MergeFrom, PartialEq, Eq, Hash, PartialOrd, Ord)] pub enum DynamicImportFetchPriority { Low, @@ -120,6 +124,7 @@ impl fmt::Display for DynamicImportFetchPriority { } } +#[cacheable] #[derive(Debug, Clone, Copy, MergeFrom)] pub enum JavascriptParserUrl { Enable, @@ -137,6 +142,7 @@ impl From<&str> for JavascriptParserUrl { } } +#[cacheable] #[derive(Debug, Clone, Copy, MergeFrom)] pub enum JavascriptParserOrder { Disable, @@ -168,6 +174,7 @@ impl From<&str> for JavascriptParserOrder { } } +#[cacheable] #[derive(Debug, Clone, Copy, MergeFrom)] pub enum ExportPresenceMode { None, @@ -203,6 +210,7 @@ impl ExportPresenceMode { } } +#[cacheable] #[derive(Debug, Clone, Copy, MergeFrom)] pub enum OverrideStrict { Strict, @@ -219,6 +227,7 @@ impl From<&str> for OverrideStrict { } } +#[cacheable] #[derive(Debug, Clone, MergeFrom)] pub struct JavascriptParserOptions { pub dynamic_import_mode: Option, @@ -242,32 +251,38 @@ pub struct JavascriptParserOptions { pub import_dynamic: Option, } +#[cacheable] #[derive(Debug, Clone, MergeFrom)] pub struct AssetParserOptions { pub data_url_condition: Option, } +#[cacheable] #[derive(Debug, Clone, MergeFrom)] pub enum AssetParserDataUrl { Options(AssetParserDataUrlOptions), // TODO: Function } +#[cacheable] #[derive(Debug, Clone, MergeFrom)] pub struct AssetParserDataUrlOptions { pub max_size: Option, } +#[cacheable] #[derive(Debug, Clone, MergeFrom)] pub struct CssParserOptions { pub named_exports: Option, } +#[cacheable] #[derive(Debug, Clone, MergeFrom)] pub struct CssAutoParserOptions { pub named_exports: Option, } +#[cacheable] #[derive(Debug, Clone, MergeFrom)] pub struct CssModuleParserOptions { pub named_exports: Option, @@ -288,6 +303,7 @@ impl GeneratorOptionsMap { } } +#[cacheable] #[derive(Debug, Clone, MergeFrom)] pub enum GeneratorOptions { Asset(AssetGeneratorOptions), @@ -344,11 +360,13 @@ impl GeneratorOptions { } } +#[cacheable] #[derive(Debug, Clone, MergeFrom)] pub struct AssetInlineGeneratorOptions { pub data_url: Option, } +#[cacheable] #[derive(Debug, Clone, MergeFrom)] pub struct AssetResourceGeneratorOptions { pub emit: Option, @@ -356,6 +374,7 @@ pub struct AssetResourceGeneratorOptions { pub public_path: Option, } +#[cacheable] #[derive(Debug, Clone, MergeFrom)] pub struct AssetGeneratorOptions { pub emit: Option, @@ -372,9 +391,10 @@ pub struct AssetGeneratorDataUrlFnArgs { pub type AssetGeneratorDataUrlFn = Arc Result + Sync + Send>; +#[cacheable] pub enum AssetGeneratorDataUrl { Options(AssetGeneratorDataUrlOptions), - Func(AssetGeneratorDataUrlFn), + Func(#[cacheable(with=Unsupported)] AssetGeneratorDataUrlFn), } impl fmt::Debug for AssetGeneratorDataUrl { @@ -401,12 +421,14 @@ impl MergeFrom for AssetGeneratorDataUrl { } } +#[cacheable] #[derive(Debug, Clone, MergeFrom, Hash)] pub struct AssetGeneratorDataUrlOptions { pub encoding: Option, pub mimetype: Option, } +#[cacheable] #[derive(Debug, Clone, MergeFrom, Hash)] pub enum DataUrlEncoding { None, @@ -432,12 +454,14 @@ impl From for DataUrlEncoding { } } +#[cacheable] #[derive(Debug, Clone, MergeFrom)] pub struct CssGeneratorOptions { pub exports_only: Option, pub es_module: Option, } +#[cacheable] #[derive(Debug, Clone, MergeFrom)] pub struct CssAutoGeneratorOptions { pub exports_convention: Option, @@ -446,6 +470,7 @@ pub struct CssAutoGeneratorOptions { pub es_module: Option, } +#[cacheable] #[derive(Debug, Clone, MergeFrom)] pub struct CssModuleGeneratorOptions { pub exports_convention: Option, @@ -454,6 +479,7 @@ pub struct CssModuleGeneratorOptions { pub es_module: Option, } +#[cacheable] #[derive(Debug, Clone, MergeFrom)] pub struct LocalIdentName { pub template: crate::FilenameTemplate, @@ -467,9 +493,11 @@ impl From for LocalIdentName { } } +#[cacheable] +#[derive(Debug, Clone, Copy)] +struct ExportsConventionFlags(u8); bitflags! { - #[derive(Debug, Clone, Copy)] - struct ExportsConventionFlags: u8 { + impl ExportsConventionFlags: u8 { const ASIS = 1 << 0; const CAMELCASE = 1 << 1; const DASHES = 1 << 2; @@ -482,6 +510,7 @@ impl MergeFrom for ExportsConventionFlags { } } +#[cacheable] #[derive(Debug, Clone, Copy, MergeFrom)] pub struct CssExportsConvention(ExportsConventionFlags); diff --git a/crates/rspack_core/src/options/output.rs b/crates/rspack_core/src/options/output.rs index 101d3712796..3f9b611fc2b 100644 --- a/crates/rspack_core/src/options/output.rs +++ b/crates/rspack_core/src/options/output.rs @@ -3,6 +3,7 @@ use std::{borrow::Cow, fmt::Debug, hash::Hash, str::FromStr, string::ParseError} use derivative::Derivative; use regex::Regex; +use rspack_cacheable::cacheable; use rspack_hash::RspackHash; pub use rspack_hash::{HashDigest, HashFunction, HashSalt}; use rspack_macros::MergeFrom; @@ -91,6 +92,7 @@ pub struct TrustedTypes { pub on_policy_creation_failure: OnPolicyCreationFailure, } +#[cacheable] #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] pub enum ChunkLoading { Enable(ChunkLoadingType), @@ -124,6 +126,7 @@ impl From<&ChunkLoading> for &str { } } +#[cacheable] #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] pub enum ChunkLoadingType { Jsonp, @@ -312,6 +315,7 @@ impl<'a> PathData<'a> { } } +#[cacheable] #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, MergeFrom)] pub enum PublicPath { Filename(Filename), @@ -428,6 +432,7 @@ pub fn get_js_chunk_filename_template( } } +#[cacheable] #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] pub struct LibraryOptions { pub name: Option, @@ -443,6 +448,7 @@ pub type LibraryType = String; pub type LibraryExport = Vec; +#[cacheable] #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] pub struct LibraryAuxiliaryComment { pub root: Option, @@ -451,18 +457,21 @@ pub struct LibraryAuxiliaryComment { pub amd: Option, } +#[cacheable] #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] pub enum LibraryName { NonUmdObject(LibraryNonUmdObject), UmdObject(LibraryCustomUmdObject), } +#[cacheable] #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] pub enum LibraryNonUmdObject { Array(Vec), String(String), } +#[cacheable] #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] pub struct LibraryCustomUmdObject { pub amd: Option, diff --git a/crates/rspack_core/src/options/resolve/mod.rs b/crates/rspack_core/src/options/resolve/mod.rs index caf362f81e7..faa8724c8b8 100644 --- a/crates/rspack_core/src/options/resolve/mod.rs +++ b/crates/rspack_core/src/options/resolve/mod.rs @@ -4,6 +4,10 @@ mod value_type; use std::borrow::Cow; use hashlink::LinkedHashMap; +use rspack_cacheable::{ + cacheable, + with::{AsCacheable, AsMap, AsOption, AsPreset, AsRefStr, AsTuple2, AsVec}, +}; use rspack_paths::Utf8PathBuf; use crate::DependencyCategory; @@ -30,12 +34,14 @@ pub(super) type Modules = Vec; pub(super) type Roots = Vec; pub(super) type Restrictions = Vec; +#[cacheable] #[derive(Debug, Clone, Default, Hash, PartialEq, Eq)] pub struct Resolve { /// Tried detect file with this extension. pub extensions: Option, /// Maps key to value. /// The reason for using `Vec` instead `HashMap` to keep the order. + #[cacheable(with=AsOption>>>)] pub alias: Option, /// Prefer to resolve request as relative request and /// fallback to resolving as modules. @@ -61,6 +67,7 @@ pub struct Resolve { pub modules: Option, /// Same as `alias`, but only used if default resolving fails /// Default is `[]` + #[cacheable(with=AsOption>>>)] pub fallback: Option, /// Request passed to resolve is already fully specified and /// extensions or main files are not resolved for it. @@ -81,6 +88,7 @@ pub struct Resolve { /// Field names from the description file (usually package.json) which are used to provide internal request of a package (requests starting with # are considered as internal). pub imports_fields: Option, /// Configure resolve options by the type of module request. + #[cacheable(omit_bounds)] pub by_dependency: Option, /// The JSON files to use for descriptions /// Default is [`package.json`] @@ -92,12 +100,14 @@ pub struct Resolve { /// Tsconfig Options /// /// Derived from [tsconfig-paths-webpack-plugin](https://github.com/dividab/tsconfig-paths-webpack-plugin#options) +#[cacheable] #[derive(Debug, Clone, Hash, PartialEq, Eq, Default)] pub struct TsconfigOptions { /// Allows you to specify where to find the TypeScript configuration file. /// You may provide /// * a relative path to the configuration file. It will be resolved relative to cwd. /// * an absolute path to the configuration file. + #[cacheable(with=AsPreset)] pub config_file: Utf8PathBuf, /// Support for Typescript Project References. @@ -113,6 +123,7 @@ impl From for rspack_resolver::TsconfigOptions { } } +#[cacheable] #[derive(Debug, Clone, Hash, PartialEq, Eq, Default)] pub enum TsconfigReferences { #[default] @@ -120,7 +131,7 @@ pub enum TsconfigReferences { /// Use the `references` field from tsconfig read from `config_file`. Auto, /// Manually provided relative or absolute path. - Paths(Vec), + Paths(#[cacheable(with=AsVec)] Vec), } impl From for rspack_resolver::TsconfigReferences { @@ -174,8 +185,11 @@ impl Resolve { type DependencyCategoryStr = Cow<'static, str>; +#[cacheable] #[derive(Debug, Clone, Default, Hash, PartialEq, Eq)] -pub struct ByDependency(LinkedHashMap); +pub struct ByDependency( + #[cacheable(with=AsMap)] LinkedHashMap, +); impl FromIterator<(DependencyCategoryStr, Resolve)> for ByDependency { fn from_iter>(i: I) -> Self { diff --git a/crates/rspack_core/src/parser_and_generator.rs b/crates/rspack_core/src/parser_and_generator.rs index 5c29b46ad8d..6a3559d729a 100644 --- a/crates/rspack_core/src/parser_and_generator.rs +++ b/crates/rspack_core/src/parser_and_generator.rs @@ -3,6 +3,7 @@ use std::borrow::Cow; use std::fmt::Debug; use derivative::Derivative; +use rspack_cacheable::cacheable_dyn; use rspack_error::{Result, TWithDiagnosticArray}; use rspack_loader_runner::{AdditionalData, ResourceData}; use rspack_sources::BoxSource; @@ -85,6 +86,7 @@ pub struct GenerateContext<'a> { pub concatenation_scope: Option<&'a mut ConcatenationScope>, } +#[cacheable_dyn] pub trait ParserAndGenerator: Send + Sync + Debug + AsAny { /// The source types that the generator can generate (the source types you can make requests for) fn source_types(&self) -> &[SourceType]; diff --git a/crates/rspack_core/src/raw_module.rs b/crates/rspack_core/src/raw_module.rs index 289a4713127..94af6ce443e 100644 --- a/crates/rspack_core/src/raw_module.rs +++ b/crates/rspack_core/src/raw_module.rs @@ -1,5 +1,6 @@ use std::borrow::Cow; +use rspack_cacheable::{cacheable, cacheable_dyn, with::AsPreset}; use rspack_collections::Identifiable; use rspack_error::{impl_empty_diagnosable_trait, Diagnostic, Result}; use rspack_macros::impl_source_map_config; @@ -14,10 +15,12 @@ use crate::{ use crate::{module_update_hash, Compilation, ConcatenationScope, FactoryMeta}; #[impl_source_map_config] +#[cacheable] #[derive(Debug)] pub struct RawModule { blocks: Vec, dependencies: Vec, + #[cacheable(with=AsPreset)] source: BoxSource, identifier: ModuleIdentifier, readable_identifier: String, @@ -80,6 +83,7 @@ impl DependenciesBlock for RawModule { } } +#[cacheable_dyn] #[async_trait::async_trait] impl Module for RawModule { impl_module_meta_info!(); diff --git a/crates/rspack_core/src/resolver/factory.rs b/crates/rspack_core/src/resolver/factory.rs index 270bc2d1ec0..72e7d110c60 100644 --- a/crates/rspack_core/src/resolver/factory.rs +++ b/crates/rspack_core/src/resolver/factory.rs @@ -1,11 +1,16 @@ -use std::{hash::BuildHasherDefault, sync::Arc}; +use std::{any::Any, hash::BuildHasherDefault, sync::Arc}; use dashmap::DashMap; +use rspack_cacheable::{ + cacheable, + with::{As, AsConverter}, + DeserializeError, SerializeError, +}; use rspack_fs::FileSystem; use rustc_hash::FxHasher; use super::resolver_impl::Resolver; -use crate::{DependencyCategory, Resolve}; +use crate::{cache::persistent::CacheableContext, DependencyCategory, Resolve}; #[derive(Debug, Hash, Eq, PartialEq, Clone)] // Actually this should be ResolveOptionsWithDependencyCategory, it's a mistake from webpack, but keep the alignment for easily find the code in webpack @@ -15,6 +20,7 @@ pub struct ResolveOptionsWithDependencyType { pub dependency_category: DependencyCategory, } +#[cacheable(with=As::)] #[derive(Debug)] pub struct ResolverFactory { base_options: Resolve, @@ -24,6 +30,18 @@ pub struct ResolverFactory { resolvers: DashMap, BuildHasherDefault>, } +impl AsConverter for Resolve { + fn serialize(data: &ResolverFactory, _ctx: &dyn Any) -> Result { + Ok(data.base_options.clone()) + } + fn deserialize(self, ctx: &dyn Any) -> Result { + let Some(ctx) = ctx.downcast_ref::() else { + return Err(DeserializeError::NoContext); + }; + Ok(ResolverFactory::new(self, ctx.input_filesystem.clone())) + } +} + impl ResolverFactory { pub fn clear_cache(&self) { self.resolver.clear_cache(); diff --git a/crates/rspack_core/src/runtime.rs b/crates/rspack_core/src/runtime.rs index aa49f0325bc..bf54b9a4dc0 100644 --- a/crates/rspack_core/src/runtime.rs +++ b/crates/rspack_core/src/runtime.rs @@ -3,12 +3,18 @@ use std::collections::hash_set; use std::ops::Deref; use std::{cmp::Ordering, fmt::Debug, sync::Arc}; +use rspack_cacheable::{ + cacheable, + with::{AsRefStr, AsVec}, +}; use rustc_hash::{FxHashMap as HashMap, FxHashSet}; use crate::{EntryOptions, EntryRuntime}; +#[cacheable] #[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct RuntimeSpec { + #[cacheable(with=AsVec)] inner: FxHashSet>, key: String, } diff --git a/crates/rspack_core/src/runtime_globals.rs b/crates/rspack_core/src/runtime_globals.rs index 1266caa6c99..46ad034e11e 100644 --- a/crates/rspack_core/src/runtime_globals.rs +++ b/crates/rspack_core/src/runtime_globals.rs @@ -3,9 +3,12 @@ use std::fmt; use bitflags::bitflags; use swc_core::ecma::atoms::Atom; +#[rspack_cacheable::cacheable] +#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] +pub struct RuntimeGlobals(u128); + bitflags! { - #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] - pub struct RuntimeGlobals: u128 { + impl RuntimeGlobals: u128 { const REQUIRE_SCOPE = 1 << 0; /** diff --git a/crates/rspack_core/src/runtime_module.rs b/crates/rspack_core/src/runtime_module.rs index bc5f1587b96..2675684eba8 100644 --- a/crates/rspack_core/src/runtime_module.rs +++ b/crates/rspack_core/src/runtime_module.rs @@ -1,5 +1,6 @@ use std::sync::Arc; +use rspack_cacheable::cacheable; use rspack_collections::Identifier; use rspack_sources::{BoxSource, Source}; @@ -43,6 +44,7 @@ pub trait CustomSourceRuntimeModule { pub type BoxRuntimeModule = Box; +#[cacheable] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum RuntimeModuleStage { Normal, // Runtime modules without any dependencies to other runtime modules diff --git a/crates/rspack_core/src/self_module.rs b/crates/rspack_core/src/self_module.rs index 189440c6b85..46abd7cd07c 100644 --- a/crates/rspack_core/src/self_module.rs +++ b/crates/rspack_core/src/self_module.rs @@ -1,6 +1,7 @@ use std::borrow::Cow; use async_trait::async_trait; +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_collections::{Identifiable, Identifier}; use rspack_error::{impl_empty_diagnosable_trait, Diagnostic, Result}; use rspack_macros::impl_source_map_config; @@ -15,6 +16,7 @@ use crate::{ }; #[impl_source_map_config] +#[cacheable] #[derive(Debug)] pub struct SelfModule { identifier: ModuleIdentifier, @@ -70,6 +72,7 @@ impl DependenciesBlock for SelfModule { } } +#[cacheable_dyn] #[async_trait] impl Module for SelfModule { impl_module_meta_info!(); diff --git a/crates/rspack_core/src/ukey.rs b/crates/rspack_core/src/ukey.rs index 95261734f0b..c0b3da83bd8 100644 --- a/crates/rspack_core/src/ukey.rs +++ b/crates/rspack_core/src/ukey.rs @@ -6,6 +6,7 @@ use crate::{Chunk, ChunkGroup}; static NEXT_CHUNK_UKEY: AtomicU32 = AtomicU32::new(0); +#[rspack_cacheable::cacheable] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ChunkUkey(Ukey, std::marker::PhantomData); diff --git a/crates/rspack_core/src/utils/file_counter/incremental_info.rs b/crates/rspack_core/src/utils/file_counter/incremental_info.rs index 0ea117811e5..5bb3474c939 100644 --- a/crates/rspack_core/src/utils/file_counter/incremental_info.rs +++ b/crates/rspack_core/src/utils/file_counter/incremental_info.rs @@ -1,5 +1,3 @@ -use std::{borrow::Borrow, hash::Hash}; - use rspack_paths::ArcPath; use rustc_hash::FxHashSet as HashSet; @@ -8,6 +6,7 @@ use rustc_hash::FxHashSet as HashSet; pub struct IncrementalInfo { added_files: HashSet, removed_files: HashSet, + updated_files: HashSet, } impl IncrementalInfo { @@ -21,27 +20,27 @@ impl IncrementalInfo { &self.removed_files } + /// Get removed files + pub fn updated_files(&self) -> &HashSet { + &self.updated_files + } + + /// Update a file + pub fn update(&mut self, path: &ArcPath) { + self.updated_files.insert(path.clone()); + } + /// Add a file - pub fn add

(&mut self, path: &P) - where - P: Hash + Eq + ?Sized, - ArcPath: Borrow

, - for<'a> &'a P: Into, - { + pub fn add(&mut self, path: &ArcPath) { if !self.removed_files.remove(path) { - self.added_files.insert(path.into()); + self.added_files.insert(path.clone()); } } /// Remove a file - pub fn remove

(&mut self, path: &P) - where - P: Hash + Eq + ?Sized, - ArcPath: Borrow

, - for<'a> &'a P: Into, - { + pub fn remove(&mut self, path: &ArcPath) { if !self.added_files.remove(path) { - self.removed_files.insert(path.into()); + self.removed_files.insert(path.clone()); } } @@ -49,6 +48,7 @@ impl IncrementalInfo { pub fn reset(&mut self) { self.added_files.clear(); self.removed_files.clear(); + self.updated_files.clear(); } } @@ -56,27 +56,32 @@ impl IncrementalInfo { mod test { use std::path::PathBuf; + use rspack_paths::ArcPath; + use super::IncrementalInfo; #[test] fn incremental_info_is_available() { let mut info = IncrementalInfo::default(); - let file_a = PathBuf::from("/a"); + let file_a = ArcPath::from(PathBuf::from("/a")); - info.add(file_a.as_path()); - info.add(file_a.as_path()); + info.add(&file_a); + info.add(&file_a); assert_eq!(info.added_files().len(), 1); assert_eq!(info.removed_files().len(), 0); - info.remove(file_a.as_path()); + info.remove(&file_a); assert_eq!(info.added_files().len(), 0); assert_eq!(info.removed_files().len(), 0); - info.remove(file_a.as_path()); + info.remove(&file_a); assert_eq!(info.added_files().len(), 0); assert_eq!(info.removed_files().len(), 1); - info.remove(file_a.as_path()); + info.remove(&file_a); assert_eq!(info.added_files().len(), 0); assert_eq!(info.removed_files().len(), 1); + + info.update(&file_a); + assert_eq!(info.updated_files().len(), 1); } } diff --git a/crates/rspack_core/src/utils/file_counter/mod.rs b/crates/rspack_core/src/utils/file_counter/mod.rs index 27ab38f9ac2..a0955f152a8 100644 --- a/crates/rspack_core/src/utils/file_counter/mod.rs +++ b/crates/rspack_core/src/utils/file_counter/mod.rs @@ -12,10 +12,24 @@ pub struct FileCounter { } impl FileCounter { - /// Add a [`PathBuf``] to counter + /// Generate FileCounter by HashMap + pub fn new(inner: HashMap) -> Self { + Self { + inner, + incremental_info: Default::default(), + } + } + + /// Returns `true` if the [`PathBuf`] is in counter. + pub fn contains(&self, path: &ArcPath) -> bool { + self.inner.contains_key(path) + } + + /// Add a [`PathBuf`] to counter /// /// It will +1 to the PathBuf in inner hashmap fn add_file(&mut self, path: ArcPath) { + self.incremental_info.update(&path); if let Some(value) = self.inner.get_mut(&path) { *value += 1; } else { @@ -31,6 +45,7 @@ impl FileCounter { /// If the PathBuf usage is 0 after reduction, the record will be deleted /// If PathBuf does not exist, panic will occur. fn remove_file(&mut self, path: &ArcPath) { + self.incremental_info.update(path); if let Some(value) = self.inner.get_mut(path) { *value -= 1; if value == &0 { @@ -63,7 +78,7 @@ impl FileCounter { /// reset incremental info pub fn reset_incremental_info(&mut self) { - self.incremental_info.reset() + self.incremental_info.reset(); } /// Added files compared to the `files()` when call reset_incremental_info @@ -75,6 +90,14 @@ impl FileCounter { pub fn removed_files(&self) -> &HashSet { self.incremental_info.removed_files() } + + /// Return updated files compared to the `files()` when call reset_incremental_info and their count info + pub fn updated_files_count_info(&self) -> impl ExactSizeIterator { + self.incremental_info.updated_files().iter().map(|file| { + let count = self.inner.get(file).unwrap_or(&0); + (file, *count) + }) + } } #[cfg(test)] @@ -97,21 +120,31 @@ mod test { assert_eq!(counter.files().collect::>().len(), 2); assert_eq!(counter.added_files().len(), 2); assert_eq!(counter.removed_files().len(), 0); + assert_eq!(counter.updated_files_count_info().len(), 2); counter.remove_file(&file_a); + assert!(counter.contains(&file_a)); + assert!(counter.contains(&file_b)); assert_eq!(counter.files().collect::>().len(), 2); assert_eq!(counter.added_files().len(), 2); assert_eq!(counter.removed_files().len(), 0); + assert_eq!(counter.updated_files_count_info().len(), 2); counter.remove_file(&file_b); + assert!(counter.contains(&file_a)); + assert!(!counter.contains(&file_b)); assert_eq!(counter.files().collect::>().len(), 1); assert_eq!(counter.added_files().len(), 1); assert_eq!(counter.removed_files().len(), 0); + assert_eq!(counter.updated_files_count_info().len(), 2); counter.remove_file(&file_a); + assert!(!counter.contains(&file_a)); + assert!(!counter.contains(&file_b)); assert_eq!(counter.files().collect::>().len(), 0); assert_eq!(counter.added_files().len(), 0); assert_eq!(counter.removed_files().len(), 0); + assert_eq!(counter.updated_files_count_info().len(), 2); } #[test] @@ -158,21 +191,26 @@ mod test { counter.add_file(file_a.clone()); assert_eq!(counter.added_files().len(), 1); assert_eq!(counter.removed_files().len(), 0); + assert_eq!(counter.updated_files_count_info().len(), 1); counter.reset_incremental_info(); assert_eq!(counter.added_files().len(), 0); assert_eq!(counter.removed_files().len(), 0); + assert_eq!(counter.updated_files_count_info().len(), 0); counter.add_file(file_a.clone()); assert_eq!(counter.added_files().len(), 0); assert_eq!(counter.removed_files().len(), 0); + assert_eq!(counter.updated_files_count_info().len(), 1); counter.remove_file(&file_a); assert_eq!(counter.added_files().len(), 0); assert_eq!(counter.removed_files().len(), 0); + assert_eq!(counter.updated_files_count_info().len(), 1); counter.remove_file(&file_a); assert_eq!(counter.added_files().len(), 0); assert_eq!(counter.removed_files().len(), 1); + assert_eq!(counter.updated_files_count_info().len(), 1); } } diff --git a/crates/rspack_error/Cargo.toml b/crates/rspack_error/Cargo.toml index 1c386d798ba..56e4dd7a0c4 100644 --- a/crates/rspack_error/Cargo.toml +++ b/crates/rspack_error/Cargo.toml @@ -15,6 +15,7 @@ futures = { workspace = true } miette = { version = "5", features = ["fancy"] } once_cell = { workspace = true } owo-colors = "3.5.0" +rspack_cacheable = { workspace = true } rspack_collections = { workspace = true } rspack_paths = { workspace = true } swc_core = { workspace = true, features = ["common", "common_concurrent"] } diff --git a/crates/rspack_error/src/diagnostic.rs b/crates/rspack_error/src/diagnostic.rs index 75b9bf3818d..7c5e80d0db5 100644 --- a/crates/rspack_error/src/diagnostic.rs +++ b/crates/rspack_error/src/diagnostic.rs @@ -2,6 +2,7 @@ use std::{fmt, ops::Deref, sync::Arc}; use cow_utils::CowUtils; use miette::{GraphicalTheme, IntoDiagnostic, MietteDiagnostic}; +use rspack_cacheable::{cacheable, with::Unsupported}; use rspack_collections::Identifier; use rspack_paths::{Utf8Path, Utf8PathBuf}; use swc_core::common::{SourceMap, Span}; @@ -59,12 +60,14 @@ impl fmt::Display for RspackSeverity { } } +#[cacheable] #[derive(Debug, Clone, Copy)] pub struct SourcePosition { pub line: usize, pub column: usize, } +#[cacheable] #[derive(Debug, Clone, Copy)] pub struct ErrorLocation { pub start: SourcePosition, @@ -89,6 +92,7 @@ impl ErrorLocation { } } +#[cacheable(with=Unsupported)] #[derive(Debug, Clone)] pub struct Diagnostic { inner: Arc, diff --git a/crates/rspack_hash/Cargo.toml b/crates/rspack_hash/Cargo.toml index e0a2727bb87..810f62d909a 100644 --- a/crates/rspack_hash/Cargo.toml +++ b/crates/rspack_hash/Cargo.toml @@ -7,7 +7,8 @@ repository = "https://github.com/web-infra-dev/rspack" version = "0.2.0" [dependencies] -data-encoding = { version = "2.6.0" } -md4 = "0.10.2" -smol_str = { version = "0.3.0" } -xxhash-rust = { workspace = true, features = ["xxh64"] } +data-encoding = { version = "2.6.0" } +md4 = "0.10.2" +rspack_cacheable = { workspace = true } +smol_str = { workspace = true } +xxhash-rust = { workspace = true, features = ["xxh64"] } diff --git a/crates/rspack_hash/src/lib.rs b/crates/rspack_hash/src/lib.rs index fa6b9234eb6..54f5549158f 100644 --- a/crates/rspack_hash/src/lib.rs +++ b/crates/rspack_hash/src/lib.rs @@ -5,6 +5,7 @@ use std::{ use data_encoding::HEXLOWER_PERMISSIVE; use md4::Digest; +use rspack_cacheable::{cacheable, with::AsPreset}; use smol_str::SmolStr; use xxhash_rust::xxh64::Xxh64; @@ -121,8 +122,10 @@ impl Hasher for RspackHash { } } +#[cacheable] #[derive(Debug, Clone, Eq)] pub struct RspackHashDigest { + #[cacheable(with=AsPreset)] encoded: SmolStr, } diff --git a/crates/rspack_loader_lightningcss/Cargo.toml b/crates/rspack_loader_lightningcss/Cargo.toml index 697f083d926..0c44f343b0d 100644 --- a/crates/rspack_loader_lightningcss/Cargo.toml +++ b/crates/rspack_loader_lightningcss/Cargo.toml @@ -12,6 +12,7 @@ async-trait = { workspace = true } derivative = { workspace = true } lightningcss = { workspace = true, features = ["sourcemap", "browserslist", "visitor", "into_owned"] } parcel_sourcemap = { workspace = true } +rspack_cacheable = { workspace = true } rspack_core = { workspace = true } rspack_error = { workspace = true } rspack_loader_runner = { workspace = true } diff --git a/crates/rspack_loader_lightningcss/src/config.rs b/crates/rspack_loader_lightningcss/src/config.rs index 428af730b6c..85c43d5216e 100644 --- a/crates/rspack_loader_lightningcss/src/config.rs +++ b/crates/rspack_loader_lightningcss/src/config.rs @@ -1,18 +1,25 @@ use lightningcss::targets::Browsers; +use rspack_cacheable::{ + cacheable, + with::{AsOption, AsPreset}, +}; use serde::Deserialize; +#[cacheable] #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Draft { pub custom_media: bool, } +#[cacheable] #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct NonStandard { pub deep_selector_combinator: bool, } +#[cacheable] #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct PseudoClasses { @@ -23,10 +30,12 @@ pub struct PseudoClasses { pub focus_within: Option, } +#[cacheable] #[derive(Debug, Default)] pub struct Config { pub minify: Option, pub error_recovery: Option, + #[cacheable(with=AsOption)] pub targets: Option, pub include: Option, pub exclude: Option, diff --git a/crates/rspack_loader_lightningcss/src/lib.rs b/crates/rspack_loader_lightningcss/src/lib.rs index f023e5caf0e..d325b666d63 100644 --- a/crates/rspack_loader_lightningcss/src/lib.rs +++ b/crates/rspack_loader_lightningcss/src/lib.rs @@ -9,6 +9,7 @@ use lightningcss::{ targets::{Features, Targets}, traits::IntoOwned, }; +use rspack_cacheable::{cacheable, cacheable_dyn, with::Skip}; use rspack_core::{ rspack_sources::{encode_mappings, Mapping, OriginalLocation, SourceMap}, Loader, LoaderContext, RunnerContext, @@ -23,11 +24,13 @@ pub const LIGHTNINGCSS_LOADER_IDENTIFIER: &str = "builtin:lightningcss-loader"; pub type LightningcssLoaderVisitor = Box)>; +#[cacheable] #[derive(Derivative)] #[derivative(Debug)] pub struct LightningCssLoader { id: Identifier, #[derivative(Debug = "ignore")] + #[cacheable(with=Skip)] visitors: Option>>, config: Config, } @@ -226,6 +229,7 @@ impl Identifiable for LightningCssLoader { } } +#[cacheable_dyn] #[async_trait::async_trait] impl Loader for LightningCssLoader { async fn run(&self, loader_context: &mut LoaderContext) -> Result<()> { diff --git a/crates/rspack_loader_preact_refresh/Cargo.toml b/crates/rspack_loader_preact_refresh/Cargo.toml index d9334ef4eb7..de42e86aa42 100644 --- a/crates/rspack_loader_preact_refresh/Cargo.toml +++ b/crates/rspack_loader_preact_refresh/Cargo.toml @@ -9,6 +9,7 @@ version = "0.2.0" [dependencies] async-trait = { workspace = true } +rspack_cacheable = { workspace = true } rspack_core = { workspace = true } rspack_error = { workspace = true } rspack_loader_runner = { workspace = true } diff --git a/crates/rspack_loader_preact_refresh/src/lib.rs b/crates/rspack_loader_preact_refresh/src/lib.rs index 3861ff41b69..2a9b7f5ed41 100644 --- a/crates/rspack_loader_preact_refresh/src/lib.rs +++ b/crates/rspack_loader_preact_refresh/src/lib.rs @@ -1,7 +1,9 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::RunnerContext; use rspack_error::Result; use rspack_loader_runner::{Identifiable, Identifier, Loader, LoaderContext}; +#[cacheable] pub struct PreactRefreshLoader { identifier: Identifier, } @@ -24,6 +26,7 @@ impl PreactRefreshLoader { } } +#[cacheable_dyn] #[async_trait::async_trait] impl Loader for PreactRefreshLoader { async fn run(&self, loader_context: &mut LoaderContext) -> Result<()> { diff --git a/crates/rspack_loader_react_refresh/Cargo.toml b/crates/rspack_loader_react_refresh/Cargo.toml index 8cfb4557be8..0879bcf90b3 100644 --- a/crates/rspack_loader_react_refresh/Cargo.toml +++ b/crates/rspack_loader_react_refresh/Cargo.toml @@ -9,6 +9,7 @@ version = "0.2.0" [dependencies] async-trait = { workspace = true } +rspack_cacheable = { workspace = true } rspack_core = { workspace = true } rspack_error = { workspace = true } rspack_loader_runner = { workspace = true } diff --git a/crates/rspack_loader_react_refresh/src/lib.rs b/crates/rspack_loader_react_refresh/src/lib.rs index aac81039a2b..89fbb1c06bc 100644 --- a/crates/rspack_loader_react_refresh/src/lib.rs +++ b/crates/rspack_loader_react_refresh/src/lib.rs @@ -1,7 +1,9 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::RunnerContext; use rspack_error::Result; use rspack_loader_runner::{Identifiable, Identifier, Loader, LoaderContext}; +#[cacheable] pub struct ReactRefreshLoader { identifier: Identifier, } @@ -24,6 +26,7 @@ impl ReactRefreshLoader { } } +#[cacheable_dyn] #[async_trait::async_trait] impl Loader for ReactRefreshLoader { async fn run(&self, loader_context: &mut LoaderContext) -> Result<()> { diff --git a/crates/rspack_loader_runner/Cargo.toml b/crates/rspack_loader_runner/Cargo.toml index 6d6b9c2574c..6bcff2b978a 100644 --- a/crates/rspack_loader_runner/Cargo.toml +++ b/crates/rspack_loader_runner/Cargo.toml @@ -14,6 +14,7 @@ tokio = { workspace = true, features = ["rt", "rt-multi-thread", "macros", once_cell = { workspace = true } regex = { workspace = true } +rspack_cacheable = { workspace = true } rspack_collections = { workspace = true } rspack_error = { workspace = true } rspack_fs = { workspace = true } diff --git a/crates/rspack_loader_runner/src/content.rs b/crates/rspack_loader_runner/src/content.rs index b71d3239b70..488b45cdc68 100644 --- a/crates/rspack_loader_runner/src/content.rs +++ b/crates/rspack_loader_runner/src/content.rs @@ -6,6 +6,10 @@ use std::{ use anymap::CloneAny; use once_cell::sync::OnceCell; +use rspack_cacheable::{ + cacheable, + with::{AsInner, AsOption, AsPreset, AsString}, +}; use rspack_error::{Error, Result}; use rspack_paths::Utf8PathBuf; @@ -104,11 +108,13 @@ impl Debug for Content { } } +#[cacheable] #[derive(Debug, Clone)] pub struct ResourceData { /// Resource with absolute path, query and fragment pub resource: String, /// Absolute resource path only + #[cacheable(with=AsOption)] pub resource_path: Option, /// Resource query with `?` prefix pub resource_query: Option, @@ -119,6 +125,7 @@ pub struct ResourceData { pub parameters: Option, pub encoding: Option, pub encoded_content: Option, + #[cacheable(with=AsInner)] pub(crate) scheme: OnceCell, } @@ -244,12 +251,15 @@ impl ResourceData { /// Used for [Rule.descriptionData](https://www.rspack.dev/config/module.html#ruledescriptiondata) and /// package.json.sideEffects in tree shaking. +#[cacheable] #[derive(Debug, Clone)] pub struct DescriptionData { /// Path to package.json + #[cacheable(with=AsString)] path: PathBuf, /// Raw package.json + #[cacheable(with=AsInner)] json: Arc, } diff --git a/crates/rspack_loader_runner/src/loader.rs b/crates/rspack_loader_runner/src/loader.rs index 787a3d1b2eb..24ec38f1989 100644 --- a/crates/rspack_loader_runner/src/loader.rs +++ b/crates/rspack_loader_runner/src/loader.rs @@ -11,6 +11,7 @@ use std::{ use async_trait::async_trait; use derivative::Derivative; use regex::Regex; +use rspack_cacheable::cacheable_dyn; use rspack_collections::{Identifiable, Identifier}; use rspack_error::Result; use rspack_paths::Utf8PathBuf; @@ -164,6 +165,7 @@ impl Identifiable for LoaderItem { } } +#[cacheable_dyn] #[async_trait] pub trait Loader: Identifiable + Send + Sync where @@ -260,13 +262,15 @@ static PATH_QUERY_FRAGMENT_REGEXP: LazyLock = LazyLock::new(|| { pub(crate) mod test { use std::{path::PathBuf, sync::Arc}; + use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_collections::{Identifiable, Identifier}; use super::{Loader, LoaderItem}; + #[cacheable] #[allow(dead_code)] pub(crate) struct Custom; - + #[cacheable_dyn] #[async_trait::async_trait] impl Loader<()> for Custom {} impl Identifiable for Custom { @@ -275,8 +279,10 @@ pub(crate) mod test { } } + #[cacheable] #[allow(dead_code)] pub(crate) struct Custom2; + #[cacheable_dyn] #[async_trait::async_trait] impl Loader<()> for Custom2 {} impl Identifiable for Custom2 { @@ -285,8 +291,10 @@ pub(crate) mod test { } } + #[cacheable] #[allow(dead_code)] pub(crate) struct Builtin; + #[cacheable_dyn] #[async_trait::async_trait] impl Loader<()> for Builtin {} impl Identifiable for Builtin { @@ -295,8 +303,10 @@ pub(crate) mod test { } } + #[cacheable] pub(crate) struct PosixNonLenBlankUnicode; + #[cacheable_dyn] #[async_trait::async_trait] impl Loader<()> for PosixNonLenBlankUnicode {} impl Identifiable for PosixNonLenBlankUnicode { @@ -305,7 +315,9 @@ pub(crate) mod test { } } + #[cacheable] pub(crate) struct WinNonLenBlankUnicode; + #[cacheable_dyn] #[async_trait::async_trait] impl Loader<()> for WinNonLenBlankUnicode {} impl Identifiable for WinNonLenBlankUnicode { diff --git a/crates/rspack_loader_runner/src/runner.rs b/crates/rspack_loader_runner/src/runner.rs index b8436d730d0..5f1e131d7d4 100644 --- a/crates/rspack_loader_runner/src/runner.rs +++ b/crates/rspack_loader_runner/src/runner.rs @@ -239,6 +239,7 @@ mod test { use std::{cell::RefCell, sync::Arc}; use once_cell::sync::OnceCell; + use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_collections::{Identifiable, Identifier}; use rspack_error::Result; use rspack_fs::NativeFileSystem; @@ -271,6 +272,7 @@ mod test { static IDENTS: RefCell> = RefCell::default(); } + #[cacheable] struct Pitching; impl Identifiable for Pitching { @@ -279,6 +281,7 @@ mod test { } } + #[cacheable_dyn] #[async_trait::async_trait] impl Loader<()> for Pitching { async fn pitch(&self, _loader_context: &mut LoaderContext<()>) -> Result<()> { @@ -287,6 +290,7 @@ mod test { } } + #[cacheable] struct Pitching2; impl Identifiable for Pitching2 { @@ -295,6 +299,7 @@ mod test { } } + #[cacheable_dyn] #[async_trait::async_trait] impl Loader<()> for Pitching2 { async fn pitch(&self, _loader_context: &mut LoaderContext<()>) -> Result<()> { @@ -303,6 +308,7 @@ mod test { } } + #[cacheable] struct Normal; impl Identifiable for Normal { @@ -311,6 +317,7 @@ mod test { } } + #[cacheable_dyn] #[async_trait::async_trait] impl Loader<()> for Normal { async fn run(&self, _loader_context: &mut LoaderContext<()>) -> Result<()> { @@ -319,6 +326,7 @@ mod test { } } + #[cacheable] struct Normal2; impl Identifiable for Normal2 { @@ -327,6 +335,7 @@ mod test { } } + #[cacheable_dyn] #[async_trait::async_trait] impl Loader<()> for Normal2 { async fn run(&self, _loader_context: &mut LoaderContext<()>) -> Result<()> { @@ -335,6 +344,7 @@ mod test { } } + #[cacheable] struct PitchNormalBase; impl Identifiable for PitchNormalBase { @@ -343,6 +353,7 @@ mod test { } } + #[cacheable_dyn] #[async_trait::async_trait] impl Loader<()> for PitchNormalBase { async fn run(&self, _loader_context: &mut LoaderContext<()>) -> Result<()> { @@ -356,6 +367,7 @@ mod test { } } + #[cacheable] struct PitchNormal; impl Identifiable for PitchNormal { @@ -364,6 +376,7 @@ mod test { } } + #[cacheable_dyn] #[async_trait::async_trait] impl Loader<()> for PitchNormal { async fn run(&self, _loader_context: &mut LoaderContext<()>) -> Result<()> { @@ -378,6 +391,7 @@ mod test { } } + #[cacheable] struct PitchNormal2; impl Identifiable for PitchNormal2 { @@ -386,6 +400,7 @@ mod test { } } + #[cacheable_dyn] #[async_trait::async_trait] impl Loader<()> for PitchNormal2 { async fn run(&self, _loader_context: &mut LoaderContext<()>) -> Result<()> { @@ -463,6 +478,7 @@ mod test { #[tokio::test] async fn should_able_to_consume_additional_data() { + #[cacheable] struct Normal; impl Identifiable for Normal { @@ -471,6 +487,7 @@ mod test { } } + #[cacheable_dyn] #[async_trait::async_trait] impl Loader<()> for Normal { async fn run(&self, loader_context: &mut LoaderContext<()>) -> Result<()> { @@ -486,6 +503,7 @@ mod test { } } + #[cacheable] struct Normal2; impl Identifiable for Normal2 { @@ -494,6 +512,7 @@ mod test { } } + #[cacheable_dyn] #[async_trait::async_trait] impl Loader<()> for Normal2 { async fn run(&self, loader_context: &mut LoaderContext<()>) -> Result<()> { @@ -530,6 +549,7 @@ mod test { #[tokio::test] async fn should_override_data_if_finish_with_is_not_called() { + #[cacheable] struct Normal; impl Identifiable for Normal { @@ -538,6 +558,7 @@ mod test { } } + #[cacheable_dyn] #[async_trait::async_trait] impl Loader<()> for Normal { async fn run(&self, loader_context: &mut LoaderContext<()>) -> Result<()> { @@ -560,6 +581,7 @@ mod test { encoded_content: None, }); + #[cacheable] struct Normal2; impl Identifiable for Normal2 { @@ -568,6 +590,7 @@ mod test { } } + #[cacheable_dyn] #[async_trait::async_trait] impl Loader<()> for Normal2 { async fn run(&self, loader_context: &mut LoaderContext<()>) -> Result<()> { diff --git a/crates/rspack_loader_runner/src/scheme.rs b/crates/rspack_loader_runner/src/scheme.rs index 1243e90592e..d7ff6d6018a 100644 --- a/crates/rspack_loader_runner/src/scheme.rs +++ b/crates/rspack_loader_runner/src/scheme.rs @@ -1,5 +1,8 @@ use std::fmt; +use rspack_cacheable::cacheable; + +#[cacheable] #[derive(Debug, Clone, PartialEq, Eq)] pub enum Scheme { None, diff --git a/crates/rspack_loader_swc/Cargo.toml b/crates/rspack_loader_swc/Cargo.toml index 0d4d9be6f28..1c1444f3d0b 100644 --- a/crates/rspack_loader_swc/Cargo.toml +++ b/crates/rspack_loader_swc/Cargo.toml @@ -23,6 +23,7 @@ base64 = { version = "0.22.1" } either = { workspace = true } jsonc-parser = { version = "0.26.2", features = ["serde"] } rspack_ast = { workspace = true } +rspack_cacheable = { workspace = true } rspack_core = { workspace = true } rspack_error = { workspace = true } rspack_loader_runner = { workspace = true } diff --git a/crates/rspack_loader_swc/src/lib.rs b/crates/rspack_loader_swc/src/lib.rs index e303cbf0a90..a1d659f7eb1 100644 --- a/crates/rspack_loader_swc/src/lib.rs +++ b/crates/rspack_loader_swc/src/lib.rs @@ -9,6 +9,7 @@ use std::default::Default; use compiler::{IntoJsAst, SwcCompiler}; use options::SwcCompilerOptionsWithAdditional; pub use options::SwcLoaderJsOptions; +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{Mode, RunnerContext}; use rspack_error::{error, AnyhowError, Diagnostic, Result}; use rspack_loader_runner::{Identifiable, Identifier, Loader, LoaderContext}; @@ -21,6 +22,7 @@ use swc_core::base::config::{InputSourceMap, OutputCharset, TransformConfig}; use swc_core::ecma::visit::VisitWith; use transformer::IdentCollector; +#[cacheable] #[derive(Debug)] pub struct SwcLoader { identifier: Identifier, @@ -28,11 +30,11 @@ pub struct SwcLoader { } impl SwcLoader { - pub fn new(options: SwcLoaderJsOptions) -> Self { - Self { + pub fn new(raw_options: &str) -> Result { + Ok(Self { identifier: SWC_LOADER_IDENTIFIER.into(), - options_with_additional: options.into(), - } + options_with_additional: raw_options.try_into()?, + }) } /// Panics: @@ -136,6 +138,7 @@ impl SwcLoader { pub const SWC_LOADER_IDENTIFIER: &str = "builtin:swc-loader"; +#[cacheable_dyn] #[async_trait::async_trait] impl Loader for SwcLoader { async fn run(&self, loader_context: &mut LoaderContext) -> Result<()> { diff --git a/crates/rspack_loader_swc/src/options.rs b/crates/rspack_loader_swc/src/options.rs index 8feadfc6321..3395fc60f23 100644 --- a/crates/rspack_loader_swc/src/options.rs +++ b/crates/rspack_loader_swc/src/options.rs @@ -1,3 +1,7 @@ +use rspack_cacheable::{ + cacheable, + with::{AsRefStr, AsRefStrConverter}, +}; use rspack_swc_plugin_import::{ImportOptions, RawImportOptions}; use serde::Deserialize; use swc_config::config_types::BoolConfig; @@ -74,16 +78,30 @@ pub struct SwcLoaderJsOptions { pub rspack_experiments: Option, } +#[cacheable(with=AsRefStr)] #[derive(Debug)] pub(crate) struct SwcCompilerOptionsWithAdditional { + raw_options: String, pub(crate) swc_options: Options, pub(crate) rspack_experiments: RspackExperiments, } +impl AsRefStrConverter for SwcCompilerOptionsWithAdditional { + fn as_str(&self) -> &str { + &self.raw_options + } + fn from_str(s: &str) -> Self { + s.try_into() + .expect("failed to generate SwcCompilerOptionsWithAdditional") + } +} + const SOURCE_MAP_INLINE: &str = "inline"; -impl From for SwcCompilerOptionsWithAdditional { - fn from(value: SwcLoaderJsOptions) -> Self { +impl TryFrom<&str> for SwcCompilerOptionsWithAdditional { + type Error = serde_json::Error; + fn try_from(value: &str) -> Result { + let option: SwcLoaderJsOptions = serde_json::from_str(value)?; let SwcLoaderJsOptions { source_maps, source_map, @@ -100,7 +118,7 @@ impl From for SwcCompilerOptionsWithAdditional { is_module, schema, rspack_experiments, - } = value; + } = option; let mut source_maps: Option = source_maps; if source_maps.is_none() && source_map.is_some() { source_maps = source_map @@ -110,7 +128,8 @@ impl From for SwcCompilerOptionsWithAdditional { source_maps = Some(SourceMapsConfig::Bool(true)) } } - SwcCompilerOptionsWithAdditional { + Ok(SwcCompilerOptionsWithAdditional { + raw_options: value.into(), swc_options: Options { config: Config { env, @@ -130,6 +149,6 @@ impl From for SwcCompilerOptionsWithAdditional { ..Default::default() }, rspack_experiments: rspack_experiments.unwrap_or_default().into(), - } + }) } } diff --git a/crates/rspack_loader_testing/Cargo.toml b/crates/rspack_loader_testing/Cargo.toml index c436ccb0962..8afc56c41d0 100644 --- a/crates/rspack_loader_testing/Cargo.toml +++ b/crates/rspack_loader_testing/Cargo.toml @@ -9,6 +9,7 @@ version = "0.2.0" [dependencies] async-trait = { workspace = true } +rspack_cacheable = { workspace = true } rspack_core = { workspace = true } rspack_error = { workspace = true } rspack_loader_runner = { workspace = true } diff --git a/crates/rspack_loader_testing/src/lib.rs b/crates/rspack_loader_testing/src/lib.rs index 3c0c55d0e02..fb33ae5562a 100644 --- a/crates/rspack_loader_testing/src/lib.rs +++ b/crates/rspack_loader_testing/src/lib.rs @@ -1,12 +1,15 @@ #![feature(let_chains)] use async_trait::async_trait; +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{Loader, LoaderContext, RunnerContext}; use rspack_error::Result; use rspack_loader_runner::{DisplayWithSuffix, Identifiable, Identifier}; use serde_json::json; +#[cacheable] pub struct SimpleLoader; +#[cacheable_dyn] #[async_trait] impl Loader for SimpleLoader { async fn run(&self, loader_context: &mut LoaderContext) -> Result<()> { @@ -25,7 +28,9 @@ impl Identifiable for SimpleLoader { } pub const SIMPLE_LOADER_IDENTIFIER: &str = "builtin:test-simple-loader"; +#[cacheable] pub struct SimpleAsyncLoader; +#[cacheable_dyn] #[async_trait] impl Loader for SimpleAsyncLoader { async fn run(&self, loader_context: &mut LoaderContext) -> Result<()> { @@ -43,7 +48,9 @@ impl Identifiable for SimpleAsyncLoader { } pub const SIMPLE_ASYNC_LOADER_IDENTIFIER: &str = "builtin:test-simple-async-loader"; +#[cacheable] pub struct PitchingLoader; +#[cacheable_dyn] #[async_trait] impl Loader for PitchingLoader { async fn pitch(&self, loader_context: &mut LoaderContext) -> Result<()> { @@ -66,7 +73,9 @@ impl Identifiable for PitchingLoader { } pub const PITCHING_LOADER_IDENTIFIER: &str = "builtin:test-pitching-loader"; +#[cacheable] pub struct PassthroughLoader; +#[cacheable_dyn] #[async_trait] impl Loader for PassthroughLoader { async fn run(&self, loader_context: &mut LoaderContext) -> Result<()> { @@ -82,7 +91,9 @@ impl Identifiable for PassthroughLoader { } pub const PASS_THROUGH_LOADER_IDENTIFIER: &str = "builtin:test-passthrough-loader"; +#[cacheable] pub struct NoPassthroughLoader; +#[cacheable_dyn] #[async_trait] impl Loader for NoPassthroughLoader { async fn run(&self, loader_context: &mut LoaderContext) -> Result<()> { diff --git a/crates/rspack_macros/src/cacheable_dyn.rs b/crates/rspack_macros/src/cacheable_dyn.rs index 81765e8c365..6e8b31ff1b1 100644 --- a/crates/rspack_macros/src/cacheable_dyn.rs +++ b/crates/rspack_macros/src/cacheable_dyn.rs @@ -139,6 +139,15 @@ pub fn impl_trait(mut input: ItemTrait) -> TokenStream { /// For impl block providing trait or associated items: `impl Trait /// for Data { ... }`. pub fn impl_impl(mut input: ItemImpl) -> TokenStream { + if input.generics.type_params().next().is_some() { + // not support for generics + input.items.push(parse_quote! { + fn __dyn_id(&self) -> u64 { + panic!("#[cacheable_dyn] not support for impl with generics"); + } + }); + return quote! { #input }.into(); + } let trait_ident = &input.trait_.as_ref().expect("should have trait ident").1; let trait_ident_str = trait_ident .segments diff --git a/crates/rspack_macros/src/runtime_module.rs b/crates/rspack_macros/src/runtime_module.rs index 2ad699f46af..33c7fd08bb9 100644 --- a/crates/rspack_macros/src/runtime_module.rs +++ b/crates/rspack_macros/src/runtime_module.rs @@ -19,12 +19,18 @@ pub fn impl_runtime_module( ); fields.named.push( syn::Field::parse_named - .parse2(quote! { pub custom_source: Option<::rspack_core::rspack_sources::BoxSource> }) + .parse2(quote! { + #[cacheable(with=rspack_cacheable::with::AsOption)] + pub custom_source: Option<::rspack_core::rspack_sources::BoxSource> + }) .expect("Failed to parse new field for custom_source"), ); fields.named.push( syn::Field::parse_named - .parse2(quote! { pub cached_generated_code: std::sync::RwLock> }) + .parse2(quote! { + #[cacheable(with=rspack_cacheable::with::Skip)] + pub cached_generated_code: std::sync::RwLock> + }) .expect("Failed to parse new field for cached_generated_code"), ); } @@ -47,6 +53,7 @@ pub fn impl_runtime_module( }; quote! { + #[rspack_cacheable::cacheable] #input impl #impl_generics #name #ty_generics #where_clause { @@ -109,6 +116,7 @@ pub fn impl_runtime_module( } } + #[rspack_cacheable::cacheable_dyn] impl #impl_generics ::rspack_core::Module for #name #ty_generics #where_clause { fn module_type(&self) -> &::rspack_core::ModuleType { &::rspack_core::ModuleType::Runtime diff --git a/crates/rspack_macros/src/source_map_config.rs b/crates/rspack_macros/src/source_map_config.rs index ddba652af73..97178dfb251 100644 --- a/crates/rspack_macros/src/source_map_config.rs +++ b/crates/rspack_macros/src/source_map_config.rs @@ -13,7 +13,9 @@ pub fn impl_source_map_config( if let syn::Fields::Named(ref mut fields) = input.fields { fields.named.push( syn::Field::parse_named - .parse2(quote! { pub source_map_kind: ::rspack_util::source_map::SourceMapKind }) + .parse2(quote! { + pub source_map_kind: ::rspack_util::source_map::SourceMapKind + }) .expect("Failed to parse new field for source_map_kind"), ); } diff --git a/crates/rspack_macros_test/Cargo.toml b/crates/rspack_macros_test/Cargo.toml index fea013dcca1..369d0e6e98a 100644 --- a/crates/rspack_macros_test/Cargo.toml +++ b/crates/rspack_macros_test/Cargo.toml @@ -7,6 +7,7 @@ name = "rspack_macros_test" repository = "https://github.com/web-infra-dev/rspack" version = "0.2.0" [dev-dependencies] +rspack_cacheable = { workspace = true } rspack_collections = { workspace = true } rspack_core = { workspace = true } rspack_error = { workspace = true } @@ -18,4 +19,4 @@ tracing = { workspace = true } trybuild = { version = "1.0.101", features = ["diff"] } [package.metadata.cargo-shear] -ignored = ["tracing"] +ignored = ["tracing", "rspack_cacheable"] diff --git a/crates/rspack_paths/Cargo.toml b/crates/rspack_paths/Cargo.toml index c1051803843..cf723ce264d 100644 --- a/crates/rspack_paths/Cargo.toml +++ b/crates/rspack_paths/Cargo.toml @@ -10,3 +10,4 @@ version = "0.2.0" [dependencies] camino = { workspace = true } +rspack_cacheable = { workspace = true } diff --git a/crates/rspack_paths/src/lib.rs b/crates/rspack_paths/src/lib.rs index 95996650f3e..cab1d96580e 100644 --- a/crates/rspack_paths/src/lib.rs +++ b/crates/rspack_paths/src/lib.rs @@ -6,6 +6,10 @@ use std::{ }; pub use camino::{Utf8Component, Utf8Components, Utf8Path, Utf8PathBuf, Utf8Prefix}; +use rspack_cacheable::{ + cacheable, + with::{AsRefStr, AsRefStrConverter}, +}; pub trait AssertUtf8 { type Output; @@ -42,6 +46,7 @@ impl<'a> AssertUtf8 for &'a Path { } } +#[cacheable(with=AsRefStr, hashable)] #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct ArcPath(Arc); @@ -89,6 +94,15 @@ impl Borrow for ArcPath { } } +impl AsRefStrConverter for ArcPath { + fn as_str(&self) -> &str { + self.0.to_str().expect("expect utf8 str") + } + fn from_str(s: &str) -> Self { + Self::from(Path::new(s)) + } +} + fn _assert_size() { use std::mem::size_of; assert_eq!(size_of::(), size_of::<[usize; 2]>()); diff --git a/crates/rspack_plugin_asset/Cargo.toml b/crates/rspack_plugin_asset/Cargo.toml index fc4785dc93a..a4a641a01cd 100644 --- a/crates/rspack_plugin_asset/Cargo.toml +++ b/crates/rspack_plugin_asset/Cargo.toml @@ -12,6 +12,7 @@ async-trait = { workspace = true } mime_guess = { workspace = true } rayon = { workspace = true } rspack_base64 = { workspace = true } +rspack_cacheable = { workspace = true } rspack_core = { workspace = true } rspack_error = { workspace = true } rspack_hash = { workspace = true } diff --git a/crates/rspack_plugin_asset/src/lib.rs b/crates/rspack_plugin_asset/src/lib.rs index 0de724877dd..1f5d1198e3f 100644 --- a/crates/rspack_plugin_asset/src/lib.rs +++ b/crates/rspack_plugin_asset/src/lib.rs @@ -4,6 +4,7 @@ use std::{borrow::Cow, hash::Hasher}; use async_trait::async_trait; use rayon::prelude::*; +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ rspack_sources::{BoxSource, RawBufferSource, RawStringSource, SourceExt}, AssetGeneratorDataUrl, AssetGeneratorDataUrlFnArgs, AssetInfo, AssetParserDataUrl, @@ -30,6 +31,7 @@ static ASSET_SOURCE_MODULE_SOURCE_TYPE_LIST: &[SourceType; 1] = &[SourceType::Ja const DEFAULT_ENCODING: &str = "base64"; +#[cacheable] #[derive(Debug)] enum DataUrlOptions { Inline(bool), @@ -42,6 +44,7 @@ type IsInline = bool; const ASSET_INLINE: bool = true; const ASSET_RESOURCE: bool = false; +#[cacheable] #[derive(Debug, Clone)] enum CanonicalizedDataUrlOption { Source, @@ -62,6 +65,7 @@ impl CanonicalizedDataUrlOption { } } +#[cacheable] #[derive(Debug)] pub struct AssetParserAndGenerator { emit: bool, @@ -281,6 +285,7 @@ impl AssetParserAndGenerator { // Webpack's default parser.dataUrlCondition.maxSize const DEFAULT_MAX_SIZE: f64 = 8096.0; +#[cacheable_dyn] impl ParserAndGenerator for AssetParserAndGenerator { fn source_types(&self) -> &[SourceType] { if let Some(config) = self.parsed_asset_config.as_ref() { diff --git a/crates/rspack_plugin_css/Cargo.toml b/crates/rspack_plugin_css/Cargo.toml index ce9b6f5c000..248d4493d1a 100644 --- a/crates/rspack_plugin_css/Cargo.toml +++ b/crates/rspack_plugin_css/Cargo.toml @@ -10,10 +10,11 @@ async-trait = { workspace = true } cow-utils = { workspace = true } css-module-lexer = { workspace = true } heck = { workspace = true } -indexmap = { version = "=1.9.3", features = ["serde-1"] } +indexmap = { workspace = true } once_cell = { workspace = true } rayon = { workspace = true } regex = { workspace = true } +rspack_cacheable = { workspace = true } rspack_collections = { workspace = true } rspack_core = { workspace = true } rspack_error = { workspace = true } diff --git a/crates/rspack_plugin_css/src/dependency/compose.rs b/crates/rspack_plugin_css/src/dependency/compose.rs index e4a8a603ff8..a85a1039616 100644 --- a/crates/rspack_plugin_css/src/dependency/compose.rs +++ b/crates/rspack_plugin_css/src/dependency/compose.rs @@ -1,13 +1,19 @@ +use rspack_cacheable::{ + cacheable, cacheable_dyn, + with::{AsPreset, AsVec}, +}; use rspack_core::{ AsContextDependency, AsDependencyTemplate, Dependency, DependencyCategory, DependencyId, DependencyRange, DependencyType, ExtendedReferencedExport, ModuleDependency, RuntimeSpec, }; use rspack_util::atom::Atom; +#[cacheable] #[derive(Debug, Clone)] pub struct CssComposeDependency { id: DependencyId, request: String, + #[cacheable(with=AsVec)] names: Vec, range: DependencyRange, } @@ -23,6 +29,7 @@ impl CssComposeDependency { } } +#[cacheable_dyn] impl Dependency for CssComposeDependency { fn id(&self) -> &DependencyId { &self.id @@ -57,6 +64,7 @@ impl Dependency for CssComposeDependency { } } +#[cacheable_dyn] impl ModuleDependency for CssComposeDependency { fn request(&self) -> &str { &self.request diff --git a/crates/rspack_plugin_css/src/dependency/export.rs b/crates/rspack_plugin_css/src/dependency/export.rs index ed84a4d5dd0..7572d81ba8c 100644 --- a/crates/rspack_plugin_css/src/dependency/export.rs +++ b/crates/rspack_plugin_css/src/dependency/export.rs @@ -1,9 +1,11 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyCategory, DependencyId, DependencyTemplate, DependencyType, ExportNameOrSpec, ExportSpec, ExportsOfExportsSpec, ExportsSpec, RuntimeSpec, TemplateContext, TemplateReplaceSource, }; +#[cacheable] #[derive(Debug, Clone)] pub struct CssExportDependency { id: DependencyId, @@ -19,6 +21,7 @@ impl CssExportDependency { } } +#[cacheable_dyn] impl Dependency for CssExportDependency { fn id(&self) -> &DependencyId { &self.id @@ -56,6 +59,7 @@ impl Dependency for CssExportDependency { } } +#[cacheable_dyn] impl DependencyTemplate for CssExportDependency { fn apply( &self, diff --git a/crates/rspack_plugin_css/src/dependency/import.rs b/crates/rspack_plugin_css/src/dependency/import.rs index 6e5e1d547ab..64b27d7d285 100644 --- a/crates/rspack_plugin_css/src/dependency/import.rs +++ b/crates/rspack_plugin_css/src/dependency/import.rs @@ -1,9 +1,11 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AsContextDependency, Compilation, Dependency, DependencyCategory, DependencyId, DependencyRange, DependencyTemplate, DependencyType, ModuleDependency, RuntimeSpec, TemplateContext, TemplateReplaceSource, }; +#[cacheable] #[derive(Debug, Clone)] pub struct CssImportDependency { id: DependencyId, @@ -21,6 +23,7 @@ impl CssImportDependency { } } +#[cacheable_dyn] impl Dependency for CssImportDependency { fn id(&self) -> &DependencyId { &self.id @@ -43,6 +46,7 @@ impl Dependency for CssImportDependency { } } +#[cacheable_dyn] impl ModuleDependency for CssImportDependency { fn request(&self) -> &str { &self.request @@ -57,6 +61,7 @@ impl ModuleDependency for CssImportDependency { } } +#[cacheable_dyn] impl DependencyTemplate for CssImportDependency { fn apply( &self, diff --git a/crates/rspack_plugin_css/src/dependency/local_ident.rs b/crates/rspack_plugin_css/src/dependency/local_ident.rs index cad6305e5e0..8fcd36c2f71 100644 --- a/crates/rspack_plugin_css/src/dependency/local_ident.rs +++ b/crates/rspack_plugin_css/src/dependency/local_ident.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyCategory, DependencyId, DependencyTemplate, DependencyType, ExportNameOrSpec, ExportSpec, @@ -7,6 +8,7 @@ use rspack_util::ext::DynHash; use crate::utils::escape_css; +#[cacheable] #[derive(Debug, Clone)] pub struct CssLocalIdentDependency { id: DependencyId, @@ -28,6 +30,7 @@ impl CssLocalIdentDependency { } } +#[cacheable_dyn] impl Dependency for CssLocalIdentDependency { fn id(&self) -> &DependencyId { &self.id @@ -65,6 +68,7 @@ impl Dependency for CssLocalIdentDependency { } } +#[cacheable_dyn] impl DependencyTemplate for CssLocalIdentDependency { fn apply( &self, diff --git a/crates/rspack_plugin_css/src/dependency/self_reference.rs b/crates/rspack_plugin_css/src/dependency/self_reference.rs index a6c9a8a4963..c4fbd96af4c 100644 --- a/crates/rspack_plugin_css/src/dependency/self_reference.rs +++ b/crates/rspack_plugin_css/src/dependency/self_reference.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AsContextDependency, Compilation, Dependency, DependencyCategory, DependencyId, DependencyTemplate, DependencyType, ExtendedReferencedExport, ModuleDependency, RuntimeSpec, @@ -7,6 +8,7 @@ use rspack_util::atom::Atom; use crate::utils::escape_css; +#[cacheable] #[derive(Debug, Clone)] pub struct CssSelfReferenceLocalIdentReplacement { pub local_ident: String, @@ -14,6 +16,7 @@ pub struct CssSelfReferenceLocalIdentReplacement { pub end: u32, } +#[cacheable] #[derive(Debug, Clone)] pub struct CssSelfReferenceLocalIdentDependency { id: DependencyId, @@ -31,6 +34,7 @@ impl CssSelfReferenceLocalIdentDependency { } } +#[cacheable_dyn] impl Dependency for CssSelfReferenceLocalIdentDependency { fn id(&self) -> &DependencyId { &self.id @@ -65,12 +69,14 @@ impl Dependency for CssSelfReferenceLocalIdentDependency { } } +#[cacheable_dyn] impl ModuleDependency for CssSelfReferenceLocalIdentDependency { fn request(&self) -> &str { "self" } } +#[cacheable_dyn] impl DependencyTemplate for CssSelfReferenceLocalIdentDependency { fn apply( &self, diff --git a/crates/rspack_plugin_css/src/dependency/url.rs b/crates/rspack_plugin_css/src/dependency/url.rs index 72b23344db5..f5f3d6f639f 100644 --- a/crates/rspack_plugin_css/src/dependency/url.rs +++ b/crates/rspack_plugin_css/src/dependency/url.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AsContextDependency, CodeGenerationDataFilename, CodeGenerationDataUrl, Compilation, Dependency, DependencyCategory, DependencyId, DependencyRange, DependencyTemplate, DependencyType, @@ -7,6 +8,7 @@ use rspack_core::{ use crate::utils::{css_escape_string, AUTO_PUBLIC_PATH_PLACEHOLDER}; +#[cacheable] #[derive(Debug, Clone)] pub struct CssUrlDependency { id: DependencyId, @@ -51,6 +53,7 @@ impl CssUrlDependency { } } +#[cacheable_dyn] impl Dependency for CssUrlDependency { fn id(&self) -> &DependencyId { &self.id @@ -73,6 +76,7 @@ impl Dependency for CssUrlDependency { } } +#[cacheable_dyn] impl ModuleDependency for CssUrlDependency { fn request(&self) -> &str { &self.request @@ -87,6 +91,7 @@ impl ModuleDependency for CssUrlDependency { } } +#[cacheable_dyn] impl DependencyTemplate for CssUrlDependency { fn apply( &self, diff --git a/crates/rspack_plugin_css/src/parser_and_generator/mod.rs b/crates/rspack_plugin_css/src/parser_and_generator/mod.rs index 2a6f2e5e263..653aaeff9be 100644 --- a/crates/rspack_plugin_css/src/parser_and_generator/mod.rs +++ b/crates/rspack_plugin_css/src/parser_and_generator/mod.rs @@ -6,6 +6,10 @@ use std::{ use indexmap::{IndexMap, IndexSet}; use once_cell::sync::OnceCell; use regex::Regex; +use rspack_cacheable::{ + cacheable, cacheable_dyn, + with::{AsCacheable, AsMap, AsOption, AsVec}, +}; use rspack_core::{ diagnostics::map_box_diagnostics_to_module_parse_diagnostics, rspack_sources::{BoxSource, ConcatSource, RawStringSource, ReplaceSource, Source, SourceExt}, @@ -53,6 +57,7 @@ pub(crate) static CSS_MODULE_SOURCE_TYPE_LIST: &[SourceType; 2] = pub(crate) static CSS_MODULE_EXPORTS_ONLY_SOURCE_TYPE_LIST: &[SourceType; 1] = &[SourceType::JavaScript]; +#[cacheable] #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct CssExport { pub ident: String, @@ -72,6 +77,7 @@ fn update_css_exports(exports: &mut CssExports, name: String, css_export: CssExp } } +#[cacheable] #[derive(Debug)] pub struct CssParserAndGenerator { pub convention: Option, @@ -79,10 +85,12 @@ pub struct CssParserAndGenerator { pub exports_only: bool, pub named_exports: bool, pub es_module: bool, + #[cacheable(with=AsOption>)] pub exports: Option, pub hot: bool, } +#[cacheable_dyn] impl ParserAndGenerator for CssParserAndGenerator { fn source_types(&self) -> &[SourceType] { if self.exports_only { diff --git a/crates/rspack_plugin_dll/Cargo.toml b/crates/rspack_plugin_dll/Cargo.toml index 41c73820887..1132a215695 100644 --- a/crates/rspack_plugin_dll/Cargo.toml +++ b/crates/rspack_plugin_dll/Cargo.toml @@ -8,6 +8,7 @@ repository = "https://github.com/web-infra-dev/rspack" version = "0.2.0" [dependencies] +rspack_cacheable = { workspace = true } rspack_collections = { workspace = true } rspack_core = { workspace = true } rspack_error = { workspace = true } diff --git a/crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs b/crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs index 157c1a9546e..8be2b7b0b59 100644 --- a/crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs +++ b/crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AffectType, AsContextDependency, AsDependencyTemplate, Context, Dependency, DependencyId, DependencyType, ModuleDependency, @@ -5,6 +6,7 @@ use rspack_core::{ use crate::DllEntryPluginOptions; +#[cacheable] #[derive(Debug, Clone)] pub struct DllEntryDependency { pub context: Context, @@ -36,12 +38,14 @@ impl DllEntryDependency { // It would not create module by rspack,if dependency is not ModuleDependency. // So we impl ModuleDependency for [DllEntryDependency] +#[cacheable_dyn] impl ModuleDependency for DllEntryDependency { fn request(&self) -> &str { "dll main" } } +#[cacheable_dyn] impl Dependency for DllEntryDependency { fn id(&self) -> &DependencyId { &self.id diff --git a/crates/rspack_plugin_dll/src/dll_entry/dll_module.rs b/crates/rspack_plugin_dll/src/dll_entry/dll_module.rs index 19799238a44..0509c3c9972 100644 --- a/crates/rspack_plugin_dll/src/dll_entry/dll_module.rs +++ b/crates/rspack_plugin_dll/src/dll_entry/dll_module.rs @@ -1,6 +1,7 @@ use std::{borrow::Cow, hash::Hash, sync::Arc}; use async_trait::async_trait; +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_collections::{Identifiable, Identifier}; use rspack_core::{ impl_module_meta_info, impl_source_map_config, module_update_hash, @@ -14,6 +15,7 @@ use rspack_error::{impl_empty_diagnosable_trait, Diagnostic, Result}; use super::dll_entry_dependency::DllEntryDependency; #[impl_source_map_config] +#[cacheable] #[derive(Debug, Default)] pub struct DllModule { // TODO: it should be set to EntryDependency.loc @@ -52,6 +54,7 @@ impl DllModule { } } +#[cacheable_dyn] #[async_trait] impl Module for DllModule { impl_module_meta_info!(); diff --git a/crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs b/crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs index 09e730bdb6a..8f7786d204a 100644 --- a/crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs +++ b/crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs @@ -1,6 +1,7 @@ use std::{borrow::Cow, hash::Hash, sync::Arc}; use async_trait::async_trait; +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_collections::{Identifiable, Identifier}; use rspack_core::{ impl_module_meta_info, impl_source_map_config, module_raw, module_update_hash, @@ -19,6 +20,7 @@ use crate::{DllManifestContentItem, DllManifestContentItemExports}; pub type SourceRequest = String; #[impl_source_map_config] +#[cacheable] #[derive(Debug, Default)] pub struct DelegatedModule { source_request: SourceRequest, @@ -54,6 +56,7 @@ impl DelegatedModule { } } +#[cacheable_dyn] #[async_trait] impl Module for DelegatedModule { impl_module_meta_info!(); diff --git a/crates/rspack_plugin_dll/src/dll_reference/delegated_source_dependency.rs b/crates/rspack_plugin_dll/src/dll_reference/delegated_source_dependency.rs index 3954dcf64d4..5d7ca4730d6 100644 --- a/crates/rspack_plugin_dll/src/dll_reference/delegated_source_dependency.rs +++ b/crates/rspack_plugin_dll/src/dll_reference/delegated_source_dependency.rs @@ -1,8 +1,10 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AffectType, AsContextDependency, AsDependencyTemplate, Dependency, DependencyCategory, DependencyId, DependencyType, ModuleDependency, }; +#[cacheable] #[derive(Debug, Clone)] pub struct DelegatedSourceDependency { id: DependencyId, @@ -18,6 +20,7 @@ impl DelegatedSourceDependency { } } +#[cacheable_dyn] impl Dependency for DelegatedSourceDependency { fn id(&self) -> &DependencyId { &self.id @@ -36,6 +39,7 @@ impl Dependency for DelegatedSourceDependency { } } +#[cacheable_dyn] impl ModuleDependency for DelegatedSourceDependency { fn request(&self) -> &str { &self.request diff --git a/crates/rspack_plugin_dll/src/lib.rs b/crates/rspack_plugin_dll/src/lib.rs index 71c146ce3fb..8319ac70209 100644 --- a/crates/rspack_plugin_dll/src/lib.rs +++ b/crates/rspack_plugin_dll/src/lib.rs @@ -1,3 +1,7 @@ +use rspack_cacheable::{ + cacheable, + with::{AsPreset, AsVec}, +}; use rspack_core::{BuildMeta, LibraryType}; use rspack_util::atom::Atom; use rustc_hash::FxHashMap as HashMap; @@ -10,11 +14,12 @@ mod lib_manifest_plugin; pub type DllManifestContent = HashMap; +#[cacheable] #[derive(Debug, Default, Clone)] pub enum DllManifestContentItemExports { #[default] True, - Vec(Vec), + Vec(#[cacheable(with=AsVec)] Vec), } impl Serialize for DllManifestContentItemExports { @@ -35,6 +40,7 @@ impl Serialize for DllManifestContentItemExports { } } +#[cacheable] #[derive(Debug, Default, Clone, Serialize)] #[serde(rename_all = "camelCase")] pub struct DllManifestContentItem { diff --git a/crates/rspack_plugin_extract_css/Cargo.toml b/crates/rspack_plugin_extract_css/Cargo.toml index 46efca926f3..fe2a3eac70f 100644 --- a/crates/rspack_plugin_extract_css/Cargo.toml +++ b/crates/rspack_plugin_extract_css/Cargo.toml @@ -9,6 +9,7 @@ version = "0.2.0" async-trait = { workspace = true } cow-utils = { workspace = true } regex = { workspace = true } +rspack_cacheable = { workspace = true } rspack_collections = { workspace = true } rspack_core = { workspace = true } rspack_error = { workspace = true } diff --git a/crates/rspack_plugin_extract_css/src/css_dependency.rs b/crates/rspack_plugin_extract_css/src/css_dependency.rs index 0f33b5afa9e..3512d87f45f 100644 --- a/crates/rspack_plugin_extract_css/src/css_dependency.rs +++ b/crates/rspack_plugin_extract_css/src/css_dependency.rs @@ -1,13 +1,13 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_collections::IdentifierSet; use rspack_core::{ AffectType, AsContextDependency, AsDependencyTemplate, ConnectionState, Dependency, - DependencyCategory, DependencyId, DependencyRange, ModuleDependency, ModuleGraph, + DependencyCategory, DependencyId, DependencyRange, DependencyType, ModuleDependency, ModuleGraph, }; use rspack_paths::ArcPath; use rustc_hash::FxHashSet; -use crate::css_module::DEPENDENCY_TYPE; - +#[cacheable] #[derive(Debug, Clone)] pub struct CssDependency { pub(crate) id: DependencyId, @@ -77,6 +77,7 @@ impl CssDependency { impl AsDependencyTemplate for CssDependency {} impl AsContextDependency for CssDependency {} +#[cacheable_dyn] impl Dependency for CssDependency { fn resource_identifier(&self) -> Option<&str> { Some(&self.resource_identifier) @@ -87,7 +88,7 @@ impl Dependency for CssDependency { } fn dependency_type(&self) -> &rspack_core::DependencyType { - &DEPENDENCY_TYPE + &DependencyType::ExtractCSS } fn category(&self) -> &DependencyCategory { @@ -120,6 +121,7 @@ impl Dependency for CssDependency { } } +#[cacheable_dyn] impl ModuleDependency for CssDependency { fn request(&self) -> &str { &self.identifier diff --git a/crates/rspack_plugin_extract_css/src/css_module.rs b/crates/rspack_plugin_extract_css/src/css_module.rs index 709b01f1a5a..88a4994acbf 100644 --- a/crates/rspack_plugin_extract_css/src/css_module.rs +++ b/crates/rspack_plugin_extract_css/src/css_module.rs @@ -1,14 +1,14 @@ use std::hash::Hash; -use std::sync::LazyLock; +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_collections::{Identifiable, Identifier}; use rspack_core::rspack_sources::Source; use rspack_core::{ impl_module_meta_info, impl_source_map_config, module_update_hash, AsyncDependenciesBlockIdentifier, BuildContext, BuildInfo, BuildMeta, BuildResult, CodeGenerationResult, Compilation, CompilerOptions, ConcatenationScope, DependenciesBlock, - DependencyId, DependencyType, FactoryMeta, Module, ModuleFactory, ModuleFactoryCreateData, - ModuleFactoryResult, RuntimeSpec, SourceType, + DependencyId, FactoryMeta, Module, ModuleFactory, ModuleFactoryCreateData, ModuleFactoryResult, + RuntimeSpec, SourceType, }; use rspack_error::Result; use rspack_error::{impl_empty_diagnosable_trait, Diagnostic}; @@ -21,10 +21,8 @@ use rustc_hash::FxHashSet; use crate::css_dependency::CssDependency; use crate::plugin::{MODULE_TYPE, SOURCE_TYPE}; -pub(crate) static DEPENDENCY_TYPE: LazyLock = - LazyLock::new(|| DependencyType::Custom("mini-extract-dep")); - #[impl_source_map_config] +#[cacheable] #[derive(Debug)] pub(crate) struct CssModule { pub(crate) identifier: String, @@ -102,6 +100,7 @@ impl CssModule { } } +#[cacheable_dyn] #[async_trait::async_trait] impl Module for CssModule { impl_module_meta_info!(); diff --git a/crates/rspack_plugin_extract_css/src/plugin.rs b/crates/rspack_plugin_extract_css/src/plugin.rs index 4290f9e2297..0770ab305dd 100644 --- a/crates/rspack_plugin_extract_css/src/plugin.rs +++ b/crates/rspack_plugin_extract_css/src/plugin.rs @@ -3,6 +3,7 @@ use std::{borrow::Cow, cmp::max, hash::Hash, sync::Arc}; use cow_utils::CowUtils; use regex::Regex; +use rspack_cacheable::cacheable; use rspack_collections::{DatabaseItem, IdentifierMap, IdentifierSet, UkeySet}; use rspack_core::rspack_sources::{BoxSource, CachedSource, SourceExt}; use rspack_core::{ @@ -11,9 +12,9 @@ use rspack_core::{ }, ApplyContext, Chunk, ChunkGroupUkey, ChunkKind, ChunkUkey, Compilation, CompilationContentHash, CompilationParams, CompilationRenderManifest, CompilationRuntimeRequirementInTree, - CompilerCompilation, CompilerOptions, Filename, Module, ModuleGraph, ModuleIdentifier, - ModuleType, NormalModuleFactoryParser, ParserAndGenerator, ParserOptions, PathData, Plugin, - PluginContext, RenderManifestEntry, RuntimeGlobals, SourceType, + CompilerCompilation, CompilerOptions, DependencyType, Filename, Module, ModuleGraph, + ModuleIdentifier, ModuleType, NormalModuleFactoryParser, ParserAndGenerator, ParserOptions, + PathData, Plugin, PluginContext, RenderManifestEntry, RuntimeGlobals, SourceType, }; use rspack_core::{AssetInfo, ChunkGraph}; use rspack_error::{Diagnostic, Result}; @@ -27,7 +28,7 @@ use rustc_hash::FxHashMap; use ustr::Ustr; use crate::{ - css_module::{CssModule, CssModuleFactory, DEPENDENCY_TYPE}, + css_module::{CssModule, CssModuleFactory}, parser_plugin::PluginCssExtractParserPlugin, runtime::CssLoadingRuntimeModule, }; @@ -103,6 +104,7 @@ pub struct CssExtractOptions { // } // } +#[cacheable] #[derive(Debug, PartialEq, Eq, Clone)] pub enum InsertType { Fn(String), @@ -472,7 +474,7 @@ async fn compilation( compilation: &mut Compilation, _params: &mut CompilationParams, ) -> Result<()> { - compilation.set_dependency_factory(*DEPENDENCY_TYPE, Arc::new(CssModuleFactory)); + compilation.set_dependency_factory(DependencyType::ExtractCSS, Arc::new(CssModuleFactory)); Ok(()) } diff --git a/crates/rspack_plugin_hmr/Cargo.toml b/crates/rspack_plugin_hmr/Cargo.toml index 4367e40a703..bcd8303bca0 100644 --- a/crates/rspack_plugin_hmr/Cargo.toml +++ b/crates/rspack_plugin_hmr/Cargo.toml @@ -7,6 +7,7 @@ repository = "https://github.com/web-infra-dev/rspack" version = "0.2.0" [dependencies] +rspack_cacheable = { workspace = true } rspack_collections = { workspace = true } rspack_core = { workspace = true } rspack_error = { workspace = true } @@ -22,4 +23,4 @@ serde_json = { workspace = true } tracing = { workspace = true } [package.metadata.cargo-shear] -ignored = ["tracing"] +ignored = ["tracing", "rspack_cacheable"] diff --git a/crates/rspack_plugin_javascript/Cargo.toml b/crates/rspack_plugin_javascript/Cargo.toml index a5812fb4431..5a7a245db5d 100644 --- a/crates/rspack_plugin_javascript/Cargo.toml +++ b/crates/rspack_plugin_javascript/Cargo.toml @@ -23,6 +23,7 @@ rayon = { workspace = true } regex = { workspace = true } ropey = { workspace = true } rspack_ast = { workspace = true } +rspack_cacheable = { workspace = true } rspack_collections = { workspace = true } rspack_core = { workspace = true } rspack_error = { workspace = true } diff --git a/crates/rspack_plugin_javascript/src/dependency/amd/amd_define_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/amd/amd_define_dependency.rs index 41b08089f47..6e5caf0b5b2 100644 --- a/crates/rspack_plugin_javascript/src/dependency/amd/amd_define_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/amd/amd_define_dependency.rs @@ -1,4 +1,8 @@ use bitflags::bitflags; +use rspack_cacheable::{ + cacheable, cacheable_dyn, + with::{AsOption, AsPreset}, +}; use rspack_core::{ AffectType, AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyCategory, DependencyId, DependencyTemplate, DependencyType, RuntimeGlobals, RuntimeSpec, TemplateContext, @@ -158,6 +162,7 @@ impl Branch { } } +#[cacheable] #[derive(Debug, Clone)] pub struct AmdDefineDependency { id: DependencyId, @@ -165,6 +170,7 @@ pub struct AmdDefineDependency { array_range: Option<(u32, u32)>, function_range: Option<(u32, u32)>, object_range: Option<(u32, u32)>, + #[cacheable(with=AsOption)] named_module: Option, local_module: Option, } @@ -194,6 +200,7 @@ impl AmdDefineDependency { } } +#[cacheable_dyn] impl Dependency for AmdDefineDependency { fn id(&self) -> &DependencyId { &self.id @@ -241,6 +248,7 @@ impl AmdDefineDependency { } } +#[cacheable_dyn] impl DependencyTemplate for AmdDefineDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/amd/amd_require_array_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/amd/amd_require_array_dependency.rs index 4772ebaadc3..495d91f40a4 100644 --- a/crates/rspack_plugin_javascript/src/dependency/amd/amd_require_array_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/amd/amd_require_array_dependency.rs @@ -1,4 +1,5 @@ use itertools::Itertools; +use rspack_cacheable::{cacheable, cacheable_dyn, with::AsPreset}; use rspack_core::{ module_raw, AffectType, AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyCategory, DependencyId, DependencyTemplate, DependencyType, ModuleDependency, @@ -11,13 +12,15 @@ use super::{ local_module_dependency::LocalModuleDependency, }; +#[cacheable] #[derive(Debug, Clone)] pub enum AmdDep { - String(Atom), + String(#[cacheable(with=AsPreset)] Atom), LocalModuleDependency(LocalModuleDependency), AMDRequireItemDependency(AMDRequireItemDependency), } +#[cacheable] #[derive(Debug, Clone)] pub struct AmdRequireArrayDependency { id: DependencyId, @@ -35,6 +38,7 @@ impl AmdRequireArrayDependency { } } +#[cacheable_dyn] impl Dependency for AmdRequireArrayDependency { fn id(&self) -> &DependencyId { &self.id @@ -83,6 +87,7 @@ impl AmdRequireArrayDependency { } } +#[cacheable_dyn] impl DependencyTemplate for AmdRequireArrayDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/amd/amd_require_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/amd/amd_require_dependency.rs index fb121e64c95..c2a224a8f4a 100644 --- a/crates/rspack_plugin_javascript/src/dependency/amd/amd_require_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/amd/amd_require_dependency.rs @@ -1,9 +1,11 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ block_promise, AffectType, AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyCategory, DependencyId, DependencyTemplate, DependencyType, RuntimeGlobals, RuntimeSpec, }; +#[cacheable] #[derive(Debug, Clone)] pub struct AMDRequireDependency { id: DependencyId, @@ -37,6 +39,7 @@ impl AMDRequireDependency { } } +#[cacheable_dyn] impl Dependency for AMDRequireDependency { fn id(&self) -> &DependencyId { &self.id @@ -55,6 +58,7 @@ impl Dependency for AMDRequireDependency { } } +#[cacheable_dyn] impl DependencyTemplate for AMDRequireDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/amd/amd_require_item_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/amd/amd_require_item_dependency.rs index b30461cb707..4db07f584d7 100644 --- a/crates/rspack_plugin_javascript/src/dependency/amd/amd_require_item_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/amd/amd_require_item_dependency.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn, with::AsPreset}; use rspack_core::{ module_raw, AffectType, AsContextDependency, Compilation, Dependency, DependencyCategory, DependencyId, DependencyTemplate, DependencyType, ModuleDependency, RuntimeSpec, TemplateContext, @@ -5,9 +6,11 @@ use rspack_core::{ }; use rspack_util::atom::Atom; +#[cacheable] #[derive(Debug, Clone)] pub struct AMDRequireItemDependency { id: DependencyId, + #[cacheable(with=AsPreset)] request: Atom, range: (u32, u32), optional: bool, @@ -28,6 +31,7 @@ impl AMDRequireItemDependency { } } +#[cacheable_dyn] impl Dependency for AMDRequireItemDependency { fn id(&self) -> &DependencyId { &self.id @@ -46,6 +50,7 @@ impl Dependency for AMDRequireItemDependency { } } +#[cacheable_dyn] impl DependencyTemplate for AMDRequireItemDependency { fn apply( &self, @@ -76,6 +81,7 @@ impl DependencyTemplate for AMDRequireItemDependency { } } +#[cacheable_dyn] impl ModuleDependency for AMDRequireItemDependency { fn request(&self) -> &str { &self.request diff --git a/crates/rspack_plugin_javascript/src/dependency/amd/local_module.rs b/crates/rspack_plugin_javascript/src/dependency/amd/local_module.rs index f531d94b7fc..e0a516d340c 100644 --- a/crates/rspack_plugin_javascript/src/dependency/amd/local_module.rs +++ b/crates/rspack_plugin_javascript/src/dependency/amd/local_module.rs @@ -1,7 +1,10 @@ +use rspack_cacheable::{cacheable, with::AsPreset}; use rspack_util::atom::Atom; +#[cacheable] #[derive(Debug, Clone)] pub struct LocalModule { + #[cacheable(with=AsPreset)] name: Atom, idx: usize, used: bool, diff --git a/crates/rspack_plugin_javascript/src/dependency/amd/local_module_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/amd/local_module_dependency.rs index 319b2740653..f45297b7aa2 100644 --- a/crates/rspack_plugin_javascript/src/dependency/amd/local_module_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/amd/local_module_dependency.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AffectType, AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyId, DependencyTemplate, RuntimeSpec, TemplateContext, TemplateReplaceSource, @@ -5,6 +6,7 @@ use rspack_core::{ use super::local_module::LocalModule; +#[cacheable] #[derive(Debug, Clone)] pub struct LocalModuleDependency { id: DependencyId, @@ -28,6 +30,7 @@ impl LocalModuleDependency { } } +#[cacheable_dyn] impl Dependency for LocalModuleDependency { fn id(&self) -> &DependencyId { &self.id @@ -38,6 +41,7 @@ impl Dependency for LocalModuleDependency { } } +#[cacheable_dyn] impl DependencyTemplate for LocalModuleDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/amd/unsupported_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/amd/unsupported_dependency.rs index 21eb584e2d0..a93f47a9411 100644 --- a/crates/rspack_plugin_javascript/src/dependency/amd/unsupported_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/amd/unsupported_dependency.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn, with::AsPreset}; use rspack_core::{ AffectType, AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyCategory, DependencyId, DependencyTemplate, DependencyType, RuntimeSpec, TemplateContext, @@ -5,9 +6,11 @@ use rspack_core::{ }; use rspack_util::atom::Atom; +#[cacheable] #[derive(Debug, Clone)] pub struct UnsupportedDependency { id: DependencyId, + #[cacheable(with=AsPreset)] request: Atom, range: (u32, u32), } @@ -22,6 +25,7 @@ impl UnsupportedDependency { } } +#[cacheable_dyn] impl Dependency for UnsupportedDependency { fn id(&self) -> &DependencyId { &self.id @@ -40,6 +44,7 @@ impl Dependency for UnsupportedDependency { } } +#[cacheable_dyn] impl DependencyTemplate for UnsupportedDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_export_require_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_export_require_dependency.rs index a6628576ade..757896632e5 100644 --- a/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_export_require_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_export_require_dependency.rs @@ -1,4 +1,8 @@ use itertools::Itertools; +use rspack_cacheable::{ + cacheable, cacheable_dyn, + with::{AsPreset, AsVec}, +}; use rspack_core::{ module_raw, process_export_info, property_access, AsContextDependency, Compilation, Dependency, DependencyCategory, DependencyId, DependencyRange, DependencyTemplate, DependencyType, @@ -12,6 +16,7 @@ use swc_core::atoms::Atom; use super::ExportsBase; +#[cacheable] #[allow(unused)] #[derive(Debug, Clone)] pub struct CommonJsExportRequireDependency { @@ -20,7 +25,9 @@ pub struct CommonJsExportRequireDependency { optional: bool, range: DependencyRange, base: ExportsBase, + #[cacheable(with=AsVec)] names: Vec, + #[cacheable(with=AsVec)] ids: Vec, result_used: bool, } @@ -159,6 +166,7 @@ impl CommonJsExportRequireDependency { } } +#[cacheable_dyn] impl Dependency for CommonJsExportRequireDependency { fn id(&self) -> &DependencyId { &self.id @@ -338,6 +346,7 @@ impl Dependency for CommonJsExportRequireDependency { } } +#[cacheable_dyn] impl DependencyTemplate for CommonJsExportRequireDependency { fn apply( &self, @@ -443,6 +452,7 @@ impl DependencyTemplate for CommonJsExportRequireDependency { } } +#[cacheable_dyn] impl ModuleDependency for CommonJsExportRequireDependency { fn request(&self) -> &str { &self.request diff --git a/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_exports_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_exports_dependency.rs index 95cdc498ee8..0f8ffab8ea3 100644 --- a/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_exports_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_exports_dependency.rs @@ -1,3 +1,7 @@ +use rspack_cacheable::{ + cacheable, cacheable_dyn, + with::{AsPreset, AsVec}, +}; use rspack_core::{ property_access, AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyCategory, DependencyId, DependencyTemplate, DependencyType, ExportNameOrSpec, @@ -7,6 +11,7 @@ use rspack_core::{ }; use swc_core::atoms::Atom; +#[cacheable] #[derive(Debug, Clone, Copy)] pub enum ExportsBase { Exports, @@ -45,12 +50,14 @@ impl ExportsBase { } } +#[cacheable] #[derive(Debug, Clone)] pub struct CommonJsExportsDependency { id: DependencyId, range: (u32, u32), value_range: Option<(u32, u32)>, base: ExportsBase, + #[cacheable(with=AsVec)] names: Vec, } @@ -71,6 +78,7 @@ impl CommonJsExportsDependency { } } +#[cacheable_dyn] impl Dependency for CommonJsExportsDependency { fn id(&self) -> &DependencyId { &self.id @@ -103,6 +111,7 @@ impl Dependency for CommonJsExportsDependency { impl AsModuleDependency for CommonJsExportsDependency {} +#[cacheable_dyn] impl DependencyTemplate for CommonJsExportsDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_full_require_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_full_require_dependency.rs index 1b750ce8c7a..18e529bf342 100644 --- a/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_full_require_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_full_require_dependency.rs @@ -1,3 +1,7 @@ +use rspack_cacheable::{ + cacheable, cacheable_dyn, + with::{AsPreset, AsVec, Skip}, +}; use rspack_core::{ module_id, property_access, to_normal_comment, Compilation, DependencyLocation, DependencyRange, ExportsType, ExtendedReferencedExport, ModuleGraph, RuntimeGlobals, RuntimeSpec, SharedSourceMap, @@ -9,15 +13,18 @@ use rspack_core::{DependencyType, ModuleDependency}; use rspack_core::{TemplateContext, TemplateReplaceSource}; use swc_core::atoms::Atom; +#[cacheable] #[derive(Debug, Clone)] pub struct CommonJsFullRequireDependency { id: DependencyId, request: String, + #[cacheable(with=AsVec)] names: Vec, range: DependencyRange, is_call: bool, optional: bool, asi_safe: bool, + #[cacheable(with=Skip)] source_map: Option, } @@ -44,6 +51,7 @@ impl CommonJsFullRequireDependency { } } +#[cacheable_dyn] impl Dependency for CommonJsFullRequireDependency { fn id(&self) -> &DependencyId { &self.id @@ -93,6 +101,7 @@ impl Dependency for CommonJsFullRequireDependency { } } +#[cacheable_dyn] impl ModuleDependency for CommonJsFullRequireDependency { fn request(&self) -> &str { &self.request @@ -111,6 +120,7 @@ impl ModuleDependency for CommonJsFullRequireDependency { } } +#[cacheable_dyn] impl DependencyTemplate for CommonJsFullRequireDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_require_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_require_dependency.rs index 1ac83e1788c..c1f1c6760b0 100644 --- a/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_require_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_require_dependency.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn, with::Skip}; use rspack_core::{ module_id, Compilation, DependencyLocation, DependencyRange, RuntimeSpec, SharedSourceMap, }; @@ -6,6 +7,7 @@ use rspack_core::{DependencyId, DependencyTemplate}; use rspack_core::{DependencyType, ModuleDependency}; use rspack_core::{TemplateContext, TemplateReplaceSource}; +#[cacheable] #[derive(Debug, Clone)] pub struct CommonJsRequireDependency { id: DependencyId, @@ -13,6 +15,7 @@ pub struct CommonJsRequireDependency { optional: bool, range: DependencyRange, range_expr: Option, + #[cacheable(with=Skip)] source_map: Option, } @@ -35,6 +38,7 @@ impl CommonJsRequireDependency { } } +#[cacheable_dyn] impl Dependency for CommonJsRequireDependency { fn id(&self) -> &DependencyId { &self.id @@ -61,6 +65,7 @@ impl Dependency for CommonJsRequireDependency { } } +#[cacheable_dyn] impl ModuleDependency for CommonJsRequireDependency { fn request(&self) -> &str { &self.request @@ -79,6 +84,7 @@ impl ModuleDependency for CommonJsRequireDependency { } } +#[cacheable_dyn] impl DependencyTemplate for CommonJsRequireDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_self_reference_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_self_reference_dependency.rs index aba898321e9..01d3edbb858 100644 --- a/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_self_reference_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_self_reference_dependency.rs @@ -1,3 +1,7 @@ +use rspack_cacheable::{ + cacheable, cacheable_dyn, + with::{AsPreset, AsVec}, +}; use rspack_core::{ property_access, AsContextDependency, Compilation, Dependency, DependencyCategory, DependencyId, DependencyTemplate, DependencyType, ExtendedReferencedExport, ModuleDependency, ModuleGraph, @@ -7,11 +11,13 @@ use swc_core::atoms::Atom; use super::ExportsBase; +#[cacheable] #[derive(Debug, Clone)] pub struct CommonJsSelfReferenceDependency { id: DependencyId, range: (u32, u32), base: ExportsBase, + #[cacheable(with=AsVec)] names: Vec, is_call: bool, } @@ -28,6 +34,7 @@ impl CommonJsSelfReferenceDependency { } } +#[cacheable_dyn] impl Dependency for CommonJsSelfReferenceDependency { fn id(&self) -> &DependencyId { &self.id @@ -68,6 +75,7 @@ impl Dependency for CommonJsSelfReferenceDependency { } } +#[cacheable_dyn] impl ModuleDependency for CommonJsSelfReferenceDependency { fn request(&self) -> &str { "self" @@ -76,6 +84,7 @@ impl ModuleDependency for CommonJsSelfReferenceDependency { impl AsContextDependency for CommonJsSelfReferenceDependency {} +#[cacheable_dyn] impl DependencyTemplate for CommonJsSelfReferenceDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/commonjs/module_decorator_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/commonjs/module_decorator_dependency.rs index 6725d26ca00..61e7c489d1c 100644 --- a/crates/rspack_plugin_javascript/src/dependency/commonjs/module_decorator_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/commonjs/module_decorator_dependency.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ create_exports_object_referenced, create_no_exports_referenced, AsContextDependency, ChunkGraph, Compilation, Dependency, DependencyId, DependencyTemplate, DependencyType, InitFragmentKey, @@ -6,6 +7,7 @@ use rspack_core::{ }; use rspack_util::ext::DynHash; +#[cacheable] #[derive(Debug, Clone)] pub struct ModuleDecoratorDependency { decorator: RuntimeGlobals, @@ -23,12 +25,14 @@ impl ModuleDecoratorDependency { } } +#[cacheable_dyn] impl ModuleDependency for ModuleDecoratorDependency { fn request(&self) -> &str { "self" } } +#[cacheable_dyn] impl DependencyTemplate for ModuleDecoratorDependency { fn apply( &self, @@ -90,6 +94,7 @@ impl DependencyTemplate for ModuleDecoratorDependency { impl AsContextDependency for ModuleDecoratorDependency {} +#[cacheable_dyn] impl Dependency for ModuleDecoratorDependency { fn id(&self) -> &DependencyId { &self.id diff --git a/crates/rspack_plugin_javascript/src/dependency/commonjs/require_ensure_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/commonjs/require_ensure_dependency.rs index 71c3673873b..0a9a2ec5b1b 100644 --- a/crates/rspack_plugin_javascript/src/dependency/commonjs/require_ensure_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/commonjs/require_ensure_dependency.rs @@ -1,9 +1,11 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ block_promise, AffectType, AsContextDependency, AsModuleDependency, Dependency, DependencyCategory, DependencyId, DependencyRange, DependencyTemplate, DependencyType, RuntimeGlobals, }; +#[cacheable] #[derive(Debug, Clone)] pub struct RequireEnsureDependency { id: DependencyId, @@ -27,6 +29,7 @@ impl RequireEnsureDependency { } } +#[cacheable_dyn] impl Dependency for RequireEnsureDependency { fn id(&self) -> &rspack_core::DependencyId { &self.id @@ -51,6 +54,7 @@ impl Dependency for RequireEnsureDependency { impl AsModuleDependency for RequireEnsureDependency {} +#[cacheable_dyn] impl DependencyTemplate for RequireEnsureDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/commonjs/require_ensure_item_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/commonjs/require_ensure_item_dependency.rs index 64cf780f82c..3df215b14da 100644 --- a/crates/rspack_plugin_javascript/src/dependency/commonjs/require_ensure_item_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/commonjs/require_ensure_item_dependency.rs @@ -1,12 +1,15 @@ +use rspack_cacheable::{cacheable, cacheable_dyn, with::AsPreset}; use rspack_core::{ AffectType, AsContextDependency, AsDependencyTemplate, Dependency, DependencyCategory, DependencyId, DependencyRange, DependencyType, ModuleDependency, }; use rspack_util::atom::Atom; +#[cacheable] #[derive(Debug, Clone)] pub struct RequireEnsureItemDependency { id: DependencyId, + #[cacheable(with=AsPreset)] request: Atom, range: DependencyRange, } @@ -21,6 +24,7 @@ impl RequireEnsureItemDependency { } } +#[cacheable_dyn] impl Dependency for RequireEnsureItemDependency { fn id(&self) -> &rspack_core::DependencyId { &self.id @@ -43,6 +47,7 @@ impl Dependency for RequireEnsureItemDependency { } } +#[cacheable_dyn] impl ModuleDependency for RequireEnsureItemDependency { fn request(&self) -> &str { &self.request diff --git a/crates/rspack_plugin_javascript/src/dependency/commonjs/require_header_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/commonjs/require_header_dependency.rs index 35f8a9d105b..a83449c70b9 100644 --- a/crates/rspack_plugin_javascript/src/dependency/commonjs/require_header_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/commonjs/require_header_dependency.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn, with::Skip}; use rspack_core::{ AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyLocation, DependencyRange, RuntimeSpec, @@ -5,10 +6,12 @@ use rspack_core::{ use rspack_core::{DependencyId, SharedSourceMap}; use rspack_core::{DependencyTemplate, RuntimeGlobals, TemplateContext}; +#[cacheable] #[derive(Debug, Clone)] pub struct RequireHeaderDependency { id: DependencyId, range: DependencyRange, + #[cacheable(with=Skip)] source_map: Option, } @@ -22,6 +25,7 @@ impl RequireHeaderDependency { } } +#[cacheable_dyn] impl Dependency for RequireHeaderDependency { fn id(&self) -> &DependencyId { &self.id @@ -39,6 +43,7 @@ impl Dependency for RequireHeaderDependency { impl AsModuleDependency for RequireHeaderDependency {} impl AsContextDependency for RequireHeaderDependency {} +#[cacheable_dyn] impl DependencyTemplate for RequireHeaderDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/commonjs/require_resolve_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/commonjs/require_resolve_dependency.rs index 3fea1d71dcf..3cf98ad7dc6 100644 --- a/crates/rspack_plugin_javascript/src/dependency/commonjs/require_resolve_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/commonjs/require_resolve_dependency.rs @@ -1,9 +1,11 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ module_id, AsContextDependency, Compilation, Dependency, DependencyCategory, DependencyId, DependencyRange, DependencyTemplate, DependencyType, ExtendedReferencedExport, ModuleDependency, ModuleGraph, RuntimeSpec, TemplateContext, TemplateReplaceSource, }; +#[cacheable] #[derive(Debug, Clone)] pub struct RequireResolveDependency { pub id: DependencyId, @@ -25,6 +27,7 @@ impl RequireResolveDependency { } } +#[cacheable_dyn] impl Dependency for RequireResolveDependency { fn id(&self) -> &DependencyId { &self.id @@ -55,6 +58,7 @@ impl Dependency for RequireResolveDependency { } } +#[cacheable_dyn] impl ModuleDependency for RequireResolveDependency { fn request(&self) -> &str { &self.request @@ -77,6 +81,7 @@ impl ModuleDependency for RequireResolveDependency { } } +#[cacheable_dyn] impl DependencyTemplate for RequireResolveDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/commonjs/require_resolve_header_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/commonjs/require_resolve_header_dependency.rs index 227f995afc5..bef592f9bf0 100644 --- a/crates/rspack_plugin_javascript/src/dependency/commonjs/require_resolve_header_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/commonjs/require_resolve_header_dependency.rs @@ -1,13 +1,16 @@ +use rspack_cacheable::{cacheable, cacheable_dyn, with::Skip}; use rspack_core::{ AffectType, AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyId, DependencyLocation, DependencyRange, DependencyTemplate, RuntimeSpec, SharedSourceMap, TemplateContext, TemplateReplaceSource, }; +#[cacheable] #[derive(Debug, Clone)] pub struct RequireResolveHeaderDependency { id: DependencyId, range: DependencyRange, + #[cacheable(with=Skip)] source_map: Option, } @@ -21,6 +24,7 @@ impl RequireResolveHeaderDependency { } } +#[cacheable_dyn] impl Dependency for RequireResolveHeaderDependency { fn id(&self) -> &DependencyId { &self.id @@ -38,6 +42,7 @@ impl Dependency for RequireResolveHeaderDependency { impl AsModuleDependency for RequireResolveHeaderDependency {} impl AsContextDependency for RequireResolveHeaderDependency {} +#[cacheable_dyn] impl DependencyTemplate for RequireResolveHeaderDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/context/common_js_require_context_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/context/common_js_require_context_dependency.rs index db85cd03947..8e7f9f82891 100644 --- a/crates/rspack_plugin_javascript/src/dependency/context/common_js_require_context_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/context/common_js_require_context_dependency.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AsModuleDependency, Compilation, ContextDependency, ContextOptions, Dependency, DependencyCategory, DependencyId, DependencyRange, DependencyTemplate, DependencyType, @@ -9,6 +10,7 @@ use super::{ context_dependency_template_as_require_call, create_resource_identifier_for_context_dependency, }; +#[cacheable] #[derive(Debug, Clone)] pub struct CommonJsRequireContextDependency { id: DependencyId, @@ -40,6 +42,7 @@ impl CommonJsRequireContextDependency { } } +#[cacheable_dyn] impl Dependency for CommonJsRequireContextDependency { fn id(&self) -> &DependencyId { &self.id @@ -107,6 +110,7 @@ impl ContextDependency for CommonJsRequireContextDependency { } } +#[cacheable_dyn] impl DependencyTemplate for CommonJsRequireContextDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/context/import_context_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/context/import_context_dependency.rs index f9300be561d..20445dc8a22 100644 --- a/crates/rspack_plugin_javascript/src/dependency/context/import_context_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/context/import_context_dependency.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AsModuleDependency, Compilation, ContextDependency, ContextOptions, Dependency, DependencyCategory, DependencyId, DependencyRange, DependencyTemplate, DependencyType, @@ -9,6 +10,7 @@ use super::{ context_dependency_template_as_require_call, create_resource_identifier_for_context_dependency, }; +#[cacheable] #[derive(Debug, Clone)] pub struct ImportContextDependency { id: DependencyId, @@ -40,6 +42,7 @@ impl ImportContextDependency { } } +#[cacheable_dyn] impl Dependency for ImportContextDependency { fn id(&self) -> &DependencyId { &self.id @@ -107,6 +110,7 @@ impl ContextDependency for ImportContextDependency { } } +#[cacheable_dyn] impl DependencyTemplate for ImportContextDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/context/import_meta_context_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/context/import_meta_context_dependency.rs index 2deac51f215..62b2d2223ea 100644 --- a/crates/rspack_plugin_javascript/src/dependency/context/import_meta_context_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/context/import_meta_context_dependency.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ module_raw, AsModuleDependency, Compilation, ContextDependency, ContextOptions, Dependency, DependencyCategory, DependencyId, DependencyRange, DependencyTemplate, DependencyType, @@ -7,6 +8,7 @@ use rspack_error::Diagnostic; use super::create_resource_identifier_for_context_dependency; +#[cacheable] #[derive(Debug, Clone)] pub struct ImportMetaContextDependency { id: DependencyId, @@ -31,6 +33,7 @@ impl ImportMetaContextDependency { } } +#[cacheable_dyn] impl Dependency for ImportMetaContextDependency { fn id(&self) -> &DependencyId { &self.id @@ -98,6 +101,7 @@ impl ContextDependency for ImportMetaContextDependency { } } +#[cacheable_dyn] impl DependencyTemplate for ImportMetaContextDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/context/require_context_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/context/require_context_dependency.rs index c8bd043a876..0ca855ab4a6 100644 --- a/crates/rspack_plugin_javascript/src/dependency/context/require_context_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/context/require_context_dependency.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ module_raw, AsModuleDependency, Compilation, ContextDependency, ContextOptions, Dependency, DependencyCategory, DependencyId, DependencyRange, DependencyTemplate, DependencyType, @@ -7,6 +8,7 @@ use rspack_error::Diagnostic; use super::create_resource_identifier_for_context_dependency; +#[cacheable] #[derive(Debug, Clone)] pub struct RequireContextDependency { id: DependencyId, @@ -31,6 +33,7 @@ impl RequireContextDependency { } } +#[cacheable_dyn] impl Dependency for RequireContextDependency { fn id(&self) -> &DependencyId { &self.id @@ -98,6 +101,7 @@ impl ContextDependency for RequireContextDependency { } } +#[cacheable_dyn] impl DependencyTemplate for RequireContextDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/context/require_resolve_context_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/context/require_resolve_context_dependency.rs index 0affdac24b7..51aa62f05a1 100644 --- a/crates/rspack_plugin_javascript/src/dependency/context/require_resolve_context_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/context/require_resolve_context_dependency.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AffectType, AsModuleDependency, Compilation, ContextDependency, ContextOptions, ContextTypePrefix, Dependency, DependencyCategory, DependencyId, DependencyRange, @@ -7,6 +8,7 @@ use rspack_error::Diagnostic; use super::{context_dependency_template_as_id, create_resource_identifier_for_context_dependency}; +#[cacheable] #[derive(Debug, Clone)] pub struct RequireResolveContextDependency { id: DependencyId, @@ -31,6 +33,7 @@ impl RequireResolveContextDependency { } } +#[cacheable_dyn] impl Dependency for RequireResolveContextDependency { fn id(&self) -> &DependencyId { &self.id @@ -91,6 +94,7 @@ impl ContextDependency for RequireResolveContextDependency { } } +#[cacheable_dyn] impl DependencyTemplate for RequireResolveContextDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/esm_compatibility_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/esm_compatibility_dependency.rs index a90300ad324..b9fe14dd83e 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/esm_compatibility_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/esm_compatibility_dependency.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AsDependency, Compilation, DependencyTemplate, InitFragmentKey, InitFragmentStage, ModuleGraph, NormalInitFragment, RuntimeGlobals, RuntimeSpec, TemplateContext, TemplateReplaceSource, @@ -7,9 +8,11 @@ use swc_core::atoms::Atom; // Mark module `__esModule`. // Add `__webpack_require__.r(__webpack_exports__);`. +#[cacheable] #[derive(Debug, Clone)] pub struct ESMCompatibilityDependency; +#[cacheable_dyn] impl DependencyTemplate for ESMCompatibilityDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_expression_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_expression_dependency.rs index c9c9fbb9745..85510819578 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_expression_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_expression_dependency.rs @@ -1,4 +1,5 @@ use itertools::Itertools; +use rspack_cacheable::{cacheable, cacheable_dyn, with::Skip}; use rspack_collections::{Identifier, IdentifierSet}; use rspack_core::rspack_sources::ReplacementEnforce; use rspack_core::{ @@ -11,12 +12,14 @@ use swc_core::atoms::Atom; use crate::parser_plugin::JS_DEFAULT_KEYWORD; +#[cacheable] #[derive(Debug, Clone)] pub enum DeclarationId { Id(String), Func(DeclarationInfo), } +#[cacheable] #[derive(Debug, Clone)] pub struct DeclarationInfo { range: DependencyRange, @@ -34,6 +37,7 @@ impl DeclarationInfo { } } +#[cacheable] #[derive(Debug, Clone)] pub struct ESMExportExpressionDependency { id: DependencyId, @@ -41,6 +45,7 @@ pub struct ESMExportExpressionDependency { range_stmt: DependencyRange, prefix: String, declaration: Option, + #[cacheable(with=Skip)] source_map: Option, } @@ -63,6 +68,7 @@ impl ESMExportExpressionDependency { } } +#[cacheable_dyn] impl Dependency for ESMExportExpressionDependency { fn dependency_type(&self) -> &DependencyType { &DependencyType::EsmExportExpression @@ -107,6 +113,7 @@ impl Dependency for ESMExportExpressionDependency { impl AsModuleDependency for ESMExportExpressionDependency {} impl AsContextDependency for ESMExportExpressionDependency {} +#[cacheable_dyn] impl DependencyTemplate for ESMExportExpressionDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_header_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_header_dependency.rs index 316eb6650e7..3b1ebd7682a 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_header_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_header_dependency.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn, with::Skip}; use rspack_core::{ AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyId, DependencyLocation, DependencyRange, DependencyTemplate, DependencyType, RuntimeSpec, @@ -7,11 +8,13 @@ use rspack_core::{ // Remove `export` label. // Before: `export const a = 1` // After: `const a = 1` +#[cacheable] #[derive(Debug, Clone)] pub struct ESMExportHeaderDependency { id: DependencyId, range: DependencyRange, range_decl: Option, + #[cacheable(with=Skip)] source_map: Option, } @@ -30,6 +33,7 @@ impl ESMExportHeaderDependency { } } +#[cacheable_dyn] impl Dependency for ESMExportHeaderDependency { fn id(&self) -> &rspack_core::DependencyId { &self.id @@ -48,6 +52,7 @@ impl Dependency for ESMExportHeaderDependency { } } +#[cacheable_dyn] impl DependencyTemplate for ESMExportHeaderDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_imported_specifier_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_imported_specifier_dependency.rs index ab6519b6a36..fccb419d90c 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_imported_specifier_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_imported_specifier_dependency.rs @@ -2,6 +2,10 @@ use std::hash::BuildHasherDefault; use std::sync::Arc; use indexmap::{IndexMap, IndexSet}; +use rspack_cacheable::{ + cacheable, cacheable_dyn, + with::{AsOption, AsPreset, AsVec, Skip}, +}; use rspack_collections::IdentifierSet; use rspack_core::{ create_exports_object_referenced, create_no_exports_referenced, filter_runtime, get_exports_type, @@ -31,11 +35,15 @@ use super::{ // case1: `import { a } from 'a'; export { a }` // case2: `export { a } from 'a';` // case3: `export * from 'a'` +#[cacheable] #[derive(Debug, Clone)] pub struct ESMExportImportedSpecifierDependency { pub id: DependencyId, + #[cacheable(with=AsVec)] pub ids: Vec, + #[cacheable(with=AsOption)] pub name: Option, + #[cacheable(with=AsPreset)] pub request: Atom, pub export_all: bool, pub source_order: i32, @@ -44,6 +52,7 @@ pub struct ESMExportImportedSpecifierDependency { attributes: Option, resource_identifier: String, export_presence_mode: ExportPresenceMode, + #[cacheable(with=Skip)] source_map: Option, } @@ -991,6 +1000,7 @@ pub struct DiscoverActiveExportsFromOtherStarExportsRet<'a> { pub dependency_index: usize, } +#[cacheable_dyn] impl DependencyTemplate for ESMExportImportedSpecifierDependency { fn apply( &self, @@ -1036,6 +1046,7 @@ impl DependencyTemplate for ESMExportImportedSpecifierDependency { } } +#[cacheable_dyn] impl Dependency for ESMExportImportedSpecifierDependency { fn id(&self) -> &DependencyId { &self.id @@ -1361,6 +1372,7 @@ impl DependencyConditionFn for ESMExportImportedSpecifierDependencyCondition { } } +#[cacheable_dyn] impl ModuleDependency for ESMExportImportedSpecifierDependency { fn request(&self) -> &str { &self.request diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_specifier_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_specifier_dependency.rs index b7f98238e2a..74bb55511f0 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_specifier_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_specifier_dependency.rs @@ -1,3 +1,7 @@ +use rspack_cacheable::{ + cacheable, cacheable_dyn, + with::{AsPreset, Skip}, +}; use rspack_collections::IdentifierSet; use rspack_core::{ AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyCategory, @@ -8,12 +12,16 @@ use rspack_core::{ use swc_core::ecma::atoms::Atom; // Create _webpack_require__.d(__webpack_exports__, {}) for each export. +#[cacheable] #[derive(Debug, Clone)] pub struct ESMExportSpecifierDependency { id: DependencyId, range: DependencyRange, + #[cacheable(with=Skip)] source_map: Option, + #[cacheable(with=AsPreset)] pub name: Atom, + #[cacheable(with=AsPreset)] pub value: Atom, // id } @@ -34,6 +42,7 @@ impl ESMExportSpecifierDependency { } } +#[cacheable_dyn] impl Dependency for ESMExportSpecifierDependency { fn id(&self) -> &DependencyId { &self.id @@ -79,6 +88,7 @@ impl Dependency for ESMExportSpecifierDependency { impl AsModuleDependency for ESMExportSpecifierDependency {} +#[cacheable_dyn] impl DependencyTemplate for ESMExportSpecifierDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/esm_import_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/esm_import_dependency.rs index 34480e35b3e..2e7e1d26588 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/esm_import_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/esm_import_dependency.rs @@ -1,5 +1,9 @@ use std::sync::Arc; +use rspack_cacheable::{ + cacheable, cacheable_dyn, + with::{AsPreset, Skip}, +}; use rspack_collections::IdentifierSet; use rspack_core::Compilation; use rspack_core::DependencyConditionFn; @@ -58,8 +62,10 @@ pub mod import_emitted_runtime { } // ESMImportDependency is merged ESMImportSideEffectDependency. +#[cacheable] #[derive(Debug, Clone)] pub struct ESMImportSideEffectDependency { + #[cacheable(with=AsPreset)] pub request: Atom, pub source_order: i32, pub id: DependencyId, @@ -69,6 +75,7 @@ pub struct ESMImportSideEffectDependency { pub export_all: bool, attributes: Option, resource_identifier: String, + #[cacheable(with=Skip)] source_map: Option, } @@ -390,6 +397,7 @@ pub fn esm_import_dependency_get_linking_error( None } +#[cacheable_dyn] impl Dependency for ESMImportSideEffectDependency { fn id(&self) -> &DependencyId { &self.id @@ -469,6 +477,7 @@ impl DependencyConditionFn for ESMImportSideEffectDependencyCondition { } } +#[cacheable_dyn] impl ModuleDependency for ESMImportSideEffectDependency { fn is_export_all(&self) -> Option { Some(self.export_all) @@ -500,6 +509,7 @@ impl ModuleDependency for ESMImportSideEffectDependency { // It's from ESMImportSideEffectDependency. } +#[cacheable_dyn] impl DependencyTemplate for ESMImportSideEffectDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/esm_import_specifier_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/esm_import_specifier_dependency.rs index e1e04efbd1a..bdc5618ab6d 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/esm_import_specifier_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/esm_import_specifier_dependency.rs @@ -1,3 +1,7 @@ +use rspack_cacheable::{ + cacheable, cacheable_dyn, + with::{AsOption, AsPreset, AsVec, Skip}, +}; use rspack_collections::IdentifierSet; use rspack_core::{ create_exports_object_referenced, export_from_import, get_dependency_used_by_exports_condition, @@ -15,23 +19,29 @@ use swc_core::ecma::atoms::Atom; use super::esm_import_dependency::esm_import_dependency_get_linking_error; use super::{create_resource_identifier_for_esm_dependency, esm_import_dependency_apply}; +#[cacheable] #[derive(Debug, Clone)] pub struct ESMImportSpecifierDependency { id: DependencyId, + #[cacheable(with=AsPreset)] request: Atom, + #[cacheable(with=AsPreset)] name: Atom, source_order: i32, shorthand: bool, asi_safe: bool, range: DependencyRange, + #[cacheable(with=AsVec)] ids: Vec, call: bool, direct_import: bool, used_by_exports: Option, + #[cacheable(with=AsOption>)] referenced_properties_in_destructuring: Option>, resource_identifier: String, export_presence_mode: ExportPresenceMode, attributes: Option, + #[cacheable(with=Skip)] source_map: Option, pub namespace_object_as_context: bool, } @@ -113,6 +123,7 @@ impl ESMImportSpecifierDependency { } } +#[cacheable_dyn] impl DependencyTemplate for ESMImportSpecifierDependency { fn apply( &self, @@ -222,6 +233,7 @@ impl DependencyTemplate for ESMImportSpecifierDependency { } } +#[cacheable_dyn] impl Dependency for ESMImportSpecifierDependency { fn id(&self) -> &DependencyId { &self.id @@ -342,6 +354,7 @@ impl Dependency for ESMImportSpecifierDependency { } } +#[cacheable_dyn] impl ModuleDependency for ESMImportSpecifierDependency { fn request(&self) -> &str { &self.request diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/external_module_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/external_module_dependency.rs index fd4a4ab0f3a..59167175fb0 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/external_module_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/external_module_dependency.rs @@ -1,9 +1,11 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AsDependency, Compilation, DependencyId, DependencyTemplate, ExternalModuleInitFragment, InitFragmentExt, InitFragmentStage, RuntimeSpec, TemplateContext, TemplateReplaceSource, }; use rspack_util::ext::DynHash; +#[cacheable] #[derive(Debug, Clone)] pub struct ExternalModuleDependency { id: DependencyId, @@ -27,6 +29,7 @@ impl ExternalModuleDependency { } } +#[cacheable_dyn] impl DependencyTemplate for ExternalModuleDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/import_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/import_dependency.rs index a92c026c63f..0e8760e9118 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/import_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/import_dependency.rs @@ -1,3 +1,7 @@ +use rspack_cacheable::{ + cacheable, cacheable_dyn, + with::{AsOption, AsPreset, AsVec}, +}; use rspack_core::{ create_exports_object_referenced, module_namespace_promise, Compilation, DependencyRange, DependencyType, ExportsType, ExtendedReferencedExport, ImportAttributes, ModuleGraph, @@ -52,11 +56,14 @@ pub fn create_import_dependency_referenced_exports( } } +#[cacheable] #[derive(Debug, Clone)] pub struct ImportDependency { id: DependencyId, + #[cacheable(with=AsPreset)] pub request: Atom, pub range: DependencyRange, + #[cacheable(with=AsOption>)] referenced_exports: Option>, attributes: Option, resource_identifier: String, @@ -82,6 +89,7 @@ impl ImportDependency { } } +#[cacheable_dyn] impl Dependency for ImportDependency { fn id(&self) -> &DependencyId { &self.id @@ -120,6 +128,7 @@ impl Dependency for ImportDependency { } } +#[cacheable_dyn] impl ModuleDependency for ImportDependency { fn request(&self) -> &str { &self.request @@ -134,6 +143,7 @@ impl ModuleDependency for ImportDependency { } } +#[cacheable_dyn] impl DependencyTemplate for ImportDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/import_eager_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/import_eager_dependency.rs index 392e62a9b7a..2b3b11b1039 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/import_eager_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/import_eager_dependency.rs @@ -1,3 +1,7 @@ +use rspack_cacheable::{ + cacheable, cacheable_dyn, + with::{AsOption, AsPreset, AsVec}, +}; use rspack_core::{ module_namespace_promise, AsContextDependency, Compilation, Dependency, DependencyCategory, DependencyId, DependencyRange, DependencyTemplate, DependencyType, ImportAttributes, @@ -10,11 +14,14 @@ use super::{ import_dependency::create_import_dependency_referenced_exports, }; +#[cacheable] #[derive(Debug, Clone)] pub struct ImportEagerDependency { id: DependencyId, + #[cacheable(with=AsPreset)] request: Atom, range: DependencyRange, + #[cacheable(with=AsOption>)] referenced_exports: Option>, attributes: Option, resource_identifier: String, @@ -40,6 +47,7 @@ impl ImportEagerDependency { } } +#[cacheable_dyn] impl Dependency for ImportEagerDependency { fn id(&self) -> &DependencyId { &self.id @@ -78,6 +86,7 @@ impl Dependency for ImportEagerDependency { } } +#[cacheable_dyn] impl ModuleDependency for ImportEagerDependency { fn request(&self) -> &str { &self.request @@ -92,6 +101,7 @@ impl ModuleDependency for ImportEagerDependency { } } +#[cacheable_dyn] impl DependencyTemplate for ImportEagerDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/provide_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/provide_dependency.rs index 8615b6def1e..159e21c0c5f 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/provide_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/provide_dependency.rs @@ -1,4 +1,8 @@ use itertools::Itertools; +use rspack_cacheable::{ + cacheable, cacheable_dyn, + with::{AsPreset, AsVec, Skip}, +}; use rspack_core::{ create_exports_object_referenced, module_raw, Compilation, DependencyLocation, DependencyRange, DependencyType, ExtendedReferencedExport, ModuleGraph, NormalInitFragment, RuntimeSpec, @@ -10,13 +14,17 @@ use rspack_core::{ModuleDependency, TemplateContext, TemplateReplaceSource}; use rspack_util::ext::DynHash; use swc_core::atoms::Atom; +#[cacheable] #[derive(Debug, Clone)] pub struct ProvideDependency { id: DependencyId, + #[cacheable(with=AsPreset)] request: Atom, identifier: String, + #[cacheable(with=AsVec)] ids: Vec, range: DependencyRange, + #[cacheable(with=Skip)] source_map: Option, } @@ -39,6 +47,7 @@ impl ProvideDependency { } } +#[cacheable_dyn] impl Dependency for ProvideDependency { fn id(&self) -> &DependencyId { &self.id @@ -73,6 +82,7 @@ impl Dependency for ProvideDependency { } } +#[cacheable_dyn] impl ModuleDependency for ProvideDependency { fn request(&self) -> &str { &self.request @@ -87,6 +97,7 @@ impl ModuleDependency for ProvideDependency { } } +#[cacheable_dyn] impl DependencyTemplate for ProvideDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/export_info_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/export_info_dependency.rs index 0652c4efbb6..ade62d9dd35 100644 --- a/crates/rspack_plugin_javascript/src/dependency/export_info_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/export_info_dependency.rs @@ -1,15 +1,22 @@ use itertools::Itertools; +use rspack_cacheable::{ + cacheable, cacheable_dyn, + with::{AsPreset, AsVec}, +}; use rspack_core::{ AsDependency, Compilation, DependencyTemplate, ExportProvided, RuntimeSpec, TemplateContext, TemplateReplaceSource, UsageState, UsedExports, UsedName, }; use swc_core::ecma::atoms::Atom; +#[cacheable] #[derive(Debug, Clone)] pub struct ExportInfoDependency { start: u32, end: u32, + #[cacheable(with=AsVec)] export_name: Vec, + #[cacheable(with=AsPreset)] property: Atom, } @@ -24,6 +31,7 @@ impl ExportInfoDependency { } } +#[cacheable_dyn] impl DependencyTemplate for ExportInfoDependency { fn apply(&self, source: &mut TemplateReplaceSource, context: &mut TemplateContext) { let value = self.get_property(context); diff --git a/crates/rspack_plugin_javascript/src/dependency/hmr/esm_accept_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/hmr/esm_accept_dependency.rs index d34f4e86835..fbbbea97715 100644 --- a/crates/rspack_plugin_javascript/src/dependency/hmr/esm_accept_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/hmr/esm_accept_dependency.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn, with::Skip}; use rspack_core::{ import_statement, runtime_condition_expression, AsDependency, Compilation, DependencyId, DependencyLocation, DependencyRange, DependencyTemplate, RuntimeCondition, RuntimeSpec, @@ -6,11 +7,13 @@ use rspack_core::{ use crate::dependency::import_emitted_runtime; +#[cacheable] #[derive(Debug, Clone)] pub struct ESMAcceptDependency { range: DependencyRange, has_callback: bool, dependency_ids: Vec, + #[cacheable(with=Skip)] source_map: Option, } @@ -34,6 +37,7 @@ impl ESMAcceptDependency { } } +#[cacheable_dyn] impl DependencyTemplate for ESMAcceptDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/hmr/import_meta_hot_accept.rs b/crates/rspack_plugin_javascript/src/dependency/hmr/import_meta_hot_accept.rs index bd7dd91ecc5..7e15c27ed25 100644 --- a/crates/rspack_plugin_javascript/src/dependency/hmr/import_meta_hot_accept.rs +++ b/crates/rspack_plugin_javascript/src/dependency/hmr/import_meta_hot_accept.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn, with::AsPreset}; use rspack_core::{ module_id, AsContextDependency, Compilation, Dependency, DependencyCategory, DependencyId, DependencyRange, DependencyTemplate, DependencyType, ModuleDependency, RuntimeSpec, @@ -5,9 +6,11 @@ use rspack_core::{ }; use swc_core::ecma::atoms::Atom; +#[cacheable] #[derive(Debug, Clone)] pub struct ImportMetaHotAcceptDependency { id: DependencyId, + #[cacheable(with=AsPreset)] request: Atom, range: DependencyRange, } @@ -22,6 +25,7 @@ impl ImportMetaHotAcceptDependency { } } +#[cacheable_dyn] impl Dependency for ImportMetaHotAcceptDependency { fn id(&self) -> &DependencyId { &self.id @@ -44,6 +48,7 @@ impl Dependency for ImportMetaHotAcceptDependency { } } +#[cacheable_dyn] impl ModuleDependency for ImportMetaHotAcceptDependency { fn request(&self) -> &str { &self.request @@ -62,6 +67,7 @@ impl ModuleDependency for ImportMetaHotAcceptDependency { } } +#[cacheable_dyn] impl DependencyTemplate for ImportMetaHotAcceptDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/hmr/import_meta_hot_decline.rs b/crates/rspack_plugin_javascript/src/dependency/hmr/import_meta_hot_decline.rs index af1f1e8f373..e04c03fabb1 100644 --- a/crates/rspack_plugin_javascript/src/dependency/hmr/import_meta_hot_decline.rs +++ b/crates/rspack_plugin_javascript/src/dependency/hmr/import_meta_hot_decline.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn, with::AsPreset}; use rspack_core::{ module_id, AsContextDependency, Compilation, Dependency, DependencyCategory, DependencyId, DependencyRange, DependencyTemplate, DependencyType, ModuleDependency, RuntimeSpec, @@ -5,9 +6,11 @@ use rspack_core::{ }; use swc_core::ecma::atoms::Atom; +#[cacheable] #[derive(Debug, Clone)] pub struct ImportMetaHotDeclineDependency { id: DependencyId, + #[cacheable(with=AsPreset)] request: Atom, range: DependencyRange, } @@ -22,6 +25,7 @@ impl ImportMetaHotDeclineDependency { } } +#[cacheable_dyn] impl Dependency for ImportMetaHotDeclineDependency { fn id(&self) -> &DependencyId { &self.id @@ -44,6 +48,7 @@ impl Dependency for ImportMetaHotDeclineDependency { } } +#[cacheable_dyn] impl ModuleDependency for ImportMetaHotDeclineDependency { fn request(&self) -> &str { &self.request @@ -62,6 +67,7 @@ impl ModuleDependency for ImportMetaHotDeclineDependency { } } +#[cacheable_dyn] impl DependencyTemplate for ImportMetaHotDeclineDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/hmr/module_hot_accept.rs b/crates/rspack_plugin_javascript/src/dependency/hmr/module_hot_accept.rs index f50b48b16c3..6b73caba56b 100644 --- a/crates/rspack_plugin_javascript/src/dependency/hmr/module_hot_accept.rs +++ b/crates/rspack_plugin_javascript/src/dependency/hmr/module_hot_accept.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn, with::AsPreset}; use rspack_core::{ module_id, AsContextDependency, Compilation, Dependency, DependencyCategory, DependencyId, DependencyRange, DependencyTemplate, DependencyType, ModuleDependency, RuntimeSpec, @@ -5,9 +6,11 @@ use rspack_core::{ }; use swc_core::ecma::atoms::Atom; +#[cacheable] #[derive(Debug, Clone)] pub struct ModuleHotAcceptDependency { id: DependencyId, + #[cacheable(with=AsPreset)] request: Atom, range: DependencyRange, } @@ -22,6 +25,7 @@ impl ModuleHotAcceptDependency { } } +#[cacheable_dyn] impl Dependency for ModuleHotAcceptDependency { fn id(&self) -> &DependencyId { &self.id @@ -44,6 +48,7 @@ impl Dependency for ModuleHotAcceptDependency { } } +#[cacheable_dyn] impl ModuleDependency for ModuleHotAcceptDependency { fn request(&self) -> &str { &self.request @@ -62,6 +67,7 @@ impl ModuleDependency for ModuleHotAcceptDependency { } } +#[cacheable_dyn] impl DependencyTemplate for ModuleHotAcceptDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/hmr/module_hot_decline.rs b/crates/rspack_plugin_javascript/src/dependency/hmr/module_hot_decline.rs index 5dbea529212..02c822c22d4 100644 --- a/crates/rspack_plugin_javascript/src/dependency/hmr/module_hot_decline.rs +++ b/crates/rspack_plugin_javascript/src/dependency/hmr/module_hot_decline.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn, with::AsPreset}; use rspack_core::{ module_id, AsContextDependency, Compilation, Dependency, DependencyCategory, DependencyId, DependencyRange, DependencyTemplate, DependencyType, ModuleDependency, RuntimeSpec, @@ -5,9 +6,11 @@ use rspack_core::{ }; use swc_core::ecma::atoms::Atom; +#[cacheable] #[derive(Debug, Clone)] pub struct ModuleHotDeclineDependency { id: DependencyId, + #[cacheable(with=AsPreset)] request: Atom, range: DependencyRange, } @@ -22,6 +25,7 @@ impl ModuleHotDeclineDependency { } } +#[cacheable_dyn] impl Dependency for ModuleHotDeclineDependency { fn id(&self) -> &DependencyId { &self.id @@ -44,6 +48,7 @@ impl Dependency for ModuleHotDeclineDependency { } } +#[cacheable_dyn] impl ModuleDependency for ModuleHotDeclineDependency { fn request(&self) -> &str { &self.request @@ -62,6 +67,7 @@ impl ModuleDependency for ModuleHotDeclineDependency { } } +#[cacheable_dyn] impl DependencyTemplate for ModuleHotDeclineDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/is_included_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/is_included_dependency.rs index dab030ccc22..034e10e050f 100644 --- a/crates/rspack_plugin_javascript/src/dependency/is_included_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/is_included_dependency.rs @@ -1,9 +1,11 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AsContextDependency, Compilation, Dependency, DependencyId, DependencyTemplate, DependencyType, ExtendedReferencedExport, ModuleDependency, ModuleGraph, RuntimeSpec, TemplateContext, TemplateReplaceSource, }; +#[cacheable] #[derive(Debug, Clone)] pub struct WebpackIsIncludedDependency { pub start: u32, @@ -25,6 +27,7 @@ impl WebpackIsIncludedDependency { impl AsContextDependency for WebpackIsIncludedDependency {} +#[cacheable_dyn] impl Dependency for WebpackIsIncludedDependency { fn dependency_type(&self) -> &DependencyType { &DependencyType::WebpackIsIncluded @@ -47,6 +50,7 @@ impl Dependency for WebpackIsIncludedDependency { } } +#[cacheable_dyn] impl ModuleDependency for WebpackIsIncludedDependency { fn weak(&self) -> bool { true @@ -57,6 +61,7 @@ impl ModuleDependency for WebpackIsIncludedDependency { } } +#[cacheable_dyn] impl DependencyTemplate for WebpackIsIncludedDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/module_argument_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/module_argument_dependency.rs index 6eebdb9d0bf..00b46bf1fc5 100644 --- a/crates/rspack_plugin_javascript/src/dependency/module_argument_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/module_argument_dependency.rs @@ -1,13 +1,17 @@ +use rspack_cacheable::{cacheable, cacheable_dyn, with::Skip}; use rspack_core::{ AsDependency, Compilation, DependencyLocation, DependencyRange, DependencyTemplate, RuntimeGlobals, RuntimeSpec, SharedSourceMap, TemplateContext, TemplateReplaceSource, }; use rspack_util::ext::DynHash; +#[cacheable] #[derive(Debug, Clone)] pub struct ModuleArgumentDependency { + #[cacheable(with=Skip)] id: Option<&'static str>, range: DependencyRange, + #[cacheable(with=Skip)] source_map: Option, } @@ -29,6 +33,7 @@ impl ModuleArgumentDependency { } } +#[cacheable_dyn] impl DependencyTemplate for ModuleArgumentDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/pure_expression_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/pure_expression_dependency.rs index 1c3fd6641be..9ca02b44202 100644 --- a/crates/rspack_plugin_javascript/src/dependency/pure_expression_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/pure_expression_dependency.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_collections::IdentifierSet; use rspack_core::{ filter_runtime, runtime_condition_expression, AsContextDependency, AsModuleDependency, @@ -7,6 +8,7 @@ use rspack_core::{ }; use rspack_util::ext::DynHash; +#[cacheable] #[derive(Debug, Clone)] pub struct PureExpressionDependency { pub start: u32, @@ -56,6 +58,7 @@ impl PureExpressionDependency { } } +#[cacheable_dyn] impl Dependency for PureExpressionDependency { fn id(&self) -> &rspack_core::DependencyId { &self.id @@ -80,6 +83,7 @@ impl Dependency for PureExpressionDependency { impl AsModuleDependency for PureExpressionDependency {} +#[cacheable_dyn] impl DependencyTemplate for PureExpressionDependency { fn apply(&self, source: &mut TemplateReplaceSource, ctx: &mut TemplateContext) { let runtime_condition = self.get_runtime_condition(ctx.compilation, ctx.runtime); diff --git a/crates/rspack_plugin_javascript/src/dependency/url/mod.rs b/crates/rspack_plugin_javascript/src/dependency/url/mod.rs index da6440037b1..035258f6804 100644 --- a/crates/rspack_plugin_javascript/src/dependency/url/mod.rs +++ b/crates/rspack_plugin_javascript/src/dependency/url/mod.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn, with::AsPreset}; use rspack_core::{ get_dependency_used_by_exports_condition, module_id, AsContextDependency, Compilation, Dependency, DependencyCategory, DependencyCondition, DependencyId, DependencyRange, @@ -6,9 +7,11 @@ use rspack_core::{ }; use swc_core::ecma::atoms::Atom; +#[cacheable] #[derive(Debug, Clone)] pub struct URLDependency { id: DependencyId, + #[cacheable(with=AsPreset)] request: Atom, range: DependencyRange, range_url: DependencyRange, @@ -34,6 +37,7 @@ impl URLDependency { } } +#[cacheable_dyn] impl Dependency for URLDependency { fn id(&self) -> &DependencyId { &self.id @@ -56,6 +60,7 @@ impl Dependency for URLDependency { } } +#[cacheable_dyn] impl ModuleDependency for URLDependency { fn request(&self) -> &str { &self.request @@ -74,6 +79,7 @@ impl ModuleDependency for URLDependency { } } +#[cacheable_dyn] impl DependencyTemplate for URLDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/worker/create_script_url_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/worker/create_script_url_dependency.rs index 627bd7f2da4..fb3616665ee 100644 --- a/crates/rspack_plugin_javascript/src/dependency/worker/create_script_url_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/worker/create_script_url_dependency.rs @@ -1,9 +1,11 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyCategory, DependencyId, DependencyRange, DependencyTemplate, DependencyType, RuntimeGlobals, RuntimeSpec, TemplateContext, TemplateReplaceSource, }; +#[cacheable] #[derive(Debug, Clone)] pub struct CreateScriptUrlDependency { id: DependencyId, @@ -21,6 +23,7 @@ impl CreateScriptUrlDependency { } } +#[cacheable_dyn] impl Dependency for CreateScriptUrlDependency { fn id(&self) -> &DependencyId { &self.id @@ -43,6 +46,7 @@ impl Dependency for CreateScriptUrlDependency { } } +#[cacheable_dyn] impl DependencyTemplate for CreateScriptUrlDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/dependency/worker/mod.rs b/crates/rspack_plugin_javascript/src/dependency/worker/mod.rs index b2c0dd1c99e..a2c81eb9663 100644 --- a/crates/rspack_plugin_javascript/src/dependency/worker/mod.rs +++ b/crates/rspack_plugin_javascript/src/dependency/worker/mod.rs @@ -1,5 +1,6 @@ mod create_script_url_dependency; pub use create_script_url_dependency::CreateScriptUrlDependency; +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AsContextDependency, Compilation, Dependency, DependencyCategory, DependencyId, DependencyRange, DependencyTemplate, DependencyType, ExtendedReferencedExport, ModuleDependency, ModuleGraph, @@ -7,6 +8,7 @@ use rspack_core::{ }; use rspack_util::ext::DynHash; +#[cacheable] #[derive(Debug, Clone)] pub struct WorkerDependency { id: DependencyId, @@ -33,6 +35,7 @@ impl WorkerDependency { } } +#[cacheable_dyn] impl Dependency for WorkerDependency { fn id(&self) -> &DependencyId { &self.id @@ -63,6 +66,7 @@ impl Dependency for WorkerDependency { } } +#[cacheable_dyn] impl ModuleDependency for WorkerDependency { fn request(&self) -> &str { &self.request @@ -77,6 +81,7 @@ impl ModuleDependency for WorkerDependency { } } +#[cacheable_dyn] impl DependencyTemplate for WorkerDependency { fn apply( &self, diff --git a/crates/rspack_plugin_javascript/src/parser_and_generator/mod.rs b/crates/rspack_plugin_javascript/src/parser_and_generator/mod.rs index 34f3f495945..4e8624b27d2 100644 --- a/crates/rspack_plugin_javascript/src/parser_and_generator/mod.rs +++ b/crates/rspack_plugin_javascript/src/parser_and_generator/mod.rs @@ -2,6 +2,8 @@ use std::borrow::Cow; use std::sync::Arc; use itertools::Itertools; +use rspack_cacheable::with::Skip; +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::diagnostics::map_box_diagnostics_to_module_parse_diagnostics; use rspack_core::rspack_sources::{BoxSource, ReplaceSource, Source, SourceExt}; use rspack_core::{ @@ -25,8 +27,11 @@ use crate::visitors::{scan_dependencies, swc_visitor::resolver}; use crate::visitors::{semicolon, ScanDependenciesResult}; use crate::{BoxJavascriptParserPlugin, SideEffectsFlagPluginVisitor, SyntaxContextInfo}; +#[cacheable] #[derive(Default)] pub struct JavaScriptParserAndGenerator { + // TODO + #[cacheable(with=Skip)] parser_plugins: Vec, } @@ -84,6 +89,7 @@ impl JavaScriptParserAndGenerator { static SOURCE_TYPES: &[SourceType; 1] = &[SourceType::JavaScript]; +#[cacheable_dyn] impl ParserAndGenerator for JavaScriptParserAndGenerator { fn source_types(&self) -> &[SourceType] { SOURCE_TYPES diff --git a/crates/rspack_plugin_json/Cargo.toml b/crates/rspack_plugin_json/Cargo.toml index ab280909835..ba4deeddbb8 100644 --- a/crates/rspack_plugin_json/Cargo.toml +++ b/crates/rspack_plugin_json/Cargo.toml @@ -11,6 +11,7 @@ version = "0.2.0" cow-utils = { workspace = true } json = { workspace = true } ropey = "1.6.1" +rspack_cacheable = { workspace = true } rspack_core = { workspace = true } rspack_error = { workspace = true } rspack_util = { workspace = true } diff --git a/crates/rspack_plugin_json/src/json_exports_dependency.rs b/crates/rspack_plugin_json/src/json_exports_dependency.rs index c18884bb07b..629b3d6899d 100644 --- a/crates/rspack_plugin_json/src/json_exports_dependency.rs +++ b/crates/rspack_plugin_json/src/json_exports_dependency.rs @@ -1,4 +1,5 @@ use json::JsonValue; +use rspack_cacheable::{cacheable, cacheable_dyn, with::AsPreset}; use rspack_core::{ AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyId, DependencyTemplate, ExportNameOrSpec, ExportSpec, ExportsOfExportsSpec, ExportsSpec, ModuleGraph, @@ -6,9 +7,11 @@ use rspack_core::{ }; use rspack_util::{ext::DynHash, itoa}; +#[cacheable] #[derive(Debug, Clone)] pub struct JsonExportsDependency { id: DependencyId, + #[cacheable(with=AsPreset)] data: JsonValue, } @@ -21,6 +24,7 @@ impl JsonExportsDependency { } } +#[cacheable_dyn] impl Dependency for JsonExportsDependency { fn id(&self) -> &rspack_core::DependencyId { &self.id @@ -41,6 +45,7 @@ impl Dependency for JsonExportsDependency { impl AsModuleDependency for JsonExportsDependency {} impl AsContextDependency for JsonExportsDependency {} +#[cacheable_dyn] impl DependencyTemplate for JsonExportsDependency { fn apply( &self, diff --git a/crates/rspack_plugin_json/src/lib.rs b/crates/rspack_plugin_json/src/lib.rs index b1fa917c14f..f9abb2bef1e 100644 --- a/crates/rspack_plugin_json/src/lib.rs +++ b/crates/rspack_plugin_json/src/lib.rs @@ -11,6 +11,7 @@ use json::{ }, JsonValue, }; +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ diagnostics::ModuleParseError, rspack_sources::{BoxSource, RawStringSource, Source, SourceExt}, @@ -29,9 +30,11 @@ use crate::json_exports_dependency::JsonExportsDependency; mod json_exports_dependency; mod utils; +#[cacheable] #[derive(Debug)] struct JsonParserAndGenerator; +#[cacheable_dyn] impl ParserAndGenerator for JsonParserAndGenerator { fn source_types(&self) -> &[SourceType] { &[SourceType::JavaScript] diff --git a/crates/rspack_plugin_lazy_compilation/Cargo.toml b/crates/rspack_plugin_lazy_compilation/Cargo.toml index b01d5180806..e6ed0209e5a 100644 --- a/crates/rspack_plugin_lazy_compilation/Cargo.toml +++ b/crates/rspack_plugin_lazy_compilation/Cargo.toml @@ -15,6 +15,7 @@ rustc-hash = { workspace = true } tokio = { workspace = true } tracing = { workspace = true } +rspack_cacheable = { workspace = true } rspack_collections = { workspace = true } rspack_core = { workspace = true } rspack_error = { workspace = true } diff --git a/crates/rspack_plugin_lazy_compilation/src/dependency.rs b/crates/rspack_plugin_lazy_compilation/src/dependency.rs index f6e46e61490..69daf74d664 100644 --- a/crates/rspack_plugin_lazy_compilation/src/dependency.rs +++ b/crates/rspack_plugin_lazy_compilation/src/dependency.rs @@ -1,8 +1,10 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AsContextDependency, AsDependencyTemplate, Dependency, DependencyCategory, DependencyId, DependencyType, ModuleDependency, ModuleFactoryCreateData, }; +#[cacheable] #[derive(Debug, Clone)] pub(crate) struct LazyCompilationDependency { id: DependencyId, @@ -25,6 +27,7 @@ impl LazyCompilationDependency { } } +#[cacheable_dyn] impl ModuleDependency for LazyCompilationDependency { fn request(&self) -> &str { &self.request @@ -34,6 +37,7 @@ impl ModuleDependency for LazyCompilationDependency { impl AsDependencyTemplate for LazyCompilationDependency {} impl AsContextDependency for LazyCompilationDependency {} +#[cacheable_dyn] impl Dependency for LazyCompilationDependency { fn id(&self) -> &rspack_core::DependencyId { &self.id diff --git a/crates/rspack_plugin_lazy_compilation/src/module.rs b/crates/rspack_plugin_lazy_compilation/src/module.rs index d5aeee085ed..cff20702a5c 100644 --- a/crates/rspack_plugin_lazy_compilation/src/module.rs +++ b/crates/rspack_plugin_lazy_compilation/src/module.rs @@ -1,6 +1,7 @@ use std::{path::Path, sync::Arc}; use cow_utils::CowUtils; +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_collections::Identifiable; use rspack_core::{ impl_module_meta_info, module_namespace_promise, module_update_hash, @@ -24,6 +25,7 @@ use crate::dependency::LazyCompilationDependency; static MODULE_TYPE: ModuleType = ModuleType::JsAuto; static SOURCE_TYPE: [SourceType; 1] = [SourceType::JavaScript]; +#[cacheable] #[derive(Debug)] pub(crate) struct LazyCompilationProxyModule { build_info: Option, @@ -100,6 +102,7 @@ impl Diagnosable for LazyCompilationProxyModule { } } +#[cacheable_dyn] #[async_trait::async_trait] impl Module for LazyCompilationProxyModule { impl_module_meta_info!(); diff --git a/crates/rspack_plugin_library/Cargo.toml b/crates/rspack_plugin_library/Cargo.toml index c40e25ce5e6..295c5091b7d 100644 --- a/crates/rspack_plugin_library/Cargo.toml +++ b/crates/rspack_plugin_library/Cargo.toml @@ -12,6 +12,7 @@ version = "0.2.0" [dependencies] async-trait = { workspace = true } regex = { workspace = true } +rspack_cacheable = { workspace = true } rspack_collections = { workspace = true } rspack_core = { workspace = true } rspack_error = { workspace = true } diff --git a/crates/rspack_plugin_library/src/modern_module/import_dependency.rs b/crates/rspack_plugin_library/src/modern_module/import_dependency.rs index 6dbb1afc8a0..79a18b26c5f 100644 --- a/crates/rspack_plugin_library/src/modern_module/import_dependency.rs +++ b/crates/rspack_plugin_library/src/modern_module/import_dependency.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn, with::AsPreset}; use rspack_core::{AsContextDependency, Dependency}; use rspack_core::{ Compilation, DependencyRange, DependencyType, ExternalRequest, ExternalType, ImportAttributes, @@ -8,9 +9,11 @@ use rspack_core::{ModuleDependency, TemplateContext, TemplateReplaceSource}; use rspack_plugin_javascript::dependency::create_resource_identifier_for_esm_dependency; use swc_core::ecma::atoms::Atom; +#[cacheable] #[derive(Debug, Clone)] pub struct ModernModuleImportDependency { id: DependencyId, + #[cacheable(with=AsPreset)] request: Atom, target_request: ExternalRequest, external_type: ExternalType, @@ -41,6 +44,7 @@ impl ModernModuleImportDependency { } } +#[cacheable_dyn] impl Dependency for ModernModuleImportDependency { fn id(&self) -> &DependencyId { &self.id @@ -71,6 +75,7 @@ impl Dependency for ModernModuleImportDependency { } } +#[cacheable_dyn] impl ModuleDependency for ModernModuleImportDependency { fn request(&self) -> &str { &self.request @@ -85,6 +90,7 @@ impl ModuleDependency for ModernModuleImportDependency { } } +#[cacheable_dyn] impl DependencyTemplate for ModernModuleImportDependency { fn apply( &self, diff --git a/crates/rspack_plugin_library/src/modern_module/reexport_star_external_dependency.rs b/crates/rspack_plugin_library/src/modern_module/reexport_star_external_dependency.rs index fc704191a45..8a2779bffa0 100644 --- a/crates/rspack_plugin_library/src/modern_module/reexport_star_external_dependency.rs +++ b/crates/rspack_plugin_library/src/modern_module/reexport_star_external_dependency.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn, with::AsPreset}; use rspack_core::{ AsContextDependency, Dependency, InitFragmentExt, InitFragmentKey, InitFragmentStage, NormalInitFragment, @@ -8,9 +9,11 @@ use rspack_core::{ModuleDependency, TemplateContext, TemplateReplaceSource}; use rspack_plugin_javascript::dependency::create_resource_identifier_for_esm_dependency; use swc_core::ecma::atoms::Atom; +#[cacheable] #[derive(Debug, Clone)] pub struct ModernModuleReexportStarExternalDependency { id: DependencyId, + #[cacheable(with=AsPreset)] request: Atom, target_request: ExternalRequest, external_type: ExternalType, @@ -30,6 +33,7 @@ impl ModernModuleReexportStarExternalDependency { } } +#[cacheable_dyn] impl Dependency for ModernModuleReexportStarExternalDependency { fn id(&self) -> &DependencyId { &self.id @@ -52,6 +56,7 @@ impl Dependency for ModernModuleReexportStarExternalDependency { } } +#[cacheable_dyn] impl ModuleDependency for ModernModuleReexportStarExternalDependency { fn request(&self) -> &str { &self.request @@ -66,6 +71,7 @@ impl ModuleDependency for ModernModuleReexportStarExternalDependency { } } +#[cacheable_dyn] impl DependencyTemplate for ModernModuleReexportStarExternalDependency { fn apply( &self, diff --git a/crates/rspack_plugin_mf/Cargo.toml b/crates/rspack_plugin_mf/Cargo.toml index 94347505487..541b0aca7ed 100644 --- a/crates/rspack_plugin_mf/Cargo.toml +++ b/crates/rspack_plugin_mf/Cargo.toml @@ -8,6 +8,7 @@ version = "0.2.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +rspack_cacheable = { workspace = true } rspack_collections = { workspace = true } rspack_core = { workspace = true } rspack_error = { workspace = true } diff --git a/crates/rspack_plugin_mf/src/container/container_entry_dependency.rs b/crates/rspack_plugin_mf/src/container/container_entry_dependency.rs index c81333051fa..a41470c7b53 100644 --- a/crates/rspack_plugin_mf/src/container/container_entry_dependency.rs +++ b/crates/rspack_plugin_mf/src/container/container_entry_dependency.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AsContextDependency, AsDependencyTemplate, Dependency, DependencyCategory, DependencyId, DependencyType, ModuleDependency, @@ -5,6 +6,7 @@ use rspack_core::{ use crate::ExposeOptions; +#[cacheable] #[derive(Debug, Clone)] pub struct ContainerEntryDependency { id: DependencyId, @@ -34,6 +36,7 @@ impl ContainerEntryDependency { } } +#[cacheable_dyn] impl Dependency for ContainerEntryDependency { fn id(&self) -> &DependencyId { &self.id @@ -56,6 +59,7 @@ impl Dependency for ContainerEntryDependency { } } +#[cacheable_dyn] impl ModuleDependency for ContainerEntryDependency { fn request(&self) -> &str { &self.resource_identifier diff --git a/crates/rspack_plugin_mf/src/container/container_entry_module.rs b/crates/rspack_plugin_mf/src/container/container_entry_module.rs index d8c1845eeea..5e863bfd885 100644 --- a/crates/rspack_plugin_mf/src/container/container_entry_module.rs +++ b/crates/rspack_plugin_mf/src/container/container_entry_module.rs @@ -1,6 +1,7 @@ use std::borrow::Cow; use async_trait::async_trait; +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_collections::{Identifiable, Identifier}; use rspack_core::{ basic_function, block_promise, impl_module_meta_info, impl_source_map_config, module_raw, @@ -23,6 +24,7 @@ use super::{ use crate::utils::json_stringify; #[impl_source_map_config] +#[cacheable] #[derive(Debug)] pub struct ContainerEntryModule { blocks: Vec, @@ -93,6 +95,7 @@ impl DependenciesBlock for ContainerEntryModule { } } +#[cacheable_dyn] #[async_trait] impl Module for ContainerEntryModule { impl_module_meta_info!(); diff --git a/crates/rspack_plugin_mf/src/container/container_exposed_dependency.rs b/crates/rspack_plugin_mf/src/container/container_exposed_dependency.rs index d92bf1ed3ed..e1188d5337e 100644 --- a/crates/rspack_plugin_mf/src/container/container_exposed_dependency.rs +++ b/crates/rspack_plugin_mf/src/container/container_exposed_dependency.rs @@ -1,8 +1,10 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AsContextDependency, AsDependencyTemplate, Dependency, DependencyCategory, DependencyId, DependencyType, ModuleDependency, }; +#[cacheable] #[derive(Debug, Clone)] pub struct ContainerExposedDependency { id: DependencyId, @@ -23,6 +25,7 @@ impl ContainerExposedDependency { } } +#[cacheable_dyn] impl Dependency for ContainerExposedDependency { fn id(&self) -> &DependencyId { &self.id @@ -45,6 +48,7 @@ impl Dependency for ContainerExposedDependency { } } +#[cacheable_dyn] impl ModuleDependency for ContainerExposedDependency { fn request(&self) -> &str { &self.request diff --git a/crates/rspack_plugin_mf/src/container/container_plugin.rs b/crates/rspack_plugin_mf/src/container/container_plugin.rs index 225381d6ef6..abee691aa52 100644 --- a/crates/rspack_plugin_mf/src/container/container_plugin.rs +++ b/crates/rspack_plugin_mf/src/container/container_plugin.rs @@ -30,6 +30,7 @@ pub struct ContainerPluginOptions { pub enhanced: bool, } +#[rspack_cacheable::cacheable] #[derive(Debug, Clone, Serialize)] pub struct ExposeOptions { pub name: Option, diff --git a/crates/rspack_plugin_mf/src/container/expose_runtime_module.rs b/crates/rspack_plugin_mf/src/container/expose_runtime_module.rs index a8b236f0309..0f9506af624 100644 --- a/crates/rspack_plugin_mf/src/container/expose_runtime_module.rs +++ b/crates/rspack_plugin_mf/src/container/expose_runtime_module.rs @@ -16,6 +16,7 @@ pub struct ExposeRuntimeModule { } impl ExposeRuntimeModule { + #[allow(clippy::new_without_default)] pub fn new() -> Self { Self::with_default(Identifier::from("webpack/runtime/initialize_exposes"), None) } diff --git a/crates/rspack_plugin_mf/src/container/fallback_dependency.rs b/crates/rspack_plugin_mf/src/container/fallback_dependency.rs index 6b0cf0f077f..404c8ebf329 100644 --- a/crates/rspack_plugin_mf/src/container/fallback_dependency.rs +++ b/crates/rspack_plugin_mf/src/container/fallback_dependency.rs @@ -1,8 +1,10 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AsContextDependency, AsDependencyTemplate, Dependency, DependencyCategory, DependencyId, DependencyType, ModuleDependency, }; +#[cacheable] #[derive(Debug, Clone)] pub struct FallbackDependency { id: DependencyId, @@ -21,6 +23,7 @@ impl FallbackDependency { } } +#[cacheable_dyn] impl Dependency for FallbackDependency { fn id(&self) -> &DependencyId { &self.id @@ -43,6 +46,7 @@ impl Dependency for FallbackDependency { } } +#[cacheable_dyn] impl ModuleDependency for FallbackDependency { fn request(&self) -> &str { &self.resource_identifier diff --git a/crates/rspack_plugin_mf/src/container/fallback_item_dependency.rs b/crates/rspack_plugin_mf/src/container/fallback_item_dependency.rs index 1edbc87df82..7a66f5b8da4 100644 --- a/crates/rspack_plugin_mf/src/container/fallback_item_dependency.rs +++ b/crates/rspack_plugin_mf/src/container/fallback_item_dependency.rs @@ -1,8 +1,10 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AsContextDependency, AsDependencyTemplate, Dependency, DependencyCategory, DependencyId, DependencyType, ModuleDependency, }; +#[cacheable] #[derive(Debug, Clone)] pub struct FallbackItemDependency { id: DependencyId, @@ -18,6 +20,7 @@ impl FallbackItemDependency { } } +#[cacheable_dyn] impl Dependency for FallbackItemDependency { fn id(&self) -> &DependencyId { &self.id @@ -36,6 +39,7 @@ impl Dependency for FallbackItemDependency { } } +#[cacheable_dyn] impl ModuleDependency for FallbackItemDependency { fn request(&self) -> &str { &self.request diff --git a/crates/rspack_plugin_mf/src/container/fallback_module.rs b/crates/rspack_plugin_mf/src/container/fallback_module.rs index 6f17f949280..90efce81606 100644 --- a/crates/rspack_plugin_mf/src/container/fallback_module.rs +++ b/crates/rspack_plugin_mf/src/container/fallback_module.rs @@ -1,6 +1,7 @@ use std::borrow::Cow; use async_trait::async_trait; +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_collections::{Identifiable, Identifier}; use rspack_core::{ impl_module_meta_info, impl_source_map_config, module_update_hash, @@ -17,6 +18,7 @@ use super::fallback_item_dependency::FallbackItemDependency; use crate::utils::json_stringify; #[impl_source_map_config] +#[cacheable] #[derive(Debug)] pub struct FallbackModule { blocks: Vec, @@ -83,6 +85,7 @@ impl DependenciesBlock for FallbackModule { } } +#[cacheable_dyn] #[async_trait] impl Module for FallbackModule { impl_module_meta_info!(); diff --git a/crates/rspack_plugin_mf/src/container/remote_module.rs b/crates/rspack_plugin_mf/src/container/remote_module.rs index 301fef537fe..1cde75b10eb 100644 --- a/crates/rspack_plugin_mf/src/container/remote_module.rs +++ b/crates/rspack_plugin_mf/src/container/remote_module.rs @@ -1,6 +1,7 @@ use std::borrow::Cow; use async_trait::async_trait; +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_collections::{Identifiable, Identifier}; use rspack_core::{ impl_module_meta_info, impl_source_map_config, module_update_hash, @@ -22,6 +23,7 @@ use crate::{ }; #[impl_source_map_config] +#[cacheable] #[derive(Debug)] pub struct RemoteModule { blocks: Vec, @@ -101,6 +103,7 @@ impl DependenciesBlock for RemoteModule { } } +#[cacheable_dyn] #[async_trait] impl Module for RemoteModule { impl_module_meta_info!(); diff --git a/crates/rspack_plugin_mf/src/container/remote_to_external_dependency.rs b/crates/rspack_plugin_mf/src/container/remote_to_external_dependency.rs index 6da4e7ec6dd..320a648e9e8 100644 --- a/crates/rspack_plugin_mf/src/container/remote_to_external_dependency.rs +++ b/crates/rspack_plugin_mf/src/container/remote_to_external_dependency.rs @@ -1,8 +1,10 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AsContextDependency, AsDependencyTemplate, Dependency, DependencyCategory, DependencyId, DependencyType, ModuleDependency, }; +#[cacheable] #[derive(Debug, Clone)] pub struct RemoteToExternalDependency { id: DependencyId, @@ -18,6 +20,7 @@ impl RemoteToExternalDependency { } } +#[cacheable_dyn] impl Dependency for RemoteToExternalDependency { fn id(&self) -> &DependencyId { &self.id @@ -36,6 +39,7 @@ impl Dependency for RemoteToExternalDependency { } } +#[cacheable_dyn] impl ModuleDependency for RemoteToExternalDependency { fn request(&self) -> &str { &self.request diff --git a/crates/rspack_plugin_mf/src/sharing/consume_shared_fallback_dependency.rs b/crates/rspack_plugin_mf/src/sharing/consume_shared_fallback_dependency.rs index e77a6ab2933..64fa7f0dca6 100644 --- a/crates/rspack_plugin_mf/src/sharing/consume_shared_fallback_dependency.rs +++ b/crates/rspack_plugin_mf/src/sharing/consume_shared_fallback_dependency.rs @@ -1,8 +1,10 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AsContextDependency, AsDependencyTemplate, Dependency, DependencyCategory, DependencyId, DependencyType, ModuleDependency, }; +#[cacheable] #[derive(Debug, Clone)] pub struct ConsumeSharedFallbackDependency { id: DependencyId, @@ -18,6 +20,7 @@ impl ConsumeSharedFallbackDependency { } } +#[cacheable_dyn] impl Dependency for ConsumeSharedFallbackDependency { fn id(&self) -> &DependencyId { &self.id @@ -36,6 +39,7 @@ impl Dependency for ConsumeSharedFallbackDependency { } } +#[cacheable_dyn] impl ModuleDependency for ConsumeSharedFallbackDependency { fn request(&self) -> &str { &self.request diff --git a/crates/rspack_plugin_mf/src/sharing/consume_shared_module.rs b/crates/rspack_plugin_mf/src/sharing/consume_shared_module.rs index ca1e489fd6d..53c5707382e 100644 --- a/crates/rspack_plugin_mf/src/sharing/consume_shared_module.rs +++ b/crates/rspack_plugin_mf/src/sharing/consume_shared_module.rs @@ -1,6 +1,7 @@ use std::borrow::Cow; use async_trait::async_trait; +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_collections::{Identifiable, Identifier}; use rspack_core::{ async_module_factory, impl_module_meta_info, impl_source_map_config, rspack_sources::Source, @@ -21,6 +22,7 @@ use super::{ use crate::{utils::json_stringify, ConsumeOptions}; #[impl_source_map_config] +#[cacheable] #[derive(Debug)] pub struct ConsumeSharedModule { blocks: Vec, @@ -111,6 +113,7 @@ impl DependenciesBlock for ConsumeSharedModule { } } +#[cacheable_dyn] #[async_trait] impl Module for ConsumeSharedModule { impl_module_meta_info!(); diff --git a/crates/rspack_plugin_mf/src/sharing/consume_shared_plugin.rs b/crates/rspack_plugin_mf/src/sharing/consume_shared_plugin.rs index 4e6caed05cc..5c9692c8b98 100644 --- a/crates/rspack_plugin_mf/src/sharing/consume_shared_plugin.rs +++ b/crates/rspack_plugin_mf/src/sharing/consume_shared_plugin.rs @@ -5,6 +5,7 @@ use std::{fmt, path::Path, sync::Arc}; use async_trait::async_trait; use regex::Regex; +use rspack_cacheable::cacheable; use rspack_core::{ ApplyContext, BoxModule, ChunkUkey, Compilation, CompilationAdditionalTreeRuntimeRequirements, CompilationParams, CompilerOptions, CompilerThisCompilation, Context, DependencyCategory, @@ -21,6 +22,7 @@ use super::{ consume_shared_runtime_module::ConsumeSharedRuntimeModule, }; +#[cacheable] #[derive(Debug, Clone, Hash)] pub struct ConsumeOptions { pub import: Option, @@ -34,6 +36,7 @@ pub struct ConsumeOptions { pub eager: bool, } +#[cacheable] #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum ConsumeVersion { Version(String), diff --git a/crates/rspack_plugin_mf/src/sharing/provide_for_shared_dependency.rs b/crates/rspack_plugin_mf/src/sharing/provide_for_shared_dependency.rs index f1a428176a0..c45f91596b7 100644 --- a/crates/rspack_plugin_mf/src/sharing/provide_for_shared_dependency.rs +++ b/crates/rspack_plugin_mf/src/sharing/provide_for_shared_dependency.rs @@ -1,8 +1,10 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AsContextDependency, AsDependencyTemplate, Dependency, DependencyCategory, DependencyId, DependencyType, ModuleDependency, }; +#[cacheable] #[derive(Debug, Clone)] pub struct ProvideForSharedDependency { id: DependencyId, @@ -18,6 +20,7 @@ impl ProvideForSharedDependency { } } +#[cacheable_dyn] impl Dependency for ProvideForSharedDependency { fn id(&self) -> &DependencyId { &self.id @@ -36,6 +39,7 @@ impl Dependency for ProvideForSharedDependency { } } +#[cacheable_dyn] impl ModuleDependency for ProvideForSharedDependency { fn request(&self) -> &str { &self.request diff --git a/crates/rspack_plugin_mf/src/sharing/provide_shared_dependency.rs b/crates/rspack_plugin_mf/src/sharing/provide_shared_dependency.rs index be7558e00a0..cf2bd540a69 100644 --- a/crates/rspack_plugin_mf/src/sharing/provide_shared_dependency.rs +++ b/crates/rspack_plugin_mf/src/sharing/provide_shared_dependency.rs @@ -1,3 +1,4 @@ +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_core::{ AsContextDependency, AsDependencyTemplate, Dependency, DependencyCategory, DependencyId, DependencyType, ModuleDependency, @@ -6,6 +7,7 @@ use rspack_core::{ use super::provide_shared_plugin::ProvideVersion; use crate::ConsumeVersion; +#[cacheable] #[derive(Debug, Clone)] pub struct ProvideSharedDependency { id: DependencyId, @@ -55,6 +57,7 @@ impl ProvideSharedDependency { } } +#[cacheable_dyn] impl Dependency for ProvideSharedDependency { fn id(&self) -> &DependencyId { &self.id @@ -77,6 +80,7 @@ impl Dependency for ProvideSharedDependency { } } +#[cacheable_dyn] impl ModuleDependency for ProvideSharedDependency { fn request(&self) -> &str { &self.request diff --git a/crates/rspack_plugin_mf/src/sharing/provide_shared_module.rs b/crates/rspack_plugin_mf/src/sharing/provide_shared_module.rs index 17ef1318b29..ed0d1adf099 100644 --- a/crates/rspack_plugin_mf/src/sharing/provide_shared_module.rs +++ b/crates/rspack_plugin_mf/src/sharing/provide_shared_module.rs @@ -1,6 +1,7 @@ use std::borrow::Cow; use async_trait::async_trait; +use rspack_cacheable::{cacheable, cacheable_dyn}; use rspack_collections::{Identifiable, Identifier}; use rspack_core::{ async_module_factory, impl_module_meta_info, impl_source_map_config, module_update_hash, @@ -23,6 +24,7 @@ use super::{ use crate::ConsumeVersion; #[impl_source_map_config] +#[cacheable] #[derive(Debug)] pub struct ProvideSharedModule { blocks: Vec, @@ -109,6 +111,7 @@ impl DependenciesBlock for ProvideSharedModule { } } +#[cacheable_dyn] #[async_trait] impl Module for ProvideSharedModule { impl_module_meta_info!(); diff --git a/crates/rspack_plugin_mf/src/sharing/provide_shared_plugin.rs b/crates/rspack_plugin_mf/src/sharing/provide_shared_plugin.rs index b381c261eba..7d8fe8d45ae 100644 --- a/crates/rspack_plugin_mf/src/sharing/provide_shared_plugin.rs +++ b/crates/rspack_plugin_mf/src/sharing/provide_shared_plugin.rs @@ -61,6 +61,7 @@ impl ProvideOptions { } } +#[rspack_cacheable::cacheable] #[derive(Debug, Default, Clone, PartialEq, Eq, Hash)] pub enum ProvideVersion { Version(String), diff --git a/crates/rspack_plugin_runtime/Cargo.toml b/crates/rspack_plugin_runtime/Cargo.toml index fad65445b26..63c63020da6 100644 --- a/crates/rspack_plugin_runtime/Cargo.toml +++ b/crates/rspack_plugin_runtime/Cargo.toml @@ -13,6 +13,7 @@ cow-utils = { workspace = true } derivative = { workspace = true } indexmap = { workspace = true } itertools = { workspace = true } +rspack_cacheable = { workspace = true } rspack_collections = { workspace = true } rspack_core = { workspace = true } rspack_error = { workspace = true } @@ -20,6 +21,7 @@ rspack_hash = { workspace = true } rspack_hook = { workspace = true } rspack_plugin_javascript = { workspace = true } rspack_util = { workspace = true } + rustc-hash = { workspace = true } serde_json = { workspace = true } tracing = { workspace = true } diff --git a/crates/rspack_plugin_runtime/src/runtime_module/chunk_prefetch_trigger.rs b/crates/rspack_plugin_runtime/src/runtime_module/chunk_prefetch_trigger.rs index 39b544a7569..9743a087fab 100644 --- a/crates/rspack_plugin_runtime/src/runtime_module/chunk_prefetch_trigger.rs +++ b/crates/rspack_plugin_runtime/src/runtime_module/chunk_prefetch_trigger.rs @@ -2,6 +2,7 @@ use std::hash::BuildHasherDefault; use cow_utils::CowUtils; use indexmap::IndexMap; +use rspack_cacheable::with::AsMap; use rspack_collections::Identifier; use rspack_core::{ impl_runtime_module, @@ -14,6 +15,7 @@ use rustc_hash::FxHasher; #[derive(Debug)] pub struct ChunkPrefetchTriggerRuntimeModule { id: Identifier, + #[cacheable(with=AsMap)] chunk_map: IndexMap, BuildHasherDefault>, } diff --git a/crates/rspack_plugin_runtime/src/runtime_module/chunk_preload_trigger.rs b/crates/rspack_plugin_runtime/src/runtime_module/chunk_preload_trigger.rs index 1870801742d..eea026a7059 100644 --- a/crates/rspack_plugin_runtime/src/runtime_module/chunk_preload_trigger.rs +++ b/crates/rspack_plugin_runtime/src/runtime_module/chunk_preload_trigger.rs @@ -2,6 +2,7 @@ use std::hash::BuildHasherDefault; use cow_utils::CowUtils; use indexmap::IndexMap; +use rspack_cacheable::with::AsMap; use rspack_collections::Identifier; use rspack_core::{ impl_runtime_module, @@ -14,6 +15,7 @@ use rustc_hash::FxHasher; #[derive(Debug)] pub struct ChunkPreloadTriggerRuntimeModule { id: Identifier, + #[cacheable(with=AsMap)] chunk_map: IndexMap, BuildHasherDefault>, } diff --git a/crates/rspack_plugin_runtime/src/runtime_module/get_chunk_filename.rs b/crates/rspack_plugin_runtime/src/runtime_module/get_chunk_filename.rs index 9ab6bb12a09..e97dff98a22 100644 --- a/crates/rspack_plugin_runtime/src/runtime_module/get_chunk_filename.rs +++ b/crates/rspack_plugin_runtime/src/runtime_module/get_chunk_filename.rs @@ -2,6 +2,7 @@ use std::{cmp::Ordering, fmt}; use indexmap::IndexMap; use itertools::Itertools; +use rspack_cacheable::with::Unsupported; use rspack_collections::{DatabaseItem, Identifier, UkeyIndexMap, UkeyIndexSet}; use rspack_core::{ get_filename_without_hash_length, impl_runtime_module, @@ -23,10 +24,13 @@ type GetFilenameForChunk = Box Option pub struct GetChunkFilenameRuntimeModule { id: Identifier, chunk: Option, + #[cacheable(with=Unsupported)] content_type: &'static str, source_type: SourceType, global: String, + #[cacheable(with=Unsupported)] all_chunks: GetChunkFilenameAllChunks, + #[cacheable(with=Unsupported)] filename_for_chunk: GetFilenameForChunk, } diff --git a/crates/rspack_plugin_runtime/src/runtime_module_from_js.rs b/crates/rspack_plugin_runtime/src/runtime_module_from_js.rs index 327b8af46df..e0f9814197d 100644 --- a/crates/rspack_plugin_runtime/src/runtime_module_from_js.rs +++ b/crates/rspack_plugin_runtime/src/runtime_module_from_js.rs @@ -1,6 +1,7 @@ use std::sync::Arc; use derivative::Derivative; +use rspack_cacheable::with::Unsupported; use rspack_collections::Identifier; use rspack_core::{ impl_runtime_module, @@ -16,6 +17,7 @@ type GenerateFn = Arc rspack_error::Result + Send + Sync>; pub struct RuntimeModuleFromJs { pub name: String, #[derivative(Debug = "ignore")] + #[cacheable(with=Unsupported)] pub generator: GenerateFn, pub full_hash: bool, pub dependent_hash: bool, diff --git a/crates/rspack_plugin_wasm/Cargo.toml b/crates/rspack_plugin_wasm/Cargo.toml index 3de8962855e..0f178845fca 100644 --- a/crates/rspack_plugin_wasm/Cargo.toml +++ b/crates/rspack_plugin_wasm/Cargo.toml @@ -14,6 +14,7 @@ cow-utils = { workspace = true } dashmap = { workspace = true } indexmap = { workspace = true } rayon = { workspace = true } +rspack_cacheable = { workspace = true } rspack_collections = { workspace = true } rspack_core = { workspace = true } rspack_error = { workspace = true } diff --git a/crates/rspack_plugin_wasm/src/dependency/wasm_import_dependency.rs b/crates/rspack_plugin_wasm/src/dependency/wasm_import_dependency.rs index fd772e81bba..1dc29bd9811 100644 --- a/crates/rspack_plugin_wasm/src/dependency/wasm_import_dependency.rs +++ b/crates/rspack_plugin_wasm/src/dependency/wasm_import_dependency.rs @@ -1,3 +1,7 @@ +use rspack_cacheable::{ + cacheable, cacheable_dyn, + with::{AsPreset, Unsupported}, +}; use rspack_core::{ AsContextDependency, AsDependencyTemplate, Dependency, DependencyCategory, DependencyId, DependencyRange, DependencyType, ExtendedReferencedExport, ModuleDependency, ModuleGraph, @@ -8,13 +12,16 @@ use swc_core::ecma::atoms::Atom; use crate::WasmNode; #[allow(dead_code)] +#[cacheable] #[derive(Debug, Clone)] pub struct WasmImportDependency { id: DependencyId, + #[cacheable(with=AsPreset)] name: Atom, request: String, // only_direct_import: bool, /// the WASM AST node + #[cacheable(with=Unsupported)] pub desc: WasmNode, span: Option, } @@ -35,6 +42,7 @@ impl WasmImportDependency { } } +#[cacheable_dyn] impl Dependency for WasmImportDependency { fn id(&self) -> &DependencyId { &self.id @@ -61,6 +69,7 @@ impl Dependency for WasmImportDependency { } } +#[cacheable_dyn] impl ModuleDependency for WasmImportDependency { fn request(&self) -> &str { &self.request diff --git a/crates/rspack_plugin_wasm/src/parser_and_generator.rs b/crates/rspack_plugin_wasm/src/parser_and_generator.rs index 2125c6e86ad..195435acf05 100644 --- a/crates/rspack_plugin_wasm/src/parser_and_generator.rs +++ b/crates/rspack_plugin_wasm/src/parser_and_generator.rs @@ -3,6 +3,7 @@ use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; use indexmap::IndexMap; +use rspack_cacheable::{cacheable, cacheable_dyn, with::Unsupported}; use rspack_collections::Identifier; use rspack_core::rspack_sources::{BoxSource, RawStringSource, Source, SourceExt}; use rspack_core::DependencyType::WasmImport; @@ -21,13 +22,16 @@ use wasmparser::{Import, Parser, Payload}; use crate::dependency::WasmImportDependency; use crate::ModuleIdToFileName; +#[cacheable] #[derive(Debug)] pub struct AsyncWasmParserAndGenerator { + #[cacheable(with=Unsupported)] pub(crate) module_id_to_filename: ModuleIdToFileName, } pub(crate) static WASM_SOURCE_TYPE: &[SourceType; 2] = &[SourceType::Wasm, SourceType::JavaScript]; +#[cacheable_dyn] impl ParserAndGenerator for AsyncWasmParserAndGenerator { fn source_types(&self) -> &[SourceType] { WASM_SOURCE_TYPE diff --git a/crates/rspack_regex/Cargo.toml b/crates/rspack_regex/Cargo.toml index a1002bde998..179498d3894 100644 --- a/crates/rspack_regex/Cargo.toml +++ b/crates/rspack_regex/Cargo.toml @@ -13,6 +13,7 @@ cow-utils = { workspace = true } napi = { workspace = true } regex-syntax = { version = "0.8.5", default-features = false, features = ["std"] } regress = "0.10.1" +rspack_cacheable = { workspace = true } rspack_error = { workspace = true } swc_core = { workspace = true, features = ["ecma_ast"] } diff --git a/crates/rspack_regex/src/lib.rs b/crates/rspack_regex/src/lib.rs index a66d24fba06..059220976e9 100644 --- a/crates/rspack_regex/src/lib.rs +++ b/crates/rspack_regex/src/lib.rs @@ -6,12 +6,17 @@ mod napi; use std::fmt::Debug; use cow_utils::CowUtils; +use rspack_cacheable::{ + cacheable, + with::{AsString, AsStringConverter}, +}; use rspack_error::Error; use swc_core::ecma::ast::Regex as SwcRegex; use self::algo::Algo; /// Using wrapper type required by [TryFrom] trait +#[cacheable(with=AsString)] #[derive(Clone, Hash)] pub struct RspackRegex { algo: Box, @@ -104,3 +109,16 @@ impl TryFrom for RspackRegex { RspackRegex::with_flags(value.exp.as_ref(), value.flags.as_ref()) } } + +impl AsStringConverter for RspackRegex { + fn to_string(&self) -> Result { + Ok(format!("{}#{}", self.flags, self.source)) + } + fn from_str(s: &str) -> Result + where + Self: Sized, + { + let (flags, source) = s.split_once("#").expect("should have flags"); + Ok(RspackRegex::with_flags(source, flags).expect("should generate regex")) + } +} diff --git a/crates/rspack_util/Cargo.toml b/crates/rspack_util/Cargo.toml index 34af242b5f9..889b3efbac3 100644 --- a/crates/rspack_util/Cargo.toml +++ b/crates/rspack_util/Cargo.toml @@ -17,6 +17,7 @@ indexmap = { workspace = true } itoa = { version = "1.0.14" } regex = { workspace = true } ropey = { workspace = true } +rspack_cacheable = { workspace = true } rustc-hash = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } diff --git a/crates/rspack_util/src/source_map.rs b/crates/rspack_util/src/source_map.rs index bffad9a7584..7bc418ba63f 100644 --- a/crates/rspack_util/src/source_map.rs +++ b/crates/rspack_util/src/source_map.rs @@ -1,8 +1,12 @@ use bitflags::bitflags; +use rspack_cacheable::cacheable; + +#[cacheable] +#[derive(Debug, PartialEq, Hash, Eq, Clone, Copy, PartialOrd, Ord)] +pub struct SourceMapKind(u8); bitflags! { - #[derive(Debug, PartialEq, Eq, Clone, Copy)] - pub struct SourceMapKind: u8 { + impl SourceMapKind: u8 { const SourceMap = 1 << 0; const SimpleSourceMap = 1 << 1; const Cheap = 1 << 2;