A rollup plugin that bundles TypeScript definition files (.d.ts) into consolidated declaration files.
npx @tessl/cli install tessl/npm-rollup-plugin-dts@6.2.0Rollup Plugin DTS is a Rollup plugin that bundles TypeScript definition files (.d.ts) into consolidated declaration files. It uses the TypeScript compiler API to intelligently process and merge multiple declaration files while preserving type information and module relationships.
npm install --save-dev rollup-plugin-dtsimport { dts } from "rollup-plugin-dts";For legacy support (default import - both exports are identical):
import dts from "rollup-plugin-dts";Type-only imports:
import type { Options } from "rollup-plugin-dts";CommonJS:
const { dts } = require("rollup-plugin-dts");import { dts } from "rollup-plugin-dts";
const config = [
// Your regular build configuration
{
input: "src/index.ts",
output: [{ file: "dist/my-library.js", format: "cjs" }],
plugins: [/* other plugins */],
},
// Declaration file bundling configuration
{
input: "./src/index.d.ts", // or generated .d.ts files
output: [{ file: "dist/my-library.d.ts", format: "es" }],
plugins: [dts()],
},
];
export default config;The main plugin function that creates a Rollup plugin for bundling TypeScript declaration files.
/**
* Creates a Rollup plugin that bundles TypeScript declaration files
* @param options - Plugin configuration options
* @returns Rollup plugin instance
*/
function dts(options?: Options): Plugin;Usage Examples:
import { dts } from "rollup-plugin-dts";
// Basic usage with no options
export default {
input: "types/index.d.ts",
output: { file: "dist/bundle.d.ts", format: "es" },
plugins: [dts()],
};
// With configuration options
export default {
input: "types/index.d.ts",
output: { file: "dist/bundle.d.ts", format: "es" },
plugins: [
dts({
respectExternal: true,
includeExternal: ["some-types-package"],
compilerOptions: {
baseUrl: "./",
paths: {
"@/*": ["src/*"]
}
},
tsconfig: "./tsconfig.build.json"
})
],
};Plugin configuration interface for customizing bundling behavior.
interface Options {
/**
* Controls external library classification. When true, uses rollup's external
* configuration instead of automatically marking all node_modules as external.
* @default false
*/
respectExternal?: boolean;
/**
* List of external module names to include types from instead of marking as external.
* Useful for bundling specific @types packages or external library types.
* @default []
*/
includeExternal?: Array<string>;
/**
* TypeScript compiler options for path-mapping and module resolution.
* Commonly used for baseUrl and paths configuration.
* @default {}
*/
compilerOptions?: ts.CompilerOptions;
/**
* Path to custom tsconfig.json file for TypeScript configuration.
* By default, will try to load 'tsconfig.json' from the current directory.
*/
tsconfig?: string;
}Configuration Examples:
// Path mapping support
const config = dts({
compilerOptions: {
baseUrl: "./",
paths: {
"@utils/*": ["src/utils/*"],
"@types/*": ["types/*"]
}
}
});
// Include external types
const config = dts({
includeExternal: ["@types/node", "typescript"],
respectExternal: false
});
// Custom tsconfig
const config = dts({
tsconfig: "./tsconfig.declarations.json"
});// Handle multiple entry points
export default {
input: {
main: "types/index.d.ts",
utils: "types/utils.d.ts",
helpers: "types/helpers.d.ts"
},
output: {
dir: "dist/types",
format: "es"
},
plugins: [dts()],
};// Bundle TypeScript-generated declaration files
export default [
// First, build your TypeScript source
{
input: "src/index.ts",
output: { file: "dist/index.js", format: "cjs" },
plugins: [typescript({ declaration: true, outDir: "temp" })],
},
// Then bundle the generated .d.ts files
{
input: "temp/index.d.ts",
output: { file: "dist/index.d.ts", format: "es" },
plugins: [dts()],
},
];// When using resolveJsonModule in TypeScript
export default {
input: "src/index.ts",
output: { file: "dist/bundle.d.ts", format: "es" },
plugins: [
dts({
compilerOptions: {
resolveJsonModule: true
}
})
],
};The plugin automatically marks all external libraries from node_modules as external, preventing them from being bundled. This behavior can be controlled with the respectExternal option.
Uses the TypeScript compiler API for:
Creates separate TypeScript programs for different entry points when they have different compiler configurations or are in different directories.
Handles TypeScript triple-slash references:
/// <reference path="..." /> for file references/// <reference types="..." /> for type referencesConverts JavaScript-style namespace exports to proper TypeScript namespace declarations in the final output.
The plugin will emit TypeScript compilation errors and halt the build if:
Common errors include: