vite-plugin-dts
A Vite plugin that generates declaration files (*.d.ts
) from .ts(x)
or .vue
source files when using Vite in library mode.
English | 中文
Install
pnpm i vite-plugin-dts -D
Usage
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.
If you want to merge all declarations into one file, just specify rollupTypes: true
:
{
plugins: [dts({ rollupTypes: true })]
}
Starting with 3.0.0
, you can use this plugin with Rollup.
FAQ
Here are some FAQ's and solutions.
Missing some declaration files after build (before 1.7.0
)
By default, the skipDiagnostics
option is set to true
which means type diagnostics will be skipped during the build process (some projects may have diagnostic tools such as vue-tsc
). Files with type errors which interrupt the build process will not be emitted (declaration files won't be generated).
If your project doesn't use type diagnostic tools, you can set skipDiagnostics: false
and logDiagnostics: true
to turn on diagnostic and logging features of this plugin. Type errors during build will be logged to the terminal.
Type error when using both script
and setup-script
in Vue component (before 3.0.0
)
This is usually caused by using the defineComponent
function in both script
and setup-script
. When vue/compiler-sfc
compiles these files, the default export result from script
gets merged with the parameter object of defineComponent
from setup-script
. This is incompatible with parameters and types returned from defineComponent
. This results in a type error.
Here is a simple example. You should remove the defineComponent
in script
and export a native object directly.
Type errors that are unable to infer types from packages in node_modules
This is an existing TypeScript issue where TypeScript infers types from packages located in node_modules
through soft links (pnpm). A workaround is to add baseUrl
to your tsconfig.json
and specify the paths
for these packages:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"third-lib": ["node_modules/third-lib"]
}
}
}
Options
import type ts from 'typescript'
import type { IExtractorConfigPrepareOptions } from '@microsoft/api-extractor'
import type { LogLevel } from 'vite'
type MaybePromise<T> = T | Promise<T>
export type RollupConfig = Omit<
IExtractorConfigPrepareOptions['configObject'],
| 'projectFolder'
| 'mainEntryPointFilePath'
| 'compiler'
| 'dtsRollup'
>
export interface Resolver {
name: string,
supports: (id: string) => void | boolean,
transform: (payload: {
id: string,
code: string,
root: string,
outDir: string,
host: ts.CompilerHost,
program: ts.Program,
service: ts.LanguageService
}) => MaybePromise<{ path: string, content: string }[]>
}
export interface PluginOptions {
root?: string,
outDir?: string | string[],
entryRoot?: string,
strictOutput?: boolean,
compilerOptions?: ts.CompilerOptions | null,
tsconfigPath?: string,
resolvers?: Resolver[],
pathsToAliases?: boolean,
aliasesExclude?: (string | RegExp)[],
cleanVueFileName?: boolean,
staticImport?: boolean,
include?: string | string[],
exclude?: string | string[],
clearPureImport?: boolean,
insertTypesEntry?: boolean,
rollupTypes?: boolean,
bundledPackages?: string[],
rollupConfig?: RollupConfig,
copyDtsFiles?: boolean,
declarationOnly?: boolean,
logLevel?: LogLevel,
afterDiagnostic?: (diagnostics: readonly ts.Diagnostic[]) => MaybePromise<void>,
beforeWriteFile?: (
filePath: string,
content: string
) => MaybePromise<
| void
| false
| {
filePath?: string,
content?: string
}
>,
afterBuild?: () => MaybePromise<void>
}
Contributors
Thanks for all the contributions!
Example
Clone and run the following script:
pnpm run test:ts
Then check examples/ts/types
.
Also Vue and React cases under examples
.
A real project using this plugin: Vexip UI.
License
MIT License.