[api-extractor] Add handling of file reference directives#1955
[api-extractor] Add handling of file reference directives#1955willmtemple wants to merge 2 commits intomicrosoft:mainfrom
Conversation
| /** | ||
| * Resolve the name of a file to an absolute path relative to a TypeScript SourceFile node. | ||
| */ | ||
| function resolveNameRelativeToSourceFile(name: string, source: ts.SourceFile): string { |
There was a problem hiding this comment.
For this one, I don't think so. I need to boot into Windows and check it, but I believe that the reference directive in text has to follow the POSIX convention and use forward slash as its path separator just like a node module, but the file name of the source file could be in Windows convention, so relying on path to process the reference path may not be safe. Need to do some testing.
There was a problem hiding this comment.
If the input and output paths are POSIX format, then you can use path.posix.relative().
If the input path is Windows style and you want to convert to POSIX, there is no theoretically correct API for that, but Text.replaceAll(thePath, '\\', '/') works reasonably well with Node.js
| /** | ||
| * Compute a relative path between two absolute paths. | ||
| */ | ||
| function computeRelativePath(fromPath: string, toPath: string): string { |
There was a problem hiding this comment.
In this instance, sure. The Path module in the node-core-library package in this repo made me think that there was some reason for avoiding it.
There was a problem hiding this comment.
FileSystem is a complete replacement for fs, because the native fs library was designed as low-level primitives that are not suitable for everyday usage.
Path contains supplementary stuff that merely extends the native path, since that API is fine to use.
We should probably document this better.
|
Thanks for making a PR! 😁 Could you add a test case to the rushstack\build-tests\api-extractor-scenarios folder? (The test cases get registered in rushstack\build-tests\api-extractor-scenarios\config\build-config.json.) |
| * /// <reference path="runtime-library" /> | ||
| * ``` | ||
| */ | ||
| public get dtsFileReferenceDirectives(): ReadonlySet<string> { |
There was a problem hiding this comment.
The runtime-library example is not intended to be a path. Maybe something like this:
/**
* A list of paths (e.g. "path/to/file.d.ts") that should appear in a reference like this:
*
* ```
* /// <reference path="path/to/file.d.ts" />
* ```
*
* The paths are resolved relative to the source file containing the reference.
*/There was a problem hiding this comment.
ah yeah just missed this when copying the doc string 👍
|
@octogonz I am getting a build failure on Windows in I'm just building using Here is the error: |
@willmtemple I cannot repro this. What is the full path of your repo ( |
|
@willmtemple Following up -- are you still working on this PR? Do you need anything else from us? |
|
@octogonz Had to put this down for a while. The build issue I was running into was indeed a MAX_PATH issue. This week I'm going to revisit this. Referring to your comment in the linked issue:
Do you have any thoughts on the two approaches to take here? This implementation is leaving the file references intact in the output bundle, just rewriting them to be relative to the output location, but the other alternative of course is that we could include the referenced file in the rolled-up d.ts. |
|
This PR also seems to fix #3000 Is there anything I can do to help? |
This is a first stab at supporting path-based references like:
/// <reference path="path/to/xyz.d.ts" />in API Extractor.
Would fix: #1761 (CC @jeremymeng)
We use these references as part of a shimming pattern in the Azure SDK for JavaScript, but we aren't able to use dtsRollup for packages that need them because d.ts rollup currently just doesn't handle them.
Because these references are relative to the SourceFile that they are collected from, the path that is emitted has to be corrected to be relative to the ultimate file. I've written the logic for this as internal functions next to the Collector and DtsRollupGenerator, but could relocate them as needed.
I have not tested this on Windows yet.