Gathering detailed insights and metrics for vite-plugin-magical-svg
Gathering detailed insights and metrics for vite-plugin-magical-svg
Gathering detailed insights and metrics for vite-plugin-magical-svg
Gathering detailed insights and metrics for vite-plugin-magical-svg
An all-in-one Vite plugin that magically makes working with SVGs and bundling them a breeze.
npm install vite-plugin-magical-svg
Typescript
Module System
Node Version
NPM Version
65.7
Supply Chain
89.8
Quality
83.2
Maintenance
100
Vulnerability
97.9
License
TypeScript (81.04%)
JavaScript (18.96%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
BSD-3-Clause License
63 Stars
53 Commits
6 Forks
4 Watchers
1 Branches
2 Contributors
Updated on Jul 15, 2025
Latest Version
1.7.2
Package Id
vite-plugin-magical-svg@1.7.2
Unpacked Size
61.86 kB
Size
13.45 kB
File Count
19
NPM Version
10.9.2
Node Version
22.15.0
Published on
Jun 08, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
3
1
An all-in-one Vite plugin that magically makes working with SVGs and bundling them a breeze.
This plugin lets you import SVG files as components, with advanced pre-processing of assets referenced within SVGs, without the inefficient approach used by other tools. SVGs will be bundled in a spritesheet that is fast and lightweight for the browser. See the backstory for more details.
[!TIP] Can mostly be used as a drop-in replacement of SVGR and similar tools.
Inspired by a tweet from Preact's creator Jason Miller, I've been looking at plugins that would let me work with SVGs, as I myself did the error of embedding SVGs as React components. Shame!
What I wanted was a plugin that would let me import SVGs, and make a sprite of symbols and give me the identifier I
can use in <use href='???'/>
. And I couldn't find any decent plugin that makes working with them easy. They all had
a problem that made using them a pain, or outright impractical. Here's a list of the problems I encountered:
<image href='...'/>
would never get processed and the referenced asset is ignored.So I decided to make my own tool to solve all these problems. Introducing: the Magical SVG plugin. 🪄
pnpm i vite-plugin-magical-svg
yarn add vite-plugin-magical-svg
npm i vite-plugin-magical-svg
[!NOTE] During development, to keep HMR fast and efficient, the sprite is not produced and instead the plugin falls back to embedding the symbol inside the generated component.
1import { defineConfig } from 'vite' 2import magicalSvg from 'vite-plugin-magical-svg' 3 4export default defineConfig({ 5 plugins: [ 6 magicalSvg({ 7 // By default, the output will be a dom element (the <svg> you can use inside the webpage). 8 // You can also change the output to react (or any supported target) to get a component you can use. 9 target: 'preact', 10 11 // By default, the svgs are optimized with svgo. You can disable this by setting this to false. 12 svgo: false, 13 14 // By default, width and height set on SVGs are not preserved. 15 // Set to true to preserve `width` and `height` on the generated SVG. 16 preserveWidthHeight: false, 17 18 // *Experimental* - set the width and height on generated SVGs. 19 // If used with `preserveWidthHeight`, will only apply to SVGs without a width/height. 20 setWidthHeight: { width: '24', height: '24' }, 21 22 // *Experimental* - replace all instances of `fill="..."` and `stroke="..."`. 23 // Set to `true` for 'currentColor`, or use a text value to set it to this value. 24 // When enabled, use query param ?skip-recolor to not alter colors. 25 // Disabled by default. 26 setFillStrokeColor: true, 27 28 // *Experimental* - if a SVG comes with `width` and `height` set but no `viewBox`, 29 // assume the viewbox is `0 0 {width} {height}` and add it to the SVG. 30 // Disabled by default. 31 restoreMissingViewBox: true, 32 }) 33 ] 34})
dom
(default): exports a function you can call (takes no arguments) and returns a DOM element.react19
: exports a functional React component (classic runtime)react19-jsx
: exports a functional React component (automatic runtime)react
: exports a functional React component (classic runtime; wrapped in forwardRef
)react-jsx
: exports a functional React component (automatic runtime; wrapped in forwardRef
)preact
: exports a functional Preact component (classic runtime; wrapped in forwardRef
)preact-jsx
: exports a functional Preact component (automatic runtime; wrapped in forwardRef
)vue
: exports a Vue component (as if it was a .vue
file)solid
: exports a Solid component1import MySvg from './assets/icon.svg' // Basic import, as a sprite 2import MySvg from './assets/icon.svg?sprite=owo' // Named sprites 3import MySvg from './assets/icon.svg?sprite=inline' // Special sprite, inlined in the HTML document 4import fileUrl from './assets/icon.svg?file' // Works like .png and other file imports
1<svg viewBox='0 0 250 250'> 2 <image href='./assets/image.png' /> <!-- Image will be imported, bundled, and the href will be replaced --> 3 <image href='./assets/icon.svg' /> <!-- SVG will be imported as a file (implicit ?file) --> 4 <use href='./assets/icon.svg' /> <!-- SVG will be imported and added to the sprite --> 5</svg>
If you import an icon but end up not using it, the icon will not be included in the final sprites. If tree-shaking has been disabled in your config, then all imported icons will be included regardless if they've been used or not.
exports
noteThis plugin does not respect the exports
field when importing svg files from third-party packages such as
simple-icons
, which do not expose them.
This plugin can mostly be used as a drop-in replacement of SVGR; unless you're dealing with complex SVGs or need to style/animate individual parts of the SVG, the code generated by this plugin should behave just as you're used to, plus the preprocessing of your SVGs.
[!IMPORTANT] Make sure to configure the
target
, so the plugin produces the correct type of component.
Make sure to enable preserveWidthHeight
for best compatibility. This option is not enabled by default as it was not
done historically by the plugin, and suddenly doing it would be a breaking change.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
all dependencies are pinned
Details
Reason
license file detected
Details
Reason
packaging workflow detected
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
7 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 6
Reason
Found 2/30 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
12 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-14
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More