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.
It is complete enough to generate its own definition file, and it is used successfully for intl-codegen as well.
Install the package from npm
:
$ npm install --save-dev tslib rollup rollup-plugin-dts rollup-plugin-dts
Create your rollup.config.js
:
import resolve from "rollup-plugin-node-resolve";
// NOTE: The plugin has two different modes:
// * one to transpile `.ts -> .js`
// * one to create `.ts -> .d.ts` bundles
import { ts, dts } from "rollup-plugin-dts";
const config = [
{
input: "./src/index.ts",
// NOTE: The first output is your transpiled typescript
output: [{ file: "dist/my-library.js", format: "cjs" }, { file: "dist/my-library.mjs", format: "es" }],
plugins: [
resolve({
jsnext: true,
extensions: [".ts"],
}),
ts(),
],
},
{
input: "./src/index.ts",
// NOTE: The second output is your bundled `.d.ts` file
output: [{ file: "dist/my-library.d.ts", format: "es" }],
plugins: [dts()],
},
];
export default config;
And then instruct node or other bundles where to find your code
"main": "dist/my-library.js",
"module": "dist/my-library.mjs",
"types": "dist/my-library.d.ts",
Exporting namespaces currently does not work and will create invalid .d.ts
files.
import * as ns from "./namespace";
export { ns };
Well, ideally TypeScript should just do all this itself, and it even has a proposal to do that. But there hasn’t been any progress in ~3 years.
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.
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.