Gathering detailed insights and metrics for vite-plugin-solid-svg2
Gathering detailed insights and metrics for vite-plugin-solid-svg2
Gathering detailed insights and metrics for vite-plugin-solid-svg2
Gathering detailed insights and metrics for vite-plugin-solid-svg2
Vite plugin to Import SVG files as Solid.js Components
npm install vite-plugin-solid-svg2
Typescript
Module System
Node Version
NPM Version
41.4
Supply Chain
88.1
Quality
74.4
Maintenance
100
Vulnerability
98.6
License
TypeScript (81.86%)
JavaScript (18.14%)
Total Downloads
173
Last Day
1
Last Week
1
Last Month
3
Last Year
47
55 Commits
1 Branches
1 Contributors
Latest Version
1.0.0
Package Id
vite-plugin-solid-svg2@1.0.0
Unpacked Size
12.55 kB
Size
4.29 kB
File Count
8
NPM Version
8.19.2
Node Version
18.12.1
Publised On
25 Jul 2023
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
0%
1
Compared to previous week
Last month
-25%
3
Compared to previous month
Last year
-62.7%
47
Compared to previous year
2
Extend Vite with ability to use SVG files as Solid.js components.
This package is exactly the same as the original vite-plugin-solid-svg with a little fix for the build process
### Features:?component-solid
query string2.4.4 or above
1yarn add --dev vite-plugin-solid-svg 2 3pnpm i -D vite-plugin-solid-svg
1// vite.config.js 2import solidPlugin from 'vite-plugin-solid' 3import solidSvg from 'vite-plugin-solid-svg' 4import { defineConfig } from 'vite' 5 6export default defineConfig({ 7 plugins: [solidPlugin(), solidSvg()], 8})
vite adds its own definition for "*.svg"
and defines them as string
. As far as I know this cannot be overridden.
So we have two options: put our types before those of vite, or use imports with querystring.
If you are using defaultAsComponent
which is the default, you need to put our types definition before vite in the tsconfig.
1// tsconfig.json 2"compilerOptions": { 3 "types": [ 4 "vite-plugin-solid-svg/types-component-solid" 5 "vite/client", 6 ], 7},
if you change to defaultAsComponent=false
, you should use a different type definition that only identifies an svg import as a solid component when it matches the querystring. And in this case, put it before "vite/client"
1// tsconfig.json 2"compilerOptions": { 3 "types": [ 4 "vite-plugin-solid-svg/types", 5 "vite/client" 6 ], 7},
1import MyIcon from './my-icon.svg' // <-- this will match vite module definition, and therefore identified as string 2import MyIcon from './my-icon.svg?component-solid' // <-- this will match the definition in this plugin, and therefore identified as Solid Component
1SolidSvg({
2 /**
3 * If true, will export as JSX component if `as` isn't specified.
4 *
5 * Otherwise will export as url, or as JSX component if '?as=component-solid'
6 *
7 */
8 defaultAsComponent: true,
9
10 svgo: {
11 enabled: true, // optional, by default is true
12 svgoConfig: <svgo config> // optional, if defined, the file svgo.config.js is not loaded.
13 }
14})
If you need to configure svgo
, you can also create a config file svgo.config.js
in the project's root, following the instructions at svgo docs. The svgo.svgoConfig
has precedence over the file.
Import as a Solid.js component:
1import MyIcon from './my-icon.svg?component-solid' 2// or './my-icon.svg' if `defaultAsComponent` is `true` 3import MyIcon from './my-icon.svg' 4 5const App = () => { 6 return ( 7 <div> 8 <h1> Title </h1> 9 <MyIcon /> 10 </div> 11 ) 12} 13 14export default App
To import all svg inside a folder, use import.meta.glob('@/svgs/*.svg', { as: 'component-solid' })
. See Vite docs for more details.
1const icons = import.meta.glob('./*.svg', { as: 'component-solid' }) 2 3/* 4 icons = { 5 icon1: () => import("./icon1.svg"), 6 icon2: () => import("./icon2.svg") 7 } 8*/ 9 10const App = () => { 11 const Icon1 = lazy(() => iconsDic.icon1()) 12 return ( 13 <div> 14 <p>hello</p> 15 <Icon1 /> 16 </div> 17 ) 18} 19 20export default App
In a Solidjs + vite project, you can easily add an svg image using:
1import iconUrl from './icon.svg' 2 3const App = () => { 4 return ( 5 <div> 6 <img href={iconUrl}> 7 </div> 8 ) 9} 10
However the fill color of the image cannot be overriden. Importing the svg as a component solves this and allows css styles to cascade into the svg content. Furthermore there may be situations where you'll need to ensure the image has been fully loaded, for example, before starting an animation. This module gives you a component that you can control when it's loaded.
This plugin is based on the work from the following projects:
No vulnerabilities found.
No security vulnerabilities found.