Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(wip): Support #[cache_item] in external rust plugins #1606

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Cargo.lock

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

4 changes: 3 additions & 1 deletion crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ pub mod record;
pub mod resource;
pub mod stats;

pub use farmfe_macro_cache_item::cache_item;
pub use farmfe_macro_cache_item::{cache_item, custom_meta_data};

/// Version of this core crate, if the core data structures changed, and the changes will affect the memory layout,
/// like adding or removing a field, this version should be bumped. So plugin loader can recognize compatibility of the dynamic library plugins and the core.
pub const VERSION: &str = "0.5.0";

// re-export common external crates
pub use dashmap;
pub use downcast_rs;
pub use enhanced_magic_string;
pub use parking_lot;
pub use petgraph;
pub use ptr_meta;
#[cfg(feature = "profile")]
pub use puffin;
pub use rayon;
Expand Down
8 changes: 4 additions & 4 deletions crates/core/src/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use blake2::{
Blake2bVar,
};
use downcast_rs::{impl_downcast, Downcast};
use farmfe_macro_cache_item::cache_item;
use farmfe_macro_cache_item::{cache_item, custom_meta_data};
use farmfe_utils::relative;
use heck::AsLowerCamelCase;
use relative_path::RelativePath;
Expand Down Expand Up @@ -262,7 +262,7 @@ pub trait CustomModuleMetaData: Any + Send + Sync + Downcast {}
impl_downcast!(SerializeCustomModuleMetaData);

/// initial empty custom data, plugins may replace this
#[cache_item(CustomModuleMetaData)]
#[custom_meta_data]
pub struct EmptyModuleMetaData;

#[cache_item]
Expand Down Expand Up @@ -754,7 +754,7 @@ impl serde::Serialize for ModuleId {
#[cfg(test)]
mod tests {
use crate::config::Mode;
use farmfe_macro_cache_item::cache_item;
use farmfe_macro_cache_item::custom_meta_data;
use rkyv_dyn::archive_dyn;
use rkyv_typename::TypeName;
use std::collections::HashSet;
Expand Down Expand Up @@ -858,7 +858,7 @@ mod tests {
fn module_serialization() {
let mut module = Module::new(ModuleId::new("/root/index.ts", "", "/root"));

#[cache_item(CustomModuleMetaData)]
#[custom_meta_data]
pub struct StructModuleData {
ast: String,
imports: Vec<String>,
Expand Down
67 changes: 37 additions & 30 deletions crates/macro_cache_item/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,52 @@
use proc_macro::TokenStream;
use proc_macro2::Ident;
use proc_macro2::Span;
use quote::quote;
use syn::{parse_macro_input, parse_quote, Item};
use syn::{parse_macro_input, parse_quote, Ident, Item};

#[proc_macro_attribute]
pub fn cache_item(attr: TokenStream, input: TokenStream) -> TokenStream {
pub fn cache_item(_attr: TokenStream, input: TokenStream) -> TokenStream {
let item: Item = parse_macro_input!(input);

if !attr.is_empty() {
let args: Ident = parse_macro_input!(attr);

let item_ident = match &item {
Item::Enum(e) => e.ident.clone(),
Item::Struct(s) => s.ident.clone(),
_ => {
let ts: proc_macro2::TokenStream = parse_quote! {
compile_error!("#[cache_item] can only be used on struct or enum")
};
return ts.into();
}
};
let derives = quote! {
use rkyv::*;

#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)]
#[archive_attr(derive(TypeName))]
#item

#[archive_dyn(deserialize)]
impl #args for #item_ident {}
impl #args for rkyv::Archived<#item_ident> {}
};

return derives.into();
}
let derives = quote! {
use rkyv::*;

#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)]
#item
};

derives.into()
}

#[proc_macro_attribute]
pub fn custom_meta_data(attr: TokenStream, input: TokenStream) -> TokenStream {
let item: Item = parse_macro_input!(input);
let args: Ident = if !attr.is_empty() {
parse_macro_input!(attr)
} else {
Ident::new("CustomModuleMetaData", Span::call_site())
};

let item_ident = match &item {
Item::Enum(e) => e.ident.clone(),
Item::Struct(s) => s.ident.clone(),
_ => {
let ts: proc_macro2::TokenStream = parse_quote! {
compile_error!("#[cache_item] can only be used on struct or enum")
};
return ts.into();
}
};

let derives = quote! {
use rkyv::*;

#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)]
#[archive_attr(derive(TypeName))]
#item

#[archive_dyn(deserialize)]
impl #args for #item_ident {}
impl #args for rkyv::Archived<#item_ident> {}
};

derives.into()
Expand Down
1 change: 0 additions & 1 deletion crates/plugin_css/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ documentation = "https://docs.rs/farmfe_plugin_css"
farmfe_core = { path = "../core", version = "0.6.1" }
farmfe_toolkit = { path = "../toolkit", version = "0.0.10" }
farmfe_utils = { path = "../utils", version = "0.1.5" }
farmfe_macro_cache_item = { path = "../macro_cache_item", version = "0.1.3" }
rkyv = { version = "0.7.42" }

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/plugin_css/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::collections::HashMap;
use std::{path::PathBuf, sync::Arc};

use dep_analyzer::DepAnalyzer;
use farmfe_core::cache_item;
use farmfe_core::config::minify::MinifyOptions;
use farmfe_core::module::CommentsMetaData;
use farmfe_core::{
Expand Down Expand Up @@ -32,7 +33,6 @@ use farmfe_core::{
serde_json, serialize,
swc_css_ast::Stylesheet,
};
use farmfe_macro_cache_item::cache_item;
use farmfe_toolkit::common::{create_swc_source_map, load_source_original_source_map, PathFilter};
use farmfe_toolkit::css::ParseCssModuleResult;
use farmfe_toolkit::lazy_static::lazy_static;
Expand Down
4 changes: 4 additions & 0 deletions crates/plugin_progress/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ farmfe_core = { path = "../core", version = "0.6.1" }
farmfe_testing_helpers = { path = "../testing_helpers", version = "0.0.10" }
indicatif = "0.17.7"
console = "0.15.8"
rkyv = "0.7"
rkyv_dyn = "0.7"
rkyv_typename = "0.7"
ptr_meta = "0.1"
Loading
Loading