|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2021 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import * as fs from 'fs'; |
| 19 | +import * as path from 'path'; |
| 20 | +import * as tmp from 'tmp'; |
| 21 | + |
| 22 | +import { Bundler, Mode, run as runBundleAnalysis } from './bundle-analysis'; |
| 23 | +import { Report } from '../../scripts/size_report/report_binary_size'; |
| 24 | + |
| 25 | +/** |
| 26 | + * Runs bundle analysis for all bundle definition files under: |
| 27 | + * |
| 28 | + * `firebase-js-sdk/repo-scripts/size-analysis/bundle-definitions` |
| 29 | + * |
| 30 | + * The method accepts an optional parameter `version`: |
| 31 | + * 1. when presented (for example, `version` = '9.0.0'), this method measures the bundle size by |
| 32 | + * building test bundles with dependencies at that specific version downloaded from npm |
| 33 | + * 2. when omitted (in this case, `version` = null), this method measures the bundle size by |
| 34 | + * building test bundles with dependencies from local artifacts (e.g. produced by `yarn build`) |
| 35 | + * |
| 36 | + * #1 is intended only for manual runs for the purpose of back-filling historical size data. #2 is |
| 37 | + * intended for CI runs that measure size for the current commit. |
| 38 | + * |
| 39 | + * More details on how a test bundle is built can be found in `bundle-analysis.ts`. |
| 40 | + * |
| 41 | + * @param version - If present, the SDK version to run measurement against |
| 42 | + * @returns A list of bundle size measurements |
| 43 | + */ |
| 44 | +export async function generateReportForBundles( |
| 45 | + version?: string |
| 46 | +): Promise<Report[]> { |
| 47 | + const definitionDir = `${__dirname}/bundle-definitions`; |
| 48 | + const outputDir = tmp.dirSync().name; |
| 49 | + console.log(`Bundle definitions are located at "${definitionDir}".`); |
| 50 | + console.log(`Analysis output are located at "${outputDir}".`); |
| 51 | + |
| 52 | + const bundles = fs.readdirSync(definitionDir); |
| 53 | + const results: Report[] = []; |
| 54 | + for (const bundle of bundles) { |
| 55 | + const product = path.basename(bundle, '.json'); |
| 56 | + const output = `${outputDir}/${product}.analysis.json`; |
| 57 | + if (version) { |
| 58 | + overwriteVersion(definitionDir, bundle, outputDir, version); |
| 59 | + } |
| 60 | + const option = { |
| 61 | + input: version ? `${outputDir}/${bundle}` : `${definitionDir}/${bundle}`, |
| 62 | + bundler: Bundler.Rollup, |
| 63 | + mode: version ? Mode.Npm : Mode.Local, |
| 64 | + output: output, |
| 65 | + debug: true |
| 66 | + }; |
| 67 | + console.log(`Running for bundle "${bundle}" with mode "${option.mode}".`); |
| 68 | + await runBundleAnalysis(option); |
| 69 | + const measurements = parseAnalysisOutput(product, output); |
| 70 | + results.push(...measurements); |
| 71 | + } |
| 72 | + console.log(results); |
| 73 | + return results; |
| 74 | +} |
| 75 | + |
| 76 | +function overwriteVersion( |
| 77 | + definitionDir: string, |
| 78 | + bundle: string, |
| 79 | + temp: string, |
| 80 | + version: string |
| 81 | +) { |
| 82 | + const definitions = JSON.parse( |
| 83 | + fs.readFileSync(`${definitionDir}/${bundle}`, { encoding: 'utf-8' }) |
| 84 | + ); |
| 85 | + for (const definition of definitions) { |
| 86 | + const dependencies = definition.dependencies; |
| 87 | + for (const dependency of dependencies) { |
| 88 | + dependency.versionOrTag = version; |
| 89 | + } |
| 90 | + } |
| 91 | + fs.writeFileSync(`${temp}/${bundle}`, JSON.stringify(definitions, null, 2), { |
| 92 | + encoding: 'utf-8' |
| 93 | + }); |
| 94 | +} |
| 95 | + |
| 96 | +function parseAnalysisOutput(product: string, output: string) { |
| 97 | + const analyses = JSON.parse(fs.readFileSync(output, { encoding: 'utf-8' })); |
| 98 | + const results: Report[] = []; |
| 99 | + for (const analysis of analyses) { |
| 100 | + // The API of the backend for persisting size measurements currently requires data to be |
| 101 | + // organized strictly in the below json format: |
| 102 | + // |
| 103 | + // { |
| 104 | + // sdk: <some-string>, |
| 105 | + // type: <some-string>, |
| 106 | + // value: <some-integer> |
| 107 | + // } |
| 108 | + // |
| 109 | + // We are reusing this API here, although its semantics does not make sense in the context of |
| 110 | + // bundle-analysis. |
| 111 | + const sdk = 'bundle'; // to accommodate above API syntax, can be any string |
| 112 | + const value = analysis.results[0].size; |
| 113 | + const type = `${product} (${analysis.name})`; |
| 114 | + results.push({ sdk, type, value }); |
| 115 | + } |
| 116 | + return results; |
| 117 | +} |
0 commit comments