{"id":13417315,"url":"https://github.com/qmhc/vite-plugin-dts","last_synced_at":"2025-05-08T23:35:15.651Z","repository":{"id":38345738,"uuid":"372805929","full_name":"qmhc/vite-plugin-dts","owner":"qmhc","description":"A Vite plugin for generating `.d.ts` files.","archived":false,"fork":false,"pushed_at":"2025-05-08T10:40:20.000Z","size":3488,"stargazers_count":1423,"open_issues_count":42,"forks_count":95,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-05-08T22:32:24.362Z","etag":null,"topics":["dts","react","tsc","typescript","vite","vite-plugin","volar","vue","vue-tsc"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/qmhc.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-06-01T11:30:09.000Z","updated_at":"2025-05-08T18:49:30.000Z","dependencies_parsed_at":"2024-01-15T09:38:24.658Z","dependency_job_id":"f936c2e3-1abe-4a44-95d9-36c56b43722b","html_url":"https://github.com/qmhc/vite-plugin-dts","commit_stats":{"total_commits":446,"total_committers":38,"mean_commits":"11.736842105263158","dds":0.09865470852017932,"last_synced_commit":"3273d3f71937ea4b0e34b457f00163f1e263c258"},"previous_names":[],"tags_count":122,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qmhc%2Fvite-plugin-dts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qmhc%2Fvite-plugin-dts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qmhc%2Fvite-plugin-dts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qmhc%2Fvite-plugin-dts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/qmhc","download_url":"https://codeload.github.com/qmhc/vite-plugin-dts/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253165731,"owners_count":21864460,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dts","react","tsc","typescript","vite","vite-plugin","volar","vue","vue-tsc"],"created_at":"2024-07-30T22:00:35.248Z","updated_at":"2025-05-08T23:35:15.623Z","avatar_url":"https://github.com/qmhc.png","language":"TypeScript","funding_links":[],"categories":["Plugins","TypeScript","Vite 插件","vue"],"sub_categories":["Framework-agnostic Plugins"],"readme":"\u003ch1 align=\"center\"\u003evite-plugin-dts\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n  A Vite plugin that generates declaration files (\u003ccode\u003e*.d.ts\u003c/code\u003e) from \u003ccode\u003e.ts(x)\u003c/code\u003e or \u003ccode\u003e.vue\u003c/code\u003e source files when using Vite in \u003ca href=\"https://vitejs.dev/guide/build.html#library-mode\"\u003elibrary mode\u003c/a\u003e.\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://www.npmjs.com/package/vite-plugin-dts\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/v/vite-plugin-dts?color=orange\u0026label=\" alt=\"version\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/qmhc/vite-plugin-dts/blob/main/LICENSE\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/l/vite-plugin-dts\" alt=\"license\" /\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n**English** | [中文](./README.zh-CN.md)\n\n## Install\n\n```sh\npnpm i vite-plugin-dts -D\n```\n\n## Usage\n\nIn `vite.config.ts`:\n\n```ts\nimport { resolve } from 'path'\nimport { defineConfig } from 'vite'\nimport dts from 'vite-plugin-dts'\n\nexport default defineConfig({\n  build: {\n    lib: {\n      entry: resolve(__dirname, 'src/index.ts'),\n      name: 'MyLib',\n      formats: ['es'],\n      fileName: 'my-lib'\n    }\n  },\n  plugins: [dts()]\n})\n```\n\nBy default, the generated declaration files are following the source structure.\n\nIf you want to merge all declarations into one file, just specify `rollupTypes: true`:\n\n```ts\n{\n  plugins: [dts({ rollupTypes: true })]\n}\n```\n\nIf you start with official Vite template, you should specify the `tsconfigPath`:\n\n```ts\n{\n  plugins: [dts({ tsconfigPath: './tsconfig.app.json' })]\n}\n```\n\nStarting with `3.0.0`, you can use this plugin with Rollup.\n\n## FAQ\n\nHere are some FAQ's and solutions.\n\n### Type errors that are unable to infer types from packages in `node_modules`\n\nThis is an existing [TypeScript issue](https://github.com/microsoft/TypeScript/issues/42873) 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:\n\n```json\n{\n  \"compilerOptions\": {\n    \"baseUrl\": \".\",\n    \"paths\": {\n      \"third-lib\": [\"node_modules/third-lib\"]\n    }\n  }\n}\n```\n\n### `Internal Error` occurs when using `rollupTypes: true`\n\nRefer to this [issue](https://github.com/microsoft/rushstack/issues/3875), it's due to a limitation of `@microsoft/api-extractor` or TypeScript resolver.\n\nThe main reason is that `baseUrl` is specified in `tsconfig.json` and non-standard paths are used directly when imported.\n\nFor example: `baseUrl: 'src'` is specified and importing from `\u003croot\u003e/src/components/index.ts` in `\u003croot\u003e/src/index.ts`, and `import 'components'` is used instead of `import './components'`.\n\nCurrently, you need to avoid the above situation, or use aliases instead (with the `paths` option).\n\n### Get module not found errors during build\n\nThis is likely due to incorrect configuration of the `include` property in your default `tsconfig.json`.\n\nDue to some limitations, the plugin relies on the top-level `tsconfig.json` to resolve the files to include. Therefore, you need to specify the correct `include` property in the top-level `tsconfig.json`, or you can specify a configuration file path with the correct `include` property using the `tsconfigPath` option of the plugin. For example, in the Vite initial template, it is `tsconfig.app.json`.\n\nYou can refer to this [comment](https://github.com/qmhc/vite-plugin-dts/issues/343#issuecomment-2198111439).\n\n\u003cdetails\u003e\n  \u003csummary\u003eLegacy\u003c/summary\u003e\n\n### Missing some declaration files after build (before `1.7.0`)\n\nBy 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).\n\nIf 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.\n\n### Type error when using both `script` and `setup-script` in Vue component (before `3.0.0`)\n\nThis 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.\n\nHere is a simple [example](https://github.com/qmhc/vite-plugin-dts/blob/main/examples/vue/components/BothScripts.vue). You should remove the `defineComponent` in `script` and export a native object directly.\n\n\u003c/details\u003e\n\n## Options\n\n```ts\nimport type ts from 'typescript'\nimport type { IExtractorConfigPrepareOptions, IExtractorInvokeOptions } from '@microsoft/api-extractor'\nimport type { LogLevel } from 'vite'\n\ntype MaybePromise\u003cT\u003e = T | Promise\u003cT\u003e\n\nexport type RollupConfig = Omit\u003c\n  IExtractorConfigPrepareOptions['configObject'],\n  | 'projectFolder'\n  | 'mainEntryPointFilePath'\n  | 'compiler'\n  | 'dtsRollup'\n  \u003e\n\nexport interface Resolver {\n  /**\n   * The name of the resolver\n   *\n   * The later resolver with the same name will overwrite the earlier\n   */\n  name: string,\n  /**\n   * Determine whether the resolver supports the file\n   */\n  supports: (id: string) =\u003e void | boolean,\n  /**\n   * Transform source to declaration files\n   *\n   * Note that the path of the returns should base on `outDir`, or relative path to `root`\n   */\n  transform: (payload: {\n    id: string,\n    code: string,\n    root: string,\n    outDir: string,\n    host: ts.CompilerHost,\n    program: ts.Program\n  }) =\u003e MaybePromise\u003c{ path: string, content: string }[]\u003e\n}\n\nexport interface PluginOptions {\n  /**\n   * Specify root directory.\n   *\n   * Defaults to the 'root' of the Vite config, or `process.cwd()` if using Rollup.\n   */\n  root?: string,\n\n  /**\n   * Output directory for declaration files.\n   *\n   * Can be an array to output to multiple directories.\n   *\n   * Defaults to 'build.outDir' of the Vite config, or `outDir` of tsconfig.json if using Rollup.\n   */\n  outDir?: string | string[],\n\n  /**\n   * Override root path of entry files (useful in monorepos).\n   *\n   * The output path of each file will be calculated based on the value provided.\n   *\n   * The default is the smallest public path for all source files.\n   */\n  entryRoot?: string,\n\n  /**\n   * Restrict declaration files output to `outDir`.\n   *\n   * If true, generated declaration files outside `outDir` will be ignored.\n   *\n   * @default true\n   */\n  strictOutput?: boolean,\n\n  /**\n   * Override compilerOptions.\n   *\n   * @default null\n   */\n  compilerOptions?: ts.CompilerOptions | null,\n\n  /**\n   * Specify tsconfig.json path.\n   *\n   * Plugin resolves `include` and `exclude` globs from tsconfig.json.\n   *\n   * If not specified, plugin will find config file from root.\n   */\n  tsconfigPath?: string,\n\n  /**\n   * Specify custom resolvers.\n   *\n   * @default []\n   */\n  resolvers?: Resolver[],\n\n  /**\n   * Parsing `paths` of tsconfig.json to aliases.\n   *\n   * Note that these aliases only use for declaration files.\n   *\n   * @default true\n   * @remarks Only use first replacement of each path.\n   */\n  pathsToAliases?: boolean,\n\n  /**\n   * Set which paths should be excluded when transforming aliases.\n   *\n   * @default []\n   */\n  aliasesExclude?: (string | RegExp)[],\n\n  /**\n   * Whether to transform file names ending in '.vue.d.ts' to '.d.ts'.\n   *\n   * If there is a duplicate name after transform, it will fall back to the original name.\n   *\n   * @default false\n   */\n  cleanVueFileName?: boolean,\n\n  /**\n   * Whether to transform dynamic imports to static (eg `import('vue').DefineComponent` to `import { DefineComponent } from 'vue'`).\n   *\n   * Value is forced to `true` when `rollupTypes` is `true`.\n   *\n   * @default false\n   */\n  staticImport?: boolean,\n\n  /**\n   * Override `include` glob (relative to root).\n   *\n   * Defaults to `include` property of tsconfig.json (relative to tsconfig.json located).\n   */\n  include?: string | string[],\n\n  /**\n   * Override `exclude` glob.\n   *\n   * Defaults to `exclude` property of tsconfig.json or `'node_modules/**'` if not supplied.\n   */\n  exclude?: string | string[],\n\n  /**\n   * Whether to remove `import 'xxx'`.\n   *\n   * @default true\n   */\n  clearPureImport?: boolean,\n\n  /**\n   * Whether to generate types entry file(s).\n   *\n   * When `true`, uses package.json `types` property if it exists or `${outDir}/index.d.ts`.\n   *\n   * Value is forced to `true` when `rollupTypes` is `true`.\n   *\n   * @default false\n   */\n  insertTypesEntry?: boolean,\n\n  /**\n   * Rollup type declaration files after emitting them.\n   *\n   * Powered by `@microsoft/api-extractor` - time-intensive operation.\n   *\n   * @default false\n   */\n  rollupTypes?: boolean,\n\n  /**\n   * Bundled packages for `@microsoft/api-extractor`.\n   *\n   * @default []\n   * @see https://api-extractor.com/pages/configs/api-extractor_json/#bundledpackages\n   */\n  bundledPackages?: string[],\n\n  /**\n   * Override the config of `@microsoft/api-extractor`.\n   *\n   * @default null\n   * @see https://api-extractor.com/pages/setup/configure_api_report/\n   */\n  rollupConfig?: RollupConfig,\n\n  /**\n   * Override the invoke options of `@microsoft/api-extractor`.\n   *\n   * @default null\n   * @see https://api-extractor.com/pages/setup/invoking/#invoking-from-a-build-script\n   */\n  rollupOptions?: IExtractorInvokeOptions,\n\n  /**\n   * Whether to copy .d.ts source files to `outDir`.\n   *\n   * @default false\n   * @remarks Before 2.0, the default was `true`.\n   */\n  copyDtsFiles?: boolean,\n\n  /**\n   * Whether to emit declaration files only.\n   *\n   * When `true`, all the original outputs of vite (rollup) will be force removed.\n   *\n   * @default false\n   */\n  declarationOnly?: boolean,\n\n  /**\n   * Logging level for this plugin.\n   *\n   * Defaults to the 'logLevel' property of your Vite config.\n   */\n  logLevel?: LogLevel,\n\n  /**\n   * Hook called after diagnostic is emitted.\n   *\n   * According to the `diagnostics.length`, you can judge whether there is any type error.\n   *\n   * @default () =\u003e {}\n   */\n  afterDiagnostic?: (diagnostics: readonly ts.Diagnostic[]) =\u003e MaybePromise\u003cvoid\u003e,\n\n  /**\n   * Hook called prior to writing each declaration file.\n   *\n   * This allows you to transform the path or content.\n   *\n   * The file will be skipped when the return value `false` or `Promise\u003cfalse\u003e`.\n   *\n   * @default () =\u003e {}\n   */\n  beforeWriteFile?: (\n    filePath: string,\n    content: string\n  ) =\u003e MaybePromise\u003c\n    | void\n    | false\n    | {\n      filePath?: string,\n      content?: string\n    }\n  \u003e,\n\n  /**\n   * Hook called after rolling up declaration files.\n   *\n   * @default () =\u003e {}\n   */\n  afterRollup?: (result: ExtractorResult) =\u003e MaybePromise\u003cvoid\u003e,\n\n  /**\n   * Hook called after all declaration files are written.\n   *\n   * It will be received a map (path -\u003e content) that records those emitted files.\n   *\n   * @default () =\u003e {}\n   */\n  afterBuild?: (emittedFiles: Map\u003cstring, string\u003e) =\u003e MaybePromise\u003cvoid\u003e\n}\n```\n\n## Contributors\n\nThanks for all the contributions!\n\n\u003ca href=\"https://github.com/qmhc/vite-plugin-dts/graphs/contributors\"\u003e\n  \u003cimg src=\"https://contrib.rocks/image?repo=qmhc/vite-plugin-dts\" alt=\"contributors\" /\u003e\n\u003c/a\u003e\n\n## Example\n\nClone and run the following script:\n\n```sh\npnpm run test:ts\n```\n\nThen check `examples/ts/types`.\n\nAlso Vue and React cases under `examples`.\n\nA real project using this plugin: [Vexip UI](https://github.com/vexip-ui/vexip-ui).\n\n## License\n\nMIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqmhc%2Fvite-plugin-dts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqmhc%2Fvite-plugin-dts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqmhc%2Fvite-plugin-dts/lists"}