15
15
* limitations under the License.
16
16
*/
17
17
18
- import { resolve } from 'path' ;
18
+ import { resolve as pathResolve } from 'path' ;
19
19
import * as fs from 'fs' ;
20
20
import { execSync } from 'child_process' ;
21
21
import * as terser from 'terser' ;
@@ -25,6 +25,8 @@ import {
25
25
RequestBody ,
26
26
RequestEndpoint
27
27
} from './size_report_helper' ;
28
+ import * as rollup from 'rollup' ;
29
+ import commonjs from '@rollup/plugin-commonjs' ;
28
30
29
31
interface Report {
30
32
sdk : string ;
@@ -38,7 +40,7 @@ interface BinarySizeRequestBody extends RequestBody {
38
40
// CDN scripts
39
41
function generateReportForCDNScripts ( ) : Report [ ] {
40
42
const reports = [ ] ;
41
- const firebaseRoot = resolve ( __dirname , '../../packages/firebase' ) ;
43
+ const firebaseRoot = pathResolve ( __dirname , '../../packages/firebase' ) ;
42
44
const pkgJson = require ( `${ firebaseRoot } /package.json` ) ;
43
45
44
46
const special_files = [
@@ -103,8 +105,16 @@ async function generateReportForNPMPackages(): Promise<Report[]> {
103
105
104
106
for ( const field of fields ) {
105
107
if ( packageJson [ field ] ) {
106
- const filePath = `${ path } /${ packageJson [ field ] } ` ;
107
- const rawCode = fs . readFileSync ( filePath , 'utf-8' ) ;
108
+ const filePath = pathResolve ( path , packageJson [ field ] ) ;
109
+ // Need to create a bundle and get the size of the bundle instead of reading the size of the file directly.
110
+ // It is because some packages might be split into multiple files in order to share code between entry points.
111
+ const bundle = await rollup . rollup ( {
112
+ input : filePath ,
113
+ plugins : [ commonjs ( ) ]
114
+ } ) ;
115
+
116
+ const { output } = await bundle . generate ( { format : 'es' } ) ;
117
+ const rawCode = output [ 0 ] . code ;
108
118
109
119
// remove comments and whitespaces, then get size
110
120
const { code } = await terser . minify ( rawCode , {
@@ -122,7 +132,6 @@ async function generateReportForNPMPackages(): Promise<Report[]> {
122
132
123
133
resolve ( ) ;
124
134
} ) ;
125
-
126
135
taskPromises . push ( promise ) ;
127
136
}
128
137
}
0 commit comments