This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

vite-plugin-vue-element-plug-icon
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

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

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

Package Sidebar

Install

npm i vite-plugin-vue-element-plug-icon

Weekly Downloads

4

Version

1.0.3

License

MIT

Unpacked Size

13.5 kB

Total Files

9

Last publish

Collaborators

  • tjyuanpeng