-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathlib.rs
More file actions
113 lines (103 loc) · 3.29 KB
/
lib.rs
File metadata and controls
113 lines (103 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#![deny(clippy::all)]
use base64::engine::general_purpose;
use base64::Engine;
use farmfe_core::config::config_regex::ConfigRegex;
use farmfe_core::module::ModuleType;
use farmfe_core::plugin::PluginLoadHookResult;
use farmfe_core::serde_json;
use farmfe_core::{config::Config, plugin::Plugin};
use farmfe_macro_plugin::farm_plugin;
use farmfe_toolkit::fs::read_file_raw;
use farmfe_toolkit::plugin_utils::path_filter::PathFilter;
use mime_guess::from_path;
use mime_guess::mime::IMAGE;
#[derive(Debug, serde::Deserialize, Default, Clone)]
pub struct Options {
dom: Option<bool>,
pub include: Option<Vec<ConfigRegex>>,
pub exclude: Option<Vec<ConfigRegex>>,
}
#[farm_plugin]
pub struct FarmfePluginImage {
options: Options,
}
impl FarmfePluginImage {
fn new(_config: &Config, options: String) -> Self {
let options: Options = serde_json::from_str(&options).unwrap();
Self { options }
}
}
impl Plugin for FarmfePluginImage {
fn name(&self) -> &str {
"FarmfePluginImage"
}
fn load(
&self,
param: &farmfe_core::plugin::PluginLoadHookParam,
_context: &std::sync::Arc<farmfe_core::context::CompilationContext>,
_hook_context: &farmfe_core::plugin::PluginHookContext,
) -> farmfe_core::error::Result<Option<farmfe_core::plugin::PluginLoadHookResult>> {
let options: Options = self.options.clone();
let include = options.include.unwrap_or(vec![]);
let exclude = options.exclude.unwrap_or(vec![]);
let filter = PathFilter::new(&include, &exclude);
if !filter.execute(¶m.module_id) {
return Ok(None);
}
let mime_type = from_path(¶m.resolved_path).first_or_octet_stream();
if mime_type.type_() == IMAGE {
let dom = options.dom.unwrap_or(false);
let file_base64 =
general_purpose::STANDARD.encode(read_file_raw(param.resolved_path).unwrap_or(vec![]));
let data_uri = format!("data:{};base64,{}", mime_type.to_string(), file_base64);
let content = if dom {
format!(
"var img = new Image();
img.src = \"{}\";
export default img;",
data_uri
)
} else {
format!("export default \"{}\"", data_uri)
};
return Ok(Some(PluginLoadHookResult {
content,
module_type: ModuleType::Js,
source_map: None,
}));
}
Ok(None)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_plugin_name() {
let config = Config::default();
let options = r#"{}"#.to_string();
let plugin = FarmfePluginImage::new(&config, options);
assert_eq!(plugin.name(), "FarmfePluginImage");
}
#[test]
fn test_options_deserialization_default() {
let json = r#"{}"#;
let options: Options = serde_json::from_str(json).unwrap();
assert_eq!(options.dom, None);
assert!(options.include.is_none());
assert!(options.exclude.is_none());
}
#[test]
fn test_options_deserialization_with_dom() {
let json = r#"{"dom":true}"#;
let options: Options = serde_json::from_str(json).unwrap();
assert_eq!(options.dom, Some(true));
}
#[test]
fn test_options_deserialization_with_filters() {
let json = r#"{"include":[".*\\.png$"],"exclude":[".*\\.svg$"]}"#;
let options: Options = serde_json::from_str(json).unwrap();
assert!(options.include.is_some());
assert!(options.exclude.is_some());
}
}