@@ -72,6 +72,7 @@ export function countOccurrences(source: string, match: string, wordBreak = fals
7272class AnalyticsBuildStats {
7373 public errors : string [ ] = [ ] ;
7474 public numberOfNgOnInit = 0 ;
75+ public numberOfComponents = 0 ;
7576 public initialChunkSize = 0 ;
7677 public totalChunkCount = 0 ;
7778 public totalChunkSize = 0 ;
@@ -107,6 +108,7 @@ export class NgBuildAnalyticsPlugin {
107108 const metrics : ( string | number ) [ ] = [ ] ;
108109 metrics [ analytics . NgCliAnalyticsMetrics . BuildTime ] = ( endTime - startTime ) ;
109110 metrics [ analytics . NgCliAnalyticsMetrics . NgOnInitCount ] = this . _stats . numberOfNgOnInit ;
111+ metrics [ analytics . NgCliAnalyticsMetrics . NgComponentCount ] = this . _stats . numberOfComponents ;
110112 metrics [ analytics . NgCliAnalyticsMetrics . InitialChunkSize ] = this . _stats . initialChunkSize ;
111113 metrics [ analytics . NgCliAnalyticsMetrics . TotalChunkCount ] = this . _stats . totalChunkCount ;
112114 metrics [ analytics . NgCliAnalyticsMetrics . TotalChunkSize ] = this . _stats . totalChunkSize ;
@@ -141,10 +143,29 @@ export class NgBuildAnalyticsPlugin {
141143
142144 protected _checkTsNormalModule ( module : NormalModule ) {
143145 if ( module . _source ) {
146+ // PLEASE REMEMBER:
144147 // We're dealing with ES5 _or_ ES2015 JavaScript at this point (we don't know for sure).
148+
145149 // Just count the ngOnInit occurences. Comments/Strings/calls occurences should be sparse
146150 // so we just consider them within the margin of error. We do break on word break though.
147151 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(' ) ;
148169 }
149170 }
150171
@@ -231,8 +252,10 @@ export class NgBuildAnalyticsPlugin {
231252 }
232253
233254 // 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 ) ;
236259 }
237260 }
238261
0 commit comments