
Research
Two Malicious Rust Crates Impersonate Popular Logger to Steal Wallet Keys
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
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 add 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()]
})
In your component:
<template>
<div></div>
</template>
<script lang="ts">
// using defineComponent for inferring types
import { defineComponent } from 'vue'
export default defineComponent({
name: 'Component'
})
</script>
<script setup lang="ts">
// Need to access the defineProps returned value to
// infer types although you never use the props directly
const props = defineProps<{
color: 'blue' | 'red'
}>()
</script>
<template>
<div>{{ color }}</div>
</template>
Here are some FAQ's and solutions.
1.7.0
)By default, the skipDiagnostics
option is set to true
which means type diagnostic will be skipped during the build process (some projects may have diagnostic tools such as vue-tsc
). If there are some files with type errors which interrupt the build process, these files 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. It will help you check the type errors during build and log error information 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
, which results in a type error.
Here is a simple example. You should remove the defineComponent
which in script
and export a native object directly.
node_modules
This is an existing issue when TypeScript infers types from packages located in node_modules
through soft links (pnpm). Please refer to this TypeScript issue. The current 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 { LogLevel } from 'vite'
interface TransformWriteFile {
filePath?: string
content?: string
}
export interface PluginOptions {
/**
* Specify root directory
*
* By Default it base on 'root' of your Vite config, or `process.cwd()` if using Rollup
*/
root?: string
/**
* Specify declaration files output directory
*
* Can be specified a array to output to multiple directories
*
* By Default it base on 'build.outDir' of your Vite config, or `outDir` of tsconfig.json if using Rollup
*/
outDir?: string | string[]
/**
* Manually set the root path of the entry files, useful in monorepo
*
* The output path of each file will be calculated base on it
*
* By Default it is the smallest public path for all files
*/
entryRoot?: string
/**
* Specify a CompilerOptions to override
*
* @default null
*/
compilerOptions?: ts.CompilerOptions | null
/**
* Specify tsconfig.json path
*
* Plugin also resolve include and exclude files from tsconfig.json
*
* By default plugin will find config form root if not specify
*/
tsconfigPath?: string
/**
* Set which paths should exclude when transform aliases
*
* @default []
*/
aliasesExclude?: (string | RegExp)[]
/**
* Whether transform file name '.vue.d.ts' to '.d.ts'
*
* @default false
*/
cleanVueFileName?: boolean
/**
* Whether transform dynamic import to static
*
* Force `true` when `rollupTypes` is effective
*
* eg. `import('vue').DefineComponent` to `import { DefineComponent } from 'vue'`
*
* @default false
*/
staticImport?: boolean
/**
* Manual set include glob
*
* By Default it base on 'include' option of the tsconfig.json
*/
include?: string | string[]
/**
* Manual set exclude glob
*
* By Default it base on 'exclude' option of the tsconfig.json, be 'node_module/**' when empty
*/
exclude?: string | string[]
/**
* Whether remove those `import 'xxx'`
*
* @default true
*/
clearPureImport?: boolean
/**
* Whether generate types entry file
*
* When `true` will from package.json types field if exists or `${outDir}/index.d.ts`
*
* Force `true` when `rollupTypes` is effective
*
* @default false
*/
insertTypesEntry?: boolean
/**
* Set whether rollup declaration files after emit
*
* Power by `@microsoft/api-extractor`, it will start a new program which takes some time
*
* @default false
*/
rollupTypes?: boolean
/**
* Bundled packages for `@microsoft/api-extractor`
*
* @default []
* @see https://api-extractor.com/pages/configs/api-extractor_json/#bundledpackages
*/
bundledPackages?: string[]
/**
* Whether copy .d.ts source files into `outDir`
*
* @default false
* @remarks Before 2.0 it defaults to true
*/
copyDtsFiles?: boolean
/**
* Specify the log level of plugin
*
* By Default it base on 'logLevel' option of your Vite config
*/
logLevel?: LogLevel
/**
* Hook after diagnostic emitted
*
* According to the length to judge whether there is any type error
*
* @default () => {}
*/
afterDiagnostic?: (diagnostics: readonly ts.Diagnostic[]) => void | Promise<void>
/**
* Hook before each declaration file is written
*
* You can transform declaration file's path and content through it
*
* The file will be skipped when return exact `false`
*
* @default () => {}
*/
beforeWriteFile?: (filePath: string, content: string) => void | false | TransformWriteFile
/**
* Hook after built
*
* It wil be called after all declaration files are written
*
* @default () => {}
*/
afterBuild?: () => void | Promise<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 1,386,041 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.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
Research
A malicious package uses a QR code as steganography in an innovative technique.
Research
/Security News
Socket identified 80 fake candidates targeting engineering roles, including suspected North Korean operators, exposing the new reality of hiring as a security function.