
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
vite-plugin-dts
Advanced tools
vite-plugin-dts is a Vite plugin that generates TypeScript declaration files (d.ts) for your project. It helps in ensuring type safety and better developer experience by providing type definitions for your code.
Generate TypeScript Declarations
This feature allows you to automatically generate TypeScript declaration files for your project. By including the plugin in your Vite configuration, it will generate .d.ts files for your TypeScript code.
import dts from 'vite-plugin-dts';
export default {
plugins: [dts()]
};
Custom Output Directory
You can specify a custom output directory for the generated declaration files. This is useful if you want to organize your type definitions in a specific folder.
import dts from 'vite-plugin-dts';
export default {
plugins: [
dts({
outputDir: 'types'
})
]
};
Include/Exclude Specific Files
This feature allows you to include or exclude specific files or folders from the declaration generation process. You can use glob patterns to specify the files to include or exclude.
import dts from 'vite-plugin-dts';
export default {
plugins: [
dts({
include: ['src/**/*.ts'],
exclude: ['src/excluded-folder/**']
})
]
};
The TypeScript compiler (tsc) can be used to generate declaration files. It is a more general tool compared to vite-plugin-dts and can be used in any TypeScript project, not just those using Vite. However, it requires more configuration and does not integrate as seamlessly with Vite.
rollup-plugin-dts is a Rollup plugin that generates TypeScript declaration files. It is similar to vite-plugin-dts but is designed for use with Rollup instead of Vite. If you are using Rollup as your bundler, this plugin would be a better fit.
(Do not recommend anymore, please use unplugin-dts
instead)
pnpm i -D vite-plugin-dts
In vite.config.ts
:
import { resolve } from 'path'
import { defineConfig } from 'vite'
import dts from 'vite-plugin-dts'
export default defineConfig({
build: {
lib: {
entry: resolve(__dirname, 'src/index.ts'),
name: 'MyLib',
formats: ['es'],
fileName: 'my-lib'
}
},
plugins: [dts()]
})
By default, the generated declaration files are following the source structure.
Fortunately, with the help of API Extractor, the plugin can bundle all types into a single file. You just need to install @microsoft/api-extractor
and set bundleTypes: true
:
{
plugins: [dts({ bundleTypes: true })]
}
If you start with official Vite template, you should specify the tsconfigPath
:
{
plugins: [dts({ tsconfigPath: './tsconfig.app.json' })]
}
A real project using this plugin: Vexip UI.
MIT License.
FAQs
vite-plugin-dts
The npm package vite-plugin-dts receives a total of 1,288,379 weekly downloads. As such, vite-plugin-dts popularity was classified as popular.
We found that vite-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's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.