Compiler hooks allow Rspack plugins to intervene at specific stages of the build process. These hooks represent various lifecycle stages from initialization to asset output.
This document lists the available compiler hooks in Rspack, their trigger timing, parameters, and usage examples.
See Compiler for more information about the Compiler object.
environmentCalled while preparing the compiler environment, right after initializing the plugins in the configuration file.
SyncHook<[]>afterEnvironmentCalled right after the environment hook, when the compiler environment setup is complete.
SyncHook<[]>entryOptionCalled after the entry configuration from Rspack options has been processed.
SyncBailHook<[string, EntryNormalized]>afterPluginsCalled after setting up initial set of internal plugins.
SyncHook<[Compiler]>Compiler: current compiler instanceafterResolversTriggered after resolver setup is complete.
SyncHook<[Compiler]>Compiler: current compiler instanceinitializeCalled when a compiler object is initialized.
SyncHook<[]>beforeRunAdds a hook right before running the compiler.
This hook is only triggered when calling compiler.run() (which is used by the rspack build command), and will not be executed in watch mode. You can use the watchRun hook in watch mode.
AsyncSeriesHook<[Compiler]>Compiler: current compiler instancerunCalled at the beginning of a build execution.
This hook is only triggered when calling compiler.run() (which is used by the rspack build command), and will not be executed in watch mode. You can use the watchRun hook in watch mode.
AsyncSeriesHook<[Compiler]>Compiler: current compiler instancewatchRunExecutes a plugin during watch mode after a new compilation is triggered but before the compilation is actually started.
You can use compiler.modifiedFiles and compiler.removedFiles to get the changed file paths and removed file paths.
This hook is only triggered when calling compiler.watch(), and will not be called in non-watch mode. You can use the run or beforeRun hook in non-watch mode.
AsyncSeriesHook<[Compiler]>Compiler: current compiler instancebeforeCompileExecutes a plugin after compilation parameters are created.
AsyncSeriesHook<[]>compileCalled right after beforeCompile, before a new compilation object is created.
SyncHook<[]>thisCompilationCalled while initializing the compilation, can be used to get the current compilation object.
You can use the compilation parameter to access the properties of the compilation object, or register compilation hooks.
SyncHook<[Compilation]>compilation: created compilation objectcompilationCalled after the compilation object is created, can be used to get the current compilation object.
You can use the compilation parameter to access the properties of the compilation object, or register compilation hooks.
compilation hook is called after the thisCompilation hook, and thisCompilation hook is not copied to child compiler, while compilation hook is copied to child compiler.
SyncHook<[Compilation]>compilation: created compilation objectmakeCalled before the make phase.
In the make phase, Rspack will build the module graph starting from the entry, and use the loader to handle each module.
AsyncParallelHook<[Compilation]>Compilation: current compilation objectfinishMakeCalled after finishing the make phase.
In the make phase, Rspack builds the module graph starting from the entry and uses loaders to handle each module. This hook is called when that process completes.
AsyncSeriesHook<[Compilation]>Compilation: current compilation objectafterCompileCalled after the make phase and before the seal phase.
In the seal phase, Rspack will create chunk graph from the module graph and then generate the assets.
AsyncSeriesHook<[Compilation]>Compilation: current compilation objectshouldEmitCalled before emitting assets. Should return a boolean telling whether to emit.
SyncBailHook<[Compilation], boolean>Compilation: current compilation objectemitCalled right before emitting assets to output dir.
AsyncSeriesHook<[Compilation]>Compilation: current compilation objectafterEmitCalled after emitting assets to output directory.
AsyncSeriesHook<[Compilation]>Compilation: current compilation objectdoneCalled when the compilation has completed.
AsyncSeriesHook<Stats>Stats: generated stats objectafterDoneCalled after done hook.
SyncHook<Stats>Stats: generated stats objectfailedCalled if the compilation fails.
SyncHook<[Error]>invalidExecuted when a watching compilation has been invalidated. This hook is not copied to child compilers.
SyncHook<[string | null, number]>fileName: the file path of the invalid filechangeTime: the change time of the invalid fileWhen triggering a re-compilation, this hook can be used to get the changed file path and change time, for example:
watchCloseCalled when a watching compilation has stopped.
SyncHook<[]>shutdownCalled when the compiler is closing.
AsyncSeriesHook<[]>Called when infrastructure logging is triggered, allowing plugins to intercept, modify, or handle log messages.
This hook provides a way to customize Rspack's infrastructure logs - you can filter specific log types, add custom formatting, or completely override the default logging behavior.
If the hook returns true, the default infrastructure logging will be prevented. If it returns undefined, the default logging will proceed.
SyncBailHook<[string, string, any[]], true | void>name: The name of the loggertype: The log type (e.g., 'log', 'warn', 'error', ...)args: An array of arguments passed to the logging methodSee infrastructureLogging to learn more about infrastructure logging.