Skip to content

Commit

Permalink
local module import: support dts rollup
Browse files Browse the repository at this point in the history
  • Loading branch information
adventure-yunfei committed Feb 16, 2020
1 parent 1c0cc01 commit f6d9605
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions apps/api-extractor/src/collector/Collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}
Expand Down
34 changes: 33 additions & 1 deletion apps/api-extractor/src/generators/DtsRollupGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit f6d9605

Please sign in to comment.