vite-plugin-dts

raw JSON →
4.5.4 verified Sat Apr 25 auth: no javascript

vite-plugin-dts is a Vite plugin that generates TypeScript declaration files (.d.ts) from your source code during the build process. Current stable version is 4.5.4, with frequent releases. It integrates with both TypeScript's built-in compiler and vue-tsc for Vue SFC support. Key differentiators include support for bundling declarations via @microsoft/api-extractor, path alias resolution, and a flexible plugin system with hooks like afterBootstrap. It is the de facto standard for declaration generation in Vite projects, actively maintained with a strong focus on compatibility and performance.

error Error: [vite-plugin-dts] Failed to resolve module specifier './some-module'
cause The plugin cannot resolve a module path that uses aliases not defined in TypeScript paths.
fix
Ensure your tsconfig has the correct 'paths' mapping and set 'tsconfigPath' option to the correct config.
error TypeError: dts is not a function
cause Using default import incorrectly, or the plugin version does not support the import style being used.
fix
Use 'import dts from 'vite-plugin-dts'' with ESM, and ensure the package is installed and not a type-only export.
error Cannot find module 'vite-plugin-dts' or its corresponding type declarations.
cause Package not installed or TypeScript resolution not aware of the module.
fix
Run 'npm install -D vite-plugin-dts' and ensure tsconfig 'types' includes 'vite/client' if needed.
error The 'rollupTypes' option is deprecated. Use 'bundleTypes' instead.
cause Using an older option name from a previous version.
fix
Replace 'rollupTypes: true' with 'bundleTypes: true' in the plugin options.
deprecated The 'beforeBundle' hook has been deprecated in v5.0.0-beta. Use 'afterBootstrap' instead.
fix Replace 'beforeBundle' with 'afterBootstrap' in the plugin options.
breaking In v5.0.0, the plugin no longer supports CommonJS require() directly; it is ESM-only.
fix Switch to ESM imports: import dts from 'vite-plugin-dts'
gotcha If using TypeScript path aliases, you must configure the 'tsconfigPath' option to match the resolved path.
fix Set 'tsconfigPath' to the correct tsconfig file path, e.g., 'tsconfig.json'.
gotcha The plugin may generate duplicate declarations if 'include' overlaps with resolved 'tsconfig' includes.
fix Specify a more precise 'include' pattern, e.g., exclude 'test' or 'stories'.
npm install vite-plugin-dts
yarn add vite-plugin-dts
pnpm add vite-plugin-dts

Shows the basic configuration of vite-plugin-dts in a Vite config file. Includes include, outDir, and optional bundleTypes.

import dts from 'vite-plugin-dts'
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [
    dts({
      include: ['src/**/*.ts'],
      outDir: 'dist',
      // Optional: enable declaration bundling
      // bundleTypes: true
    })
  ]
})
// Then run 'vite build' to generate .d.ts files