The main compilation logic of Rspack runs on the Rust side. For factors such as stability, performance, and architecture, after the Rust side compilation objects are transferred to the JavaScript side when using hooks, the modifications on these objects will not be synchronized to the Rust side. Therefore, most of hooks are "read-only".
buildModule
Triggered before a module build has started.
SyncHook<[Module]>
Module
: module instanceexecuteModule
If there exists compiled-time execution modules, this hook will be called when they are executed.
SyncHook<[ExecuteModuleArgument, ExecuteModuleContext]>
ExecuteModuleArgument
: arguments of compiled-time execution moduleExecuteModuleContext
: context of compiled-time execution modulesucceedModule
Executed when a module has been built successfully.
SyncHook<[Module]>
Module
: module instancefinishModules
Called when all modules have been built without errors.
AsyncSeriesHook<[Module[]]>
Module[]
: List of module instancesseal
Called when the compilation stops accepting new modules and starts to optimize modules.
SyncHook<[]>
optimizeModules
Called at the beginning of the module optimization phase.
SyncBailHook<[Module[]]>
Module[]
: list of module instancesafterOptimizeModules
Called after modules optimization has completed.
SyncBailHook<[Module[]]>
Module[]
: list of module instancesoptimizeTree
Called before optimizing the dependency tree.
AsyncSeriesHook<[Chunk[], Module[]]>
Chunk[]
: list of chunk instancesModule[]
: list of module instancesoptimizeChunkModules
Called after the tree optimization, at the beginning of the chunk modules optimization.
AsyncSeriesBailHook<[Chunk[], Module[]]>
Chunk[]
: list of chunk instancesModule[]
: list of module instancesadditionalTreeRuntimeRequirements
Called after the tree runtime requirements collection.
SyncHook<[Chunk, Set<RuntimeGlobals>]>
Chunk
: chunk instanceSet<RuntimeGlobals>
: runtime requirementsAdditional builtin runtime modules can be added here by modifying the runtime requirements set.
runtimeRequirementInTree
Called during adding runtime modules to the compilation.
HookMap<SyncBailHook<[Chunk, Set<RuntimeGlobals>]>>
Chunk
: chunk instanceSet<RuntimeGlobals>
: runtime requirementsAdditional builtin runtime modules can be added here by modifying the runtime requirements set or calling compilation.addRuntimeModule
to add custom runtime modules.
runtimeModule
Called after a runtime module is added into the compilation.
SyncHook<[RuntimeModule, Chunk]>
RuntimeModule
: runtime module instanceChunk
: chunk instanceGenerated code of this runtime module can be modified through its source
property.
processAssets
Process the assets before emit.
AsyncSeriesHook<Assets>
name: string
— a name of the pluginstage: Stage
— a stage to tap into (see the process assets stages below)Assets: Record<string, Source>
: a plain object, where key is the asset's pathname, and the value is data of the asset represented by the Source.PROCESS_ASSETS_STAGE_ADDITIONAL
stage:Here's the list of supported stages. Rspack will execute these stages sequentially from top to bottom. Please select the appropriate stage based on the operation you need to perform.
PROCESS_ASSETS_STAGE_ADDITIONAL
— add additional assets to the compilation.PROCESS_ASSETS_STAGE_PRE_PROCESS
— basic preprocessing of the assets.PROCESS_ASSETS_STAGE_DERIVED
— derive new assets from the existing assets.PROCESS_ASSETS_STAGE_ADDITIONS
— add additional sections to the existing assets e.g. banner or initialization code.PROCESS_ASSETS_STAGE_OPTIMIZE
— optimize existing assets in a general way.PROCESS_ASSETS_STAGE_OPTIMIZE_COUNT
— optimize the count of existing assets, e.g. by merging them.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY
— optimize the compatibility of existing assets, e.g. add polyfills or vendor prefixes.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE
— optimize the size of existing assets, e.g. by minimizing or omitting whitespace.PROCESS_ASSETS_STAGE_DEV_TOOLING
— add development tooling to the assets, e.g. by extracting a source map.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE
— optimize the numbers of existing assets by inlining assets into other assets.PROCESS_ASSETS_STAGE_SUMMARIZE
— summarize the list of existing assets.PROCESS_ASSETS_STAGE_OPTIMIZE_HASH
— optimize the hashes of the assets, e.g. by generating real hashes of the asset content.PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER
— optimize the transfer of existing assets, e.g. by preparing a compressed (gzip) file as separate asset.PROCESS_ASSETS_STAGE_ANALYSE
— analyze the existing assets.PROCESS_ASSETS_STAGE_REPORT
— creating assets for the reporting purposes.afterProcessAssets
Called after the processAssets hook had finished without error.
SyncHook<Assets>
Assets: Record<string, Source>
: list of asset instancesafterSeal
Called after the seal phase.
AsyncSeriesHook<[]>
chunkHash
Triggered to emit the hash for each chunk.
SyncHook<[Chunk, Hash]>
Chunk
: chunk instanceHash
: chunk hash instancechunkAsset
Triggered when an asset from a chunk was added to the compilation.
SyncHook<[Chunk, string]>
Chunk
: chunk instancestring
: asset filenamechildCompiler
Executed after setting up a child compiler.
SyncHook<[Compiler, string, number]>
Compiler
: child compiler instancestring
: child compiler namenumber
: child compiler indexstatsPreset
This hook is like a list of actions that gets triggered when a preset is used. It takes in an options object. When a plugin manages a preset, it should change settings in this object carefully without replacing existing ones.
SyncHook<[Partial<StatsOptions>, CreateStatsOptionsContext]>
Partial<StatsOptions>
: stats optionsCreateStatsOptionsContext
: stats contextHere's an illustrative plugin example:
This plugin ensures that for the preset "my-preset"
, if the all
option is undefined, it defaults to true
.
statsNormalize
This hook is used to transform an options object into a consistent format that can be easily used by subsequent hooks. It also ensures that missing options are set to their default values.
SyncHook<[Partial<StatsOptions>, CreateStatsOptionsContext]>
Partial<StatsOptions>
: stats optionsCreateStatsOptionsContext
: stats contextHere's an illustrative plugin example:
In this plugin, if the myOption
is missing, it sets it to []
. Additionally, it ensures that myOption
is always an array even if it was originally defined as a single value.
statsFactory
This hook provides access to the StatsFactory class for specific options.
SyncHook<[StatsFactory, StatsOptions]>
StatsFactory
: stats factory instance, see Stats Factory Hooks for more detailsStatsOptions
: stats optionsstatsPrinter
This hook provides access to the StatsPrinter class for specific options.
SyncHook<[StatsPrinter, StatsOptions]>
StatsPrinter
: stats printer instance, see Stats Printer Hooks for more details.StatsOptions
: stats options