Skip to content
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 .changeset/silver-phones-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rspack/binding": patch
"@rspack/core": patch
---

add finishModules hook
1 change: 1 addition & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ export interface JsHooks {
afterEmit: (...args: any[]) => any
make: (...args: any[]) => any
optimizeChunkModule: (...args: any[]) => any
finishModules: (...args: any[]) => any
normalModuleFactoryResolveForScheme: (...args: any[]) => any
}
export interface JsModule {
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 @@ -15,6 +15,7 @@ pub enum Hook {
Emit,
AfterEmit,
OptimizeChunkModules,
FinishModules,
}

impl From<String> for Hook {
Expand All @@ -32,6 +33,7 @@ impl From<String> for Hook {
"emit" => Hook::Emit,
"afterEmit" => Hook::AfterEmit,
"optimizeChunkModules" => Hook::OptimizeChunkModules,
"finishModules" => Hook::FinishModules,
hook_name => panic!("{hook_name} is an invalid hook name"),
}
}
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 @@ -14,5 +14,6 @@ pub struct JsHooks {
pub after_emit: JsFunction,
pub make: JsFunction,
pub optimize_chunk_module: JsFunction,
pub finish_modules: JsFunction,
pub normal_module_factory_resolve_for_scheme: JsFunction,
}
28 changes: 27 additions & 1 deletion crates/node_binding/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct JsHooksAdapter {
pub emit_tsfn: ThreadsafeFunction<(), ()>,
pub after_emit_tsfn: ThreadsafeFunction<(), ()>,
pub optimize_chunk_modules_tsfn: ThreadsafeFunction<JsCompilation, ()>,
pub finish_modules_tsfn: ThreadsafeFunction<JsCompilation, ()>,
pub normal_module_factory_resolve_for_scheme:
ThreadsafeFunction<SchemeAndJsResourceData, JsResourceData>,
}
Expand Down Expand Up @@ -257,6 +258,28 @@ impl rspack_core::Plugin for JsHooksAdapter {
.map_err(|err| internal_error!("Failed to compilation: {err}"))?
}

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

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

self
.finish_modules_tsfn
.call(compilation, ThreadsafeFunctionCallMode::NonBlocking)
.into_rspack_result()?
.await
.map_err(|err| internal_error!("Failed to finish modules: {err}"))?
}

async fn emit(&mut self, _: &mut rspack_core::Compilation) -> rspack_error::Result<()> {
if self.is_hook_disabled(&Hook::Emit) {
return Ok(());
Expand Down Expand Up @@ -300,6 +323,7 @@ impl JsHooksAdapter {
after_emit,
optimize_chunk_module,
normal_module_factory_resolve_for_scheme,
finish_modules,
} = js_hooks;

let process_assets_stage_additional_tsfn: ThreadsafeFunction<(), ()> =
Expand All @@ -323,7 +347,8 @@ impl JsHooksAdapter {
let make_tsfn: ThreadsafeFunction<(), ()> = js_fn_into_theadsafe_fn!(make, env);
let optimize_chunk_modules_tsfn: ThreadsafeFunction<JsCompilation, ()> =
js_fn_into_theadsafe_fn!(optimize_chunk_module, env);

let finish_modules_tsfn: ThreadsafeFunction<JsCompilation, ()> =
js_fn_into_theadsafe_fn!(finish_modules, env);
let normal_module_factory_resolve_for_scheme: ThreadsafeFunction<
SchemeAndJsResourceData,
JsResourceData,
Expand All @@ -344,6 +369,7 @@ impl JsHooksAdapter {
after_emit_tsfn,
optimize_chunk_modules_tsfn,
normal_module_factory_resolve_for_scheme,
finish_modules_tsfn,
})
}

Expand Down
7 changes: 7 additions & 0 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,13 @@ impl Compilation {
.get(ukey)
.expect("entrypoint not found by ukey")
}

#[instrument(name = "compilation:finish", skip_all)]
pub async fn finish(&mut self, plugin_driver: SharedPluginDriver) -> Result<()> {
plugin_driver.write().await.finish_modules(self).await?;
Ok(())
}

#[instrument(name = "compilation:seal", skip_all)]
pub async fn seal(&mut self, plugin_driver: SharedPluginDriver) -> Result<()> {
use_code_splitting_cache(self, |compilation| async {
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ where
async fn compile(&mut self, params: SetupMakeParam) -> Result<()> {
let option = self.options.clone();
self.compilation.make(params).await?;
self.compilation.finish(self.plugin_driver.clone()).await?;
if option.builtins.tree_shaking {
let (analyze_result, diagnostics) = self
.compilation
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 @@ -244,6 +244,10 @@ pub trait Plugin: Debug + Send + Sync {
Ok(())
}

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

async fn build_module(&self, _module: &mut dyn Module) -> Result<()> {
Ok(())
}
Expand Down
8 changes: 8 additions & 0 deletions crates/rspack_core/src/plugin/plugin_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,14 @@ impl PluginDriver {
Ok(())
}

#[instrument(name = "plugin:finish_modules", skip_all)]
pub async fn finish_modules(&mut self, modules: &mut Compilation) -> Result<()> {
for plugin in &mut self.plugins {
plugin.finish_modules(modules).await?;
}
Ok(())
}

pub async fn build_module(&self, module: &mut dyn Module) -> Result<()> {
for plugin in &self.plugins {
plugin.build_module(module).await?;
Expand Down
4 changes: 3 additions & 1 deletion packages/rspack/src/compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export class Compilation {
[Iterable<JsChunk>, Iterable<JsModule>],
undefined
>;
finishModules: tapable.AsyncSeriesHook<[Iterable<JsModule>], undefined>;
};
options: RspackOptionsNormalized;
outputOptions: OutputNormalized;
Expand All @@ -103,7 +104,8 @@ export class Compilation {
optimizeChunkModules: new tapable.AsyncSeriesBailHook([
"chunks",
"modules"
])
]),
finishModules: new tapable.AsyncSeriesHook(["modules"])
};
this.compiler = compiler;
this.resolverFactory = compiler.resolverFactory;
Expand Down
12 changes: 11 additions & 1 deletion packages/rspack/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class Compiler {
// No matter how it will be implemented, it will be copied to the child compiler.
compilation: this.#compilation.bind(this),
optimizeChunkModule: this.#optimize_chunk_modules.bind(this),
finishModules: this.#finish_modules.bind(this),
normalModuleFactoryResolveForScheme:
this.#normalModuleFactoryResolveForScheme.bind(this)
},
Expand Down Expand Up @@ -303,7 +304,8 @@ class Compiler {
Compilation.PROCESS_ASSETS_STAGE_REPORT
),
compilation: this.hooks.compilation,
optimizeChunkModules: this.compilation.hooks.optimizeChunkModules
optimizeChunkModules: this.compilation.hooks.optimizeChunkModules,
finishModules: this.compilation.hooks.finishModules
// normalModuleFactoryResolveForScheme: this.#
};
for (const [name, hook] of Object.entries(hookMap)) {
Expand Down Expand Up @@ -342,6 +344,14 @@ class Compiler {
);
this.#updateDisabledHooks();
}

async #finish_modules() {
await this.compilation.hooks.finishModules.promise(
this.compilation.getModules()
);
this.#updateDisabledHooks();
}

async #make() {
await this.hooks.make.promise(this.compilation);
this.#updateDisabledHooks();
Expand Down