百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
V

vite-plugin-svgr

> 编程语言
开源

一个插件,用于将 SVG 转换为 React 组件

746 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

一个插件,用于将 SVG 转换为 React 组件

vite-plugin-svgr

一个插件,用于将 SVG 转换为 React 组件。在后台使用 svgr。

安装

bash
# npm
npm install --save-dev vite-plugin-svgr

# yarn
yarn add -D vite-plugin-svgr

# pnpm
pnpm add -D vite-plugin-svgr

使用方法

javascript
// vite.config.js
import svgr from "vite-plugin-svgr";

export default {
  // ...
  plugins: [svgr()],
};

然后,可以将 SVG 文件导入为 React 组件:

javascript
import Logo from "./logo.svg?react";

如果您使用 TypeScript,还有一个声明辅助工具,可用于更好地推断类型。请将以下内容添加到 vite-env.d.ts:

typescript
/// 

选项

javascript
svgr({
  // svgr options: https://react-svgr.com/docs/options/
  svgrOptions: {
    // ...
  },

  // esbuild options, to transform jsx to js
  esbuildOptions: {
    // ...
  },

  // oxc options, used by rolldown / vite 8+
  oxcOptions: {
    // ...
  },

  // A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should include.
  include: "**/*.svg?react",

  //  A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should ignore. By default no files are ignored.
  exclude: "",
});

如果您想启用 SVGO,可以安装 @svgr/plugin-svgo 并使用以下选项来启用和配置它:

javascript
svgr({
  svgrOptions: {
    plugins: ["@svgr/plugin-svgo", "@svgr/plugin-jsx"],
    svgoConfig: {
      floatPrecision: 2,
    },
  },
  // ...
});

许可证

麻省理工学院

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言