Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

vite-plugin-svg4vue

yisibell1.1kMIT4.5.0TypeScript support: included

A vite plugin which can transform svg icon to vue component, support optimization via SVGO, easy to customize svg color and size.

vite, vite-plugin, vue, svg, svgo, svg-component, svg-icon, icon, vite-svg-loader

readme


vite-plugin-svg4vue

A vite (3.x || 4.x) plugin which can transform SVG icon to vue (2.7.X || 3.x) component.

this plugin dependencies on vue/compiler-sfc, so keep your vue version to 3.2.13+ or 2.7.14+.

Features

  • SVGO optimization.
  • Hot Module Replacement support.
  • Support for ?url , ?component and ?raw query string.
  • Support custom svg icon (monochrome) color with fill , fill-opacity , stroke , stroke-opacity attribute.
  • Support change svg icon size with font-size and it will be responsive.
  • Support Iconfont SVG Icons.

Installation

# pnpm
$ pnpm add vite-plugin-svg4vue -D

# yarn
$ yarn add vite-plugin-svg4vue --dev

# npm
$ npm i vite-plugin-svg4vue -D

Usage

Setup

// vite.config.ts

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { svg4VuePlugin } from 'vite-plugin-svg4vue'

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

In Vue

<template>
  <div>
    <h2>SVG component: </h2>

    <!-- you can change the svg icon color with `fill` attribute when it's a monochrome icon -->
    <LogoSvg fill="red" />

    <!-- you can change the svg icon size with `font-size` or `width`. both of this will be responsive -->
    <LogoSvg font-size="48" />
    <LogoSvg width="48" />

    <h2>SVG url: </h2>

    <p>
      <img :src="logoSvgUrl" alt="" width="36" />
    </p>

    <h2>SVG raw: </h2>

    <p>
      <span v-html="logoSvgRaw"></span>
    </p>
  </div>
</template>

<script setup lang="ts">
import LogoSvg from '@/icons/logo.svg?component'
import logoSvgUrl from '@/icons/logo.svg?url'
import logoSvgRaw from '@/icons/logo.svg?raw'
</script>

Skip SVGO for a single file (Added in v2.8.1)

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

<template>
  <div class="d-flex align-center mb-16">
    <label class="mr-12"> ?component&skipsvgo: </label>
    <PPTSvg />
  </div>

  <div class="d-flex align-center">
    <label class="mr-12"> ?raw&skipsvgo: </label>
    <span v-html="PPTSvgRaw"></span>
  </div>
</template>

<script setup lang="ts">
import PPTSvg from '@/icons/ppt.svg?component&skipsvgo'
import PPTSvgRaw from '@/icons/ppt.svg?raw&skipsvgo'
</script>

Typescript

If you are using TypeScript, vite-plugin-svg4vue/client can be added to d.ts declaration file.

/// <reference types="vite-plugin-svg4vue/client" />

Options

Key Default value Description Type
svgoConfig {} SVGO config. if set to false, will disable SVGO. object/boolean
enableSvgoPresetDefaultConfig true Whether to enable preset-default configuration for SVGO boolean
defaultExport url Default behavior when importing .svg files, possible options are: url , component and raw string
assetsDirName icons Restrict SVG to a specific folder. As long as the SVG is in the assetsDirName folder, it can be processed by this plugin, Even if the folder is nested, such as, path/to/icons/*.svg, icons/path/to/svg/*.svg , path/to/icons/path/to/*.svg and so on. If set assetsDirname to false , will make it work with arbitrary file path. string/boolean
enableBuildCache true Whether to enable caching at build time boolean
enableMonochromeSvgOptimize true Whether to enable monochrome svg icon optimize which can move child node (named path, Even the path wrapped by g**) 's fill, fill-opacity and stroke, stroke-opacity attribute to its parent node (svg** element). So that you can change the svg icon color with fill and stroke. boolean
enableSvgSizeResponsive true Whether to enable svg icon responsive. boolean
namespaceClassnames true Whether to add a namespace for class selector. boolean
namespaceIDs true Whether to add a namespace for ID selector. boolean
namespacePrefix 'a' Whether to add a namespace prefix string for selector. string
enforce 'pre' Define this plugin invocation order. string or boolean

What enableSvgSizeResponsive do ?

In fact, for the svg node, vite-plugin-svg4vue will set the width value to font-size, remove svg height and set width to 1em, so that the svg size will be responsive and you can manipulate it's size with font-size.

Just in case, it records the original size of the svg as a css variables:

<svg style="--svg-origin-width: ${origin width}; --svg-origin-height: ${origin height};"></svg>

So, you can easily apply its original size.

<template>
  <LogoSvg fill="red" style="width: var(--svg-origin-width); height: var(--svg-origin-height)" />
</template>

<script setup lang="ts">
import LogoSvg from '@/icons/logo.svg?component'
</script>

Framework Support

changelog

CHANGE LOG

v4.5.0

compare changes

🚀 Enhancements

  • Support disabled enforce config via false (8df0184)

❤️ Contributors

v4.4.0

compare changes

🚀 Enhancements

  • Add plugin invocation order config via enforce, defaults is pre (e15ba6d)

❤️ Contributors

v4.3.0

compare changes

🚀 Enhancements

  • Use file id for selector namespace generation and add namespacePrefix opts (6d0554e)

❤️ Contributors

v4.2.0

compare changes

🚀 Enhancements

  • Add selector namespace for id and class (a2ff554)

🩹 Fixes

🏡 Chore

❤️ Contributors

v4.1.1

compare changes

🩹 Fixes

  • To prevent compileTemplate from removing the style tag (c0e2426)

❤️ Contributors

v4.1.0

compare changes

🚀 Enhancements

  • Support skip monochrome and responsive size (7afc7c5)

❤️ Contributors

v4.0.4

compare changes

🩹 Fixes

❤️ Contributors

v4.0.3

compare changes

🩹 Fixes

❤️ Contributors

v4.0.2

compare changes

v4.0.1

compare changes

🩹 Fixes

🏡 Chore

  • example: Update example build (b51c545)

❤️ Contributors

v4.0.0

compare changes

🚀 Enhancements

  • ⚠️ Support for vite v6 (50b6b8a)

⚠️ Breaking Changes

  • ⚠️ Support for vite v6 (50b6b8a)

❤️ Contributors

v3.2.0

compare changes

🚀 Enhancements

  • Upgrade vite peerDependencies version (5a79d53)

❤️ Contributors

v3.1.2

compare changes

v3.1.1

compare changes

🏡 Chore

  • example: Update glob example (54f3126)
  • Update example site and docs (a7ebdfd)

❤️ Contributors

v3.1.0

compare changes

🚀 Enhancements

🏡 Chore

❤️ Contributors

v3.0.0

compare changes

🚀 Enhancements

  • vite: ⚠️ Support for vite v5 (cbd1ee8)

⚠️ Breaking Changes

  • vite: ⚠️ Support for vite v5 (cbd1ee8)

❤️ Contributors

v2.17.1

compare changes

🩹 Fixes

  • Do not return config from hook (7590dab)
  • Use configResolved + config.mode (3d091e8)
  • Use resolved config + mode rather than modifying or mutating config (8219678)

❤️ Contributors

v2.17.0

compare changes

🚀 Enhancements

  • Update typescript to v5 and other non-major deps (042091d)

❤️ Contributors

v2.16.0

compare changes

🚀 Enhancements

  • Support for vite 4.4 vue 3.3 (6e56e2b)

🏡 Chore

❤️ Contributors

v2.15.0

compare changes

🚀 Enhancements

  • cache: Using id as build cache key instead of idWithoutQuery (ab30049)

🩹 Fixes

  • example: Example 404 error (93ecab6)

🏡 Chore

  • example: Update example (a394ec4)

❤️ Contributors

v2.14.0

compare changes

🚀 Enhancements

  • Upgrade deps svgo-extra to 2.1 (7ab6d12)

🏡 Chore

❤️ Contributors

v2.13.0

compare changes

🚀 Enhancements

❤️ Contributors

v2.12.0

compare changes

🚀 Enhancements

  • Support work with arbitrary file path via set assetsDirName to false (88a21c2)

🏡 Chore

  • example: Fix example build error (af8b7e2)

❤️ Contributors

v2.11.0

compare changes

🚀 Enhancements

  • Update svgo-extra to 2.0.0 (f8d2fa4)

❤️ Contributors

v2.10.0

compare changes

🚀 Enhancements

  • Update pkg to module type (1e84f47)

🏡 Chore

  • example: Update deploy branch (80f9ecf)

❤️ Contributors

v2.9.0

compare changes

🚀 Enhancements

🏡 Chore

❤️ Contributors

v2.8.2

🏡 Chore

  • docs: Update readme (920bb21)
  • Update example (e64775d)
  • docs: Update readme (8579dd2)

❤️ Contributors

v2.8.1

🩹 Fixes

  • GetSearchParams use URL error (6e87abb)

🏡 Chore

  • Fix example deps (533c650)

❤️ Contributors

v2.8.0

🚀 Enhancements

  • Support skipsvgo search param (e3bef55)
  • Support disabled svgo global (dd31b25)

❤️ Contributors

v2.7.0

🚀 Enhancements

  • Support switch svgo preset-default configuration (d6ac771)

🏡 Chore

  • docs: Update example (0697b1e)
  • docs: Update readme (160df31)
  • docs: Update readme (d39d5e9)

❤️ Contributors

v2.6.1

🏡 Chore

  • docs: Update readme (15949b4)

❤️ Contributors

v2.6.0

🚀 Enhancements

  • svgo-plugins: Move built-in svgo plugins into svgo-extra (6bbbda6)

🏡 Chore

  • docs: Update readme (43ad361)

❤️ Contributors

v2.5.1

🩹 Fixes

  • Add css vars error (7ccce52)

❤️ Contributors

v2.5.0

🚀 Enhancements

  • Add origin size with unit css vars (2dd2453)

🏡 Chore

  • docs: Update readme (9e274b2)
  • example: Update example (b402604)

❤️ Contributors

v2.4.0

🚀 Enhancements

  • Support modifying fill-opacity and stroke-opacity on the svg node (5252255)

🏡 Chore

  • example: Update example (585ae02)
  • docs: Update readme (3282e0c)
  • example: Fix example deploy error (dcad7b9)

❤️ Contributors

v2.3.1

🏡 Chore

  • docs: Update readme (ebcb3fa)

❤️ Contributors

v2.3.0

🚀 Enhancements

  • Support optimize svg stroke attribute so that you can change a outline svg color (ba88849)

❤️ Contributors

v2.2.2

🏡 Chore

  • example: Update example (fb8f68a)

❤️ Contributors

v2.2.1

🩹 Fixes

  • Raw svg compile error (7f437be)

❤️ Contributors

v2.2.0

🚀 Enhancements

  • Support raw svg optimize (70adae7)

❤️ Contributors

v2.1.6

📦 Build

  • Upgrade rollup config for load json file (c2c4ebc)

🏡 Chore

  • Update readme docs (bdfab83)

❤️ Contributors

2.1.0 (2022-12-21)

Upgrade

  1. upgrade svgo plugin definition.

Features

  1. Support change svg icon size with font-size and it will be responsive.

2.0.0 (2022-12-21)

Breaking changes

  1. As of @vue/compiler-sfc 3.2.13+, this package is included as a dependency of the main vue package and can be accessed as vue/compiler-sfc. This means you no longer need to explicitly install this package and ensure its version match that of vue's. Just use the main vue/compiler-sfc deep import instead.
  2. Change svgo default config.

Features

  1. Support change svg icon color when the svg is a monochrome svg icon.

1.2.0 (2022-12-20)

Features

  1. Support caching at build time.

1.1.1 (2022-12-20)

Bugfixs

  1. fix package.json files.

1.1.0 (2022-12-20)

Features

  1. Support assetsDirName to Limit the svg icon in a folder.
  2. Add declaration file for Vue app. client.d.ts.
  3. Change defaultExport to url.