Skip to content

Commit

Permalink
fix(rust-plugins): 🐛 persistentCache build file is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
CCherry07 committed Oct 29, 2024
1 parent fabf3fb commit fff9708
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 30 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.

6 changes: 6 additions & 0 deletions rust-plugins/wasm/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @farmfe/plugin-wasm

## 0.0.3

### Patch Changes

- fix: persistentCache build

## 0.0.2

### Patch Changes
Expand Down
1 change: 1 addition & 0 deletions rust-plugins/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ farmfe_core = { version = "*" }
farmfe_toolkit_plugin_types = { version = "*" }
farmfe_macro_plugin = { version = "*" }
farmfe_toolkit = { version = "*" }
rkyv = { version = "0.7.42" }
2 changes: 1 addition & 1 deletion rust-plugins/wasm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@farmfe/plugin-wasm",
"version": "0.0.2",
"version": "0.0.3",
"main": "scripts/index.js",
"types": "scripts/index.d.ts",
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion rust-plugins/wasm/playground/farm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default defineConfig({
filename: 'assets/[ext]/[name].[hash].[ext]',
assetsFilename: 'static/[resourceName].[hash].[ext]'
},
persistentCache: false,
persistentCache: true,
progress: false,
},
plugins: [
Expand Down
84 changes: 65 additions & 19 deletions rust-plugins/wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
#![deny(clippy::all)]

use farmfe_core::{
cache_item,
config::Config,
context::EmitFileParams,
context::{CompilationContext, EmitFileParams},
deserialize,
module::ModuleType,
plugin::{Plugin, PluginLoadHookResult, PluginResolveHookResult},
resource::ResourceType,
resource::{Resource, ResourceOrigin, ResourceType},
serialize,
};
use std::{fs, path::Path};
use std::{fs, path::Path, sync::Arc};

use farmfe_macro_plugin::farm_plugin;
use farmfe_toolkit::fs::transform_output_filename;
const WASM_HELPER_ID_FARM: &str = "farm/wasm-helper.js";

#[cache_item]
struct CachedStaticAssets {
list: Vec<Resource>,
}

#[farm_plugin]
pub struct FarmfePluginWasm {}

Expand Down Expand Up @@ -40,15 +48,15 @@ impl Plugin for FarmfePluginWasm {
}));
}

if id.ends_with(".wasm?init") {
return Ok(Some(PluginResolveHookResult {
resolved_path: id.replace("?init", ""),
query: vec![("init".to_string(), "".to_string())]
.into_iter()
.collect(),
..Default::default()
}));
}
// if id.ends_with(".wasm?init") {
// return Ok(Some(PluginResolveHookResult {
// resolved_path: id.replace("?init", ""),
// query: vec![("init".to_string(), "".to_string())]
// .into_iter()
// .collect(),
// ..Default::default()
// }));
// }

// if id.ends_with(".wasm?url") {
// return Ok(Some(PluginResolveHookResult {
Expand Down Expand Up @@ -87,23 +95,19 @@ impl Plugin for FarmfePluginWasm {
.unwrap();
let (file_name, ext) = file_name_ext.split_once(".").unwrap();
let assets_filename_config = context.config.output.assets_filename.clone();
let file_name = transform_output_filename(
assets_filename_config,
&file_name,
file_name.as_bytes(),
ext,
);
let wasm_url = if !context.config.output.public_path.is_empty() {
let normalized_public_path = context.config.output.public_path.trim_end_matches("/");
format!("{}/{}", normalized_public_path, file_name)
} else {
format!("/{}", file_name)
};
let file_name =
transform_output_filename(assets_filename_config, &file_name, param.module_id.as_bytes(), ext);
let params = EmitFileParams {
name: file_name,
content,
resource_type: ResourceType::Asset("wasm".to_string()),
resolved_path: param.resolved_path.to_string(),
resolved_path: param.module_id.to_string(),
};
context.emit_file(params);
let mut _code = String::new();
Expand All @@ -129,4 +133,46 @@ impl Plugin for FarmfePluginWasm {
}
Ok(None)
}

fn plugin_cache_loaded(
&self,
cache: &Vec<u8>,
context: &Arc<CompilationContext>,
) -> farmfe_core::error::Result<Option<()>> {
let cached_static_assets = deserialize!(cache, CachedStaticAssets);

for asset in cached_static_assets.list {
if let ResourceOrigin::Module(m) = asset.origin {
context.emit_file(EmitFileParams {
resolved_path: m.to_string(),
name: asset.name,
content: asset.bytes,
resource_type: asset.resource_type,
});
}
}

Ok(Some(()))
}
fn write_plugin_cache(
&self,
context: &Arc<CompilationContext>,
) -> farmfe_core::error::Result<Option<Vec<u8>>> {
let mut list = vec![];
let resources_map = context.resources_map.lock();
for (_, resource) in resources_map.iter() {
if let ResourceOrigin::Module(m) = &resource.origin {
if context.cache_manager.module_cache.has_cache(m) {
list.push(resource.clone());
}
}
}

if !list.is_empty() {
let cached_static_assets = CachedStaticAssets { list };
Ok(Some(serialize!(&cached_static_assets)))
} else {
Ok(None)
}
}
}
22 changes: 13 additions & 9 deletions rust-plugins/worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use farmfe_core::{
module::ModuleType,
plugin::{Plugin, PluginLoadHookResult},
resource::{Resource, ResourceOrigin, ResourceType},
rkyv::Deserialize,
serde,
serde_json::{self, to_value, Map, Value},
serialize,
Expand Down Expand Up @@ -61,8 +60,8 @@ fn merge_configs(

serde_json::from_value(val1)
}
fn build_worker(resolved_path: &str, compiler_config: &Config) -> Vec<u8> {
let (_worker_url, full_file_name) = get_worker_url(resolved_path, compiler_config);
fn build_worker(resolved_path: &str, module_id: &str, compiler_config: &Config) -> Vec<u8> {
let (_worker_url, full_file_name) = get_worker_url(resolved_path, module_id, compiler_config);
let mut input = HashMap::new();
input.insert(full_file_name.clone(), resolved_path.to_string());
let compiler = Compiler::new(
Expand Down Expand Up @@ -104,22 +103,28 @@ fn emit_worker_file(
resolved_path: module_id.to_string(),
content: content_bytes,
name: file_name.to_string(),
resource_type: ResourceType::Asset("js".to_string()),
resource_type: ResourceType::Js,
};
context.emit_file(params);
}

fn get_worker_url(resolved_path: &str, compiler_config: &Config) -> (String, String) {
fn get_worker_url(
resolved_path: &str,
module_id: &str,
compiler_config: &Config,
) -> (String, String) {
let file_name_ext = Path::new(resolved_path)
.file_name()
.map(|x| x.to_string_lossy().to_string())
.unwrap();
let (file_name, ext) = file_name_ext.split_once(".").unwrap();
let assets_filename_config = compiler_config.output.assets_filename.clone();

// hash_bytes = resolved_path + file_name_ext bytes ,make sure that the files of the same name in different directory will not be covered;
let file_name = transform_output_filename(
assets_filename_config,
&file_name,
file_name.as_bytes(),
module_id.as_bytes(),
ext,
);
// worker.ts -> worker.js
Expand Down Expand Up @@ -159,8 +164,8 @@ fn process_worker(param: ProcessWorkerParam) -> String {
context,
} = param;

let (worker_url, file_name) = get_worker_url(resolved_path, compiler_config);
let content_bytes = build_worker(resolved_path, &compiler_config);
let (worker_url, file_name) = get_worker_url(resolved_path, module_id, compiler_config);
let content_bytes = build_worker(resolved_path, module_id, &compiler_config);

if worker_cache.get(resolved_path).is_none() {
let content_bytes =
Expand Down Expand Up @@ -405,5 +410,4 @@ impl Plugin for FarmfePluginWorker {
Ok(None)
}
}

}

0 comments on commit fff9708

Please sign in to comment.