diff --git a/Cargo.lock b/Cargo.lock index 1ab01e74698b..6197967da4de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4109,6 +4109,7 @@ version = "0.2.0" dependencies = [ "proc-macro2", "quote", + "regex", "syn 2.0.90", ] diff --git a/crates/rspack_core/src/cache/mod.rs b/crates/rspack_core/src/cache/mod.rs index 9e7dc5b3d42c..d8ddb7c64d19 100644 --- a/crates/rspack_core/src/cache/mod.rs +++ b/crates/rspack_core/src/cache/mod.rs @@ -31,7 +31,7 @@ pub trait Cache: Debug + Send + Sync { } pub fn new_cache( - compiler_path: &String, + compiler_path: &str, compiler_option: Arc, input_filesystem: Arc, intermediate_filesystem: Arc, diff --git a/crates/rspack_core/src/cache/persistent/mod.rs b/crates/rspack_core/src/cache/persistent/mod.rs index 0a8b947cb9cf..031b0f60542a 100644 --- a/crates/rspack_core/src/cache/persistent/mod.rs +++ b/crates/rspack_core/src/cache/persistent/mod.rs @@ -8,6 +8,7 @@ use std::{path::PathBuf, sync::Arc}; pub use cacheable_context::{CacheableContext, FromContext}; use occasion::MakeOccasion; use rspack_fs::{FileSystem, IntermediateFileSystem, Result}; +use rspack_macros::rspack_version; use rspack_paths::ArcPath; use rustc_hash::FxHashSet as HashSet; @@ -36,7 +37,7 @@ pub struct PersistentCache { impl PersistentCache { pub fn new( - compiler_path: &String, + compiler_path: &str, option: &PersistentCacheOptions, compiler_options: Arc, input_filesystem: Arc, @@ -45,7 +46,7 @@ impl PersistentCache { let version = version::get_version( input_filesystem.clone(), &option.build_dependencies, - vec![compiler_path, &option.version], + vec![compiler_path, &option.version, rspack_version!()], )?; let storage = create_storage(option.storage.clone(), version, intermediate_filesystem); let context = Arc::new(CacheableContext { diff --git a/crates/rspack_core/src/cache/persistent/version.rs b/crates/rspack_core/src/cache/persistent/version.rs index 5c802f67d9a2..5ea7aa79123a 100644 --- a/crates/rspack_core/src/cache/persistent/version.rs +++ b/crates/rspack_core/src/cache/persistent/version.rs @@ -8,7 +8,7 @@ use rspack_paths::AssertUtf8; pub fn get_version( fs: Arc, dependencies: &Vec, - salt: Vec<&String>, + salt: Vec<&str>, ) -> Result { let mut hasher = DefaultHasher::new(); for dep in dependencies { diff --git a/crates/rspack_macros/Cargo.toml b/crates/rspack_macros/Cargo.toml index a1e068ed7ec1..ed7796615235 100644 --- a/crates/rspack_macros/Cargo.toml +++ b/crates/rspack_macros/Cargo.toml @@ -12,4 +12,5 @@ proc-macro = true [dependencies] proc-macro2 = { workspace = true } quote = { workspace = true } +regex = { workspace = true } syn = { workspace = true, features = ["full"] } diff --git a/crates/rspack_macros/src/lib.rs b/crates/rspack_macros/src/lib.rs index 5b2363ae2003..a39c30ad44ca 100644 --- a/crates/rspack_macros/src/lib.rs +++ b/crates/rspack_macros/src/lib.rs @@ -5,6 +5,7 @@ mod cacheable_dyn; mod hook; mod merge; mod plugin; +mod rspack_version; mod runtime_module; mod source_map_config; @@ -101,3 +102,9 @@ pub fn disable_cacheable_dyn( ) -> proc_macro::TokenStream { cacheable_dyn::disable_cacheable_dyn(tokens) } + +#[proc_macro] +pub fn rspack_version(_tokens: proc_macro::TokenStream) -> proc_macro::TokenStream { + let version = rspack_version::rspack_version(); + quote::quote! { #version }.into() +} diff --git a/crates/rspack_macros/src/rspack_version.rs b/crates/rspack_macros/src/rspack_version.rs new file mode 100644 index 000000000000..b1dcc4ae3ef6 --- /dev/null +++ b/crates/rspack_macros/src/rspack_version.rs @@ -0,0 +1,9 @@ +pub fn rspack_version() -> String { + let re = regex::Regex::new(r#""version": ?"([0-9\.]+)""#).expect("should create regex"); + // package.json in project root directory + let package_json = include_str!("../../../package.json"); + let version = re + .captures(package_json) + .expect("can not found version field in project package.json"); + version[1].to_string() +}