Skip to content

Commit

Permalink
feat: add afterCompile hook (#3235)
Browse files Browse the repository at this point in the history
  • Loading branch information
edusperoni authored May 22, 2023
1 parent ac70f66 commit d32c3ae
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export interface JsHooks {
optimizeModules: (...args: any[]) => any
optimizeChunkModule: (...args: any[]) => any
beforeCompile: (...args: any[]) => any
afterCompile: (...args: any[]) => any
finishModules: (...args: any[]) => any
beforeResolve: (...args: any[]) => any
afterResolve: (...args: any[]) => any
Expand Down
2 changes: 2 additions & 0 deletions crates/node_binding/src/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub enum Hook {
AfterEmit,
OptimizeChunkModules,
BeforeCompile,
AfterCompile,
FinishModules,
OptimizeModules,
/// webpack `compilation.hooks.chunkAsset`
Expand Down Expand Up @@ -45,6 +46,7 @@ impl From<String> for Hook {
"afterEmit" => Hook::AfterEmit,
"optimizeChunkModules" => Hook::OptimizeChunkModules,
"beforeCompile" => Hook::BeforeCompile,
"afterCompile" => Hook::AfterCompile,
"finishModules" => Hook::FinishModules,
"optimizeModules" => Hook::OptimizeModules,
"chunkAsset" => Hook::ChunkAsset,
Expand Down
1 change: 1 addition & 0 deletions crates/node_binding/src/js_values/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct JsHooks {
pub optimize_modules: JsFunction,
pub optimize_chunk_module: JsFunction,
pub before_compile: JsFunction,
pub after_compile: JsFunction,
pub finish_modules: JsFunction,
pub before_resolve: JsFunction,
pub after_resolve: JsFunction,
Expand Down
27 changes: 27 additions & 0 deletions crates/node_binding/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct JsHooksAdapter {
pub optimize_modules_tsfn: ThreadsafeFunction<JsCompilation, ()>,
pub optimize_chunk_modules_tsfn: ThreadsafeFunction<JsCompilation, ()>,
pub before_compile_tsfn: ThreadsafeFunction<(), ()>,
pub after_compile_tsfn: ThreadsafeFunction<JsCompilation, ()>,
pub finish_modules_tsfn: ThreadsafeFunction<JsCompilation, ()>,
pub chunk_asset_tsfn: ThreadsafeFunction<JsChunkAssetArgs, ()>,
pub before_resolve: ThreadsafeFunction<BeforeResolveData, Option<bool>>,
Expand Down Expand Up @@ -407,6 +408,28 @@ impl rspack_core::Plugin for JsHooksAdapter {
.map_err(|err| internal_error!("Failed to call before compile: {err}",))?
}

async fn after_compile(
&mut self,
compilation: &mut rspack_core::Compilation,
) -> rspack_error::Result<()> {
if self.is_hook_disabled(&Hook::AfterCompile) {
return Ok(());
}

let compilation = JsCompilation::from_compilation(unsafe {
std::mem::transmute::<&'_ mut rspack_core::Compilation, &'static mut rspack_core::Compilation>(
compilation,
)
});

self
.after_compile_tsfn
.call(compilation, ThreadsafeFunctionCallMode::NonBlocking)
.into_rspack_result()?
.await
.map_err(|err| internal_error!("Failed to call after compile: {err}"))?
}

async fn finish_modules(
&mut self,
compilation: &mut rspack_core::Compilation,
Expand Down Expand Up @@ -479,6 +502,7 @@ impl JsHooksAdapter {
context_module_before_resolve,
normal_module_factory_resolve_for_scheme,
before_compile,
after_compile,
finish_modules,
chunk_asset,
} = js_hooks;
Expand Down Expand Up @@ -512,6 +536,8 @@ impl JsHooksAdapter {
js_fn_into_theadsafe_fn!(optimize_chunk_module, env);
let before_compile_tsfn: ThreadsafeFunction<(), ()> =
js_fn_into_theadsafe_fn!(before_compile, env);
let after_compile_tsfn: ThreadsafeFunction<JsCompilation, ()> =
js_fn_into_theadsafe_fn!(after_compile, env);
let finish_modules_tsfn: ThreadsafeFunction<JsCompilation, ()> =
js_fn_into_theadsafe_fn!(finish_modules, env);
let context_module_before_resolve: ThreadsafeFunction<BeforeResolveData, Option<bool>> =
Expand Down Expand Up @@ -545,6 +571,7 @@ impl JsHooksAdapter {
optimize_modules_tsfn,
optimize_chunk_modules_tsfn,
before_compile_tsfn,
after_compile_tsfn,
before_resolve,
context_module_before_resolve,
normal_module_factory_resolve_for_scheme,
Expand Down
7 changes: 7 additions & 0 deletions crates/rspack_core/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ where
}
self.compilation.seal(self.plugin_driver.clone()).await?;

self
.plugin_driver
.write()
.await
.after_compile(&mut self.compilation)
.await?;

// Consume plugin driver diagnostic
let plugin_driver_diagnostics = self.plugin_driver.read().await.take_diagnostic();
self
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_core/src/plugin/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ pub trait Plugin: Debug + Send + Sync {
Ok(())
}

async fn after_compile(&mut self, _compilation: &mut Compilation) -> Result<()> {
Ok(())
}

async fn finish_modules(&mut self, _modules: &mut Compilation) -> Result<()> {
Ok(())
}
Expand Down
11 changes: 11 additions & 0 deletions crates/rspack_core/src/plugin/plugin_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ impl PluginDriver {

Ok(())
}

pub async fn after_compile(
&mut self,
compilation: &mut Compilation,
) -> PluginCompilationHookOutput {
for plugin in &mut self.plugins {
plugin.after_compile(compilation).await?;
}

Ok(())
}
/// Executed while initializing the compilation, right before emitting the compilation event. This hook is not copied to child compilers.
///
/// See: https://webpack.js.org/api/compiler-hooks/#thiscompilation
Expand Down
9 changes: 9 additions & 0 deletions packages/rspack/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class Compiler {
afterResolvers: tapable.SyncHook<[Compiler]>;
make: tapable.AsyncParallelHook<[Compilation]>;
beforeCompile: tapable.AsyncSeriesHook<any>;
afterCompile: tapable.AsyncSeriesHook<[Compilation]>;
finishModules: tapable.AsyncSeriesHook<[any]>;
};
options: RspackOptionsNormalized;
Expand Down Expand Up @@ -226,6 +227,7 @@ class Compiler {
afterResolvers: new tapable.SyncHook(["compiler"]),
make: new tapable.AsyncParallelHook(["compilation"]),
beforeCompile: new tapable.AsyncSeriesHook(["params"]),
afterCompile: new tapable.AsyncSeriesHook(["compilation"]),
finishModules: new tapable.AsyncSeriesHook(["modules"])
};
this.modifiedFiles = undefined;
Expand Down Expand Up @@ -275,6 +277,7 @@ class Compiler {
options,
{
beforeCompile: this.#beforeCompile.bind(this),
afterCompile: this.#afterCompile.bind(this),
make: this.#make.bind(this),
emit: this.#emit.bind(this),
afterEmit: this.#afterEmit.bind(this),
Expand Down Expand Up @@ -554,6 +557,7 @@ class Compiler {
const hookMap = {
make: this.hooks.make,
beforeCompile: this.hooks.beforeCompile,
afterCompile: this.hooks.afterCompile,
emit: this.hooks.emit,
afterEmit: this.hooks.afterEmit,
processAssetsStageAdditional:
Expand Down Expand Up @@ -607,6 +611,11 @@ class Compiler {
// this.#updateDisabledHooks();
}

async #afterCompile() {
await this.hooks.afterCompile.promise(this.compilation);
this.#updateDisabledHooks();
}

async #finishModules() {
await this.compilation.hooks.finishModules.promise(
this.compilation.getModules()
Expand Down

0 comments on commit d32c3ae

Please sign in to comment.