Product
Introducing Ruby Support in Socket
Socket is launching Ruby support for all users. Enhance your Rails projects with AI-powered security scans for vulnerabilities and supply chain threats. Now in Beta!
rollup-plugin-dts
Advanced tools
The rollup-plugin-dts package is a Rollup plugin that allows you to bundle TypeScript declaration files (.d.ts). It is particularly useful for library authors who want to distribute their TypeScript types along with their JavaScript code.
Bundle TypeScript Declaration Files
This feature allows you to bundle multiple TypeScript declaration files into a single .d.ts file. The code sample demonstrates how to configure Rollup to use the rollup-plugin-dts to bundle TypeScript declaration files from 'src/index.d.ts' to 'dist/index.d.ts'.
import dts from 'rollup-plugin-dts';
export default {
input: 'src/index.d.ts',
output: {
file: 'dist/index.d.ts',
format: 'es'
},
plugins: [dts()]
};
The TypeScript package itself can generate declaration files using the 'declaration' and 'declarationDir' options in the tsconfig.json file. However, it does not bundle them into a single file. This is a more basic approach compared to rollup-plugin-dts, which provides more advanced bundling capabilities.
The dts-bundle package is another tool for bundling TypeScript declaration files. It is similar to rollup-plugin-dts but is not a Rollup plugin. It can be used as a standalone tool or integrated into build scripts. It offers similar functionality but may require more manual setup compared to the seamless integration provided by rollup-plugin-dts.
This is an EXPERIMENT to generate roll-upd .d.ts
definition files from
your .ts
files.
Well, ideally TypeScript should just do that itself, and it also has a proposal to do that. But since ~3 years, it hasn’t happened yet.
There are also some solutions for this already:
Some projects, like rollup itself go the route of completely separating their public interfaces in a separate file.
Well yes it does, even though I am sure there are a lot of things I haven’t covered yet. However, so far it is complete enough to even generate its own definition file
This project abuses the internals implementation of rollup in a quite interesting way. See, rollup uses string manipulation to generate its output file, by changing and removing parts of the input file content. It also does quite extensive dead code elimination by walking the AST of the input code and figuring out which parts it can safely remove from the output bundle.
We can use this knowledge to specifically direct rollup to keep and remove parts of our input file, and to rename the correct Identifiers.
What we do, is to transform the Typescript code into a virtual AST, that is in itself just really strange code, but it makes rollup do what we would like it to do.
For things on the top-level that we want removed from our source, we generate
an IfStatement
that is always false. Rollup will figure this out and just
remove that statement altogether.
if (false) {}
The trick here is to annotate this IfStatement
with a certain start
and end
.
Rollup will then just remove all the bytes between start
and end
, without
even looking into what those bytes actually are.
TODO: actually, it should be easier to just generate any identifier without a side-effect, like I do for nested code.
For each export (class
, function
, interface
or type
), we will create
a bogus FunctionDeclaration
for rollup. Again, we annotate the
FunctionDeclaration
with a set of start
and end
markers, so that rollup
will remove the correct parts of our code if it figures out the declaration is
not referenced.
function foo() {}
export function bar() {}
Rollup will actually analyze functions for side-effects and happily remove functions which are side-effect free, even though they are referenced in other parts of your code.
In order for rollup to at least consider putting a function into our bundle,
we have to introduce a side-effect into the function. How do we do that?
The answer is to generate code that rollup can not see inside. For example
by calling an unreferenced identifier. That identifier could potentially live
in window
and rollup does not know that. So it does not touch that code.
_()
If someone has looked very carefully at the previous example, you will see
that rollup actually inserts a semicolon after the CallExpression
.
This one took me a long time to figure out and work around.
In the end I decided to create references between different declarations as function argument defaults. That way rollup will not insert semicolons that would otherwise mess with out TypeScript code.
Again, all the Identifier
s are annotated with correct start
and end
markers. So if rollup decides to rename them, it will touch the correct parts
of the code. Also, the function name itself is part of the identifier list,
because there might be identifiers before the function name, such as type
parameters and maybe things we would want to remove.
function foo(_0 = foo) {}
function bar(_0 = bar, _1 = foo) {}
function baz(_0 = baz) {}
export function foobar(_0 = foobar, _1 = bar, _2 = baz) {}
Building on the previous example, we can use the list of function argument defaults to, and the thing we learned before about removing top-level code to mark nested code for deletion.
For this case, we create an arrow function with some dead code inside. As you
will see in the example, rollup will remove that code. Again, annotating it with
start
and end
markers and you are done.
function foo(_0 = foo, _1 = () => {removeme}) {}
export function bar(_0 = bar, _1 = foo) {}
With that, we have all the tools to create roll-upd .d.ts
files.
@internal
or @hidden
FAQs
A rollup plugin that will bundle up your .d.ts definition files.
The npm package rollup-plugin-dts receives a total of 396,891 weekly downloads. As such, rollup-plugin-dts popularity was classified as popular.
We found that rollup-plugin-dts demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket is launching Ruby support for all users. Enhance your Rails projects with AI-powered security scans for vulnerabilities and supply chain threats. Now in Beta!
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.