-
Notifications
You must be signed in to change notification settings - Fork 73
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
[WIP] feat: native plugin init #1691
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
|
||
use anyhow::{anyhow, Error, Result}; | ||
use colored::Colorize; | ||
use libloading::Library; | ||
use regex::Regex; | ||
use swc_core::common::sync::Lrc; | ||
use swc_core::common::{Globals, SourceMap, DUMMY_SP}; | ||
|
@@ -220,7 +221,7 @@ | |
|
||
impl Compiler { | ||
pub fn new( | ||
config: Config, | ||
mut config: Config, | ||
root: PathBuf, | ||
args: Args, | ||
extra_plugins: Option<Vec<Arc<dyn Plugin>>>, | ||
|
@@ -238,6 +239,19 @@ | |
if let Some(extra_plugins) = extra_plugins { | ||
plugins.extend(extra_plugins); | ||
} | ||
|
||
let mut external_plugins: Vec<Arc<dyn Plugin>> = vec![]; | ||
unsafe { | ||
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. Using |
||
for rust_plugin in config.experimental.rust_plugins.clone() { | ||
let lib = Arc::new(Library::new(rust_plugin.path)?); | ||
let plugin_create_fn: libloading::Symbol< | ||
unsafe extern "C" fn(option: String) -> Arc<dyn Plugin>, | ||
> = lib.get(b"_plugin_create").unwrap(); | ||
let plugin = plugin_create_fn(rust_plugin.options); | ||
external_plugins.push(plugin); | ||
} | ||
} | ||
|
||
let builtin_plugins: Vec<Arc<dyn Plugin>> = vec![ | ||
// features | ||
Arc::new(plugins::manifest::ManifestPlugin {}), | ||
|
@@ -254,10 +268,9 @@ | |
Arc::new(plugins::tree_shaking::FarmTreeShake {}), | ||
Arc::new(plugins::detect_circular_dependence::LoopDetector {}), | ||
]; | ||
plugins.extend(external_plugins); | ||
plugins.extend(builtin_plugins); | ||
|
||
let mut config = config; | ||
|
||
if let Some(progress) = &config.progress { | ||
plugins.push(Arc::new(plugins::progress::ProgressPlugin::new( | ||
plugins::progress::ProgressPluginOptions { | ||
|
@@ -480,7 +493,6 @@ | |
let mg = self.context.module_graph.read().unwrap(); | ||
cg.full_hash(&mg) | ||
} | ||
|
||
fn clean_dist(&self) -> Result<()> { | ||
// compiler 前清除 dist,如果后续 dev 环境不在 output_path 里,需要再补上 dev 的逻辑 | ||
let output_path = &self.context.config.output.path; | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,6 +2,12 @@ | |||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
use crate::create_deserialize_fn; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
#[derive(Deserialize, Serialize, Debug, Clone)] | ||||||||||||||||||||||||||||
pub struct RustPlugin { | ||||||||||||||||||||||||||||
pub path: String, | ||||||||||||||||||||||||||||
pub options: String, | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
Comment on lines
+5
to
+9
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 建议改进 RustPlugin 结构体的设计 当前实现存在以下需要改进的地方:
建议按如下方式修改: #[derive(Deserialize, Serialize, Debug, Clone)]
+/// 用于配置 Rust 原生插件的结构体
pub struct RustPlugin {
+ /// 插件的文件路径
pub path: String,
+ /// 插件的配置选项,支持 JSON 格式
- pub options: String,
+ pub options: serde_json::Value,
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
#[derive(Deserialize, Serialize, Debug)] | ||||||||||||||||||||||||||||
#[serde(rename_all = "camelCase")] | ||||||||||||||||||||||||||||
pub struct ExperimentalConfig { | ||||||||||||||||||||||||||||
|
@@ -12,6 +18,7 @@ | |||||||||||||||||||||||||||
pub magic_comment: bool, | ||||||||||||||||||||||||||||
#[serde(deserialize_with = "deserialize_detect_loop")] | ||||||||||||||||||||||||||||
pub detect_circular_dependence: Option<DetectCircularDependence>, | ||||||||||||||||||||||||||||
pub rust_plugins: Vec<RustPlugin>, | ||||||||||||||||||||||||||||
pub central_ensure: bool, | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "mako_plugin_macro" | ||
version = "0.0.1" | ||
edition = "2021" | ||
license = "MIT" | ||
|
||
[dependencies] | ||
proc-macro2 = "1" | ||
quote = "1" | ||
syn = { version = "2", features = ["full"] } | ||
|
||
[lib] | ||
proc-macro = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codebase verification
发现潜在的内存分配器冲突问题
根据代码分析,发现以下问题:
crates/mako/src/main.rs
和crates/binding/src/lib.rs
都使用了extern crate swc_malloc
#[global_allocator]
)swc_malloc
和tikv-jemallocator
可能会导致内存分配冲突建议:
tikv-jemallocator
,应该移除swc_malloc
的依赖🔗 Analysis chain
请验证内存分配器的兼容性
当前工作区同时包含了
swc_malloc
和tikv-jemallocator
两个内存分配器。请确认:🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 67
Script:
Length of output: 17159