Installations
npm install vite-plugin-solid-svg
Score
50.8
Supply Chain
89.1
Quality
77.5
Maintenance
100
Vulnerability
98.6
License
Releases
Unable to fetch releases
Developer
jfgodoy
Developer Guide
Module System
ESM
Min. Node Version
Typescript Support
Yes
Node Version
18.19.1
NPM Version
10.2.4
Statistics
79 Stars
66 Commits
10 Forks
2 Watching
3 Branches
8 Contributors
Updated on 22 Nov 2024
Languages
TypeScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
323,494
Last day
-20%
475
Compared to previous day
Last week
-11.3%
3,862
Compared to previous week
Last month
16.6%
18,503
Compared to previous month
Last year
44.3%
182,669
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
7
vite-plugin-solid-svg
Extend Vite with ability to use SVG files as Solid.js components.
Features:
- SVGO optimization
- Hot Module Replacement support
- Support for
?component-solid
query string - SSR
Currently supported Vite version:
4 or above
Install
1yarn add --dev vite-plugin-solid-svg 2 3pnpm i -D vite-plugin-solid-svg
Setup
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})
typescript
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
Options
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.
Usage
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><Icon1 /> 15 </div> 16 ); 17}; 18 19export default App;
Why
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.
Credits
This plugin is based on the work from the following projects:
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
4 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
Reason
Found 5/27 approved changesets -- score normalized to 1
Reason
0 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 10 are checked with a SAST tool
Score
3
/10
Last Scanned on 2024-11-25
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