Skip to content

Commit af6c6d4

Browse files
hanslvikerman
authored andcommitted
refactor: add count of component to analytics
1 parent 040e3ea commit af6c6d4

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

docs/design/analytics.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Note: There's a limit of 20 custom dimensions.
6969
<!--METRICS_TABLE_BEGIN-->
7070
| Id | Flag | Type |
7171
|:---:|:---|:---|
72-
| 1 | `UNUSED_1` | `none` |
72+
| 1 | `NgComponentCount` | `number` |
7373
| 2 | `UNUSED_2` | `none` |
7474
| 3 | `UNUSED_3` | `none` |
7575
| 4 | `UNUSED_4` | `none` |

etc/api/angular_devkit/core/src/_golden-api.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ export declare const NgCliAnalyticsDimensionsFlagInfo: {
626626
};
627627

628628
export declare enum NgCliAnalyticsMetrics {
629-
UNUSED_1 = 1,
629+
NgComponentCount = 1,
630630
UNUSED_2 = 2,
631631
UNUSED_3 = 3,
632632
UNUSED_4 = 4,

packages/angular_devkit/build_angular/plugins/webpack/analytics.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export function countOccurrences(source: string, match: string, wordBreak = fals
7272
class 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

packages/angular_devkit/core/src/analytics/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export enum NgCliAnalyticsDimensions {
3030
}
3131

3232
export enum NgCliAnalyticsMetrics {
33-
UNUSED_1 = 1,
33+
NgComponentCount = 1,
3434
UNUSED_2 = 2,
3535
UNUSED_3 = 3,
3636
UNUSED_4 = 4,
@@ -62,7 +62,7 @@ export const NgCliAnalyticsDimensionsFlagInfo: { [name: string]: [string, string
6262
// This table is used when generating the analytics.md file. It should match the enum above
6363
// or the validate-user-analytics script will fail.
6464
export const NgCliAnalyticsMetricsFlagInfo: { [name: string]: [string, string] } = {
65-
UNUSED_1: ['UNUSED_1', 'none'],
65+
NgComponentCount: ['NgComponentCount', 'number'],
6666
UNUSED_2: ['UNUSED_2', 'none'],
6767
UNUSED_3: ['UNUSED_3', 'none'],
6868
UNUSED_4: ['UNUSED_4', 'none'],

0 commit comments

Comments
 (0)