-
Notifications
You must be signed in to change notification settings - Fork 662
Description
Summary
While waiting for #1596 to be implemented, I tried to find a workaround to generate documentation for our packages that have multiple entry-points under different paths, but did not succeed caused by what I consider bugs in api-extractor's canonical reference resolving.
For packages with multiple entry points, which api-extractor currently does not support, this comment suggests the following approach:
If you don't need rollups, as a workaround you can simply create a fake entry point that reexports all your real entry points. That will work reasonably well for the API report and documentation features.
The "legacy" public API we want to generate documentation for uses one file per class and groups classes into subdirectories. To keep the original structure and to avoid name clashes, we must somehow retain the path name in the fake entry point.
To achieve this, I tried two approaches for the "fake" entry point index.d.ts:
- Alias the classes with a path prefix, like so:
export { Foo as subpath_Foo } from "./subpath/Foo" - Re-export only direct members and convert sub-paths to namespaces with their own
index.d.ts, which can then be re-exported like so:
import * as subpath from "./subpath"; export { subpath }
(this is the suggested workaround forexport * as subpath from "./subpath"not yet being supported by api-extractor)
Both approaches lead to incorrect references from a subclass to its (aliased) superclass.
Repro steps
To reproduce, clone the example repo and run
api-extractor run --local
The example project contains a src folder with four TypeScript .d.ts files with one class declaration each: A, B, somepath/A and somepath/B. In both cases, B is a subclass of A.
There is a "fake" entry point file src/index.d.ts, which re-exports all four entry points. It implements both approaches described above, namely using aliases and using a namespace, for which there is another "fake" sub-entry point src/somepath/index.d.ts that re-exports A and B from under the somepath path.
Expected result:
The generated file input/api-extractor-test.api.json should contain the correct canonical reference from namespaced module somepath.B to its superclass somepath.A:
"canonicalReference": "api-extractor-test!somepath.A:class"
and/or the correct canonical reference from aliased module somepath_B to its superclass somepath_A:
"canonicalReference": "api-extractor-test!somepath_A:class"
Actual result:
For both approaches, the reference incorrectly points to the top-level A:
"canonicalReference": "api-extractor-test!A:class"
If you find the JSON file too confusing, you can generate Markdown files simply by calling
api-documenter markdown
The resulting Markdown files in folder markdown contain the corresponding wrong hyperlinks in file api-extractor-test.somepath.b.md ([A](./api-extractor-test.a.md) instead of [A](./api-extractor-test.somepath.a.md)) and api-extractor-test.somepath_b.md ([A](./api-extractor-test.a.md) instead of [A](./api-extractor-test.somepath_a.md)).
Of course, this is not api-documenter's fault, as the *.api.json file already contains the wrong canonical references.
Details
It seems that neither alias support (export { __ as __ }) nor namespace aliases (import * as __) are taken into account when resolving references.
The name-clash between A and somepath/A has been used to motivate the need for aliasing, but even without an actual clash, it still does not work.
If you change the name of somepath/A to something else in the example workspace, the superclass reference of somepath/B in api-extractor's output points to an incorrect, non-existing module name (also missing the path-part of the actual module name) and thus no hyperlink at all is rendered by api-documenter.
Standard questions
Please answer these questions to help us investigate your issue more quickly:
| Question | Answer |
|---|---|
@microsoft/api-extractor version? |
7.29.2 |
| Operating system? | Linux |
| API Extractor scenario? | docs (.api.json) |
| Would you consider contributing a PR? | Maybe (need help) |
| TypeScript compiler version? | 4.7.4 |
Node.js version (node -v)? |
v16.15.1 |