🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

vite-plugin-vue-element-plug-icon

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vite-plugin-vue-element-plug-icon

a vite plugin to load svg icon for element-plug

latest
Source
npmnpm
Version
1.0.3
Version published
Maintainers
1
Created
Source

vite-plugin-vue-element-plug-icon

a vite plugin to load svg icon for element-plug

Why

Importing a svg file, you would get a url of file in vite

In most cases, we want a Vue component to render in page directly.

In element-plus, it supply a el-icon Element, and a built-in Svg resource library

Use your svg file, it`s not render properly, such as not supporting color attribute

So I create thie vite plugin to svg file, import as vue component, and optimize using for element-plug icon

Relative Resourece

  • element-plus icon
  • svgo
  • vite
  • vue

Requirements

  • vite: ^3.0.0
  • vue: ^3.2.13

Install

// pnpm
pnpm add vite-plugin-vue-element-plug-icon -D

// npm
npm install vite-plugin-vue-element-plug-icon -D

// yarn
yarn add vite-plugin-vue-element-plug-icon --D

Setup

vite.config.ts

import vpvepi from 'vite-plugin-vue-element-plug-icon'

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

Use

<script setup lang="ts">
import Coffee from './coffee.svg'
import CoffeeRaw from './coffee.svg?raw'
import CoffeeUrl from './coffee.svg?url'
</script>
<template>
  <div>
    <!-- used in element-plus, support color, size attrs and etc -->
    <el-icon color="red" class="is-loading" :size="100">
      <Coffee />
    </el-icon>
  </div>
  <div>
    <CoffeeRaw />
  </div>
  <div>
    {{ CoffeeUrl }}
  </div>
</template>

Query Options

component

default value

svg file will be imported as Vue component, with optimized for element-plus

import CoffeeComp from './coffee.svg?component'
// <CoffeeComp />

raw

svg file will be imported as Vue component, without optimized for element-plus

import CoffeeRaw from './coffee.svg?raw'
// <CoffeeRaw />

url

svg file will be imported as file path

import CoffeeUrl from './coffee.svg?url'
// {{ CoffeeUrl }}

Plugin Options

defaultQuery

import vpvepi from 'vite-plugin-vue-element-plug-icon'

export default defineConfig({
  plugins: [
    vpvepi({
      defaultQuery: 'raw', // component(default value), url, raw
    }),
  ],
})

svgoConfig

import vpvepi from 'vite-plugin-vue-element-plug-icon'

export default defineConfig({
  plugins: [
    vpvepi({
      svgoConfig(path) {
        // svgo config options
        return {
          multipass: true,
        }
      },
    }),
  ],
})

use getDefaultSvgoOptions to get default svgo options, you can merge options from the return value

import vpvepi, { getDefaultSvgoOptions } from 'vite-plugin-vue-element-plug-icon'

export default defineConfig({
  plugins: [
    vpvepi({
      svgoConfig(path) {
        // svgo config options
        const ops = getDefaultSvgoOptions(path)
        return {
          ...ops,
          multipass: false,
        }
      },
    }),
  ],
})

Svgo Plugin

you can use svg plugin svgo-plugin-replace-fill separately

it would replace fill attr directly

import sprf from 'vite-plugin-vue-element-plug-icon/dist/svgo-plugin-replace-fill.cjs'

vpvepi({
  svgoConfig() {
    return {
      plugins: [sprf],
    }
  },
}),

Inspired by vite-svg-loader

vite-svg-loader

Keywords

vite

FAQs

Package last updated on 10 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts