How to generate only 1 declaration file instead of multiple identical d.ts with multiple outputs?
See original GitHub issueI’m using rollup to bundle my code with dual cjs/esm output. As you can expect, my setup is a single input multiple output configuration.
{
"input": "source/index.ts",
"output": [
{
"file": "lib/index.mjs",
"format": "es",
"sourcemap": true
},
{
"file": "lib/index.cjs",
"format": "cjs",
"sourcemap": true
}
]
}
The plugin works just fine. The only expected thing is that 2 identical d.ts files (index.d.ts
& index.cjs.d.ts
) are generated per output. index.cjs.d.ts
is reductant and I want to get rid of it but I cannot find any options to generate the declaration file once. Any hint?
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Generate a single declaration file for external modules #2568
d.ts file for all their modules. the only way to do this now, is to either hand edit the file, or use a...
Read more >Documentation - Creating .d.ts Files from .js files - TypeScript
Setting up your Project to emit .d.ts files · Add TypeScript to your dev dependencies · Add a tsconfig.json to configure TypeScript ·...
Read more >Export single .d.ts from several typescript files + entrypoint
have the compiler auto-generate declarations for your classes by specifying "declaration": true in tsconfig.json. the compiler will also generate an entrypoint.
Read more >TypeScript library tips: Rollup your types! | by Martin Hochel
ts rolluping into one index.d.ts with optional 2 outputs. Api-extractor comes with extended feature set beyond just rollup-ing declaration files ...
Read more >Type Declarations
d.ts files are declaration files that contain only type information. These files don't produce .js outputs; they are only used for typechecking ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Because this plugin is invoked and operates per output, the content of the emitted files may change depending on the output and the output options. The same goes for the declaration files. For example, because
rollup-plugin-ts
supports code splitting inside your declaration files, in such scenarios, module specifiers within import declarations across your declaration files will be different for each bundle.However, in practice, outside of code splitting, and unless you’re doing fancy stuff with your outputs, the declaration files will most often be identical for each output. In such scenarios, while you can’t make
rollup-plugin-ts
only generate them once, you can make it only emit one of them by making sure they’re identically named.Here’s a few ways I’ve seen others solve this in the past.
First, if the two
.js
files have different names, for exampleindex.cjs.js
andindex.esm.js
, then you can use theoutputPath
hook to rewrite the name of the declaration file to something you’d like:Alternatively, if your two .js files have identical names and you instead have an output structure that looks something like this:
…And you want for it to be something like this:
…Then all you have to do is go to your
tsconfig.json
and add"declarationDir": "dist"
to your compiler options, as the two declaration files will be equally named and only one will be emitted.I may be open to doing some heuristic where if I can gather based on the Rollup- and plugin options that we’re in a single-file bundling type of scenario where the corresponding declaration file for each output will most likely be completely identical, I will apply an optimization such that they are only generated once.
It’s a separate issue, which I’ve raised here: https://github.com/wessberg/rollup-plugin-ts/issues/151