Giter Club home page Giter Club logo

vite-plugin-svg4vue's Introduction


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+.

see Example and docs.

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

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>

CHANGE LOG

see CHANGE LOG.

Framework Support

vite-plugin-svg4vue's People

Contributors

yisibell avatar

Stargazers

Alien avatar Jessie Lee avatar Socheat Sok avatar Muh. Sukrillah avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.