Skip to content
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: add buildEnd plugin hook #1644

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/binding/src/js_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ pub struct JsHooks {
pub _on_generate_file: Option<JsFunction>,
#[napi(ts_type = "() => Promise<void>;")]
pub build_start: Option<JsFunction>,
#[napi(ts_type = "() => Promise<void>;")]
pub build_end: Option<JsFunction>,
#[napi(
ts_type = "(source: string, importer: string, { isEntry: bool }) => Promise<{ id: string }>;"
)]
Expand All @@ -74,6 +76,7 @@ pub struct JsHooks {

pub struct TsFnHooks {
pub build_start: Option<ThreadsafeFunction<(), ()>>,
pub build_end: Option<ThreadsafeFunction<(), ()>>,
pub generate_end: Option<ThreadsafeFunction<Value, ()>>,
pub load: Option<ThreadsafeFunction<String, Option<LoadResult>>>,
pub load_include: Option<ThreadsafeFunction<String, Option<bool>>>,
Expand All @@ -90,6 +93,9 @@ impl TsFnHooks {
build_start: hooks.build_start.as_ref().map(|hook| unsafe {
ThreadsafeFunction::from_napi_value(env.raw(), hook.raw()).unwrap()
}),
build_end: hooks.build_end.as_ref().map(|hook| unsafe {
ThreadsafeFunction::from_napi_value(env.raw(), hook.raw()).unwrap()
}),
generate_end: hooks.generate_end.as_ref().map(|hook| unsafe {
ThreadsafeFunction::from_napi_value(env.raw(), hook.raw()).unwrap()
}),
Expand Down
5 changes: 5 additions & 0 deletions crates/binding/src/js_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,14 @@ impl Plugin for JsPlugin {
}

fn generate_end(&self, param: &PluginGenerateEndParams, _context: &Arc<Context>) -> Result<()> {
// keep generate_end for compatibility
// since build_end does not have none error params in unplugin's api spec
if let Some(hook) = &self.hooks.generate_end {
hook.call(serde_json::to_value(param)?)?
}
if let Some(hook) = &self.hooks.build_end {
hook.call(())?
}
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ Specify the plugins to use.
{
name?: string;
buildStart?: () => void;
buildEnd?: () => void;
generateEnd?: (data: {
isFirstCompile: boolean;
time: number;
Expand Down
1 change: 1 addition & 0 deletions docs/config.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ import(/* webpackIgnore: true */ "./foo");
{
name?: string;
buildStart?: () => void;
buildEnd?: () => void;
generateEnd?: (data: {
isFirstCompile: boolean;
time: number;
Expand Down
Loading