-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathoccurrences-plugin.ts
98 lines (83 loc) · 2.88 KB
/
occurrences-plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Compiler } from 'webpack';
const PLUGIN_NAME = 'angular-occurrences-plugin';
export interface OccurrencesPluginOptions {
aot?: boolean;
scriptsOptimization?: boolean;
}
export class OccurrencesPlugin {
constructor(private options: OccurrencesPluginOptions) {}
apply(compiler: Compiler) {
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
compilation.hooks.processAssets.tapPromise(
{
name: PLUGIN_NAME,
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ANALYSE,
},
async (compilationAssets) => {
for (const assetName of Object.keys(compilationAssets)) {
if (!assetName.endsWith('.js')) {
continue;
}
const scriptAsset = compilation.getAsset(assetName);
if (!scriptAsset || scriptAsset.source.size() <= 0) {
continue;
}
const src = scriptAsset.source.source().toString('utf-8');
let ngComponentCount = 0;
if (!this.options.aot) {
// Count the number of `Component({` strings (case sensitive), which happens in __decorate().
ngComponentCount += this.countOccurrences(src, 'Component({');
}
if (this.options.scriptsOptimization) {
// for ascii_only true
ngComponentCount += this.countOccurrences(src, '.\\u0275cmp', false);
} else {
// For Ivy we just count ɵcmp.src
ngComponentCount += this.countOccurrences(src, '.ɵcmp', true);
}
compilation.updateAsset(
assetName,
(s) => s,
(assetInfo) => ({
...assetInfo,
ngComponentCount,
}),
);
}
},
);
});
}
private countOccurrences(source: string, match: string, wordBreak = false): number {
let count = 0;
// We condition here so branch prediction happens out of the loop, not in it.
if (wordBreak) {
const re = /\w/;
for (let pos = source.lastIndexOf(match); pos >= 0; pos = source.lastIndexOf(match, pos)) {
if (!(re.test(source[pos - 1] || '') || re.test(source[pos + match.length] || ''))) {
count++; // 1 match, AH! AH! AH! 2 matches, AH! AH! AH!
}
pos -= match.length;
if (pos < 0) {
break;
}
}
} else {
for (let pos = source.lastIndexOf(match); pos >= 0; pos = source.lastIndexOf(match, pos)) {
count++; // 1 match, AH! AH! AH! 2 matches, AH! AH! AH!
pos -= match.length;
if (pos < 0) {
break;
}
}
}
return count;
}
}