Skip to content

svgIcons(options?)

把 SVG 目录编译为独立 Vue TSX 组件及根索引。每个 SVG 生成到根索引所在目录的 <SVG 相对路径>/index.tsx,组件使用 defineComponent 和内联 SVG JSX,消费项目必须启用 Vue JSX/TSX 转换。

ts
svgIcons({
	dir: "src/assets/icons",
	output: "src/icons/index.ts",
	componentPrefix: "App",
	removeDimensions: true,
	defaultAttributes: { "aria-hidden": "true", focusable: false },
});
选项默认值说明
dir"src/assets/icons"SVG 源目录
output"src/icons/index.ts"图标根索引
deeptrue是否递归
componentPrefix""组件名前缀
componentSuffix""组件名后缀
include / name-文件过滤器 / 命名器
defaultAttributes{}默认根属性,源 SVG 同名属性优先
removeDimensionsfalse删除 width / height
debounce80开发监听防抖毫秒数

SVG 标记会直接写入生成的 TSX 源码。不要把用户上传或其他不可信 SVG 放入扫描目录。

根索引提供每个图标的命名导出,并通过 export default { ... } as const 直接默认导出只读组件对象,不生成中间 icons 变量。

svgIcons 图标生成

将 SVG 目录编译为独立的 Vue 图标组件与根索引。

生成组件通过 defineComponent 与内联 SVG JSX 渲染,消费项目需要启用 Vue JSX/TSX 转换。

签名

ts
export function svgIcons(options: SvgIconsPluginOptions = {}): Plugin;

示例

ts
import { svgIcons } from "fast-vite-plugins";

const plugin = svgIcons({ dir: "src/assets/icons", output: "src/icons/index.ts" });

export default { plugins: [plugin] };

输入

输入值输入值类型必填/默认值输入值说明
optionsSvgIconsPluginOptions否,默认 {}SVG 目录、输出文件、命名、过滤和根属性配置。

返回

返回值返回值类型返回值说明
pluginPlugin可直接加入 Vite plugins 的 SVG 图标生成插件。