@gkatsanos/vite-svg-loader

中文文档
1.3.1发布于 2024/07/16MITTypeScriptCommonJS近 30 天下载 2916

A Vite loader to manage SVG files

原始使用文档
文档目录11 节
  1. Install
  2. Setup
  3. Import params
  4. URL
  5. Raw
  6. Component
  7. Default import config
  8. SVGO Configuration
  9. Disable SVGO
  10. Skip SVGO for a single file
  11. Use with TypeScript

This is a fork of https://github.com/jpkleemans/vite-svg-loader with the sole difference that it replaces component with inline to make it backwards compatible with components using the webpack svg loader.

Vite SVG loader

Vite plugin to load SVG files as Vue components, using SVGO for optimization.

Version Downloads Tests License

<template>
  <MyIcon />
</template>

<script setup>
import MyIcon from './my-icon.svg'
</script>

Install

npm install vite-svg-loader --save-dev

Setup

vite.config.js

import svgLoader from 'vite-svg-loader'

export default defineConfig({
  plugins: [vue(), svgLoader()]
})

Import params

URL

SVGs can be imported as URLs using the ?url suffix:

import iconUrl from './my-icon.svg?url'
// '/assets/my-icon.2d8efhg.svg'

Raw

SVGs can be imported as strings using the ?raw suffix:

import iconRaw from './my-icon.svg?raw'
// '<?xml version="1.0"?>...'

Component

SVGs can be explicitly imported as Vue components using the ?inline suffix:

import IconComponent from './my-icon.svg?inline'
// <IconComponent />

Default import config

When no explicit params are provided SVGs will be imported as Vue components by default. This can be changed using the defaultImport config setting, such that SVGs without params will be imported as URLs (or raw strings) instead.

vite.config.js

svgLoader({
  defaultImport: 'url' // or 'raw'
})

SVGO Configuration

vite.config.js

svgLoader({
  svgoConfig: {
    multipass: true
  }
})

Disable SVGO

vite.config.js

svgLoader({
  svgo: false
})

Skip SVGO for a single file

SVGO can be explicitly disabled for one file by adding the ?skipsvgo suffix:

import IconWithoutOptimizer from './my-icon.svg?skipsvgo'
// <IconWithoutOptimizer />

Use with TypeScript

If you use the loader in a Typescript project, you'll need to import your svg files with the ?inline param: import MyIcon from './my-icon.svg?inline'.

You'll also need to reference the type definitions:

/// <reference types="vite-svg-loader" />

版本与发布记录

@gkatsanos/vite-svg-loader 共发布 7 个版本,首次发布于 2023/05/04;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.3.1latest2024/07/162 年前
1.3.02023/05/233 年前
1.2.02023/05/053 年前
1.1.22023/05/043 年前
1.1.12023/05/043 年前
1.1.02023/05/043 年前

关于 @gkatsanos/vite-svg-loader 的常见问题

@gkatsanos/vite-svg-loader 是什么?

A Vite loader to manage SVG files

如何安装 @gkatsanos/vite-svg-loader?

在项目目录运行 npm install @gkatsanos/vite-svg-loader,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:vue(>=3.2.13)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@gkatsanos/vite-svg-loader 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@gkatsanos/vite-svg-loader 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@gkatsanos/vite-svg-loader 可以商用吗?

@gkatsanos/vite-svg-loader 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@gkatsanos/vite-svg-loader 的下载量有多少?

根据 npm 官方下载统计,@gkatsanos/vite-svg-loader 在 2026/10/17 – 2026/11/15 这 30 天内被下载 2,916 次,日均约 97 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@gkatsanos/vite-svg-loader 还在维护吗?

最新版本 1.3.1 发布于 2024/07/16(2 年前);至今共发布 7 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/05/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。