Skip to content

Commit

Permalink
chore(refactor): custom moudle meta data
Browse files Browse the repository at this point in the history
  • Loading branch information
wre232114 committed Nov 17, 2024
1 parent 464ca29 commit 57682c3
Show file tree
Hide file tree
Showing 20 changed files with 525 additions and 349 deletions.
81 changes: 7 additions & 74 deletions crates/compiler/src/update/regenerate_resources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,11 @@ use farmfe_core::{
enhanced_magic_string::types::SourceMapOptions,
error::CompilationError,
module::{module_group::ModuleGroupId, Module, ModuleId},
resource::resource_pot::{ResourcePot, ResourcePotMetaData, ResourcePotType},
resource::resource_pot::{ResourcePot, ResourcePotType},
};

use farmfe_plugin_runtime::render_resource_pot::{
resource_pot_to_runtime_object, RenderedJsResourcePot,
};
use farmfe_plugin_runtime::ASYNC_MODULES;
use farmfe_toolkit::hash::base64_encode;
use farmfe_utils::relative;

use crate::{
generate::render_resource_pots::{
render_resource_pot_generate_resources, render_resource_pots_and_generate_resources,
},
write_cache,
generate::render_resource_pots::render_resource_pots_and_generate_resources, write_cache,
};

use super::diff_and_patch_module_graph::DiffResult;
Expand Down Expand Up @@ -78,68 +68,11 @@ pub fn render_and_generate_update_resource(

let gen_resource_pot_code =
|resource_pot: &mut ResourcePot| -> farmfe_core::error::Result<String> {
let async_modules = context.custom.get(ASYNC_MODULES).unwrap();
let async_modules = async_modules.downcast_ref::<HashSet<ModuleId>>().unwrap();
// if !resource_pot.modules().is_empty() {
// let RenderedJsResourcePot {
// mut bundle,
// rendered_modules,
// ..
// } = resource_pot_to_runtime_object(resource_pot, &module_graph, async_modules, context)?;
// bundle.prepend("(");
// bundle.append(")", None);

// let mut rendered_map_chain = vec![];

// if context.config.sourcemap.enabled(resource_pot.immutable) {
// let root = context.config.root.clone();
// let map = bundle
// .generate_map(SourceMapOptions {
// include_content: Some(true),
// remap_source: Some(Box::new(move |src| format!("/{}", relative(&root, src)))),
// ..Default::default()
// })
// .map_err(|_| CompilationError::GenerateSourceMapError {
// id: resource_pot.id.clone(),
// })?;

// let mut buf = vec![];
// map.to_writer(&mut buf).expect("failed to write sourcemap");
// rendered_map_chain.push(Arc::new(String::from_utf8(buf).unwrap()));
// }
// // The hmr result should alway be a js resource
// resource_pot.meta = ResourcePotMetaData {
// rendered_modules,
// rendered_content: Arc::new(bundle.to_string()),
// rendered_map_chain,
// ..Default::default()
// };

// let (mut update_resources, _) = render_resource_pot_generate_resources(
// resource_pot,
// context,
// &Default::default(),
// true,
// &mut None,
// )?;

// if let Some(map) = update_resources.source_map {
// // inline source map
// update_resources.resource.bytes.append(
// &mut format!(
// "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,{}",
// base64_encode(&map.bytes)
// )
// .into_bytes(),
// );
// }

// let code = String::from_utf8(update_resources.resource.bytes).unwrap();

// return Ok(code);
// }

Ok("{}".to_string())
let res = context
.plugin_driver
.render_update_resource_pot(resource_pot, context)?;

res.map_or(Ok("{}".to_string()), |r| Ok(r.content))
};

let immutable_update_resource = gen_resource_pot_code(&mut immutable_update_resource_pot)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/cache/cache_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{

use crate::config::Mode;

const FARM_CACHE_VERSION: &str = "0.6.0";
const FARM_CACHE_VERSION: &str = "0.6.1";
const FARM_CACHE_MANIFEST_FILE: &str = "farm-cache.json";

// TODO make CacheStore a trait and implement DiskCacheStore or RemoteCacheStore or more.
Expand Down
10 changes: 10 additions & 0 deletions crates/core/src/cache/cacheable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use downcast_rs::{impl_downcast, Downcast};

pub trait Cacheable: std::any::Any + Send + Sync + Downcast {
/// Serialize the data to bytes
fn serialize_bytes(&self) -> Result<Vec<u8>, String>;
/// Deserialize the bytes to data
fn deserialize_bytes(&self, bytes: Vec<u8>) -> Result<Box<dyn Cacheable>, String>;
}

impl_downcast!(Cacheable);
1 change: 1 addition & 0 deletions crates/core/src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::config::Mode;
use self::{cache_store::CacheStore, plugin_cache::PluginCacheManager};

pub mod cache_store;
pub mod cacheable;
pub mod module_cache;
pub mod plugin_cache;
pub mod resource_cache;
Expand Down
1 change: 1 addition & 0 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod plugin;
pub mod resource;
pub mod stats;

pub use cache::cacheable::*;
pub use farmfe_macro_cache_item::cache_item;

/// Version of this core crate, if the core data structures changed,
Expand Down
145 changes: 145 additions & 0 deletions crates/core/src/module/custom_meta_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
use crate::Cacheable;
use dashmap::DashMap;
use rkyv::*;
use std::collections::{hash_map::Iter, HashMap};

#[derive(Default)]
pub struct CustomMetaDataMap {
map: HashMap<String, Box<dyn Cacheable>>,
/// The bytes map is used to store the serialized data of the map above
bytes_map: DashMap<String, Vec<u8>>,
}

impl CustomMetaDataMap {
pub fn new() -> Self {
Self {
map: HashMap::new(),
bytes_map: DashMap::new(),
}
}

pub fn is_empty(&self) -> bool {
self.map.is_empty()
}

pub fn iter(&self) -> Iter<String, Box<dyn Cacheable>> {
self.map.iter()
}

pub fn get_mut<T: Cacheable + Default>(&mut self, key: &str) -> Option<&mut T> {
if let Some((_, bytes)) = self.bytes_map.remove(key) {
let value = T::deserialize_bytes(&T::default(), bytes).unwrap();
self.map.insert(key.to_string(), value);
}

self.map.get_mut(key).and_then(|v| v.downcast_mut::<T>())
}
}

impl From<HashMap<String, Box<dyn Cacheable>>> for CustomMetaDataMap {
fn from(map: HashMap<String, Box<dyn Cacheable>>) -> Self {
Self {
map,
bytes_map: DashMap::new(),
}
}
}

impl Clone for CustomMetaDataMap {
fn clone(&self) -> Self {
let custom = if self.map.is_empty() {
HashMap::new()
} else {
let mut custom = HashMap::new();
for (k, v) in self.map.iter() {
let cloned_data = v.serialize_bytes().unwrap();
let cloned_custom = v.deserialize_bytes(cloned_data).unwrap();
custom.insert(k.clone(), cloned_custom);
}
custom
};

Self {
map: custom,
bytes_map: self.bytes_map.clone(),
}
}
}

impl<__D: Fallible + ?Sized> Deserialize<CustomMetaDataMap, __D> for Archived<CustomMetaDataMap> {
#[inline]
fn deserialize(
&self,
deserializer: &mut __D,
) -> ::core::result::Result<CustomMetaDataMap, __D::Error> {
let map = Deserialize::<HashMap<String, Vec<u8>>, __D>::deserialize(&self.map, deserializer)?;
let mut res = CustomMetaDataMap {
map: HashMap::new(),
bytes_map: DashMap::new(),
};

res.bytes_map = map.into_iter().collect();
Ok(res)
}
}

impl<__S: Fallible + ?Sized> Serialize<__S> for CustomMetaDataMap
where
__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace,
{
#[inline]
fn serialize(&self, serializer: &mut __S) -> ::core::result::Result<Self::Resolver, __S::Error> {
let mut map = HashMap::<String, Vec<u8>>::new();

for (k, v) in self.map.iter() {
let cloned_data = v.serialize_bytes().unwrap();
map.insert(k.clone(), cloned_data);
}

let resolver_map = Serialize::<__S>::serialize(&map, serializer)?;

for (k, v) in map {
self.bytes_map.insert(k, v);
}

Ok(CustomMetaDataMapResolver { map: resolver_map })
}
}

pub struct ArchivedCustomMetaDataMap {
///The archived counterpart of [`CustomMetaDataMap::map`]
pub map: ::rkyv::Archived<HashMap<String, Vec<u8>>>,
}

pub struct CustomMetaDataMapResolver {
map: ::rkyv::Resolver<HashMap<String, Vec<u8>>>,
}

impl Archive for CustomMetaDataMap {
type Archived = ArchivedCustomMetaDataMap;
type Resolver = CustomMetaDataMapResolver;
#[allow(clippy::unit_arg)]
#[inline]
unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) {
let (fp, fo) = {
#[allow(unused_unsafe)]
unsafe {
let fo = &raw mut (*out).map;
(fo.cast::<u8>().offset_from(out.cast::<u8>()) as usize, fo)
}
};
let mut map = HashMap::<String, Vec<u8>>::new();
let mut keys = vec![];

for item in self.bytes_map.iter() {
keys.push(item.key().clone());
}

for key in keys {
let (k, v) = self.bytes_map.remove(&key).unwrap();
map.insert(k, v);
}

::rkyv::Archive::resolve(&map, pos + fp, resolver.map, fo);
}
}
Loading

0 comments on commit 57682c3

Please sign in to comment.