-
-
Notifications
You must be signed in to change notification settings - Fork 231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add experimental dts rollup using @microsoft/api-extractor #983
Conversation
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
New dependencies detected. Learn more about Socket for GitHub ↗︎
|
3bd020d
to
3943d73
Compare
I think @microsoft/api-extractor can be a peer dependecy and use |
I am amazed to discover this. One issue I encountered was that, to ensure proper auto-import behavior in various IDEs, we needed to employ a little trick. For reference: https://github.com/sendbird/sendbird-chat-sdk-javascript/blob/main/index.d.ts#L1-L5 |
This is amazing work and looks promising! Any updates on the state of it? |
This would be very cool. I'd love to use tsup for building declaration maps as well as everything else. "Go to definition" taking you to the source instead of the d.ts files would be a great devx improvement |
🎉 This PR is included in version 8.0.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
@ocavue out of curiosity if you considered dts-bundle-generator as an alternative and if so, what was your opinion and pros/cons that leaned you towards api-extractor? Thanks! |
I want to know it, too. Any updates? Thanks. |
This PR introduces a new
--experimental-dts
flag to generate declaration files. This flag is intended to eventually replace the current--dts
flag. As it's experimental, we can release it in a minor version without disrupting existing users. Once we have sufficient confidence in its functionality, we can deprecate the--dts
flag and make--experimental-dts
the default behavior in a major version.Motivation
The
--dts
flag currently usesrollup-plugin-dts
to generate declaration files. However, this plugin is no longer actively maintained1 and has known issues.The
--experimental-dts
flag aims to improve upon this by using @microsoft/api-extractor to generate declaration files.Limitations
One limitation of @microsoft/api-extractor is that it doesn't support multiple entry files. For instance,
react-dom
has multiple entry files,including
react-dom/client
,react-dom/server
, etc. Although there are plans2 to support multiple entry files, progress has been slow.Therefore, I have implemented a workaround, which we'll discuss in the next section.
Implementation
Let's take an example, we have a library with the following structure:
This library has two entry files,
client.ts
andserver.ts
. They both importshared.ts
.The first step is to generate declaration files using TypeScript compiler. The source code of this part is in
src/tsc.ts
. It will generate declaration files for the whole project, just like whattsc
does. The generated declaration files will be put in a temporary directory.tsup
. Here is the file structure after this step:We cannot run api-extractor separately for each entry file, because it will generate duplicated declaration for exported types in
shared.ts
. So we need to give it a single entry file. Insrc/tsc.ts
, we use TypeScript to find all exported types for each entry file, and later we will generate a merged declaration file for them. Here is the file structure after this step:The
.gitignore
file is used to ignore all files in.tsup
directory, so users don't need to modify their.gitignore
file. I tried to put it undernode_modules/.tsup
but it seems that api-extractor will ignore declaration files undernode_modules
.In
_tsup-dts-aggregation.d.ts
, it will simply re-export all exported types fromclient.d.ts
,server.d.ts
. Here is the content of_tsup-dts-aggregation.d.ts
:Note that if there are multiple exported types with the same name, it will generate a unique name for them.
The next step will run api-extractor to generate declaration files. Api-extractor will read
_tsup-dts-aggregation.d.ts
and rollup all exported types into a single declaration file_tsup-dts-rollup.d.ts
. Here is the file structure after this step:For the last step, we will create one declaration file for each entry file under the
dist/
, and re-export needed exported types from_tsup-dts-rollup.d.ts
. Here is the final file structure and content:Future Work
--experimental-dts
flag should use a shorter path to generate the declaration files (i.e., without the final_tsup-dts-rollup.d.ts
file).export * from 'react-dom/server'
will result in multiple specific exports likeexport { render } from 'react-dom/server'
andexport { renderToString } from 'react-dom/server'
, etc. This is not good because the exported types for a module could vary between different versions.Footnotes
https://github.com/Swatinem/rollup-plugin-dts/issues/277 ↩
https://github.com/microsoft/rushstack/issues/1596#issuecomment-546790721 ↩