Skip to content

Commit

Permalink
fix: plugin options string
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinbao1001 committed Dec 18, 2024
1 parent c175fc1 commit 9dedf58
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 21 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.

1 change: 0 additions & 1 deletion crates/mako/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub(crate) mod error;
pub mod file;
pub(crate) mod js_ast;
pub(crate) mod sourcemap;
#[cfg(test)]
pub mod tests;
pub mod utils;

Expand Down
3 changes: 2 additions & 1 deletion crates/mako/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use anyhow::{anyhow, Error, Result};
use colored::Colorize;
use libloading::Library;
use regex::Regex;
use serde_json::Value;
use swc_core::common::sync::Lrc;
use swc_core::common::{Globals, SourceMap, DUMMY_SP};
use swc_core::ecma::ast::Ident;
Expand Down Expand Up @@ -245,7 +246,7 @@ impl Compiler {
for rust_plugin in config.experimental.rust_plugins.clone() {
let lib = Arc::new(Library::new(rust_plugin.path)?);

Check warning on line 247 in crates/mako/src/compiler.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/compiler.rs#L247

Added line #L247 was not covered by tests
let plugin_create_fn: libloading::Symbol<
unsafe extern "C" fn(option: String) -> Arc<dyn Plugin>,
unsafe extern "C" fn(option: Value) -> Arc<dyn Plugin>,
> = lib.get(b"_plugin_create").unwrap();
let plugin = plugin_create_fn(rust_plugin.options);
external_plugins.push(plugin);

Check warning on line 252 in crates/mako/src/compiler.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/compiler.rs#L250-L252

Added lines #L250 - L252 were not covered by tests
Expand Down
32 changes: 15 additions & 17 deletions crates/mako/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -233,7 +234,7 @@ 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<String> = None;
let mut overrides_json: Option<Value> = None;
let c = config::Config::builder();
// default config
let c = c.add_source(config::File::from_str(
Expand All @@ -245,13 +246,11 @@ impl Config {
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(experimental) = config.get("experimental") {
overrides_json = Some(
serde_json::to_string(&json!({
"experimental": experimental
}))
.unwrap(),
);
if let Some(rust_plugins) = config
.get("experimental")
.and_then(|experimental| experimental.get("rustPlugins"))
{
overrides_json = Some(json!({ "rust_plugins": rust_plugins }));
}

Check warning on line 254 in crates/mako/src/config.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config.rs#L254

Added line #L254 was not covered by tests
};
c.add_source(config::File::from_str(
Expand All @@ -274,20 +273,19 @@ impl Config {
} else {
c
};
// overrides config
let c = if let Some(overrides) = overrides_json {
c.add_source(config::File::from_str(
overrides.as_str(),
config::FileFormat::Json5,
))
} else {
c
};

let c = c.build()?;
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;
}

Check warning on line 288 in crates/mako/src/config.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config.rs#L288

Added line #L288 was not covered by tests
// normalize output
if config.output.path.is_relative() {
config.output.path = root.join(config.output.path.to_string_lossy().to_string());
Expand Down
3 changes: 2 additions & 1 deletion crates/mako/src/config/experimental.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::create_deserialize_fn;

#[derive(Deserialize, Serialize, Debug, Clone)]

Check warning on line 6 in crates/mako/src/config/experimental.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/experimental.rs#L6

Added line #L6 was not covered by tests
pub struct RustPlugin {
pub path: String,
pub options: String,
pub options: Value,

Check warning on line 9 in crates/mako/src/config/experimental.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/config/experimental.rs#L8-L9

Added lines #L8 - L9 were not covered by tests
}

#[derive(Deserialize, Serialize, Debug)]
Expand Down
1 change: 1 addition & 0 deletions crates/mako_plugin_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ license = "MIT"
[dependencies]
proc-macro2 = "1"
quote = "1"
serde_json = { workspace = true }
syn = { version = "2", features = ["full"] }

[lib]
Expand Down
2 changes: 1 addition & 1 deletion crates/mako_plugin_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn handler_create(input: TokenStream2) -> TokenStream2 {
let struct_name = &ast.ident;
let ts = quote! {
#[no_mangle]
pub fn _plugin_create(option: std::string::String) -> std::sync::Arc<dyn Plugin> {
pub fn _plugin_create(option: serde_json::Value) -> std::sync::Arc<dyn Plugin> {
std::sync::Arc::new(#struct_name::new(option))
}
};
Expand Down

0 comments on commit 9dedf58

Please sign in to comment.