vbenjs / vite-plugin-svg-icons

Vite Plugin for fast creating SVG sprites.
MIT License
801 stars 109 forks source link

feat: support `.d.ts` file generation #64

Closed innocenzi closed 1 year ago

innocenzi commented 2 years ago

Fixes https://github.com/vbenjs/vite-plugin-svg-icons/issues/22

This PR adds a few quality of life features as well a .d.ts file generation.

.d.ts file generation

This feature is inspired by unplugin-auto-import and other plugins like that. To provide TypeScript support, they generate .d.ts files which contain generated declarations.

This PR does the same by creating a (configurable) generated type that can be used wherever. .d.ts generation is disabled by default, but can be enabled by setting dts: true. Optionally, it can be configured, as shown in the docs.

export interface DtsOptions {
    /**
     * Name of the generated namespace, or false to use the global namespace.
     * @default false
     */
    namespace?: string | false
    /**
     * Name of the generated type.
     * @default "SvgIcons"
     */
    type?: string
    /**
     * Name of the generated declaration file.
     * @default "icons.d.ts"
     */
    filename?: string
    /**
     * Custom function to generate individual icon name in the generated type.
     */
    symbol?: (name: string) => string
}

Quality of life

First, iconDirs is no longer required to be an array of string. It can now be a single string, for people who only have one icon directory.

Second, iconDirs now properly support relative icon directories. Previously, we were forced to use path.resolve(process.cwd(), 'directory'). Now we can just use 'directory'. Both will work.

Third, a default export has been added. Previously we needed to use the named createSvgIconsPlugin export, but now this is exported by default to be consistent with the rest of the ecosystem

I also wanted to tweak the symbolId generation because I found it to be weird to work with but that started to be way out of scope for this PR.