Stats
The stats object that is passed as a second argument of the rspack() callback, is a good source of information about the code compilation process. It includes:
- Errors and Warnings (if any)
- Timings
- Module and Chunk information
The Stats object provides two important methods:
toJson(): Output information in the form of a Stats JSON object, which is often used in analysis tools.toString(): Output information in the form of a string, which is often used in the CLI tools.
Rspack also provides StatsFactory and StatsPrinter to fine-grained control the output object or string.
Create a stats object related to a compilation through compilation.getStats() or new Stats(compilation).
Stats methods
hasErrors
Can be used to check if there were errors while compiling.
hasWarnings
Can be used to check if there were warnings while compiling.
toJson
Return the compilation information in the form of a Stats JSON object. The Stats configuration can be a string (preset value) or an object for granular control:
toString
Return the compilation information in the form of a formatted string (similar to the output of CLI).
Options are the same as stats.toJson(options) with one addition:
Here's an example of stats.toString() usage:
Stats properties
compilation
Type: Compilation
Get the related compilation object.
hash
Type: string
Get the hash of this compilation, same as Compilation.hash.
MultiStats
When using MultiCompiler to execute multiple compilation tasks, their compilation stats will be packaged as a MultiStats object, which provides similar methods and properties as Stats.
hash
ReadOnlyType: string
Get the unique hash after merging the hashes of all compilations.
hasErrors
Used to check if there are errors during the compilation period, and only if there are no errors in all compilations will it return false.
hasWarnings
Used to check if there are warnings during the compilation period, and only if there are no warnings in all compilations will it return false.
toJson
According to the stats configuration, generate all the stats json objects, wrap them in the children field, and also summarize the errors and warnings.
toString
Concatenate the stats output strings of all compilations according to the stats configuration.
Stats factory
Used to generate the stats json object from the Compilation, and provides hooks for fine-grained control during the generation process.
It can be got through compilation.hooks.statsFactory. Or create a new one by new StatsFactory().
Hooks
See StatsFactory hooks for more details.
create
The core method of StatsFactory, according to the type to specify the current data structure, find and run the corresponding generator to generate the stats json item.
The
StatsFactoryobject only handles the calling of hooks, and the processing code of the corresponding type can be found inDefaultStatsFactoryPlugin.
Stats printer
Used to generate the output string from the stats json object, and provides hooks for fine-grained control during the generation process.
It can be got through compilation.hooks.statsPrinter. Or create a new one by new StatsPrinter().
Hooks
See StatsPrinter hooks for more details.
The core method of StatsPrinter, according to the type to specify the current data structure, find and run the corresponding generator to generate the output string of the stats item.
The
StatsPrinterobject only handles the calling of hooks, and the processing code of the corresponding type can be found inDefaultStatsPrinterPlugin.

