Thanks to visit codestin.com
Credit goes to github.com

Skip to content

unplugin-dts No Longer Support Custom File Format #444

@Kynson

Description

@Kynson

Describe the bug

Aim

Generate types for format beyond .svelte, .vue, .json.

Details

Sometimes I import custom file formats (e.g. .data, .grammar) and vite will transform it accordingly with the proper plugin. I would also like to generate declarations for these format when the transformation result is exported. This used to work for vite-plugin-dts by using a custom resolver for that format and output the correct declaration. Yet, unplugin-dts limits files it should process to TS, JS, Svelte, Vue and JSON (see here), therefore, file formats beyond the specified values are ignored and my custom resolver not longer works.

Example

// Assume there is a vite plugin that transform the grammar file to JS.
import { parser } from './syntax.grammar';
export { parser }

Custom Resolver:

{
  name: 'grammar-resolver',
  supports: (id) => {
    return id.endsWith('.grammar');
  },
  transform({ id, root }) {
    return [
      {
        path: relative(root, `${id}.d.ts`),
       // Return the correct declaration here
        content: `...`,
      },
    ];
  },
}

The above yields the syntax.grammar.d.ts in the output directory when using vite-plugin-dts (v4.5.4), but not unplugin-dts (v1.0.0-beta.6)

Side note: I am not sure if this is an intended use case, if there is a better solution to this, please tell me thanks.

Reproduction

https://stackblitz.com/edit/vitejs-vite-9egymozi?file=vite.config.js

Steps to reproduce

Simple run npm run build in the reproduction repo, syntax.grammar.d.ts should be generated (used to resolve types for "./syntax.grammar") when using vite-plugin-dts, but not when using unplugin-dts.

System Info

System:
    OS: macOS 15.6
    CPU: (16) arm64 Apple M4 Max
    Memory: 17.46 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.18.0 - /usr/local/bin/node
    npm: 10.9.3 - /usr/local/bin/npm
    pnpm: 10.14.0 - /etc/profiles/per-user/kynsonszetau/bin/pnpm
  Browsers:
    Chrome: 139.0.7258.139
    Safari: 18.6
  npmPackages:
    unplugin-dts: 1.0.0-beta.6 => 1.0.0-beta.6 
    vite: ^7.0.0 => 7.1.3

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingNeed further confirmation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions