Skip to content

Commit 201a364

Browse files
committed
handle files with no exports
1 parent 1242874 commit 201a364

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

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

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -236,32 +236,30 @@ export function extractAllTopLevelSymbols(filePath: string) {
236236
* Extract exports of a module
237237
*/
238238
export function extractExports(filePath: string) {
239-
const input = tmp.fileSync({}).name + '.js';
240-
const exportStatement = `export * from '${path.resolve(
241-
filePath
242-
)}';`;
243-
fs.writeFileSync(input, exportStatement);
244-
const program = ts.createProgram([input], {
239+
const exportDeclarations: MemberList = {
240+
functions: [],
241+
classes: [],
242+
variables: [],
243+
enums: []
244+
};
245+
246+
const program = ts.createProgram([filePath], {
245247
allowJs: true,
246248
baseUrl: path.resolve(`${projectRoot}/node_modules`)
247249
});
248250
const checker = program.getTypeChecker();
249-
const sourceFile = program.getSourceFile(input);
250-
const module = checker.getSymbolAtLocation((sourceFile?.statements[0] as ts.ExportDeclaration).moduleSpecifier!)!;
251+
const sourceFile = program.getSourceFile(filePath)!;
252+
const module = checker.getSymbolAtLocation(sourceFile);
253+
// no export from the file
254+
if(!module) {
255+
return exportDeclarations;
256+
}
257+
251258
const exports = checker.getExportsOfModule(module);
252259

253-
let exportDeclarations: MemberList = {
254-
functions: [],
255-
classes: [],
256-
variables: [],
257-
enums: []
258-
};
259260

260261
for (const expt of exports) {
261262
// get the source declaration where we can determine the type of the export. e.g. class vs function
262-
if (expt.name === 'Config') {
263-
console.log('stop');
264-
}
265263
let sourceSymbol = expt;
266264
if (sourceSymbol.declarations[0].kind === ts.SyntaxKind.ExportSpecifier) {
267265
sourceSymbol = checker.getAliasedSymbol(expt);
@@ -280,7 +278,7 @@ export function extractExports(filePath: string) {
280278
} else if (ts.isVariableDeclaration(sourceDeclaration)) {
281279
exportDeclarations.variables.push(expt.name);
282280
} else {
283-
console.log('unhandled export type', expt.name);
281+
console.log('unhandled export:', expt.name);
284282
}
285283
}
286284

@@ -658,6 +656,7 @@ export async function generateReport(
658656
throw new Error(ErrorCode.INPUT_BUNDLE_FILE_DOES_NOT_EXIST);
659657
}
660658

659+
console.log('generating report for ', name);
661660
const publicAPI = extractExports(resolvedBundleFile);
662661
const map: Map<string, string> = buildMap(publicAPI);
663662
return buildJsonReport(name, publicAPI, bundleFile, map);

0 commit comments

Comments
 (0)