From 41b80c0b88e9f61cf341a8958f9d6e91e18005d2 Mon Sep 17 00:00:00 2001 From: brightwu <1521488775@qq.com> Date: Mon, 15 Jul 2024 09:59:28 +0800 Subject: [PATCH] chore(wip): stash code --- Cargo.lock | 5 +- crates/core/src/lib.rs | 4 +- crates/core/src/module/mod.rs | 8 +- crates/macro_cache_item/src/lib.rs | 67 ++-- crates/plugin_css/Cargo.toml | 1 - crates/plugin_css/src/lib.rs | 2 +- crates/plugin_progress/Cargo.toml | 4 + crates/plugin_progress/src/lib.rs | 487 ++++++++++++++++------------- 8 files changed, 314 insertions(+), 264 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f6c4f24cb..a13b815c2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1608,7 +1608,6 @@ name = "farmfe_plugin_css" version = "0.0.10" dependencies = [ "farmfe_core", - "farmfe_macro_cache_item", "farmfe_testing_helpers", "farmfe_toolkit", "farmfe_utils 0.1.5", @@ -1689,6 +1688,10 @@ dependencies = [ "farmfe_core", "farmfe_testing_helpers", "indicatif", + "ptr_meta", + "rkyv", + "rkyv_dyn", + "rkyv_typename", ] [[package]] diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 13e8bf4b7..16395a695 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -16,7 +16,7 @@ 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. @@ -24,9 +24,11 @@ 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; diff --git a/crates/core/src/module/mod.rs b/crates/core/src/module/mod.rs index 817fe5fa3..38c4b194f 100644 --- a/crates/core/src/module/mod.rs +++ b/crates/core/src/module/mod.rs @@ -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; @@ -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] @@ -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; @@ -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, diff --git a/crates/macro_cache_item/src/lib.rs b/crates/macro_cache_item/src/lib.rs index efe129f06..e9a94173b 100644 --- a/crates/macro_cache_item/src/lib.rs +++ b/crates/macro_cache_item/src/lib.rs @@ -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() diff --git a/crates/plugin_css/Cargo.toml b/crates/plugin_css/Cargo.toml index 14c6e9449..d8955e22b 100644 --- a/crates/plugin_css/Cargo.toml +++ b/crates/plugin_css/Cargo.toml @@ -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] diff --git a/crates/plugin_css/src/lib.rs b/crates/plugin_css/src/lib.rs index 2ccf4f9d1..eebb53e76 100644 --- a/crates/plugin_css/src/lib.rs +++ b/crates/plugin_css/src/lib.rs @@ -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::{ @@ -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; diff --git a/crates/plugin_progress/Cargo.toml b/crates/plugin_progress/Cargo.toml index c917396a7..97db8d925 100644 --- a/crates/plugin_progress/Cargo.toml +++ b/crates/plugin_progress/Cargo.toml @@ -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" diff --git a/crates/plugin_progress/src/lib.rs b/crates/plugin_progress/src/lib.rs index 27ef851e8..ef529131f 100644 --- a/crates/plugin_progress/src/lib.rs +++ b/crates/plugin_progress/src/lib.rs @@ -1,236 +1,271 @@ -use farmfe_core::{ - config::Config, context::CompilationContext, error::Result, parking_lot::Mutex, plugin::Plugin, +// use farmfe_core::{ +// cache_item, config::Config, context::CompilationContext, custom_meta_data, error::Result, +// parking_lot::Mutex, plugin::Plugin, +// }; +// use indicatif::{ProgressBar, ProgressStyle}; +// use std::{sync::Arc, time::Duration}; + +// pub struct FarmPluginProgress { +// module_count: Arc>, +// progress_bar: ProgressBar, +// first_build: Mutex, +// } + +// impl FarmPluginProgress { +// pub fn new(_config: &Config) -> Self { +// let spinner_style = +// ProgressStyle::with_template("{prefix:.bold.dim} {spinner:.green} {wide_msg}") +// .unwrap() +// .tick_chars("⠁⠂⠄⡀⢀⠠⠐⠈ "); + +// let progress_bar = ProgressBar::new(1); +// progress_bar.set_style(spinner_style.clone()); +// progress_bar.set_prefix("[ building ]"); + +// // tick every 200ms +// progress_bar.enable_steady_tick(Duration::from_millis(200)); + +// Self { +// module_count: Arc::new(Mutex::new(0)), +// progress_bar, +// first_build: Mutex::new(true), +// } +// } + +// pub fn increment_module_count(&self) { +// let mut count = self.module_count.lock(); +// *count += 1; +// } + +// pub fn reset_module_count(&self) { +// let mut count = self.module_count.lock(); +// *count = 0; +// } + +// pub fn get_module_count(&self) -> u32 { +// let count = self.module_count.lock(); +// *count +// } +// } + +// impl Plugin for FarmPluginProgress { +// fn name(&self) -> &'static str { +// "FarmPluginProgress" +// } + +// fn priority(&self) -> i32 { +// i32::MAX +// } + +// fn update_modules( +// &self, +// _params: &mut farmfe_core::plugin::PluginUpdateModulesHookParams, +// _context: &Arc, +// ) -> Result> { +// self.progress_bar.reset(); +// self.reset_module_count(); +// Ok(None) +// } + +// fn build_start(&self, _context: &Arc) -> Result> { +// self.reset_module_count(); +// Ok(None) +// } + +// fn transform( +// &self, +// param: &farmfe_core::plugin::PluginTransformHookParam, +// _context: &Arc, +// ) -> Result> { +// self.increment_module_count(); +// let count = self.get_module_count(); +// let module = ¶m.module_id; +// self +// .progress_bar +// .set_message(format!("transform ({count}) {module}")); +// self.progress_bar.inc(1); + +// Ok(None) +// } + +// fn handle_persistent_cached_module( +// &self, +// module: &farmfe_core::module::Module, +// _context: &Arc, +// ) -> Result> { +// self.increment_module_count(); +// let count = self.get_module_count(); +// let module = &module.id; +// self.progress_bar.set_message(format!( +// "load cached module({count}) {}", +// module.to_string() +// )); +// self.progress_bar.inc(1); + +// Ok(None) +// } + +// fn module_graph_updated( +// &self, +// _param: &farmfe_core::plugin::PluginModuleGraphUpdatedHookParams, +// _context: &Arc, +// ) -> Result> { +// let first_build = self.first_build.lock(); + +// if !*first_build { +// self.progress_bar.finish_and_clear(); +// } + +// Ok(None) +// } + +// fn optimize_module_graph( +// &self, +// _module_graph: &mut farmfe_core::module::module_graph::ModuleGraph, +// _context: &Arc, +// ) -> Result> { +// let first_build = self.first_build.lock(); + +// if *first_build { +// self.progress_bar.set_message("optimize module graph"); +// self.progress_bar.inc(1); +// } + +// Ok(None) +// } + +// fn render_resource_pot_modules( +// &self, +// resource_pot: &farmfe_core::resource::resource_pot::ResourcePot, +// _context: &Arc, +// _hook_context: &farmfe_core::plugin::PluginHookContext, +// ) -> Result> { +// let first_build = self.first_build.lock(); + +// if *first_build { +// self +// .progress_bar +// .set_message(format!("render resource pot modules {}", resource_pot.name)); +// self.progress_bar.inc(1); +// } + +// Ok(None) +// } + +// fn render_resource_pot( +// &self, +// param: &farmfe_core::plugin::PluginRenderResourcePotHookParam, +// _context: &Arc, +// ) -> Result> { +// let first_build = self.first_build.lock(); + +// if *first_build { +// let name: &String = ¶m.resource_pot_info.name.clone(); +// self.progress_bar.set_message(format!("render {name}")); +// self.progress_bar.inc(1); +// } + +// Ok(None) +// } + +// fn optimize_resource_pot( +// &self, +// resource: &mut farmfe_core::resource::resource_pot::ResourcePot, +// _context: &Arc, +// ) -> Result> { +// let first_build = self.first_build.lock(); + +// if *first_build { +// self +// .progress_bar +// .set_message(format!("optimize resource pot {}", resource.name)); +// self.progress_bar.inc(1); +// } + +// Ok(None) +// } + +// fn generate_resources( +// &self, +// resource_pot: &mut farmfe_core::resource::resource_pot::ResourcePot, +// _context: &Arc, +// _hook_context: &farmfe_core::plugin::PluginHookContext, +// ) -> Result> { +// let first_build = self.first_build.lock(); + +// if *first_build { +// self.progress_bar.set_message(format!( +// "generate resources for resource pot {}", +// resource_pot.name +// )); +// self.progress_bar.inc(1); +// } + +// Ok(None) +// } + +// fn finalize_resources( +// &self, +// _param: &mut farmfe_core::plugin::PluginFinalizeResourcesHookParams, +// _context: &Arc, +// ) -> Result> { +// let first_build = self.first_build.lock(); + +// if *first_build { +// self.progress_bar.set_message("finalize resources..."); +// self.progress_bar.inc(1); +// } + +// Ok(None) +// } + +// fn finish( +// &self, +// _stat: &farmfe_core::stats::Stats, +// _context: &Arc, +// ) -> Result> { +// let mut first_build = self.first_build.lock(); + +// if *first_build { +// self.progress_bar.finish_and_clear(); + +// *first_build = false; +// } + +// Ok(None) +// } +// } + +use farmfe_core::custom_meta_data; +use farmfe_core::module::{ + CustomModuleMetaData, Module, ModuleMetaData, SerializeCustomModuleMetaData, }; -use indicatif::{ProgressBar, ProgressStyle}; -use std::{sync::Arc, time::Duration}; +use rkyv::*; +use rkyv_dyn::archive_dyn; +use rkyv_typename::TypeName; -pub struct FarmPluginProgress { - module_count: Arc>, - progress_bar: ProgressBar, - first_build: Mutex, +#[archive_dyn(deserialize)] +trait ExampleTrait: CustomModuleMetaData { + fn value(&self) -> String; } -impl FarmPluginProgress { - pub fn new(_config: &Config) -> Self { - let spinner_style = - ProgressStyle::with_template("{prefix:.bold.dim} {spinner:.green} {wide_msg}") - .unwrap() - .tick_chars("⠁⠂⠄⡀⢀⠠⠐⠈ "); - - let progress_bar = ProgressBar::new(1); - progress_bar.set_style(spinner_style.clone()); - progress_bar.set_prefix("[ building ]"); - - // tick every 200ms - progress_bar.enable_steady_tick(Duration::from_millis(200)); - - Self { - module_count: Arc::new(Mutex::new(0)), - progress_bar, - first_build: Mutex::new(true), - } - } - - pub fn increment_module_count(&self) { - let mut count = self.module_count.lock(); - *count += 1; - } +#[derive(Archive, Serialize, Deserialize)] +#[archive_attr(derive(TypeName))] +struct StringStruct(String); - pub fn reset_module_count(&self) { - let mut count = self.module_count.lock(); - *count = 0; - } - - pub fn get_module_count(&self) -> u32 { - let count = self.module_count.lock(); - *count +#[archive_dyn(deserialize)] +impl ExampleTrait for StringStruct { + fn value(&self) -> String { + self.0.clone() } } -impl Plugin for FarmPluginProgress { - fn name(&self) -> &'static str { - "FarmPluginProgress" - } - - fn priority(&self) -> i32 { - i32::MAX - } - - fn update_modules( - &self, - _params: &mut farmfe_core::plugin::PluginUpdateModulesHookParams, - _context: &Arc, - ) -> Result> { - self.progress_bar.reset(); - self.reset_module_count(); - Ok(None) - } - - fn build_start(&self, _context: &Arc) -> Result> { - self.reset_module_count(); - Ok(None) - } - - fn transform( - &self, - param: &farmfe_core::plugin::PluginTransformHookParam, - _context: &Arc, - ) -> Result> { - self.increment_module_count(); - let count = self.get_module_count(); - let module = ¶m.module_id; - self - .progress_bar - .set_message(format!("transform ({count}) {module}")); - self.progress_bar.inc(1); - - Ok(None) - } - - fn handle_persistent_cached_module( - &self, - module: &farmfe_core::module::Module, - _context: &Arc, - ) -> Result> { - self.increment_module_count(); - let count = self.get_module_count(); - let module = &module.id; - self.progress_bar.set_message(format!( - "load cached module({count}) {}", - module.to_string() - )); - self.progress_bar.inc(1); - - Ok(None) - } - - fn module_graph_updated( - &self, - _param: &farmfe_core::plugin::PluginModuleGraphUpdatedHookParams, - _context: &Arc, - ) -> Result> { - let first_build = self.first_build.lock(); - - if !*first_build { - self.progress_bar.finish_and_clear(); - } - - Ok(None) - } - - fn optimize_module_graph( - &self, - _module_graph: &mut farmfe_core::module::module_graph::ModuleGraph, - _context: &Arc, - ) -> Result> { - let first_build = self.first_build.lock(); - - if *first_build { - self.progress_bar.set_message("optimize module graph"); - self.progress_bar.inc(1); - } - - Ok(None) - } - - fn render_resource_pot_modules( - &self, - resource_pot: &farmfe_core::resource::resource_pot::ResourcePot, - _context: &Arc, - _hook_context: &farmfe_core::plugin::PluginHookContext, - ) -> Result> { - let first_build = self.first_build.lock(); - - if *first_build { - self - .progress_bar - .set_message(format!("render resource pot modules {}", resource_pot.name)); - self.progress_bar.inc(1); - } - - Ok(None) - } - - fn render_resource_pot( - &self, - param: &farmfe_core::plugin::PluginRenderResourcePotHookParam, - _context: &Arc, - ) -> Result> { - let first_build = self.first_build.lock(); - - if *first_build { - let name: &String = ¶m.resource_pot_info.name.clone(); - self.progress_bar.set_message(format!("render {name}")); - self.progress_bar.inc(1); - } - - Ok(None) - } - - fn optimize_resource_pot( - &self, - resource: &mut farmfe_core::resource::resource_pot::ResourcePot, - _context: &Arc, - ) -> Result> { - let first_build = self.first_build.lock(); - - if *first_build { - self - .progress_bar - .set_message(format!("optimize resource pot {}", resource.name)); - self.progress_bar.inc(1); - } - - Ok(None) - } - - fn generate_resources( - &self, - resource_pot: &mut farmfe_core::resource::resource_pot::ResourcePot, - _context: &Arc, - _hook_context: &farmfe_core::plugin::PluginHookContext, - ) -> Result> { - let first_build = self.first_build.lock(); - - if *first_build { - self.progress_bar.set_message(format!( - "generate resources for resource pot {}", - resource_pot.name - )); - self.progress_bar.inc(1); - } - - Ok(None) - } - - fn finalize_resources( - &self, - _param: &mut farmfe_core::plugin::PluginFinalizeResourcesHookParams, - _context: &Arc, - ) -> Result> { - let first_build = self.first_build.lock(); - - if *first_build { - self.progress_bar.set_message("finalize resources..."); - self.progress_bar.inc(1); - } - - Ok(None) - } - - fn finish( - &self, - _stat: &farmfe_core::stats::Stats, - _context: &Arc, - ) -> Result> { - let mut first_build = self.first_build.lock(); - - if *first_build { - self.progress_bar.finish_and_clear(); - - *first_build = false; - } - - Ok(None) +impl ExampleTrait for Archived { + fn value(&self) -> String { + self.0.as_str().to_string() } } + +impl CustomModuleMetaData for ArchivedStringStruct {} +impl CustomModuleMetaData for StringStruct {} +impl SerializeCustomModuleMetaData for StringStruct {}