@@ -65,7 +65,7 @@ function generateReportForCDNScripts(): Report[] {
65
65
}
66
66
67
67
// NPM packages
68
- function generateReportForNPMPackages ( ) : Report [ ] {
68
+ async function generateReportForNPMPackages ( ) : Promise < Report [ ] > {
69
69
const reports : Report [ ] = [ ] ;
70
70
const fields = [
71
71
'main' ,
@@ -81,41 +81,50 @@ function generateReportForNPMPackages(): Report[] {
81
81
execSync ( 'npx lerna ls --json --scope @firebase/*' ) . toString ( )
82
82
) ;
83
83
84
+ const taskPromises : Promise < void > [ ] = [ ] ;
84
85
for ( const pkg of packageInfo ) {
85
86
// we traverse the dir in order to include binaries for submodules, e.g. @firebase/firestore/memory
86
87
// Currently we only traverse 1 level deep because we don't have any submodule deeper than that.
87
88
traverseDirs ( pkg . location , collectBinarySize , 0 , 1 ) ;
88
89
}
89
90
90
- function collectBinarySize ( path : string ) {
91
- const packageJsonPath = `${ path } /package.json` ;
92
- if ( ! fs . existsSync ( packageJsonPath ) ) {
93
- return ;
94
- }
95
-
96
- const packageJson = require ( packageJsonPath ) ;
91
+ await Promise . all ( taskPromises ) ;
97
92
98
- for ( const field of fields ) {
99
- if ( packageJson [ field ] ) {
100
- const filePath = `${ path } /${ packageJson [ field ] } ` ;
101
- const rawCode = fs . readFileSync ( filePath , 'utf-8' ) ;
93
+ return reports ;
102
94
103
- // remove comments and whitespaces, then get size
104
- const { code } = terser . minify ( rawCode , {
105
- output : {
106
- comments : false
107
- } ,
108
- mangle : false ,
109
- compress : false
110
- } ) ;
95
+ function collectBinarySize ( path : string ) {
96
+ const promise = new Promise < void > ( async resolve => {
97
+ const packageJsonPath = `${ path } /package.json` ;
98
+ if ( ! fs . existsSync ( packageJsonPath ) ) {
99
+ return ;
100
+ }
111
101
112
- const size = Buffer . byteLength ( code ! , 'utf-8' ) ;
113
- reports . push ( makeReportObject ( packageJson . name , field , size ) ) ;
102
+ const packageJson = require ( packageJsonPath ) ;
103
+
104
+ for ( const field of fields ) {
105
+ if ( packageJson [ field ] ) {
106
+ const filePath = `${ path } /${ packageJson [ field ] } ` ;
107
+ const rawCode = fs . readFileSync ( filePath , 'utf-8' ) ;
108
+
109
+ // remove comments and whitespaces, then get size
110
+ const { code } = await terser . minify ( rawCode , {
111
+ format : {
112
+ comments : false
113
+ } ,
114
+ mangle : false ,
115
+ compress : false
116
+ } ) ;
117
+
118
+ const size = Buffer . byteLength ( code ! , 'utf-8' ) ;
119
+ reports . push ( makeReportObject ( packageJson . name , field , size ) ) ;
120
+ }
114
121
}
115
- }
116
- }
117
122
118
- return reports ;
123
+ resolve ( ) ;
124
+ } ) ;
125
+
126
+ taskPromises . push ( promise ) ;
127
+ }
119
128
}
120
129
121
130
function traverseDirs (
@@ -147,10 +156,10 @@ function makeReportObject(sdk: string, type: string, value: number): Report {
147
156
} ;
148
157
}
149
158
150
- function generateSizeReport ( ) : BinarySizeRequestBody {
159
+ async function generateSizeReport ( ) : Promise < BinarySizeRequestBody > {
151
160
const reports : Report [ ] = [
152
161
...generateReportForCDNScripts ( ) ,
153
- ...generateReportForNPMPackages ( )
162
+ ...( await generateReportForNPMPackages ( ) )
154
163
] ;
155
164
156
165
for ( const r of reports ) {
@@ -168,5 +177,6 @@ function generateSizeReport(): BinarySizeRequestBody {
168
177
} ;
169
178
}
170
179
171
- const report = generateSizeReport ( ) ;
172
- upload ( report , RequestEndpoint . BINARY_SIZE ) ;
180
+ generateSizeReport ( ) . then ( report => {
181
+ upload ( report , RequestEndpoint . BINARY_SIZE ) ;
182
+ } ) ;
0 commit comments