Hooks API
Hooks are used to tap into different stages of the compilation process. They allow plugins to modify the behavior of the compiler and compilation.
SyncHook
A SyncHook
is a synchronous hook that can be tapped into by plugins.
const hook = new SyncHook('test');
hook.tap('MyPlugin');
hook.call();
Properties
name
The name
property is a string that represents the name of the hook.
console.log(hook.name);
Implementation Details
#[napi(object)]
pub struct SyncHook {
pub name: String,
// ...
}
taps
The taps
property is an array of strings that represents the names of the plugins that have tapped into the hook.
console.log(hook.taps);
Implementation Details
#[napi(object)]
pub struct SyncHook {
// ...
pub taps: Vec<String>,
}
Methods
new(name)
Creates a new hook with the given name.
const hook = new SyncHook('test');
Parameters
name
(String): The name of the hook
Returns
SyncHook
: A new hook instance
Implementation Details
impl SyncHook {
pub fn new(name: &str) -> Self {
Self {
name: name.to_string(),
taps: Vec::new(),
}
}
}
tap(name)
Taps into the hook with the given name.
hook.tap('MyPlugin');
Parameters
name
(String): The name of the plugin
Implementation Details
impl SyncHook {
pub fn tap(&mut self, name: &str) {
self.taps.push(name.to_string());
}
}
call(args)
Calls the hook with the given arguments.
hook.call();
Parameters
args
(Any): The arguments to pass to the hook
Implementation Details
impl SyncHook {
pub fn call(&self, args: Option<&mut HashMap<String, String>>) {
// Call the tapped functions
println!("Hook '{}' called with {} taps", self.name, self.taps.len());
// For now, we'll just print the taps
for tap in &self.taps {
println!(" - Tap: {}", tap);
}
}
}
CompilerHooks
The CompilerHooks
object contains hooks that are called during the compilation process.
compiler.hooks.done.tap('MyPlugin', (stats) => {
console.log('Compilation done!');
});
Properties
run
The run
hook is called before the compilation starts.
compiler.hooks.run.tap('MyPlugin', () => {
console.log('Compilation starting!');
});
Implementation Details
pub struct CompilerHooks {
pub run: SyncHook<Option<&mut HashMap<String, String>>>,
// ...
}
emit
The emit
hook is called before emitting assets to the output directory.
compiler.hooks.emit.tap('MyPlugin', (compilation) => {
console.log('Emitting assets!');
});
Implementation Details
pub struct CompilerHooks {
// ...
pub emit: SyncHook<Option<&mut HashMap<String, String>>>,
// ...
}
done
The done
hook is called when the compilation is complete.
compiler.hooks.done.tap('MyPlugin', (stats) => {
console.log('Compilation done!');
});
Implementation Details
pub struct CompilerHooks {
// ...
pub done: SyncHook<Option<&mut HashMap<String, String>>>,
}
CompilationHooks
The CompilationHooks
object contains hooks that are called during the compilation process.
compilation.hooks.emit.tap('MyPlugin', (assets) => {
console.log('Emitting assets!');
});
Properties
emit
The emit
hook is called before emitting assets to the output directory.
compilation.hooks.emit.tap('MyPlugin', (assets) => {
console.log('Emitting assets!');
});
Implementation Details
pub struct CompilationHooks {
pub emit: SyncHook<Option<&mut HashMap<String, String>>>,
}
Next Steps
- Compiler: Learn about the Compiler API
- Compilation: Learn about the Compilation API
- Module: Learn about the Module API