generated fromunplugin/unplugin-starter
- Notifications
You must be signed in to change notification settings - Fork9
generate a vue/react component through svg files, supporting svg file HMR, Tree-shaking, SSR and TS hint.
License
NotificationsYou must be signed in to change notification settings
Jevon617/unplugin-svg-component
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
English |中文
unplugin-svg-component inspired byvite-plugin-svg-icons, it will generate a Vue/React component through SVG files, use the SVG icon through the component.
- 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 sincev0.5.0.
- SSR: Support SSR by enabling the
option.domInsertionStrategy = replaceHtml
(default) sincev0.6.0.
yarn add unplugin-svg-component -D# ornpm i unplugin-svg-component -D# orpnpm install unplugin-svg-component -D
Vite config
// vite.config.tsimport{defineConfig}from'vite'importUnpluginSvgComponentfrom'unplugin-svg-component/vite'exportdefaultdefineConfig({plugins:[UnpluginSvgComponent({/* options */}),],})
Vue-cli config
// vue.config.jsconst{ defineConfig}=require('@vue/cli-service')constUnpluginSvgComponent=require('unplugin-svg-component/webpack').defaultmodule.exports=defineConfig({configureWebpack:{plugins:[UnpluginSvgComponent({/* options */})]}})
Webpack config
// webpack.config.jsconstUnpluginSvgComponent=require('unplugin-svg-component/webpack').defaultmodule.exports={/* ... */plugins:[UnpluginSvgComponent({/* options */}),],}
Rollup config
// rollup.config.jsimportUnpluginSvgComponentfrom'unplugin-svg-component/rollup'exportdefault{plugins:[UnpluginSvgComponent({/* options */}),],}
Esbuild config
// esbuild.config.jsimport{build}from'esbuild'importUnpluginSvgComponentfrom'unplugin-svg-component/esbuild'build({/* ... */plugins:[UnpluginSvgComponent({/* options */}),],})
// main.tsimportSvgIconfrom'~virtual/svg-component'app.component(SvgIcon.name,SvgIcon)
// App.tsximportSvgIconfrom'~virtual/svg-component'functionApp(){return(<divclassName="logo"><SvgIconname="icon-react"></SvgIcon></div>)}
// all svg icon nameimport{svgNames}from'~virtual/svg-component'// type SvgName = "icon-icon-addUser" | "icon-icon-barCode"importtype{SvgName}from'~virtual/svg-component'
Parameter | Type | Default | Description |
---|---|---|---|
iconDir | string | string[] | - | the icon folder of the Sprite image |
projectType | vue | react | auto | auto | the project type, detect automatically |
dts | boolean | - | whether generate d.ts file |
dtsDir | string | - | d.ts location |
prefix | string | - | symbolId prefix |
componentName | string | SvgIcon | the name of component |
componentStyle | string | width: 1em; height: 1em; fill:currentColor; | the style of component |
preserveColor | RegExp | - | 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)=>string | look this way | the symbolId's formatter |
optimizeOptions | SvgoOptions | - | svgo optimizeOptions |
svgSpriteDomId | string | svg_sprite__dom | Customize the ID of the svgDom |
vueVersion | 2 | 3 | auto | auto | Vue version |
treeShaking | boolean | true | whether enable tree-shaking |
scanGlob | string[] | look this way | the glob pattern used in tree-shaking |
scanStrategy | text | component | (code: string[], options: Options)=>string[] | component | the strategy used for tree-shaking will not be bundled into the final bundle if it is not successfully matched. Thetext option indicates matching by svg name, so you should ensure the uniqueness of your svg name (you can consider customizing it with the 'symbolIdFormatter' option) to avoid erroneous tree-shaking, while the default value of thecomponent option indicates matching by component as a whole, In addition, you can also implement a tree-shaking strategy by passing a function whose return value represents the set of SVG icons used. |
domInsertionStrategy | replaceHtml | dynamic | replaceHtml | controls the method of injecting SVG elements,replaceHtml(default) : Injects the SVG elements by replacing the HTML string in server.dynamic : Injects the SVG elements through JavaScript dynamically in client.Warning: if you are in ssr mode, you should usereplaceHtml strategy. |
// tsconfig.json{"include": ["svg-component.d.ts","svg-component-global.d.ts"]}
You can refer toexamples as required. Note that the configuration in examples is the default configuration.
- Fork it (https://github.com/Jevon617/unplugin-svg-component/fork)
- Create your feature branch (
git checkout -b feature/fooBar
) - Commit your changes (
git commit -am 'Add some fooBar'
) - Push to the branch (
git push origin feature/fooBar
) - Create a new Pull Request
MIT License © 2022-PRESENTJevon617
About
generate a vue/react component through svg files, supporting svg file HMR, Tree-shaking, SSR and TS hint.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
No packages published
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.