Catalog npm
PACKAGE EXPLORERView in registry

@0x-jerry/unplugin-svg-component

npm

generate a vue/react component through svg files, supporting svg file HMR and typescript intelligence prompt.

rollupsvgsvg-spritetransformunpluginvitewebpack
Versions collected1
Direct dependencies23From release 0.5.1
Last observed20 Oct, 22:16 UTC

README

Current package documentation

unplugin-svg-component

English | 中文

NPM Version Downloads Stats

unplugin-svg-component inspired by vite-plugin-svg-icons, it will generate a Vue/React component through SVG files, use the SVG icon through the component.

IDE Intelligent

Vue

image

React

image

Features

  • intelligent: When using components, the name of the SVG file will be prompted with Typescript
  • HMR: HMR for svg file
  • Vue & React: Support Vue&React and automatically detect project types.
  • Tree-shaking: Only bundle the icons you really use since v0.5.0.

Installation

yarn add unplugin-svg-component -D
# or
npm i unplugin-svg-component -D
# or
pnpm install unplugin-svg-component -D

Usage

Vite config
// vite.config.ts
import { defineConfig } from 'vite'
import UnpluginSvgComponent from 'unplugin-svg-component/vite'
export default defineConfig({
  plugins: [
    UnpluginSvgComponent({ /* options */ }),
  ],
})


Vue-cli config
// vue.config.js
const { defineConfig } = require('@vue/cli-service')
const UnpluginSvgComponent = require('unplugin-svg-component/webpack').default

module.exports = defineConfig({
  configureWebpack: {
    plugins: [
      UnpluginSvgComponent({ /* options */ })
    ]
  }
})


Webpack config
// webpack.config.js
const UnpluginSvgComponent = require('unplugin-svg-component/webpack').default

module.exports = {
  /* ... */
  plugins: [
    UnpluginSvgComponent({ /* options */ }),
  ],
}


Rollup config
// rollup.config.js
import UnpluginSvgComponent from 'unplugin-svg-component/rollup'

export default {
  plugins: [
    UnpluginSvgComponent({ /* options */ }),
  ],
}


Esbuild config
// esbuild.config.js
import { build } from 'esbuild'
import UnpluginSvgComponent from 'unplugin-svg-component/esbuild'

build({
  /* ... */
  plugins: [
    UnpluginSvgComponent({
      /* options */
    }),
  ],
})


Vue

// main.ts
import SvgIcon from '~virtual/svg-component'
app.component(SvgIcon.name, SvgIcon)

React

// App.tsx
import SvgIcon from '~virtual/svg-component'

function App() {
  return (
    <div className="logo">
      <SvgIcon name='icon-react'></SvgIcon>
    </div>
  )
}

Plugin Configuration

ParameterTypeDefaultDescription
iconDirstring-the icon folder of the Sprite image
projectTypevue | react | autoautothe project type, detect automatically
dtsboolean-whether generate d.ts file
dtsDirstring-d.ts location
prefixstring-symbolId prefix
componentNamestringSvgIconthe name of component
componentStylestringwidth: 1em; height: 1em; fill:currentColor;the style of component
preserveColorRegExp-usually, the plugin will set SVG's fill and stroke with 'currentColor', this option will perform regular matching on each SVG path, the successfully matched SVG will not do the replacement, but retain the original color.
symbolIdFormatter(svgName:string, prefix: string)=>stringlook this waythe symbolId's formatter
optimizeOptionsSvgoOptions-svgo optimize Options
svgSpriteDomIdstringsvg_sprite__domCustomize the ID of the svgDom
vueVersion2 | 3 | autoautoVue version
scanGlobstring[]look this waythe glob pattern used in tree-shaking

Typescript support

// tsconfig.json
{
  "include": ["svg-component.d.ts", "svg-component-global.d.ts"]
}

Best Practices

You can refer to examples as required. Note that the configuration in examples is the default configuration.

Contributing

  1. Fork it (https://github.com/Jevon617/unplugin-svg-component/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

License

MIT License © 2022-PRESENT Jevon617

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Versions 1

Collected releases
VersionPublishedDirect dependenciesOpen version
0.5.1Registry latest17 Mar 202323

Package advisories

0
No matching advisories collected

This describes our collected data; it is not a security assessment.

Repository

PENDING
jevon617/unplugin-svg-component

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 22:16 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package:6ef39616bfb856c6a54b2ad535ecabbb2daae2e2959f98470358fc9e22704a1b