Skip to content

Commit

Permalink
feat: support custom handler of progress plugin (#7896)
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder authored Sep 13, 2024
1 parent b0f8efb commit fc3b9fa
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 120 deletions.
9 changes: 5 additions & 4 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1599,11 +1599,12 @@ export interface RawPathData {
}

export interface RawProgressPluginOptions {
prefix: string
profile: boolean
template: string
prefix?: string
profile?: boolean
template?: string
tick?: string | Array<string>
progressChars: string
progressChars?: string
handler?: (percent: number, msg: string, items: string[]) => void
}

export interface RawProvideOptions {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,51 @@
use std::sync::Arc;

use derivative::Derivative;
use napi::Either;
use napi_derive::napi;
use rspack_plugin_progress::ProgressPluginOptions;
use rspack_napi::threadsafe_function::ThreadsafeFunction;
use rspack_plugin_progress::{ProgressPluginDisplayOptions, ProgressPluginOptions};

#[derive(Debug, Clone)]
#[napi(object)]
type HandlerFn = ThreadsafeFunction<(f64, String, Vec<String>), ()>;
#[derive(Derivative)]
#[derivative(Debug)]
#[napi(object, object_to_js = false)]
pub struct RawProgressPluginOptions {
// the prefix name of progress bar
pub prefix: String,
pub prefix: Option<String>,
// tells ProgressPlugin to collect profile data for progress steps.
pub profile: bool,
pub profile: Option<bool>,
// the template of progress bar
pub template: String,
pub template: Option<String>,
// the tick string sequence for spinners, if it's string then it will be split into characters
pub tick: Option<Either<String, Vec<String>>>,
// the progress characters
pub progress_chars: String,
pub progress_chars: Option<String>,
// the handler for progress event
#[derivative(Debug = "ignore")]
#[napi(ts_type = "(percent: number, msg: string, items: string[]) => void")]
pub handler: Option<HandlerFn>,
}

impl From<RawProgressPluginOptions> for ProgressPluginOptions {
fn from(value: RawProgressPluginOptions) -> Self {
Self {
prefix: value.prefix,
profile: value.profile,
template: value.template,
progress_chars: value.progress_chars,
tick_strings: value.tick.map(|tick| match tick {
Either::A(str) => str.chars().map(|c| c.to_string()).collect(),
Either::B(vec) => vec,
}),
if let Some(f) = value.handler {
Self::Handler(Arc::new(move |percent, msg, items| {
f.blocking_call_with_sync((percent, msg, items))
}))
} else {
Self::Default(ProgressPluginDisplayOptions {
prefix: value.prefix.unwrap_or_default(),
profile: value.profile.unwrap_or_default(),
template: value.template.unwrap_or(
"● {prefix:.bold} {bar:25.green/white.dim} ({percent}%) {wide_msg:.dim}".to_string(),
),
progress_chars: value.progress_chars.unwrap_or("━━".to_string()),
tick_strings: value.tick.map(|tick| match tick {
Either::A(str) => str.chars().map(|c| c.to_string()).collect(),
Either::B(vec) => vec,
}),
})
}
}
}
Loading

2 comments on commit fc3b9fa

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-09-13 56a30b4) Current Change
10000_development-mode + exec 2.24 s ± 49 ms 2.23 s ± 33 ms -0.42 %
10000_development-mode_hmr + exec 692 ms ± 15 ms 690 ms ± 17 ms -0.16 %
10000_production-mode + exec 2.84 s ± 28 ms 2.84 s ± 53 ms +0.12 %
arco-pro_development-mode + exec 1.85 s ± 66 ms 1.87 s ± 90 ms +0.94 %
arco-pro_development-mode_hmr + exec 435 ms ± 2 ms 434 ms ± 2.4 ms -0.15 %
arco-pro_production-mode + exec 3.23 s ± 46 ms 3.27 s ± 99 ms +1.49 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.31 s ± 61 ms 3.32 s ± 80 ms +0.42 %
threejs_development-mode_10x + exec 1.7 s ± 12 ms 1.68 s ± 12 ms -1.05 %
threejs_development-mode_10x_hmr + exec 802 ms ± 11 ms 784 ms ± 12 ms -2.21 %
threejs_production-mode_10x + exec 5.18 s ± 26 ms 5.18 s ± 36 ms +0.05 %

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ✅ success
_selftest ✅ success
nx ❌ failure
rspress ✅ success
rslib ✅ success
rsbuild ❌ failure
examples ✅ success

Please sign in to comment.