Skip to content

Commit 5823d0b

Browse files
authored
Fix health metrics (#5509)
* ignore folders without package.json * simplify implementation * handle files with no exports * fix binary size report * remove code for debugging * address comments * remove unused code * fix tests
1 parent f90c1d0 commit 5823d0b

File tree

11 files changed

+148
-416
lines changed

11 files changed

+148
-416
lines changed

packages/messaging/sw/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@firebase/messaging",
2+
"name": "@firebase/messaging-sw",
33
"description": "",
44
"author": "Firebase <[email protected]> (https://firebase.google.com/)",
55
"module": "../dist/index.sw.esm2017.js",

repo-scripts/size-analysis/analysis-helper.ts

+79-366
Large diffs are not rendered by default.

repo-scripts/size-analysis/bundle-analysis.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { bundleWithRollup } from './bundle/rollup';
2222
import { bundleWithWebpack } from './bundle/webpack';
2323
import { calculateContentSize } from './util';
2424
import { minify } from './bundle/minify';
25-
import { extractDeclarations, MemberList } from './analysis-helper';
25+
import { extractAllTopLevelSymbols, MemberList } from './analysis-helper';
2626

2727
interface BundleAnalysisArgs {
2828
input: string;
@@ -392,7 +392,7 @@ async function analyzeBundleWithBundler(
392392
analysisResult.debugInfo = {
393393
pathToBundle: bundleFilePath,
394394
pathToMinifiedBundle: minifiedBundleFilePath,
395-
dependencies: extractDeclarations(bundleFilePath)
395+
dependencies: extractAllTopLevelSymbols(bundleFilePath)
396396
};
397397
}
398398

repo-scripts/size-analysis/cli.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ yargs
2929
type: 'array',
3030
alias: 'im',
3131
desc:
32-
'The name of the module(s) to be analyzed. example: --inputModule "@firebase/functions-exp" "firebase/auth-exp"'
32+
'The name of the module(s) to be analyzed. example: --inputModule "@firebase/functions" "firebase/auth"'
3333
},
3434
inputDtsFile: {
3535
type: 'string',
@@ -90,4 +90,4 @@ yargs
9090
// eslint-disable-next-line @typescript-eslint/no-explicit-any
9191
argv => runBundleAnalysis(argv as any)
9292
)
93-
.help().argv;
93+
.help().parseSync();

repo-scripts/size-analysis/package-analysis.ts

+5
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ export async function analyzePackageSize(
7373
`${projectRoot}/packages/*`
7474
]);
7575
allModulesLocation = allModulesLocation.filter(path => {
76+
const pkgJsonPath = `${path}/package.json`;
77+
if (!fs.existsSync(pkgJsonPath)){
78+
return false;
79+
}
80+
7681
const json = JSON.parse(
7782
fs.readFileSync(`${path}/package.json`, { encoding: 'utf-8' })
7883
);

repo-scripts/size-analysis/rollup.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const deps = Object.keys(
2424
Object.assign({}, pkg.peerDependencies, pkg.dependencies)
2525
);
2626

27-
const nodeInternals = ['fs', 'path'];
27+
const nodeInternals = ['fs', 'path', 'util'];
2828

2929
export default [
3030
{

repo-scripts/size-analysis/test/size-analysis.test.ts

+27-30
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import { expect } from 'chai';
1919

2020
import {
21-
extractDeclarations,
2221
MemberList,
2322
dedup,
2423
mapSymbolToType,
@@ -27,8 +26,9 @@ import {
2726
ErrorCode,
2827
writeReportToDirectory,
2928
extractExternalDependencies,
30-
buildMap,
31-
Report
29+
Report,
30+
extractExports,
31+
extractAllTopLevelSymbols
3232
} from '../analysis-helper';
3333

3434
import {
@@ -39,14 +39,14 @@ import {
3939
import * as fs from 'fs';
4040
import { resolve } from 'path';
4141

42-
describe('extractDeclarations on .d.ts file', () => {
42+
describe('extractExports', () => {
4343
let testModuleDtsFile: string;
4444
let extractedDeclarations: MemberList;
4545
before(() => {
4646
const start = Date.now();
4747
testModuleDtsFile = getTestModuleDtsFilePath();
48-
extractedDeclarations = extractDeclarations(testModuleDtsFile);
49-
console.log('extractDeclarations on .d.ts file took ', Date.now() - start);
48+
extractedDeclarations = extractExports(testModuleDtsFile);
49+
console.log('extractExports took ', Date.now() - start);
5050
});
5151
// export {tar as tarr, tar1 as tarr1} from '..'
5252
it('test export rename', () => {
@@ -187,42 +187,34 @@ describe('extractDeclarations on .d.ts file', () => {
187187
});
188188

189189
// import * as fs from 'fs'
190-
// import * as tmp from 'tmp'
191-
// export declare const aVar: tmp.FileOptions;
192190
// export { fs as fs1 };
193191
it('test namespace export', () => {
194-
expect(extractedDeclarations.variables).to.include.members(['fs1']);
195-
expect(extractedDeclarations.variables).to.not.include.members(['tmp']);
196-
expect(extractedDeclarations.variables).to.include.members(['aVar']);
192+
expect(extractedDeclarations.unknown).to.include.members(['fs1']);
197193
});
198194
});
199-
describe('extractDeclarations on js bundle file', () => {
195+
196+
describe('extractAllTopLevelSymbols', () => {
200197
let subsetExportsBundleFile: string;
201198
let extractedDeclarations: MemberList;
202199
before(function () {
203-
this.timeout(120000);
204200
const start = Date.now();
205-
const testModuleDtsFile: string = getTestModuleDtsFilePath();
206-
const map: Map<string, string> = buildMap(
207-
extractDeclarations(testModuleDtsFile)
208-
);
209201
subsetExportsBundleFile = getSubsetExportsBundleFilePath();
210-
extractedDeclarations = extractDeclarations(subsetExportsBundleFile, map);
202+
extractedDeclarations = extractAllTopLevelSymbols(subsetExportsBundleFile);
211203
console.log(
212204
'extractDeclarations on js bundle file took ',
213205
Date.now() - start
214206
);
215207
});
216208
it('test variable extractions', () => {
217-
const variablesArray = ['aVar', 'fs1'];
209+
const variablesArray = ['aVar'];
218210
variablesArray.sort();
219-
expect(extractedDeclarations.variables).to.have.members(variablesArray);
211+
expect(extractedDeclarations.variables).to.include.members(variablesArray);
220212
});
221213

222214
it('test functions extractions', () => {
223215
const functionsArray = [
224216
'tar',
225-
'tarr1',
217+
'tar1',
226218
'basicFuncExportEnumDependencies',
227219
'd1',
228220
'd2',
@@ -236,16 +228,15 @@ describe('extractDeclarations on js bundle file', () => {
236228
it('test enums extractions', () => {
237229
const enumsArray = [
238230
'BasicEnumExport',
239-
'LogLevel2',
240231
'BasicEnumExportBar',
241232
'BasicEnumExportFar'
242233
];
243234
enumsArray.sort();
244-
expect(extractedDeclarations.enums).to.have.members(enumsArray);
235+
expect(extractedDeclarations.variables).to.include.members(enumsArray);
245236
});
246237

247238
it('test classes extractions', () => {
248-
const classesArray = ['Logger1'];
239+
const classesArray = ['BasicClassExport'];
249240
classesArray.sort();
250241
expect(extractedDeclarations.classes).to.have.members(classesArray);
251242
});
@@ -257,7 +248,8 @@ describe('test dedup helper function', () => {
257248
functions: ['aFunc', 'aFunc', 'bFunc', 'cFunc'],
258249
classes: ['aClass', 'bClass', 'aClass', 'cClass'],
259250
variables: ['aVar', 'bVar', 'cVar', 'aVar'],
260-
enums: ['aEnum', 'bEnum', 'cEnum', 'dEnum']
251+
enums: ['aEnum', 'bEnum', 'cEnum', 'dEnum'],
252+
unknown: []
261253
};
262254
memberList = dedup(memberList);
263255

@@ -287,7 +279,8 @@ describe('test dedup helper function', () => {
287279
functions: [],
288280
classes: [],
289281
variables: ['aVar', 'bVar', 'cVar', 'aVar'],
290-
enums: []
282+
enums: [],
283+
unknown: []
291284
};
292285
memberList = dedup(memberList);
293286
expect(memberList.functions).to.have.length(0);
@@ -308,7 +301,8 @@ describe('test replaceAll helper function', () => {
308301
functions: ['aFunc', 'aFunc', 'bFunc', 'cFunc'],
309302
classes: ['aClass', 'bClass', 'aClass', 'cClass'],
310303
variables: ['aVar', 'bVar', 'cVar', 'aVar'],
311-
enums: ['aEnum', 'bEnum', 'cEnum', 'dEnum']
304+
enums: ['aEnum', 'bEnum', 'cEnum', 'dEnum'],
305+
unknown: []
312306
};
313307
const original: string = 'aFunc';
314308
const replaceTo: string = 'replacedFunc';
@@ -331,7 +325,8 @@ describe('test replaceAll helper function', () => {
331325
functions: ['aFunc', 'aFunc', 'bFunc', 'cFunc'],
332326
classes: ['aClass', 'bClass', 'aClass', 'cClass'],
333327
variables: ['aVar', 'bVar', 'cVar', 'aVar'],
334-
enums: ['aEnum', 'bEnum', 'cEnum', 'dEnum']
328+
enums: ['aEnum', 'bEnum', 'cEnum', 'dEnum'],
329+
unknown: []
335330
};
336331
const replaceTo: string = 'replacedClass';
337332
const original: string = 'bClass';
@@ -354,7 +349,8 @@ describe('test replaceAll helper function', () => {
354349
functions: ['aFunc', 'aFunc', 'bFunc', 'cFunc'],
355350
classes: ['aClass', 'bClass', 'aClass', 'cClass'],
356351
variables: ['aVar', 'bVar', 'cVar', 'aVar'],
357-
enums: ['aEnum', 'bEnum', 'cEnum', 'dEnum']
352+
enums: ['aEnum', 'bEnum', 'cEnum', 'dEnum'],
353+
unknown: []
358354
};
359355
const replaceTo: string = 'replacedEnum';
360356
const original: string = 'eEnum';
@@ -377,7 +373,8 @@ describe('test mapSymbolToType helper function', () => {
377373
functions: ['aVar', 'bFunc', 'cFunc'],
378374
classes: ['bClass', 'cClass'],
379375
variables: ['aClass', 'bVar', 'cVar', 'aEnum'],
380-
enums: ['bEnum', 'cEnum', 'dEnum', 'aFunc']
376+
enums: ['bEnum', 'cEnum', 'dEnum', 'aFunc'],
377+
unknown: []
381378
};
382379

383380
const map: Map<string, string> = new Map([

repo-scripts/size-analysis/test/test-inputs/subsetExports.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
1817
export {
1918
aVar,
2019
LogLevel2,
21-
Logger1,
2220
tarr1,
23-
fs1,
2421
basicFuncExportEnumDependencies,
25-
basicFuncExportFuncDependenciesBar
22+
basicFuncExportFuncDependenciesBar,
23+
BasicClassExport
2624
} from './index';

repo-scripts/size-analysis/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"module": "commonjs",
66
"moduleResolution": "node",
77
"resolveJsonModule": true,
8-
"target": "es5",
8+
"target": "es2017",
99
"esModuleInterop": true,
1010
"declaration": true,
1111
"strict": true

scripts/size_report/report_binary_size.ts

+21-8
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,21 @@ function generateReportForCDNScripts(): Report[] {
4545
const reports = [];
4646
const firebaseRoot = path.resolve(__dirname, '../../packages/firebase');
4747
const pkgJson = require(`${firebaseRoot}/package.json`);
48+
const compatPkgJson = require(`${firebaseRoot}/compat/package.json`);
4849

4950
const special_files = [
50-
'firebase-performance-standalone.es2017.js',
51-
'firebase-performance-standalone.js',
52-
'firebase-firestore.memory.js',
53-
'firebase.js'
51+
'firebase-performance-standalone-compat.es2017.js',
52+
'firebase-performance-standalone-compat.js',
53+
'firebase-compat.js'
5454
];
5555

5656
const files = [
5757
...special_files.map((file: string) => `${firebaseRoot}/${file}`),
5858
...pkgJson.components.map(
59-
(component: string) => `${firebaseRoot}/firebase-${component}.js`
59+
(component: string) => `${firebaseRoot}/firebase-${component.replace('/', '-')}.js`
60+
),
61+
...compatPkgJson.components.map(
62+
(component: string) => `${firebaseRoot}/firebase-${component}-compat.js`
6063
)
6164
];
6265

@@ -77,7 +80,16 @@ async function generateReportForNPMPackages(): Promise<Report[]> {
7780
for (const info of packageInfo) {
7881
const packages = await findAllPackages(info.location);
7982
for (const pkg of packages) {
80-
reports.push(...(await collectBinarySize(pkg)));
83+
try {
84+
reports.push(...(await collectBinarySize(pkg)));
85+
} catch (e) {
86+
// log errors and continue to the next package
87+
console.log(
88+
`failed to generate report for ${pkg}.
89+
error: ${e}`
90+
);
91+
}
92+
8193
}
8294
}
8395
return reports;
@@ -104,11 +116,12 @@ async function collectBinarySize(pkg: string): Promise<Report[]> {
104116
const fields = [
105117
'main',
106118
'module',
107-
'esm2017',
108119
'browser',
120+
'esm5',
109121
'react-native',
122+
'cordova',
110123
'lite',
111-
'lite-esm2017'
124+
'lite-esm5'
112125
];
113126
const json = require(pkg);
114127
for (const field of fields) {

scripts/size_report/report_modular_export_binary_size.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
RequestBody,
2828
RequestEndpoint
2929
} from './size_report_helper';
30+
import { existsSync } from 'fs';
3031
interface ModularExportBinarySizeRequestBody extends RequestBody {
3132
modules: Report[];
3233
}
@@ -37,7 +38,12 @@ async function generateReport(): Promise<ModularExportBinarySizeRequestBody> {
3738
]);
3839

3940
allModulesLocation = allModulesLocation.filter(path => {
40-
const json = require(`${path}/package.json`);
41+
const pkgJsonPath = `${path}/package.json`;
42+
if (!existsSync(pkgJsonPath)){
43+
return false;
44+
}
45+
46+
const json = require(pkgJsonPath);
4147
return (
4248
json.name.startsWith('@firebase') &&
4349
!json.name.includes('-compat') &&

0 commit comments

Comments
 (0)