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.toJson() and stats.toString() rely on compilation artifacts that are finalized in compiler.hooks.done. If they are called at other times (for example, on stale Stats objects captured earlier), some stats fields can be incomplete.
For complete and stable stats output, call these methods in compiler.hooks.done:
Stats methods
hasErrors
Can be used to check if there were errors while compiling.
Type:
Use the return value to handle compilation errors:
hasWarnings
Can be used to check if there were warnings while compiling.
Type:
Use the return value to handle compilation warnings:
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:
Type:
Use the 'minimal' preset and print the number of errors:
Use an options object to select the fields to include, then print the compilation hash:
toString
Return the compilation information in the form of a formatted string (similar to the output of CLI).
Type:
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 | null
Get the hash of this compilation, same as Compilation.hash.
When using it as a string, check for null first:
MultiStats
When using MultiCompiler to run multiple compilation tasks, their results are packaged as a MultiStats object. It provides a combined hash and methods for checking, serializing, and formatting all child compilation results.
hash
ReadOnlyType: string
Get the hash formed by concatenating the hashes of all child compilations.
Print the concatenated hash:
hasErrors
Returns true if any child compilation has errors.
Type:
Use the return value to handle errors across all child compilations:
hasWarnings
Returns true if any child compilation has warnings.
Type:
Use the return value to handle warnings across all child compilations:
toJson
Returns a StatsCompilation whose children array contains the Stats JSON for each child compilation. Fields enabled for every child, such as errors and warnings, are also aggregated at the top level.
Type:
Use one preset for every child compilation and inspect the number of results:
MultiStatsOptions also supports a children option for configuring each child compilation individually. The following example prints the errors from the first child compilation:
toString
Format each child compilation according to the stats configuration, then concatenate the results into one string.
Type:
Use one preset for every child compilation and print the combined output:
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.

