1
1
import color from '../../compiled/picocolors/index.js' ;
2
2
import { logger } from '../logger' ;
3
- import type { RsbuildStats , Rspack } from '../types' ;
3
+ import type { ActionType , RsbuildStats , Rspack } from '../types' ;
4
4
import { isMultiCompiler } from './' ;
5
5
import { formatStatsError } from './format' ;
6
6
@@ -77,11 +77,32 @@ export const getAssetsFromStats = (
77
77
return statsJson . assets || [ ] ;
78
78
} ;
79
79
80
- export function getStatsOptions (
80
+ function getStatsOptions (
81
81
compiler : Rspack . Compiler | Rspack . MultiCompiler ,
82
+ action ?: ActionType ,
82
83
) : Rspack . StatsOptions {
84
+ const defaultOptions : Rspack . StatsOptions = {
85
+ all : false ,
86
+ // for displaying the build time
87
+ timings : true ,
88
+ // for displaying the build errors
89
+ errors : true ,
90
+ // for displaying the build warnings
91
+ warnings : true ,
92
+ // for displaying the module trace when build failed
93
+ moduleTrace : true ,
94
+ } ;
95
+
96
+ if ( action === 'dev' ) {
97
+ // for HMR to compare the hash
98
+ defaultOptions . hash = true ;
99
+ // for HMR to compare the entrypoints
100
+ defaultOptions . entrypoints = true ;
101
+ }
102
+
83
103
if ( isMultiCompiler ( compiler ) ) {
84
104
return {
105
+ ...defaultOptions ,
85
106
children : compiler . compilers . map ( ( compiler ) =>
86
107
compiler . options ? compiler . options . stats : undefined ,
87
108
) ,
@@ -91,13 +112,23 @@ export function getStatsOptions(
91
112
const { stats } = compiler . options ;
92
113
93
114
if ( typeof stats === 'string' ) {
94
- return { preset : stats } ;
115
+ return { ... defaultOptions , preset : stats } ;
95
116
}
117
+
96
118
if ( typeof stats === 'object' ) {
97
- return stats ;
119
+ return { ... defaultOptions , ... stats } ;
98
120
}
99
121
100
- return { } ;
122
+ return defaultOptions ;
123
+ }
124
+
125
+ export function getRsbuildStats (
126
+ statsInstance : Rspack . Stats | Rspack . MultiStats ,
127
+ compiler : Rspack . Compiler | Rspack . MultiCompiler ,
128
+ action ?: ActionType ,
129
+ ) : RsbuildStats {
130
+ const statsOptions = getStatsOptions ( compiler , action ) ;
131
+ return statsInstance . toJson ( statsOptions ) as RsbuildStats ;
101
132
}
102
133
103
134
export function formatStats (
@@ -111,26 +142,28 @@ export function formatStats(
111
142
const verbose = logger . level === 'verbose' ;
112
143
113
144
if ( hasErrors ) {
114
- const statsErrors = getStatsErrors ( stats ) ;
115
- const errors = statsErrors . map ( ( item ) => formatStatsError ( item , verbose ) ) ;
145
+ const errors = getStatsErrors ( stats ) ;
146
+ const errorMessages = errors . map ( ( item ) => formatStatsError ( item , verbose ) ) ;
116
147
return {
117
- message : formatErrorMessage ( errors ) ,
148
+ message : formatErrorMessage ( errorMessages ) ,
118
149
level : 'error' ,
119
150
} ;
120
151
}
121
152
122
- const statsWarnings = getStatsWarnings ( stats ) ;
123
- const warnings = statsWarnings . map ( ( item ) => formatStatsError ( item , verbose ) ) ;
153
+ const warnings = getStatsWarnings ( stats ) ;
154
+ const warningMessages = warnings . map ( ( item ) =>
155
+ formatStatsError ( item , verbose ) ,
156
+ ) ;
124
157
125
- if ( warnings . length ) {
158
+ if ( warningMessages . length ) {
126
159
const title = color . bold (
127
160
color . yellow (
128
- warnings . length > 1 ? 'Build warnings: \n' : 'Build warning: \n' ,
161
+ warningMessages . length > 1 ? 'Build warnings: \n' : 'Build warning: \n' ,
129
162
) ,
130
163
) ;
131
164
132
165
return {
133
- message : `${ title } ${ warnings . join ( '\n\n' ) } \n` ,
166
+ message : `${ title } ${ warningMessages . join ( '\n\n' ) } \n` ,
134
167
level : 'warning' ,
135
168
} ;
136
169
}
0 commit comments