|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2020 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 tmp from 'tmp'; |
| 19 | +import * as path from 'path'; |
| 20 | +import * as fs from 'fs'; |
| 21 | +import * as rollup from 'rollup'; |
| 22 | +import * as terser from 'terser'; |
| 23 | +import * as ts from 'typescript'; |
| 24 | + |
| 25 | +/** Contains the dependencies and the size of their code for a single export. */ |
| 26 | +export type ExportData = { dependencies: string[]; sizeInBytes: number }; |
| 27 | + |
| 28 | +/** |
| 29 | + * This functions builds a simple JS app that only depends on the provided |
| 30 | + * export. It then uses Rollup to gather all top-level classes and functions |
| 31 | + * that that the export depends on. |
| 32 | + * |
| 33 | + * @param exportName The name of the export to verify |
| 34 | + * @param jsBundle The file name of the source bundle that contains the export |
| 35 | + * @return A list of dependencies for the given export |
| 36 | + */ |
| 37 | +export async function extractDependencies( |
| 38 | + exportName: string, |
| 39 | + jsBundle: string |
| 40 | +): Promise<string[]> { |
| 41 | + const { dependencies } = await extractDependenciesAndSize( |
| 42 | + exportName, |
| 43 | + jsBundle |
| 44 | + ); |
| 45 | + return dependencies; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Helper for extractDependencies that extracts the dependencies and the size |
| 50 | + * of the minified build. |
| 51 | + */ |
| 52 | +export async function extractDependenciesAndSize( |
| 53 | + exportName: string, |
| 54 | + jsBundle: string |
| 55 | +): Promise<ExportData> { |
| 56 | + const input = tmp.fileSync().name + '.js'; |
| 57 | + const output = tmp.fileSync().name + '.js'; |
| 58 | + |
| 59 | + // JavaScript content that exports a single API from the bundle |
| 60 | + const beforeContent = `export { ${exportName} } from '${path.resolve( |
| 61 | + jsBundle |
| 62 | + )}';`; |
| 63 | + fs.writeFileSync(input, beforeContent); |
| 64 | + |
| 65 | + // Run Rollup on the JavaScript above to produce a tree-shaken build |
| 66 | + const bundle = await rollup.rollup({ |
| 67 | + input, |
| 68 | + external: id => id.startsWith('@firebase/') |
| 69 | + }); |
| 70 | + await bundle.write({ file: output, format: 'es' }); |
| 71 | + |
| 72 | + const dependencies = extractFunctionsAndClasses(output); |
| 73 | + dependencies.sort(); |
| 74 | + |
| 75 | + // Extract size of minified build |
| 76 | + const afterContent = fs.readFileSync(output, 'utf-8'); |
| 77 | + const { code } = terser.minify(afterContent, { |
| 78 | + output: { |
| 79 | + comments: false |
| 80 | + }, |
| 81 | + mangle: false, |
| 82 | + compress: false |
| 83 | + }); |
| 84 | + |
| 85 | + fs.unlinkSync(input); |
| 86 | + fs.unlinkSync(output); |
| 87 | + |
| 88 | + return { dependencies, sizeInBytes: Buffer.byteLength(code!, 'utf-8') }; |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Extracts all top-level function and classes using the TypeScript compiler |
| 93 | + * API. |
| 94 | + */ |
| 95 | +export function extractFunctionsAndClasses(jsFile: string): string[] { |
| 96 | + const program = ts.createProgram([jsFile], { allowJs: true }); |
| 97 | + const sourceFile = program.getSourceFile(jsFile); |
| 98 | + if (!sourceFile) { |
| 99 | + throw new Error('Failed to parse file: ' + jsFile); |
| 100 | + } |
| 101 | + |
| 102 | + const exports: string[] = []; |
| 103 | + ts.forEachChild(sourceFile, node => { |
| 104 | + if (ts.isFunctionDeclaration(node)) { |
| 105 | + exports.push(node.name!.text); |
| 106 | + } else if (ts.isClassDeclaration(node)) { |
| 107 | + exports.push(node.name!.text); |
| 108 | + } |
| 109 | + }); |
| 110 | + return exports; |
| 111 | +} |
0 commit comments