@@ -72,6 +72,7 @@ export function countOccurrences(source: string, match: string, wordBreak = fals
72
72
class AnalyticsBuildStats {
73
73
public errors : string [ ] = [ ] ;
74
74
public numberOfNgOnInit = 0 ;
75
+ public numberOfComponents = 0 ;
75
76
public initialChunkSize = 0 ;
76
77
public totalChunkCount = 0 ;
77
78
public totalChunkSize = 0 ;
@@ -107,6 +108,7 @@ export class NgBuildAnalyticsPlugin {
107
108
const metrics : ( string | number ) [ ] = [ ] ;
108
109
metrics [ analytics . NgCliAnalyticsMetrics . BuildTime ] = ( endTime - startTime ) ;
109
110
metrics [ analytics . NgCliAnalyticsMetrics . NgOnInitCount ] = this . _stats . numberOfNgOnInit ;
111
+ metrics [ analytics . NgCliAnalyticsMetrics . NgComponentCount ] = this . _stats . numberOfComponents ;
110
112
metrics [ analytics . NgCliAnalyticsMetrics . InitialChunkSize ] = this . _stats . initialChunkSize ;
111
113
metrics [ analytics . NgCliAnalyticsMetrics . TotalChunkCount ] = this . _stats . totalChunkCount ;
112
114
metrics [ analytics . NgCliAnalyticsMetrics . TotalChunkSize ] = this . _stats . totalChunkSize ;
@@ -141,10 +143,29 @@ export class NgBuildAnalyticsPlugin {
141
143
142
144
protected _checkTsNormalModule ( module : NormalModule ) {
143
145
if ( module . _source ) {
146
+ // PLEASE REMEMBER:
144
147
// We're dealing with ES5 _or_ ES2015 JavaScript at this point (we don't know for sure).
148
+
145
149
// Just count the ngOnInit occurences. Comments/Strings/calls occurences should be sparse
146
150
// so we just consider them within the margin of error. We do break on word break though.
147
151
this . _stats . numberOfNgOnInit += countOccurrences ( module . _source . source ( ) , 'ngOnInit' , true ) ;
152
+
153
+ // Count the number of `Component({` strings (case sensitive), which happens in __decorate().
154
+ // This does not include View Engine AOT compilation, we use the ngfactory for it.
155
+ this . _stats . numberOfComponents += countOccurrences ( module . _source . source ( ) , ' Component({' ) ;
156
+ // For Ivy we just count ngComponentDef.
157
+ this . _stats . numberOfComponents += countOccurrences ( module . _source . source ( ) , 'ngComponentDef' , true ) ;
158
+ }
159
+ }
160
+
161
+ protected _checkNgFactoryNormalModule ( module : NormalModule ) {
162
+ if ( module . _source ) {
163
+ // PLEASE REMEMBER:
164
+ // We're dealing with ES5 _or_ ES2015 JavaScript at this point (we don't know for sure).
165
+
166
+ // Count the number of `.ɵccf(` strings (case sensitive). They're calls to components
167
+ // factories.
168
+ this . _stats . numberOfComponents += countOccurrences ( module . _source . source ( ) , '.ɵccf(' ) ;
148
169
}
149
170
}
150
171
@@ -231,8 +252,10 @@ export class NgBuildAnalyticsPlugin {
231
252
}
232
253
233
254
// Check that it's a source file from the project.
234
- if ( module . constructor === NormalModule && module . resource . endsWith ( '.ts' ) ) {
235
- this . _checkTsNormalModule ( module as { } as NormalModule ) ;
255
+ if ( module . resource . endsWith ( '.ts' ) ) {
256
+ this . _checkTsNormalModule ( module ) ;
257
+ } else if ( module . resource . endsWith ( '.ngfactory.js' ) ) {
258
+ this . _checkNgFactoryNormalModule ( module ) ;
236
259
}
237
260
}
238
261
0 commit comments