Skip to content

Commit

Permalink
feat: add noop feature to disable macro
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrykingxyz committed Oct 22, 2024
1 parent f0cbce4 commit 4204d6d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions crates/rspack_cacheable/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ license = "MIT"
name = "rspack_cacheable"
version = "0.1.0"

[features]
noop = []

[dependencies]
camino = { workspace = true }
dashmap = { workspace = true }
Expand Down
3 changes: 3 additions & 0 deletions crates/rspack_cacheable/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#[cfg(not(feature = "noop"))]
pub use rspack_macros::{cacheable, cacheable_dyn};
#[cfg(feature = "noop")]
pub use rspack_macros::{disable_cacheable as cacheable, disable_cacheable_dyn as cacheable_dyn};
pub mod r#dyn;
pub mod utils;
pub mod with;
Expand Down
16 changes: 16 additions & 0 deletions crates/rspack_macros/src/cacheable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,19 @@ pub fn impl_cacheable_with(tokens: TokenStream, args: CacheableArgs) -> TokenStr
}
.into()
}

/// impl cacheable when disable
pub fn disable_cacheable(tokens: TokenStream) -> TokenStream {
let mut input = parse_macro_input!(tokens as Item);

let mut visitor = FieldAttrVisitor {
clean: true,
..Default::default()
};
visitor.visit_item_mut(&mut input);

quote! {
#input
}
.into()
}
5 changes: 5 additions & 0 deletions crates/rspack_macros/src/cacheable_dyn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,8 @@ pub fn impl_impl(mut input: ItemImpl) -> TokenStream {
}
.into()
}

/// impl cacheable dyn when disable
pub fn disable_cacheable_dyn(input: TokenStream) -> TokenStream {
input
}
16 changes: 16 additions & 0 deletions crates/rspack_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ pub fn cacheable(
}
}

#[proc_macro_attribute]
pub fn disable_cacheable(
_args: proc_macro::TokenStream,
tokens: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
cacheable::disable_cacheable(tokens)
}

#[proc_macro_attribute]
pub fn cacheable_dyn(
_args: proc_macro::TokenStream,
Expand All @@ -90,3 +98,11 @@ pub fn cacheable_dyn(
_ => panic!("expect Trait or Impl"),
}
}

#[proc_macro_attribute]
pub fn disable_cacheable_dyn(
_args: proc_macro::TokenStream,
tokens: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
cacheable_dyn::disable_cacheable_dyn(tokens)
}

0 comments on commit 4204d6d

Please sign in to comment.