@@ -4,31 +4,20 @@ import { launch } from 'chrome-launcher';
4
4
import { pipe , concurrent , toAsync , toArray , map } from '@fxts/core' ;
5
5
import EventEmitter from 'events' ;
6
6
7
- import { Reporter } from './tools' ;
8
- import { WptContext , WptConfig , WptAuditPath } from './context' ;
7
+ import { WptContext , WptConfig } from './context' ;
9
8
import * as constants from './constants' ;
10
- import { WptEventHandlersEventMap } from './Wpt.types' ;
11
-
12
- export interface WebPerformanceTesterProps {
13
- reporter ?: Reporter ;
14
- }
15
-
16
- export interface WptAuditPathItem extends WptAuditPath {
17
- url : string ;
18
- }
9
+ import { WptAuditPathItem , WptEventHandlersEventMap } from './Wpt.types' ;
10
+ import { writeAuditsReportJsonFile , writeResultReportFile } from './tools' ;
19
11
20
12
const context = WptContext ( ) ;
21
13
22
14
class WebPerformanceTester extends EventEmitter {
23
- private reporter : Reporter ;
24
-
25
- constructor ( props ?: WebPerformanceTesterProps ) {
15
+ constructor ( ) {
26
16
super ( ) ;
27
- this . reporter = props ?. reporter ?? new Reporter ( ) ;
28
- this . typedEmit ( 'onReadyWpt' , { context } ) ;
17
+ this . _emit ( 'onReadyWpt' , { context } ) ;
29
18
}
30
19
31
- private typedEmit (
20
+ private _emit (
32
21
eventName : keyof WptEventHandlersEventMap ,
33
22
eventArgs : WptEventHandlersEventMap [ keyof WptEventHandlersEventMap ] ,
34
23
) {
@@ -39,10 +28,12 @@ class WebPerformanceTester extends EventEmitter {
39
28
lighthouseResult : RunnerResult ,
40
29
auditPath : WptAuditPathItem ,
41
30
) {
42
- this . typedEmit ( 'onReportStart' , { auditPath, context } ) ;
43
- this . reporter . saveAuditsReportFile ( lighthouseResult . report , auditPath . name ) ;
44
- await this . reporter . createAuditsReport ( lighthouseResult . lhr . audits ) ;
45
- this . typedEmit ( 'onReportEnd' , { auditPath, context } ) ;
31
+ if ( context . config . useReporter ) {
32
+ this . _emit ( 'onReportStart' , { auditPath, context } ) ;
33
+ writeResultReportFile ( lighthouseResult . report , auditPath . name ) ;
34
+ writeAuditsReportJsonFile ( lighthouseResult . lhr . audits , auditPath . name ) ;
35
+ this . _emit ( 'onReportEnd' , { auditPath, context } ) ;
36
+ }
46
37
}
47
38
48
39
private async runLighthouse (
@@ -55,22 +46,21 @@ class WebPerformanceTester extends EventEmitter {
55
46
port : options . port + index ,
56
47
} ) ;
57
48
try {
58
- this . typedEmit ( 'onStartedLighthouse' , { auditPath, context } ) ;
49
+ this . _emit ( 'onStartedLighthouse' , { auditPath, context } ) ;
50
+ const isDesktop = options . formFactor === 'desktop' ;
59
51
const lighthouseResult : RunnerResult = await lighthouse ( auditPath . url , {
60
52
...options ,
61
- screenEmulation :
62
- options . formFactor === 'desktop' &&
63
- constants . ScreenEmulationMetrics . desktop ,
53
+ screenEmulation : isDesktop && constants . ScreenEmulationMetrics . desktop ,
64
54
maxWaitForLoad : context . config . timeout ,
65
55
port : options . port + index ,
66
56
} ) ;
67
57
await this . createLighthouseReport ( lighthouseResult , auditPath ) ;
68
- this . typedEmit ( 'onFinishedLighthouse' , { auditPath, context } ) ;
58
+ this . _emit ( 'onFinishedLighthouse' , { auditPath, context } ) ;
69
59
} catch ( error ) {
70
60
if ( error instanceof Error ) {
71
- this . typedEmit ( 'onErrorLighthouse' , { auditPath, context, error } ) ;
61
+ this . _emit ( 'onErrorLighthouse' , { auditPath, context, error } ) ;
72
62
} else {
73
- this . typedEmit ( 'onErrorLighthouse' , { auditPath, context } ) ;
63
+ this . _emit ( 'onErrorLighthouse' , { auditPath, context } ) ;
74
64
}
75
65
} finally {
76
66
chrome . kill ( ) ;
@@ -79,7 +69,7 @@ class WebPerformanceTester extends EventEmitter {
79
69
80
70
async run ( ) {
81
71
try {
82
- this . typedEmit ( 'onStartedWpt' , { context } ) ;
72
+ this . _emit ( 'onStartedWpt' , { context } ) ;
83
73
let index = 0 ;
84
74
const { concurrency, options } = context . config ;
85
75
await pipe (
@@ -92,12 +82,12 @@ class WebPerformanceTester extends EventEmitter {
92
82
concurrent ( concurrency ) ,
93
83
( values ) => toArray ( values ) ,
94
84
) ;
95
- this . typedEmit ( 'onFinishedWpt' , { context } ) ;
85
+ this . _emit ( 'onFinishedWpt' , { context } ) ;
96
86
} catch ( error ) {
97
87
if ( error instanceof Error ) {
98
- this . typedEmit ( 'onErrorWpt' , { context, error } ) ;
88
+ this . _emit ( 'onErrorWpt' , { context, error } ) ;
99
89
} else {
100
- this . typedEmit ( 'onErrorWpt' , { context } ) ;
90
+ this . _emit ( 'onErrorWpt' , { context } ) ;
101
91
}
102
92
}
103
93
}
0 commit comments