Skip to content

Commit

Permalink
feat: add macro to get rspack version
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrykingxyz committed Dec 18, 2024
1 parent dfb1734 commit 51117f2
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions crates/rspack_core/src/cache/persistent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -36,7 +37,7 @@ pub struct PersistentCache {

impl PersistentCache {
pub fn new(
compiler_path: &String,
compiler_path: &str,
option: &PersistentCacheOptions,
compiler_options: Arc<CompilerOptions>,
input_filesystem: Arc<dyn FileSystem>,
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/cache/persistent/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rspack_paths::AssertUtf8;
pub fn get_version(
fs: Arc<dyn FileSystem>,
dependencies: &Vec<PathBuf>,
salt: Vec<&String>,
salt: Vec<&str>,
) -> Result<String> {
let mut hasher = DefaultHasher::new();
for dep in dependencies {
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ proc-macro = true
[dependencies]
proc-macro2 = { workspace = true }
quote = { workspace = true }
regex = { workspace = true }
syn = { workspace = true, features = ["full"] }
7 changes: 7 additions & 0 deletions crates/rspack_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod cacheable_dyn;
mod hook;
mod merge;
mod plugin;
mod rspack_version;
mod runtime_module;
mod source_map_config;

Expand Down Expand Up @@ -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()
}
7 changes: 7 additions & 0 deletions crates/rspack_macros/src/rspack_version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub fn rspack_version() -> String {
let re = regex::Regex::new(r#""version": ?"([0-9\.]+)""#).unwrap();

Check failure on line 2 in crates/rspack_macros/src/rspack_version.rs

View workflow job for this annotation

GitHub Actions / Rust check

used `unwrap()` on a `Result` value
let version = re
.captures(include_str!("../../../package.json"))
.expect("can not found version field in project package.json");
version[1].to_string()
}

0 comments on commit 51117f2

Please sign in to comment.