Gathering detailed insights and metrics for vite-plugin-solid-svg
Gathering detailed insights and metrics for vite-plugin-solid-svg
Gathering detailed insights and metrics for vite-plugin-solid-svg
Gathering detailed insights and metrics for vite-plugin-solid-svg
npm install vite-plugin-solid-svg
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
79 Stars
66 Commits
10 Forks
2 Watching
3 Branches
8 Contributors
Updated on 22 Nov 2024
TypeScript (100%)
Cumulative downloads
Total Downloads
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
1
7
Extend Vite with ability to use SVG files as Solid.js components.
?component-solid
query string4 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><Icon1 /> 15 </div> 16 ); 17}; 18 19export 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.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
4 existing vulnerabilities detected
Details
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
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
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