Skip to content

Commit

Permalink
refactor: after emit and after emit hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk committed Mar 21, 2024
1 parent 3ec72b0 commit 39e1d0f
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 79 deletions.
4 changes: 2 additions & 2 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,6 @@ export interface JsExecuteModuleResult {
}

export interface JsHooks {
emit: () => void
afterEmit: () => void
optimizeModules: (compilation: JsCompilation) => void
afterOptimizeModules: (compilation: JsCompilation) => void
optimizeTree: () => void
Expand Down Expand Up @@ -1281,6 +1279,8 @@ export interface RegisterJsTaps {
registerCompilerMakeTaps: (stages: Array<number>) => Array<{ function: ((arg: JsCompilation) => Promise<void>); stage: number; }>
registerCompilerFinishMakeTaps: (stages: Array<number>) => Array<{ function: ((arg: JsCompilation) => void); stage: number; }>
registerCompilerShouldEmitTaps: (stages: Array<number>) => Array<{ function: ((arg: JsCompilation) => boolean | undefined); stage: number; }>
registerCompilerEmitTaps: (stages: Array<number>) => Array<{ function: (() => Promise<void>); stage: number; }>
registerCompilerAfterEmitTaps: (stages: Array<number>) => Array<{ function: (() => Promise<void>); stage: number; }>
registerCompilerAssetEmittedTaps: (stages: Array<number>) => Array<{ function: ((arg: JsAssetEmittedArgs) => Promise<void>); stage: number; }>
registerCompilationBuildModuleTaps: (stages: Array<number>) => Array<{ function: ((arg: JsModule) => void); stage: number; }>
registerCompilationStillValidModuleTaps: (stages: Array<number>) => Array<{ function: ((arg: JsModule) => void); stage: number; }>
Expand Down
4 changes: 0 additions & 4 deletions crates/node_binding/src/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use std::sync::RwLock;
/// rust support hooks
#[derive(PartialEq)]
pub enum Hook {
Emit,
AfterEmit,
OptimizeChunkModules,
OptimizeModules,
AfterOptimizeModules,
Expand All @@ -19,8 +17,6 @@ pub enum Hook {
impl From<String> 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,
Expand Down
48 changes: 45 additions & 3 deletions crates/node_binding/src/plugins/interceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -267,6 +267,14 @@ pub struct RegisterJsTaps {
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsCompilation) => boolean | undefined); stage: number; }>"
)]
pub register_compiler_should_emit_taps: RegisterFunction<JsCompilation, Option<bool>>,
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: (() => Promise<void>); stage: number; }>"
)]
pub register_compiler_emit_taps: RegisterFunction<(), Promise<()>>,
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: (() => Promise<void>); stage: number; }>"
)]
pub register_compiler_after_emit_taps: RegisterFunction<(), Promise<()>>,
#[napi(
ts_type = "(stages: Array<number>) => Array<{ function: ((arg: JsAssetEmittedArgs) => Promise<void>); stage: number; }>"
)]
Expand Down Expand Up @@ -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<JsAssetEmittedArgs, Promise<()>> @ CompilerAssetEmittedHook,
Expand Down Expand Up @@ -512,6 +532,28 @@ impl AsyncSeriesBail<Compilation, bool> for CompilerShouldEmitTap {
}
}

#[async_trait]
impl AsyncSeries<Compilation> 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<Compilation> 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<Compilation, String, AssetEmittedInfo> for CompilerAssetEmittedTap {
async fn run(
Expand Down
38 changes: 21 additions & 17 deletions crates/node_binding/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
),
Expand Down
4 changes: 0 additions & 4 deletions crates/rspack_binding_values/src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsCompilation, ()>,
#[napi(ts_type = "(compilation: JsCompilation) => void")]
Expand Down
18 changes: 16 additions & 2 deletions crates/rspack_core/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub type CompilerMakeHook = AsyncSeries2Hook<Compilation, Vec<MakeParam>>;
pub type CompilerFinishMakeHook = AsyncSeriesHook<Compilation>;
// should be SyncBailHook, but rspack need call js hook
pub type CompilerShouldEmitHook = AsyncSeriesBailHook<Compilation, bool>;
pub type CompilerEmitHook = AsyncSeriesHook<Compilation>;
pub type CompilerAfterEmitHook = AsyncSeriesHook<Compilation>;
// 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<Compilation, String, AssetEmittedInfo>;
Expand All @@ -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,
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 0 additions & 8 deletions crates/rspack_core/src/plugin/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
16 changes: 0 additions & 16 deletions crates/rspack_core/src/plugin/plugin_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
38 changes: 23 additions & 15 deletions crates/rspack_plugin_progress/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,23 @@ async fn after_process_assets(&self, _compilation: &mut Compilation) -> Result<(
Ok(())
}

#[plugin_hook(AsyncSeries<Compilation> 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<Compilation> 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 {
Expand Down Expand Up @@ -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(())
}

Expand Down Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 39e1d0f

Please sign in to comment.