From f288c4b0074c744acd14d0c8d8c0d1bf6b51e341 Mon Sep 17 00:00:00 2001 From: Madinah <497350746@qq.com> Date: Fri, 22 Nov 2024 23:08:04 +0800 Subject: [PATCH] =?UTF-8?q?refactor(rust-plugins):=20=F0=9F=92=A1=20refact?= =?UTF-8?q?or=20yaml=20plugin=20(#92)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rust-plugins/yaml/CHANGELOG.md | 6 + rust-plugins/yaml/package.json | 2 +- rust-plugins/yaml/src/lib.rs | 203 ++++++++++++++++++--------------- 3 files changed, 121 insertions(+), 90 deletions(-) diff --git a/rust-plugins/yaml/CHANGELOG.md b/rust-plugins/yaml/CHANGELOG.md index 9d7cd08..427bce4 100644 --- a/rust-plugins/yaml/CHANGELOG.md +++ b/rust-plugins/yaml/CHANGELOG.md @@ -1,5 +1,11 @@ # @farmfe/plugin-yaml +## 0.0.8 + +### Patch Changes + +- refactor yaml plugin + ## 0.0.7 ### Patch Changes diff --git a/rust-plugins/yaml/package.json b/rust-plugins/yaml/package.json index d2037b4..4d9dd6a 100644 --- a/rust-plugins/yaml/package.json +++ b/rust-plugins/yaml/package.json @@ -1,6 +1,6 @@ { "name": "@farmfe/plugin-yaml", - "version": "0.0.7", + "version": "0.0.8", "private": false, "main": "scripts/index.js", "types": "scripts/index.d.ts", diff --git a/rust-plugins/yaml/src/lib.rs b/rust-plugins/yaml/src/lib.rs index c9bc96e..6f3d13a 100644 --- a/rust-plugins/yaml/src/lib.rs +++ b/rust-plugins/yaml/src/lib.rs @@ -1,6 +1,11 @@ #![deny(clippy::all)] -use farmfe_core::{config::Config, module::ModuleType, plugin::Plugin, serde_json}; +use farmfe_core::{ + config::Config, + module::ModuleType, + plugin::{Plugin, PluginLoadHookParam, PluginLoadHookResult, PluginTransformHookParam, PluginTransformHookResult}, + serde_json +}; use farmfe_macro_plugin::farm_plugin; use lazy_static::lazy_static; use regex::Regex; @@ -8,123 +13,143 @@ use serde::Deserialize; use std::fs::read_to_string; lazy_static! { - static ref YAML_MODULE_TYPE: String = String::from("yaml"); + static ref YAML_MODULE_TYPE: String = String::from("yaml"); } +/// 检查文件是否为 YAML 文件 fn is_yaml_file(file_name: &String) -> bool { - file_name.ends_with(".yaml") || file_name.ends_with(".yml") + file_name.ends_with(".yaml") || file_name.ends_with(".yml") } +/// YAML 文档解析模式 #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] enum DocumentMode { - Single, - Multi + /// 单文档模式 + Single, + /// 多文档模式 + Multi } +/// YAML 插件配置选项 #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct FarmPluginYamlOptions { - document_mode: Option, - include: Option, - exclude: Option, + document_mode: Option, + include: Option, + exclude: Option, } +/// Farm YAML 插件 #[farm_plugin] pub struct FarmPluginYaml { - document_mode: DocumentMode, - include: String, - exclude: String, + document_mode: DocumentMode, + include: String, + exclude: String, } impl FarmPluginYaml { - fn new(_config: &Config, options: String) -> Self { - let yaml_options: FarmPluginYamlOptions = serde_json::from_str(&options).unwrap(); - let include: String = yaml_options.include.unwrap_or(String::from("")); - let exclude: String = yaml_options.exclude.unwrap_or(String::from("")); - Self { - document_mode: yaml_options.document_mode.unwrap_or(DocumentMode::Single), - include, - exclude, + fn new(_config: &Config, options: String) -> Self { + let yaml_options: FarmPluginYamlOptions = serde_json::from_str(&options) + .expect("Failed to parse YAML plugin options"); + let include: String = yaml_options.include.unwrap_or_default(); + let exclude: String = yaml_options.exclude.unwrap_or_default(); + Self { + document_mode: yaml_options.document_mode.unwrap_or(DocumentMode::Single), + include, + exclude, + } } - } -} -impl Plugin for FarmPluginYaml { - fn name(&self) -> &str { - "FarmPluginYaml" - } - fn load( - &self, - param: &farmfe_core::plugin::PluginLoadHookParam, - _context: &std::sync::Arc, - _hook_context: &farmfe_core::plugin::PluginHookContext, - ) -> farmfe_core::error::Result> { - if is_yaml_file(¶m.module_id) { - let content = read_to_string(param.resolved_path).unwrap(); - return Ok(Some(farmfe_core::plugin::PluginLoadHookResult { - content, - source_map: None, - module_type: ModuleType::Custom(YAML_MODULE_TYPE.to_string()), - })); - } - Ok(None) - } - fn transform( - &self, - param: &farmfe_core::plugin::PluginTransformHookParam, - _context: &std::sync::Arc, - ) -> farmfe_core::error::Result> { - if param.module_type != ModuleType::Custom(YAML_MODULE_TYPE.to_string()) { - return Ok(None); + /// 检查是否应处理指定路径 + fn should_process_path(&self, path: &str) -> bool { + if !self.include.is_empty() { + let inc_reg = match Regex::new(&self.include) { + Ok(reg) => reg, + Err(_) => return false, + }; + if inc_reg.find(path).is_none() { + return false; + } + } + + if !self.exclude.is_empty() { + let exc_reg = match Regex::new(&self.exclude) { + Ok(reg) => reg, + Err(_) => return true, + }; + if exc_reg.find(path).is_some() { + return false; + } + } + + true } - if !self.include.is_empty() { - let inc_reg = Regex::new(&format!("{}", self.include)).unwrap(); - if let Some(_text) = inc_reg.find(param.resolved_path) { - } else { - return Ok(None); - } + /// 将 YAML 内容转换为 JavaScript 导出 + fn yaml_to_js(&self, content: &str) -> Result> { + let result = match self.document_mode { + DocumentMode::Single | DocumentMode::Multi => { + serde_yaml::from_str::(content)? + } + }; + + let mut export_val = String::new(); + if let serde_json::Value::Object(object) = result.clone() { + for (key, val) in object { + export_val.push_str(&format!("export var {} = {};\n", key, val)); + } + } + + Ok(format!("export default {};\n\n{}", result, export_val)) } +} - if !self.exclude.is_empty() { - let exc_reg = Regex::new(&format!("{}", self.exclude)).unwrap(); - if let Some(_text) = exc_reg.find(param.resolved_path) { - return Ok(None); - } +impl Plugin for FarmPluginYaml { + fn name(&self) -> &str { + "FarmPluginYaml" } - let code = match self.document_mode { - DocumentMode::Single => { - let result: serde_json::Value = - serde_yaml::from_str::(¶m.content).unwrap(); - let mut export_val = String::new(); + fn load( + &self, + param: &PluginLoadHookParam, + _context: &std::sync::Arc, + _hook_context: &farmfe_core::plugin::PluginHookContext, + ) -> farmfe_core::error::Result> { + if is_yaml_file(¶m.module_id) { + let content = read_to_string(param.resolved_path).unwrap(); + return Ok(Some(PluginLoadHookResult { + content, + source_map: None, + module_type: ModuleType::Custom(YAML_MODULE_TYPE.to_string()), + })); + } + Ok(None) + } - if let serde_json::Value::Object(object) = result.clone() { - for (key, val) in object { - export_val.push_str(&format!("export var {} = {};\n", key, val)); - } + fn transform( + &self, + param: &PluginTransformHookParam, + _context: &std::sync::Arc, + ) -> farmfe_core::error::Result> { + if param.module_type != ModuleType::Custom(YAML_MODULE_TYPE.to_string()) { + return Ok(None); } - format!("export default {}\n\n {}", result, export_val) - } - DocumentMode::Multi => { - let result: serde_json::Value = - serde_yaml::from_str::(¶m.content).unwrap(); - let mut export_val = String::new(); - if let serde_json::Value::Object(object) = result.clone() { - for (key, val) in object { - export_val.push_str(&format!("export var {} = {};\n", key, val)); - } + + if !self.should_process_path(param.resolved_path) { + return Ok(None); } - format!("export default {}\n\n {}", result, export_val) - } - }; - - return Ok(Some(farmfe_core::plugin::PluginTransformHookResult { - content: code, - module_type: Some(ModuleType::Js), - source_map: None, - ignore_previous_source_map: false, - })); - } + + let code = match self.yaml_to_js(¶m.content) { + Ok(code) => code, + Err(e) => panic!("Failed to parse YAML: {}", e), + }; + + Ok(Some(PluginTransformHookResult { + content: code, + module_type: Some(ModuleType::Js), + source_map: None, + ignore_previous_source_map: false, + })) + } }