From 39e1d0fb03033c3dac4e738844d827004e2a1863 Mon Sep 17 00:00:00 2001 From: ahabhgk Date: Thu, 21 Mar 2024 20:36:39 +0800 Subject: [PATCH] refactor: after emit and after emit hook --- crates/node_binding/binding.d.ts | 4 +- crates/node_binding/src/hook.rs | 4 -- .../node_binding/src/plugins/interceptor.rs | 48 +++++++++++++++++-- crates/node_binding/src/plugins/mod.rs | 38 ++++++++------- crates/rspack_binding_values/src/hooks.rs | 4 -- crates/rspack_core/src/compiler/mod.rs | 18 ++++++- crates/rspack_core/src/plugin/api.rs | 8 ---- .../rspack_core/src/plugin/plugin_driver.rs | 16 ------- crates/rspack_plugin_progress/src/lib.rs | 38 +++++++++------ packages/rspack/src/Compiler.ts | 20 ++++---- 10 files changed, 119 insertions(+), 79 deletions(-) diff --git a/crates/node_binding/binding.d.ts b/crates/node_binding/binding.d.ts index 1cd5016882ff..3a6040bd5efc 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -309,8 +309,6 @@ export interface JsExecuteModuleResult { } export interface JsHooks { - emit: () => void - afterEmit: () => void optimizeModules: (compilation: JsCompilation) => void afterOptimizeModules: (compilation: JsCompilation) => void optimizeTree: () => void @@ -1281,6 +1279,8 @@ export interface RegisterJsTaps { registerCompilerMakeTaps: (stages: Array) => Array<{ function: ((arg: JsCompilation) => Promise); stage: number; }> registerCompilerFinishMakeTaps: (stages: Array) => Array<{ function: ((arg: JsCompilation) => void); stage: number; }> registerCompilerShouldEmitTaps: (stages: Array) => Array<{ function: ((arg: JsCompilation) => boolean | undefined); stage: number; }> + registerCompilerEmitTaps: (stages: Array) => Array<{ function: (() => Promise); stage: number; }> + registerCompilerAfterEmitTaps: (stages: Array) => Array<{ function: (() => Promise); stage: number; }> registerCompilerAssetEmittedTaps: (stages: Array) => Array<{ function: ((arg: JsAssetEmittedArgs) => Promise); stage: number; }> registerCompilationBuildModuleTaps: (stages: Array) => Array<{ function: ((arg: JsModule) => void); stage: number; }> registerCompilationStillValidModuleTaps: (stages: Array) => Array<{ function: ((arg: JsModule) => void); stage: number; }> diff --git a/crates/node_binding/src/hook.rs b/crates/node_binding/src/hook.rs index ed05f69e4919..928ee36cae65 100644 --- a/crates/node_binding/src/hook.rs +++ b/crates/node_binding/src/hook.rs @@ -3,8 +3,6 @@ use std::sync::RwLock; /// rust support hooks #[derive(PartialEq)] pub enum Hook { - Emit, - AfterEmit, OptimizeChunkModules, OptimizeModules, AfterOptimizeModules, @@ -19,8 +17,6 @@ pub enum Hook { impl From for Hook { fn from(s: String) -> Self { match s.as_str() { - "emit" => Hook::Emit, - "afterEmit" => Hook::AfterEmit, "optimizeChunkModules" => Hook::OptimizeChunkModules, "optimizeModules" => Hook::OptimizeModules, "afterOptimizeModules" => Hook::AfterOptimizeModules, diff --git a/crates/node_binding/src/plugins/interceptor.rs b/crates/node_binding/src/plugins/interceptor.rs index b4eefa727f83..52d2849d6aef 100644 --- a/crates/node_binding/src/plugins/interceptor.rs +++ b/crates/node_binding/src/plugins/interceptor.rs @@ -16,9 +16,9 @@ use rspack_core::{ CompilationBuildModuleHook, CompilationChunkAssetHook, CompilationExecuteModuleHook, CompilationFinishModulesHook, CompilationParams, CompilationProcessAssetsHook, CompilationRuntimeModuleHook, CompilationStillValidModuleHook, CompilationSucceedModuleHook, - CompilerAssetEmittedHook, CompilerCompilationHook, CompilerFinishMakeHook, CompilerMakeHook, - CompilerShouldEmitHook, CompilerThisCompilationHook, ExecuteModuleId, MakeParam, - ModuleIdentifier, NormalModuleFactoryBeforeResolveHook, + CompilerAfterEmitHook, CompilerAssetEmittedHook, CompilerCompilationHook, CompilerEmitHook, + CompilerFinishMakeHook, CompilerMakeHook, CompilerShouldEmitHook, CompilerThisCompilationHook, + ExecuteModuleId, MakeParam, ModuleIdentifier, NormalModuleFactoryBeforeResolveHook, }; use rspack_hook::{ AsyncParallel3, AsyncSeries, AsyncSeries2, AsyncSeries3, AsyncSeriesBail, Hook, Interceptor, @@ -267,6 +267,14 @@ pub struct RegisterJsTaps { ts_type = "(stages: Array) => Array<{ function: ((arg: JsCompilation) => boolean | undefined); stage: number; }>" )] pub register_compiler_should_emit_taps: RegisterFunction>, + #[napi( + ts_type = "(stages: Array) => Array<{ function: (() => Promise); stage: number; }>" + )] + pub register_compiler_emit_taps: RegisterFunction<(), Promise<()>>, + #[napi( + ts_type = "(stages: Array) => Array<{ function: (() => Promise); stage: number; }>" + )] + pub register_compiler_after_emit_taps: RegisterFunction<(), Promise<()>>, #[napi( ts_type = "(stages: Array) => Array<{ function: ((arg: JsAssetEmittedArgs) => Promise); stage: number; }>" )] @@ -346,6 +354,18 @@ define_register!( cache = false, sync = false, ); +define_register!( + RegisterCompilerEmitTaps, + tap = CompilerEmitTap<(), Promise<()>> @ CompilerEmitHook, + cache = false, + sync = false, +); +define_register!( + RegisterCompilerAfterEmitTaps, + tap = CompilerAfterEmitTap<(), Promise<()>> @ CompilerAfterEmitHook, + cache = false, + sync = false, +); define_register!( RegisterCompilerAssetEmittedTaps, tap = CompilerAssetEmittedTap> @ CompilerAssetEmittedHook, @@ -512,6 +532,28 @@ impl AsyncSeriesBail for CompilerShouldEmitTap { } } +#[async_trait] +impl AsyncSeries for CompilerEmitTap { + async fn run(&self, _compilation: &mut Compilation) -> rspack_error::Result<()> { + self.function.call_with_promise(()).await + } + + fn stage(&self) -> i32 { + self.stage + } +} + +#[async_trait] +impl AsyncSeries for CompilerAfterEmitTap { + async fn run(&self, _compilation: &mut Compilation) -> rspack_error::Result<()> { + self.function.call_with_promise(()).await + } + + fn stage(&self) -> i32 { + self.stage + } +} + #[async_trait] impl AsyncParallel3 for CompilerAssetEmittedTap { async fn run( diff --git a/crates/node_binding/src/plugins/mod.rs b/crates/node_binding/src/plugins/mod.rs index fc4fb3e0bf32..f451b1084cd6 100644 --- a/crates/node_binding/src/plugins/mod.rs +++ b/crates/node_binding/src/plugins/mod.rs @@ -19,7 +19,6 @@ use rspack_core::{ }; use rspack_hook::Hook as _; -use self::interceptor::RegisterCompilationBuildModuleTaps; use self::interceptor::RegisterCompilationChunkAssetTaps; use self::interceptor::RegisterCompilationFinishModulesTaps; use self::interceptor::RegisterCompilationStillValidModuleTaps; @@ -28,6 +27,9 @@ use self::interceptor::RegisterCompilerFinishMakeTaps; use self::interceptor::{ RegisterCompilationAfterProcessAssetsTaps, RegisterCompilerAssetEmittedTaps, }; +use self::interceptor::{ + RegisterCompilationBuildModuleTaps, RegisterCompilerAfterEmitTaps, RegisterCompilerEmitTaps, +}; use self::interceptor::{ RegisterCompilationExecuteModuleTaps, RegisterCompilationProcessAssetsTaps, RegisterCompilationRuntimeModuleTaps, RegisterCompilerCompilationTaps, RegisterCompilerMakeTaps, @@ -49,6 +51,8 @@ pub struct JsHooksAdapterPlugin { register_compiler_make_taps: RegisterCompilerMakeTaps, register_compiler_finish_make_taps: RegisterCompilerFinishMakeTaps, register_compiler_should_emit_taps: RegisterCompilerShouldEmitTaps, + register_compiler_emit_taps: RegisterCompilerEmitTaps, + register_compiler_after_emit_taps: RegisterCompilerAfterEmitTaps, register_compiler_asset_emitted_taps: RegisterCompilerAssetEmittedTaps, register_compilation_build_module_taps: RegisterCompilationBuildModuleTaps, register_compilation_still_valid_module_taps: RegisterCompilationStillValidModuleTaps, @@ -114,6 +118,16 @@ impl rspack_core::Plugin for JsHooksAdapterPlugin { .compiler_hooks .should_emit .intercept(self.register_compiler_should_emit_taps.clone()); + ctx + .context + .compiler_hooks + .emit + .intercept(self.register_compiler_emit_taps.clone()); + ctx + .context + .compiler_hooks + .after_emit + .intercept(self.register_compiler_after_emit_taps.clone()); ctx .context .compiler_hooks @@ -346,22 +360,6 @@ impl rspack_core::Plugin for JsHooksAdapterPlugin { self.hooks.optimize_chunk_modules.call(compilation).await } - - async fn emit(&self, _: &mut rspack_core::Compilation) -> rspack_error::Result<()> { - if self.is_hook_disabled(&Hook::Emit) { - return Ok(()); - } - - self.hooks.emit.call(()).await - } - - async fn after_emit(&self, _: &mut rspack_core::Compilation) -> rspack_error::Result<()> { - if self.is_hook_disabled(&Hook::AfterEmit) { - return Ok(()); - } - - self.hooks.after_emit.call(()).await - } } impl JsHooksAdapterPlugin { @@ -387,6 +385,12 @@ impl JsHooksAdapterPlugin { register_compiler_should_emit_taps: RegisterCompilerShouldEmitTaps::new( register_js_taps.register_compiler_should_emit_taps, ), + register_compiler_emit_taps: RegisterCompilerEmitTaps::new( + register_js_taps.register_compiler_emit_taps, + ), + register_compiler_after_emit_taps: RegisterCompilerAfterEmitTaps::new( + register_js_taps.register_compiler_after_emit_taps, + ), register_compiler_asset_emitted_taps: RegisterCompilerAssetEmittedTaps::new( register_js_taps.register_compiler_asset_emitted_taps, ), diff --git a/crates/rspack_binding_values/src/hooks.rs b/crates/rspack_binding_values/src/hooks.rs index 1898676b315a..939da947842c 100644 --- a/crates/rspack_binding_values/src/hooks.rs +++ b/crates/rspack_binding_values/src/hooks.rs @@ -8,10 +8,6 @@ use crate::{ #[napi(object, object_to_js = false)] pub struct JsHooks { - #[napi(ts_type = "() => void")] - pub emit: ThreadsafeFunction<(), ()>, - #[napi(ts_type = "() => void")] - pub after_emit: ThreadsafeFunction<(), ()>, #[napi(ts_type = "(compilation: JsCompilation) => void")] pub optimize_modules: ThreadsafeFunction, #[napi(ts_type = "(compilation: JsCompilation) => void")] diff --git a/crates/rspack_core/src/compiler/mod.rs b/crates/rspack_core/src/compiler/mod.rs index 631374bb7075..9a545d2af061 100644 --- a/crates/rspack_core/src/compiler/mod.rs +++ b/crates/rspack_core/src/compiler/mod.rs @@ -44,6 +44,8 @@ pub type CompilerMakeHook = AsyncSeries2Hook>; pub type CompilerFinishMakeHook = AsyncSeriesHook; // should be SyncBailHook, but rspack need call js hook pub type CompilerShouldEmitHook = AsyncSeriesBailHook; +pub type CompilerEmitHook = AsyncSeriesHook; +pub type CompilerAfterEmitHook = AsyncSeriesHook; // should be AsyncSeriesHook, but rspack parallel emit asset, only accept immutable params, // and it has no effect about mutate the params in webpack pub type CompilerAssetEmittedHook = AsyncParallel3Hook; @@ -55,6 +57,8 @@ pub struct CompilerHooks { pub make: CompilerMakeHook, pub finish_make: CompilerFinishMakeHook, pub should_emit: CompilerShouldEmitHook, + pub emit: CompilerEmitHook, + pub after_emit: CompilerAfterEmitHook, pub asset_emitted: CompilerAssetEmittedHook, } @@ -349,7 +353,12 @@ where } } - self.plugin_driver.emit(&mut self.compilation).await?; + self + .plugin_driver + .compiler_hooks + .emit + .call(&mut self.compilation) + .await?; let mut new_emitted_asset_versions = HashMap::default(); let results = self @@ -378,7 +387,12 @@ where item?; } - self.plugin_driver.after_emit(&mut self.compilation).await + self + .plugin_driver + .compiler_hooks + .after_emit + .call(&mut self.compilation) + .await } async fn emit_asset( diff --git a/crates/rspack_core/src/plugin/api.rs b/crates/rspack_core/src/plugin/api.rs index a5cdeff9770c..35744c37e5a0 100644 --- a/crates/rspack_core/src/plugin/api.rs +++ b/crates/rspack_core/src/plugin/api.rs @@ -302,14 +302,6 @@ pub trait Plugin: Debug + Send + Sync { Ok(()) } - async fn emit(&self, _compilation: &mut Compilation) -> Result<()> { - Ok(()) - } - - async fn after_emit(&self, _compilation: &mut Compilation) -> Result<()> { - Ok(()) - } - fn seal(&self, _compilation: &mut Compilation) -> Result<()> { Ok(()) } diff --git a/crates/rspack_core/src/plugin/plugin_driver.rs b/crates/rspack_core/src/plugin/plugin_driver.rs index 3c07d46b4047..92b45aede6e3 100644 --- a/crates/rspack_core/src/plugin/plugin_driver.rs +++ b/crates/rspack_core/src/plugin/plugin_driver.rs @@ -515,22 +515,6 @@ impl PluginDriver { Ok(()) } - #[instrument(name = "plugin:emit", skip_all)] - pub async fn emit(&self, compilation: &mut Compilation) -> Result<()> { - for plugin in &self.plugins { - plugin.emit(compilation).await?; - } - Ok(()) - } - - #[instrument(name = "plugin:after_emit", skip_all)] - pub async fn after_emit(&self, compilation: &mut Compilation) -> Result<()> { - for plugin in &self.plugins { - plugin.after_emit(compilation).await?; - } - Ok(()) - } - #[instrument(name = "plugin:seal", skip_all)] pub fn seal(&self, compilation: &mut Compilation) -> Result<()> { for plugin in &self.plugins { diff --git a/crates/rspack_plugin_progress/src/lib.rs b/crates/rspack_plugin_progress/src/lib.rs index e200641c83f7..c9b7375c50e5 100644 --- a/crates/rspack_plugin_progress/src/lib.rs +++ b/crates/rspack_plugin_progress/src/lib.rs @@ -320,6 +320,23 @@ async fn after_process_assets(&self, _compilation: &mut Compilation) -> Result<( Ok(()) } +#[plugin_hook(AsyncSeries for ProgressPlugin)] +async fn emit(&self, _compilation: &mut Compilation) -> Result<()> { + self.handler(0.98, "emitting".to_string(), vec!["emit".to_string()], None); + Ok(()) +} + +#[plugin_hook(AsyncSeries for ProgressPlugin)] +async fn after_emit(&self, _compilation: &mut Compilation) -> Result<()> { + self.handler( + 0.98, + "emitting".to_string(), + vec!["after emit".to_string()], + None, + ); + Ok(()) +} + #[async_trait] impl Plugin for ProgressPlugin { fn name(&self) -> &'static str { @@ -372,6 +389,12 @@ impl Plugin for ProgressPlugin { .compilation_hooks .after_process_assets .tap(after_process_assets::new(self)); + ctx.context.compiler_hooks.emit.tap(emit::new(self)); + ctx + .context + .compiler_hooks + .after_emit + .tap(after_emit::new(self)); Ok(()) } @@ -424,21 +447,6 @@ impl Plugin for ProgressPlugin { Ok(()) } - async fn emit(&self, _compilation: &mut Compilation) -> Result<()> { - self.handler(0.98, "emitting".to_string(), vec!["emit".to_string()], None); - Ok(()) - } - - async fn after_emit(&self, _compilation: &mut Compilation) -> Result<()> { - self.handler( - 0.98, - "emitting".to_string(), - vec!["after emit".to_string()], - None, - ); - Ok(()) - } - async fn done<'s, 'c>( &self, _ctx: PluginContext, diff --git a/packages/rspack/src/Compiler.ts b/packages/rspack/src/Compiler.ts index e7117f08f44a..5a994dced73d 100644 --- a/packages/rspack/src/Compiler.ts +++ b/packages/rspack/src/Compiler.ts @@ -106,9 +106,9 @@ class Compiler { infrastructureLog: tapable.SyncBailHook<[string, string, any[]], true>; beforeRun: tapable.AsyncSeriesHook<[Compiler]>; run: tapable.AsyncSeriesHook<[Compiler]>; - emit: tapable.AsyncSeriesHook<[Compilation]>; + emit: liteTapable.AsyncSeriesHook<[Compilation]>; assetEmitted: liteTapable.AsyncSeriesHook<[string, AssetEmittedInfo]>; - afterEmit: tapable.AsyncSeriesHook<[Compilation]>; + afterEmit: liteTapable.AsyncSeriesHook<[Compilation]>; failed: tapable.SyncHook<[Error]>; shutdown: tapable.AsyncSeriesHook<[]>; watchRun: tapable.AsyncSeriesHook<[Compiler]>; @@ -150,9 +150,9 @@ class Compiler { afterDone: new tapable.SyncHook(["stats"]), beforeRun: new tapable.AsyncSeriesHook(["compiler"]), run: new tapable.AsyncSeriesHook(["compiler"]), - emit: new tapable.AsyncSeriesHook(["compilation"]), + emit: new liteTapable.AsyncSeriesHook(["compilation"]), assetEmitted: new liteTapable.AsyncSeriesHook(["file", "info"]), - afterEmit: new tapable.AsyncSeriesHook(["compilation"]), + afterEmit: new liteTapable.AsyncSeriesHook(["compilation"]), thisCompilation: new liteTapable.SyncHook< [Compilation, CompilationParams] >(["compilation", "params"]), @@ -236,8 +236,6 @@ class Compiler { rawOptions, this.builtinPlugins, { - emit: this.#emit.bind(this), - afterEmit: this.#afterEmit.bind(this), optimizeModules: this.#optimizeModules.bind(this), afterOptimizeModules: this.#afterOptimizeModules.bind(this), optimizeTree: this.#optimizeTree.bind(this), @@ -279,6 +277,14 @@ class Compiler { () => this.hooks.shouldEmit, queried => () => queried.call(this.compilation!) ), + registerCompilerEmitTaps: this.#createRegisterTaps( + () => this.hooks.emit, + queried => async () => await queried.promise(this.compilation!) + ), + registerCompilerAfterEmitTaps: this.#createRegisterTaps( + () => this.hooks.afterEmit, + queried => async () => await queried.promise(this.compilation!) + ), registerCompilerAssetEmittedTaps: this.#createRegisterTaps( () => this.hooks.assetEmitted, queried => @@ -664,8 +670,6 @@ class Compiler { const disabledHooks: string[] = []; type HookMap = Record; const hookMap: HookMap = { - emit: this.hooks.emit, - afterEmit: this.hooks.afterEmit, optimizeTree: this.compilation!.hooks.optimizeTree, optimizeModules: this.compilation!.hooks.optimizeModules, afterOptimizeModules: this.compilation!.hooks.afterOptimizeModules,