From f6d9605a352add2f7e68f9f78e178ce5f76fae8e Mon Sep 17 00:00:00 2001 From: adventure-yunfei Date: Sun, 16 Feb 2020 01:51:49 +0800 Subject: [PATCH] local module import: support dts rollup --- apps/api-extractor/src/collector/Collector.ts | 3 ++ .../src/generators/DtsRollupGenerator.ts | 34 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/apps/api-extractor/src/collector/Collector.ts b/apps/api-extractor/src/collector/Collector.ts index ecb6ee79f85..865bcd6e805 100644 --- a/apps/api-extractor/src/collector/Collector.ts +++ b/apps/api-extractor/src/collector/Collector.ts @@ -428,6 +428,9 @@ export class Collector { }); } else if (astEntity instanceof AstImportInternal) { this.astSymbolTable.fetchAstModuleExportInfo(astEntity.astModule).exportedLocalEntities.forEach((exportedEntity: AstEntity) => { + // Create a CollectorEntity for each top-level export of AstImportInternal entity + this._createCollectorEntity(exportedEntity, undefined); + this._createEntityForIndirectReferences(exportedEntity, alreadySeenAstEntities); // TODO- create entity for module export }); } diff --git a/apps/api-extractor/src/generators/DtsRollupGenerator.ts b/apps/api-extractor/src/generators/DtsRollupGenerator.ts index dee02f91c55..eb9b4d9a60f 100644 --- a/apps/api-extractor/src/generators/DtsRollupGenerator.ts +++ b/apps/api-extractor/src/generators/DtsRollupGenerator.ts @@ -133,7 +133,39 @@ export class DtsRollupGenerator { } } } else if (entity.astEntity instanceof AstImportInternal) { - throw new InternalError('Unsupported AstImportInternal'); + if (!entity.astEntity.astModule.astModuleExportInfo) { + // This should never happen + throw new InternalError('local imported module has not been parsed.'); + } + if (!entity.nameForEmit) { + // This should never happen + throw new InternalError('referencedEntry.nameForEmit is undefined'); + } + // manually generate typings for local imported module + stringWriter.writeLine(); + if (entity.shouldInlineExport) { + stringWriter.write('export '); + } + stringWriter.writeLine(`declare namespace ${entity.nameForEmit} {`); + + // all local exports of local imported module are just references to top-level declarations + stringWriter.writeLine(' export {'); + entity.astEntity.astModule.astModuleExportInfo.exportedLocalEntities.forEach((exportedEntity, exportedName) => { + const collectorEntity: CollectorEntity | undefined = collector.tryGetCollectorEntity(exportedEntity); + if (!collectorEntity) { + // This should never happen + // top-level exports of local imported module should be added as collector entities before + throw new Error(`Cannot find collector entity for ${entity.nameForEmit}.${exportedEntity.localName}`); + } + if (collectorEntity.nameForEmit === exportedName) { + stringWriter.writeLine(` ${collectorEntity.nameForEmit},`); + } else { + stringWriter.writeLine(` ${collectorEntity.nameForEmit} as ${exportedName},`); + } + }); + stringWriter.writeLine(' }'); // end of "export { ... }" + + stringWriter.writeLine('}'); // end of "declare namespace { ... }" } if (!entity.shouldInlineExport) {