Skip to content

Commit 7df1038

Browse files
Review
1 parent f21776b commit 7df1038

File tree

4 files changed

+56
-28
lines changed

4 files changed

+56
-28
lines changed
Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
{
2-
"foo": {
3-
"dependencies": [
4-
"bar",
5-
"foo"
6-
],
7-
"sizeInBytes": 67
8-
},
92
"bar": {
10-
"dependencies": [
11-
"bar"
12-
],
3+
"dependencies": {
4+
"functions": [
5+
"bar"
6+
],
7+
"classes": [],
8+
"variables": []
9+
},
1310
"sizeInBytes": 39
11+
},
12+
"foo": {
13+
"dependencies": {
14+
"functions": [
15+
"bar",
16+
"foo"
17+
],
18+
"classes": [],
19+
"variables": []
20+
},
21+
"sizeInBytes": 67
1422
}
15-
}
23+
}

packages/firestore/exp/test/deps/verify_dependencies.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ describe('Dependencies', () => {
2727
forEach(dependencies, (api, { dependencies }) => {
2828
it(api, () => {
2929
return extractDependencies(api, pkg.exp).then(extractedDependencies => {
30-
dependencies.sort();
31-
expect(extractedDependencies).to.have.members(dependencies);
30+
expect(extractedDependencies.classes).to.have.members(dependencies.classes, "for classes");
31+
expect(extractedDependencies.functions).to.have.members(dependencies.functions, "for functions");
32+
expect(extractedDependencies.variables).to.have.members(dependencies.variables, "for variables");
3233
});
3334
});
3435
});

scripts/exp/extract-deps.helpers.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,14 @@ import * as rollup from 'rollup';
2222
import * as terser from 'terser';
2323
import * as ts from 'typescript';
2424

25+
/** Contains a list of members by type. */
26+
export type MemberList = {
27+
classes: string[],
28+
functions: string[],
29+
variables: string[]
30+
};
2531
/** Contains the dependencies and the size of their code for a single export. */
26-
export type ExportData = { dependencies: string[]; sizeInBytes: number };
32+
export type ExportData = { dependencies: MemberList; sizeInBytes: number };
2733

2834
/**
2935
* This functions builds a simple JS app that only depends on the provided
@@ -37,7 +43,7 @@ export type ExportData = { dependencies: string[]; sizeInBytes: number };
3743
export async function extractDependencies(
3844
exportName: string,
3945
jsBundle: string
40-
): Promise<string[]> {
46+
): Promise<MemberList> {
4147
const { dependencies } = await extractDependenciesAndSize(
4248
exportName,
4349
jsBundle
@@ -69,8 +75,7 @@ export async function extractDependenciesAndSize(
6975
});
7076
await bundle.write({ file: output, format: 'es' });
7177

72-
const dependencies = extractFunctionsAndClasses(output);
73-
dependencies.sort();
78+
const dependencies = extractDeclarations(output);;
7479

7580
// Extract size of minified build
7681
const afterContent = fs.readFileSync(output, 'utf-8');
@@ -89,23 +94,31 @@ export async function extractDependenciesAndSize(
8994
}
9095

9196
/**
92-
* Extracts all top-level function and classes using the TypeScript compiler
93-
* API.
97+
* Extracts all function, class and variable declarations using the TypeScript
98+
* compiler API.
9499
*/
95-
export function extractFunctionsAndClasses(jsFile: string): string[] {
100+
export function extractDeclarations(jsFile: string): MemberList {
96101
const program = ts.createProgram([jsFile], { allowJs: true });
97102
const sourceFile = program.getSourceFile(jsFile);
98103
if (!sourceFile) {
99104
throw new Error('Failed to parse file: ' + jsFile);
100105
}
101106

102-
const exports: string[] = [];
107+
const declarations: MemberList = { functions: [], classes: [], variables: []}
103108
ts.forEachChild(sourceFile, node => {
104109
if (ts.isFunctionDeclaration(node)) {
105-
exports.push(node.name!.text);
110+
declarations.functions.push(node.name!.text);
106111
} else if (ts.isClassDeclaration(node)) {
107-
exports.push(node.name!.text);
112+
declarations.classes.push(node.name!.text);
113+
} else if (ts.isVariableDeclaration(node)) {
114+
declarations.variables.push(node.name!.getText());
108115
}
109116
});
110-
return exports;
117+
118+
// Sort to ensure stable output
119+
declarations.functions.sort();
120+
declarations.classes.sort();
121+
declarations.variables.sort();
122+
123+
return declarations;
111124
}

scripts/exp/extract-deps.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import * as yargs from 'yargs';
2121
import {
2222
ExportData,
2323
extractDependenciesAndSize,
24-
extractFunctionsAndClasses
24+
extractDeclarations, MemberList
2525
} from './extract-deps.helpers';
2626

2727
// Small command line app that builds a JSON file with a list of the
@@ -47,15 +47,21 @@ const argv = yargs.options({
4747
}
4848
}).argv;
4949

50-
async function buildJson(publicApi: string[], jsFile: string): Promise<string> {
50+
async function buildJson(publicApi: MemberList, jsFile: string): Promise<string> {
5151
const result: { [key: string]: ExportData } = {};
52-
for (const exp of publicApi) {
52+
for (const exp of publicApi.classes) {
53+
result[exp] = await extractDependenciesAndSize(exp, jsFile);
54+
}
55+
for (const exp of publicApi.functions) {
56+
result[exp] = await extractDependenciesAndSize(exp, jsFile);
57+
}
58+
for (const exp of publicApi.variables) {
5359
result[exp] = await extractDependenciesAndSize(exp, jsFile);
5460
}
5561
return JSON.stringify(result, null, 4);
5662
}
5763

58-
const publicApi = extractFunctionsAndClasses(path.resolve(argv.types));
64+
const publicApi = extractDeclarations(path.resolve(argv.types));
5965
buildJson(publicApi, path.resolve(argv.bundle)).then(json => {
6066
fs.writeFileSync(path.resolve(argv.output), json);
6167
});

0 commit comments

Comments
 (0)