1
- import type { StatsCompilation } from '@rspack/core' ;
2
1
import color from '../../compiled/picocolors/index.js' ;
3
2
import { logger } from '../logger' ;
4
- import type { Rspack } from '../types' ;
3
+ import type { RsbuildStats , Rspack } from '../types' ;
5
4
import { isMultiCompiler } from './' ;
6
5
import { formatStatsError } from './format' ;
7
6
@@ -19,19 +18,46 @@ function formatErrorMessage(errors: string[]) {
19
18
return `${ title } \n${ text } ` ;
20
19
}
21
20
22
- export const getAllStatsErrors = (
23
- statsData : StatsCompilation ,
24
- ) : Rspack . StatsError [ ] | undefined => {
25
- // stats error + childCompiler error
26
- // only append child errors when stats error does not exist, because some errors will exist in both stats and childCompiler
27
- if ( statsData . errorsCount && statsData . errors ?. length === 0 ) {
28
- return statsData . children ?. reduce < Rspack . StatsError [ ] > (
29
- ( errors , curr ) => errors . concat ( curr . errors || [ ] ) ,
21
+ /**
22
+ * If stats has errors, return stats errors directly
23
+ * If stats has no errors, return child errors, as some errors exist in both
24
+ * stats and childCompiler
25
+ */
26
+ export const getStatsErrors = ( {
27
+ errors,
28
+ children,
29
+ } : RsbuildStats ) : Rspack . StatsError [ ] => {
30
+ if ( errors !== undefined && errors . length > 0 ) {
31
+ return errors ;
32
+ }
33
+
34
+ if ( children ) {
35
+ return children . reduce < Rspack . StatsError [ ] > (
36
+ ( errors , ret ) => ( ret . errors ? errors . concat ( ret . errors ) : errors ) ,
37
+ [ ] ,
38
+ ) ;
39
+ }
40
+
41
+ return [ ] ;
42
+ } ;
43
+
44
+ export const getStatsWarnings = ( {
45
+ warnings,
46
+ children,
47
+ } : RsbuildStats ) : Rspack . StatsError [ ] => {
48
+ if ( warnings !== undefined && warnings . length > 0 ) {
49
+ return warnings ;
50
+ }
51
+
52
+ if ( children ) {
53
+ return children . reduce < Rspack . StatsError [ ] > (
54
+ ( warnings , ret ) =>
55
+ ret . warnings ? warnings . concat ( ret . warnings ) : warnings ,
30
56
[ ] ,
31
57
) ;
32
58
}
33
59
34
- return statsData . errors ;
60
+ return [ ] ;
35
61
} ;
36
62
37
63
export const getAssetsFromStats = (
@@ -51,19 +77,6 @@ export const getAssetsFromStats = (
51
77
return statsJson . assets || [ ] ;
52
78
} ;
53
79
54
- export const getAllStatsWarnings = (
55
- statsData : StatsCompilation ,
56
- ) : Rspack . StatsError [ ] | undefined => {
57
- if ( statsData . warningsCount && statsData . warnings ?. length === 0 ) {
58
- return statsData . children ?. reduce < Rspack . StatsError [ ] > (
59
- ( warnings , curr ) => warnings . concat ( curr . warnings || [ ] ) ,
60
- [ ] ,
61
- ) ;
62
- }
63
-
64
- return statsData . warnings ;
65
- } ;
66
-
67
80
export function getStatsOptions (
68
81
compiler : Rspack . Compiler | Rspack . MultiCompiler ,
69
82
) : Rspack . StatsOptions {
@@ -88,7 +101,7 @@ export function getStatsOptions(
88
101
}
89
102
90
103
export function formatStats (
91
- statsData : Rspack . StatsCompilation ,
104
+ stats : RsbuildStats ,
92
105
hasErrors : boolean ,
93
106
) : {
94
107
message ?: string ;
@@ -98,15 +111,15 @@ export function formatStats(
98
111
const verbose = logger . level === 'verbose' ;
99
112
100
113
if ( hasErrors ) {
101
- const statsErrors = getAllStatsErrors ( statsData ) ?? [ ] ;
114
+ const statsErrors = getStatsErrors ( stats ) ;
102
115
const errors = statsErrors . map ( ( item ) => formatStatsError ( item , verbose ) ) ;
103
116
return {
104
117
message : formatErrorMessage ( errors ) ,
105
118
level : 'error' ,
106
119
} ;
107
120
}
108
121
109
- const statsWarnings = getAllStatsWarnings ( statsData ) ?? [ ] ;
122
+ const statsWarnings = getStatsWarnings ( stats ) ;
110
123
const warnings = statsWarnings . map ( ( item ) => formatStatsError ( item , verbose ) ) ;
111
124
112
125
if ( warnings . length ) {
0 commit comments