-
Notifications
You must be signed in to change notification settings - Fork 107
feat: native plugin init #1691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: native plugin init #1691
Changes from all commits
1c4869c
c315018
9b01d6c
5cde533
a63f42e
7d315ba
c175fc1
9dedf58
8848886
621903d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,7 @@ | ||||||||||||||||||||
| #![deny(clippy::all)] | ||||||||||||||||||||
|
|
||||||||||||||||||||
| extern crate swc_malloc; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| use std::sync::{Arc, Once}; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| use js_hook::{JsHooks, TsFnHooks}; | ||||||||||||||||||||
|
|
@@ -18,18 +20,6 @@ mod js_hook; | |||||||||||||||||||
| mod js_plugin; | ||||||||||||||||||||
| mod threadsafe_function; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| #[cfg(not(target_os = "linux"))] | ||||||||||||||||||||
| #[global_allocator] | ||||||||||||||||||||
| static GLOBAL: mimalloc_rust::GlobalMiMalloc = mimalloc_rust::GlobalMiMalloc; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| #[cfg(all( | ||||||||||||||||||||
| target_os = "linux", | ||||||||||||||||||||
| target_env = "gnu", | ||||||||||||||||||||
| any(target_arch = "x86_64", target_arch = "aarch64") | ||||||||||||||||||||
| ))] | ||||||||||||||||||||
| #[global_allocator] | ||||||||||||||||||||
| static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| static LOG_INIT: Once = Once::new(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| #[napi(object)] | ||||||||||||||||||||
|
|
@@ -168,6 +158,7 @@ pub struct BuildParams { | |||||||||||||||||||
| }; | ||||||||||||||||||||
| experimental?: { | ||||||||||||||||||||
| webpackSyntaxValidate?: string[]; | ||||||||||||||||||||
| rustPlugins?: Array<[string, any]>; | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 建议加强类型定义和文档说明 当前的
建议修改为: - rustPlugins?: Array<[string, any]>;
+ rustPlugins?: Array<{
+ // 插件的路径
+ path: string;
+ // 插件的配置选项
+ options?: {
+ [key: string]: string | number | boolean;
+ };
+ }>;同时建议添加相关文档说明插件的配置格式和用法。 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||
| }; | ||||||||||||||||||||
| watch?: { | ||||||||||||||||||||
| ignoredPaths?: string[]; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,7 +66,7 @@ pub use resolve::ResolveConfig; | |
| pub use rsc_client::{deserialize_rsc_client, LogServerComponent, RscClientConfig}; | ||
| pub use rsc_server::{deserialize_rsc_server, RscServerConfig}; | ||
| use serde::{Deserialize, Serialize}; | ||
| use serde_json::Value; | ||
| use serde_json::{json, Value}; | ||
| pub use stats::{deserialize_stats, StatsConfig}; | ||
| use thiserror::Error; | ||
| pub use transform_import::{TransformImportConfig, TransformImportStyle}; | ||
|
|
@@ -75,6 +75,7 @@ pub use umd::{deserialize_umd, Umd}; | |
| pub use watch::WatchConfig; | ||
|
|
||
| use crate::build::load::JS_EXTENSIONS; | ||
| use crate::config::experimental::RustPlugin; | ||
| use crate::features::node::Node; | ||
|
|
||
| #[derive(Debug, Diagnostic)] | ||
|
|
@@ -236,14 +237,25 @@ impl Config { | |
| ) -> Result<Self> { | ||
| let abs_config_file = root.join(CONFIG_FILE); | ||
| let abs_config_file = abs_config_file.to_str().unwrap(); | ||
| let mut overrides_json: Option<Value> = None; | ||
| let c = config::Config::builder(); | ||
| // default config | ||
| let c = c.add_source(config::File::from_str( | ||
| DEFAULT_CONFIG, | ||
| config::FileFormat::Json5, | ||
| )); | ||
|
|
||
| // default config from args | ||
| let c = if let Some(default_config) = default_config { | ||
| let result: Result<Value, serde_json::Error> = serde_json::from_str(default_config); | ||
| if let Ok(config) = result { | ||
| if let Some(rust_plugins) = config | ||
| .get("experimental") | ||
| .and_then(|experimental| experimental.get("rustPlugins")) | ||
| { | ||
| overrides_json = Some(json!({ "rust_plugins": rust_plugins })); | ||
| } | ||
| }; | ||
|
Comment on lines
+250
to
+258
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 需要改进配置解析的健壮性 当前实现存在以下问题:
建议修改为: - if let Ok(config) = result {
- if let Some(rust_plugins) = config
- .get("experimental")
- .and_then(|experimental| experimental.get("rustPlugins"))
- {
- overrides_json = Some(json!({ "rust_plugins": rust_plugins }));
- }
- };
+ if let Ok(config) = result {
+ if let Some(experimental) = config.get("experimental") {
+ if let Some(rust_plugins) = experimental.get("rustPlugins") {
+ // 验证rust_plugins的结构
+ if !rust_plugins.is_array() {
+ return Err(anyhow!("rustPlugins must be an array"));
+ }
+
+ // 验证每个插件的配置
+ for plugin in rust_plugins.as_array().unwrap() {
+ if !plugin.is_object()
+ || plugin.get("path").is_none()
+ || !plugin.get("path").unwrap().is_string() {
+ return Err(anyhow!("Invalid plugin configuration"));
+ }
+ }
+
+ overrides_json = Some(json!({ "rust_plugins": rust_plugins }));
+ }
+ }
+ };另外建议添加以下文档: /// 实验性功能:Rust原生插件支持
///
/// # 配置示例
/// ```json
/// {
/// "experimental": {
/// "rustPlugins": [
/// {
/// "path": "./plugins/my-plugin.so",
/// "options": { ... }
/// }
/// ]
/// }
/// }
/// ```
Also applies to: 283-287 |
||
| c.add_source(config::File::from_str( | ||
| default_config, | ||
| config::FileFormat::Json5, | ||
|
|
@@ -269,6 +281,14 @@ impl Config { | |
| let mut ret = c.try_deserialize::<Config>(); | ||
| // normalize & check | ||
| if let Ok(config) = &mut ret { | ||
| // overrides config | ||
|
|
||
| if let Some(overrides) = overrides_json { | ||
| let rust_plugins: Vec<RustPlugin> = | ||
| serde_json::from_value(overrides.get("rust_plugins").unwrap().clone())?; | ||
| config.experimental.rust_plugins = rust_plugins; | ||
| } | ||
|
|
||
| // normalize output | ||
| if config.output.path.is_relative() { | ||
| config.output.path = root.join(config.output.path.to_string_lossy().to_string()); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| [package] | ||
| name = "mako_plugin_macro" | ||
| version = "0.0.1" | ||
| edition = "2021" | ||
| license = "MIT" | ||
|
|
||
| [dependencies] | ||
| proc-macro2 = "1" | ||
| quote = "1" | ||
| serde_json = { workspace = true } | ||
| syn = { version = "2", features = ["full"] } | ||
|
|
||
| [lib] | ||
| proc-macro = true |
Uh oh!
There was an error while loading. Please reload this page.