Skip to content

Commit b3ee391

Browse files
authored
bundle the package in order to get the size for an entry point (#4094)
1 parent 9822e12 commit b3ee391

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

scripts/size_report/report_binary_size.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { resolve } from 'path';
18+
import { resolve as pathResolve } from 'path';
1919
import * as fs from 'fs';
2020
import { execSync } from 'child_process';
2121
import * as terser from 'terser';
@@ -25,6 +25,8 @@ import {
2525
RequestBody,
2626
RequestEndpoint
2727
} from './size_report_helper';
28+
import * as rollup from 'rollup';
29+
import commonjs from '@rollup/plugin-commonjs';
2830

2931
interface Report {
3032
sdk: string;
@@ -38,7 +40,7 @@ interface BinarySizeRequestBody extends RequestBody {
3840
// CDN scripts
3941
function generateReportForCDNScripts(): Report[] {
4042
const reports = [];
41-
const firebaseRoot = resolve(__dirname, '../../packages/firebase');
43+
const firebaseRoot = pathResolve(__dirname, '../../packages/firebase');
4244
const pkgJson = require(`${firebaseRoot}/package.json`);
4345

4446
const special_files = [
@@ -103,8 +105,16 @@ async function generateReportForNPMPackages(): Promise<Report[]> {
103105

104106
for (const field of fields) {
105107
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;
108118

109119
// remove comments and whitespaces, then get size
110120
const { code } = await terser.minify(rawCode, {
@@ -122,7 +132,6 @@ async function generateReportForNPMPackages(): Promise<Report[]> {
122132

123133
resolve();
124134
});
125-
126135
taskPromises.push(promise);
127136
}
128137
}

0 commit comments

Comments
 (0)