Supported Bundlers
mainWhile the quick start example shows Vite, unplugin-dts is built using unplugin and supports multiple bundlers including:
- Vite
- Rollup
- Rolldown
- Webpack
- Rspack
- Esbuild
repository·main·Indexed 23 days ago
https://github.com/qmhc/unplugin-dtsAn unplugin that automatically generates TypeScript declaration files (*.d.ts) from .ts(x) or .vue source files. It supports multiple bundlers including Vite, Rollup, Rolldown, Webpack, Rspack, and Esbuild. It provides features for type bundling via bundleTypes, custom output directories with outDirs, and specific support for Vue projects using the 'vue' processor.
While the quick start example shows Vite, unplugin-dts is built using unplugin and supports multiple bundlers including:
The unplugin-dts plugin is a universal Unplugin that supports the following build tools:
To use unplugin-dts in a Vite project, import the Vite entry point from unplugin-dts/vite and add it to your defineConfig plugins array. This plugin generates type files (*.d.ts) from .ts(x) or .vue source files when building in library mode.
import dts from 'unplugin-dts/vite'
export default defineConfig({
plugins: [dts()],
})To use unplugin-dts with Esbuild, import the plugin from unplugin-dts/esbuild and add it to the plugins array in your build script.
import { build } from 'esbuild'
import dts from 'unplugin-dts/esbuild'
await build({
entryPoints: ['src/index.ts'],
format: 'esm',
outdir: 'dist',
bundle: true,
plugins: [dts()],
})To use unplugin-dts with Webpack, import the plugin from unplugin-dts/webpack and add it to the plugins array in your webpack.config.js.
import { resolve } from 'node:path'
import dts from 'unplugin-dts/webpack'
export default {
entry: {
index: './src/index.ts',
},
output: {
path: resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.ts$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
plugins: [dts()],
}Install vite-plugin-dts as a development dependency using pnpm:
pnpm i -D vite-plugin-dtsNote: The maintainers recommend using unplugin-dts instead of
vite-plugin-dtsfor new projects.
In a monorepo, if Package A depends on an unpublished Package B, the generated .d.ts files for Package A might still contain import { ... } from 'packageB', which will fail for consumers.
Configure the plugin to process the source code of the internal package instead of its build output. This ensures TypeScript treats the types as part of Package A.
vite.config.ts:
import { defineConfig } from 'vite'
import dts from 'vite-plugin-dts'
import { resolve } from 'node:path'
export default defineConfig({
resolve: {
alias: {
// Point to the source entry of the internal package
'packageB': resolve(__dirname, '../packageB/src/index.ts'),
},
},
plugins: [
dts({
// Include the internal package's source files so they are emitted together
include: ['src', '../packageB/src'],
tsconfigPath: resolve(__dirname, 'tsconfig.app.json'),
}),
],
})tsconfig.app.json:
{
"compilerOptions": {
"paths": {
"packageB": ["../packageB/src/index.ts"]
}
}
}Note: Ensure the internal package's package.json has a valid version field.
bundleTypes.bundledPackagesIf bundleTypes: true is enabled, you can instruct @microsoft/api-extractor to inline specific packages.
dts({
bundleTypes: {
bundledPackages: ['packageB', '@scope/*'],
},
})Limitations of Method 2:
import { defineConfig } from 'vite'
import dts from 'vite-plugin-dts'
import { resolve } from 'node:path'
export default defineConfig({
resolve: {
alias: {
'packageB': resolve(__dirname, '../packageB/src/index.ts'),
},
},
plugins: [
dts({
include: ['src', '../packageB/src'],
tsconfigPath: resolve(__dirname, 'tsconfig.app.json'),
}),
],
})To use unplugin-dts in a Vue project, you must install @vue/language-core as a peer dependency:
pnpm i -D @vue/language-coreWhile the plugin automatically detects .vue files and uses the 'vue' processor, it is recommended to explicitly set the processor option to 'vue'.
export default defineConfig({
plugins: [dts({ processor: 'vue' })],
})To use unplugin-dts in a Vite project, import the Vite-specific entry point from unplugin-dts/vite and add it to your defineConfig plugins array. This plugin generates declaration files (*.d.ts) from .ts(x) or .vue source files, which is particularly useful when building libraries in library mode.
import dts from 'unplugin-dts/vite'
export default defineConfig({
plugins: [dts()],
})Install unplugin-dts as a development dependency using pnpm. Note that this project requires Node.js >= 20.
If you are using Vite and want the specific Vite wrapper, you can use vite-plugin-dts, but using unplugin-dts is the recommended approach as it is more universal.
pnpm i -D unplugin-dtsWhen upgrading from vite-plugin-dts v4 to v5 (or moving to unplugin-dts v1), several breaking changes in configuration keys and import paths must be addressed.
While vite-plugin-dts maintains a compatibility layer for Vite, the recommended approach for v5 is to use unplugin-dts directly.
import dts from 'vite-plugin-dts'import dts from 'unplugin-dts/vite'import dts from 'unplugin-dts/rollup'| Old Option (v4) | New Option (v5) | Description |
|---|---|---|
rollupTypes | bundleTypes | Controls type bundling. Now supports boolean | object. |
outDir | outDirs | Specifies output directories. Now supports moduleFormat. |
rollupConfig | bundleTypes.extractorConfig | Nested within bundleTypes. |
rollupOptions | bundleTypes.invokeOptions | Nested within bundleTypes. |
bundledPackages | bundleTypes.bundledPackages | Moved from top-level to nested within bundleTypes. |
logLevel | Removed | No longer configurable. |
editSourceMapDir: (content: string, fromDir: string, toDir: string) => stringResolver type: { name: string, supports: boolean, transform: ... }// v5 (unplugin-dts, recommended)
import dts from 'unplugin-dts/vite'
import { type PluginOptions, editSourceMapDir } from 'unplugin-dts'
export { type PluginOptions, editSourceMapDir }To use unplugin-dts with Rspack, import the plugin from unplugin-dts/rspack and add it to the plugins array in your rspack.config.mjs.
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { defineConfig } from '@rspack/cli'
import dts from 'unplugin-dts/rspack'
const rootDir = resolve(fileURLToPath(import.meta.url), '..')
export default defineConfig({
entry: {
index: './src/index.ts',
},
output: {
path: resolve(rootDir, 'dist'),
},
module: {
rules: [
{
test: /\.js$/,
use: [
{
loader: 'builtin:swc-loader',
options: {
jsc: {
parser: {
syntax: 'ecmascript',
},
},
},
},
],
},
{
test: /\.ts$/,
use: [
{
loader: 'builtin:swc-loader',
options: {
jsc: {
parser: {
syntax: 'typescript',
decorators: true,
},
},
},
},
],
},
],
},
plugins: [dts()],
})