Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
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.
A Vite plugin that generates declaration files (*.d.ts
) from .ts(x)
or .vue
source files when using Vite in library mode.
English | 中文
pnpm i vite-plugin-dts -D
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.
Here are some FAQ's and solutions.
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.
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.
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"]
}
}
}
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 {
/**
* The name of the resolver
*
* The later resolver with the same name will overwrite the earlier
*/
name: string,
/**
* Determine whether the resolver supports the file
*/
supports: (id: string) => void | boolean,
/**
* Transform source to declaration files
*
* Note that the path of the returns should base on `outDir`, or relative path to `root`
*/
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 {
/**
* Specify root directory
*
* Defaults to the 'root' of the Vite config, or `process.cwd()` if using Rollup
*/
root?: string,
/**
* Output directory for declaration files
*
* Can be an array to output to multiple directories
*
* Defaults to 'build.outDir' of the Vite config, or `outDir` of tsconfig.json if using Rollup
*/
outDir?: string | string[],
/**
* Override root path of entry files (useful in monorepos)
*
* The output path of each file will be calculated based on the value provided
*
* The default is the smallest public path for all source files
*/
entryRoot?: string,
/**
* Restrict declaration files output to `outDir`
*
* If true, generated declaration files outside `outDir` will be ignored
*
* @default true
*/
strictOutput?: boolean,
/**
* Override compilerOptions
*
* @default null
*/
compilerOptions?: ts.CompilerOptions | null,
/**
* Specify tsconfig.json path
*
* Plugin resolves `include` and `exclude` globs from tsconfig.json
*
* If not specified, plugin will find config file from root
*/
tsconfigPath?: string,
/**
* Specify custom resolvers
*
* @default []
*/
resolvers?: Resolver[],
/**
* Parsing `paths` of tsconfig.json to aliases
*
* Note that these aliases only use for declaration files
*
* @default true
* @remarks Only use first replacement of each path
*/
pathsToAliases?: boolean,
/**
* Set which paths should be excluded when transforming aliases
*
* @default []
*/
aliasesExclude?: (string | RegExp)[],
/**
* Whether to transform file names ending in '.vue.d.ts' to '.d.ts'
*
* @default false
*/
cleanVueFileName?: boolean,
/**
* Whether to transform dynamic imports to static (eg `import('vue').DefineComponent` to `import { DefineComponent } from 'vue'`)
*
* Value is forced to `true` when `rollupTypes` is `true`
*
* @default false
*/
staticImport?: boolean,
/**
* Override `include` glob
*
* Defaults to `include` property of tsconfig.json
*/
include?: string | string[],
/**
* Override `exclude` glob
*
* Defaults to `exclude` property of tsconfig.json or `'node_modules/**'` if not supplied.
*/
exclude?: string | string[],
/**
* Whether to remove `import 'xxx'`
*
* @default true
*/
clearPureImport?: boolean,
/**
* Whether to generate types entry file(s)
*
* When `true`, uses package.json `types` property if it exists or `${outDir}/index.d.ts`
*
* Value is forced to `true` when `rollupTypes` is `true`
*
* @default false
*/
insertTypesEntry?: boolean,
/**
* Rollup type declaration files after emitting them
*
* Powered by `@microsoft/api-extractor` - time-intensive operation
*
* @default false
*/
rollupTypes?: boolean,
/**
* Bundled packages for `@microsoft/api-extractor`
*
* @default []
* @see https://api-extractor.com/pages/configs/api-extractor_json/#bundledpackages
*/
bundledPackages?: string[],
/**
* Override the config of `@microsoft/api-extractor`
*
* @default null
* @see https://api-extractor.com/pages/setup/configure_api_report/
*/
rollupConfig?: RollupConfig,
/**
* Whether to copy .d.ts source files to `outDir`
*
* @default false
* @remarks Before 2.0, the default was `true`
*/
copyDtsFiles?: boolean,
/**
* Whether to emit declaration files only
*
* When `true`, all the original outputs of vite (rollup) will be force removed
*
* @default false
*/
declarationOnly?: boolean,
/**
* Logging level for this plugin
*
* Defaults to the 'logLevel' property of your Vite config
*/
logLevel?: LogLevel,
/**
* Hook called after diagnostic is emitted
*
* According to the `diagnostics.length`, you can judge whether there is any type error
*
* @default () => {}
*/
afterDiagnostic?: (diagnostics: readonly ts.Diagnostic[]) => MaybePromise<void>,
/**
* Hook called prior to writing each declaration file
*
* This allows you to transform the path or content
*
* The file will be skipped when the return value `false` or `Promise<false>`
*
* @default () => {}
*/
beforeWriteFile?: (
filePath: string,
content: string
) => MaybePromise<
| void
| false
| {
filePath?: string,
content?: string
}
>,
/**
* Hook called after all declaration files are written
*
* @default () => {}
*/
afterBuild?: () => MaybePromise<void>
}
Thanks for all the contributions!
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.
MIT License.
FAQs
vite-plugin-dts
The npm package vite-plugin-dts receives a total of 382,247 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.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.