Gathering detailed insights and metrics for vite-plugin-svelte-svgr
Gathering detailed insights and metrics for vite-plugin-svelte-svgr
Gathering detailed insights and metrics for vite-plugin-svelte-svgr
Gathering detailed insights and metrics for vite-plugin-svelte-svgr
npm install vite-plugin-svelte-svgr
Typescript
Module System
Node Version
NPM Version
62
Supply Chain
89.1
Quality
75
Maintenance
100
Vulnerability
98.6
License
TypeScript (57.22%)
JavaScript (24.45%)
Svelte (12.9%)
HTML (3.15%)
Shell (2.29%)
Total Downloads
12,685
Last Day
6
Last Week
196
Last Month
661
Last Year
6,121
MIT License
2 Stars
45 Commits
1 Watchers
2 Branches
2 Contributors
Updated on Oct 24, 2024
Minified
Minified + Gzipped
Latest Version
1.0.4
Package Id
vite-plugin-svelte-svgr@1.0.4
Unpacked Size
182.49 kB
Size
36.12 kB
File Count
23
NPM Version
8.15.0
Node Version
16.17.0
Cumulative downloads
Total Downloads
Last Day
500%
6
Compared to previous day
Last Week
8.3%
196
Compared to previous week
Last Month
-23.9%
661
Compared to previous month
Last Year
15.3%
6,121
Compared to previous year
3
22
A Vite plugin which enables SVG import similar to what you may be accustomed to in React.
This plugin will preprocess SVG elements for SvelteKit that may then be easily imported into your project. Additionally the proper types for Typescript users have been provided.
Note
While this plugin or similar may be required for your needs I would encourage you to consider using a library such as Iconify if you are only interested in using Icons from icon sets.
See Instructions for Typescript
1pnpm i vite-plugin-svelte-svgr -D
1yarn add vite-plugin-svelte-svgr --dev
1npm install vite-plugin-svelte-svgr -D
Import the plugin and extend svelte.config.js
with an instance of the plugin containing your options as shown below.
Defined in vite.config.js
1import svgr from 'vite-plugin-svelte-svgr'; 2import { sveltekit } from '@sveltejs/kit/vite'; 3 4const config = { 5 // see https://github.com/svg/svgo for svgo plugin options. 6 plugins: [sveltekit(), svgr()] 7}; 8export default config;
Defined in svelte.config.js
1import svgr from 'vite-plugin-svelte-svgr'; 2import { sveltekit } from '@sveltejs/kit/vite'; 3 4/** @type {import('@sveltejs/kit').Config} */ 5const config = { 6 ..., 7 kit: { 8 ..., 9 vite: { 10 // see https://github.com/svg/svgo for svgo plugin options. 11 plugins: [sveltekit(), svgr()] 12 } 13 } 14}; 15export default config
It's not uncommon to have one group of svg icons that you use as urls or raw only, while others you may wish to use as a component. This can be achieved by defining your include/exclude parameters along with whatever SVGO options you wish to use.
Example specifying type with include
When no query is used the defined type below will be applied for any SVG in the included path.
1<script lang="ts"> 2 import SomeSvg from '$lib/icons/raw/some.svg' 3</script>
1// see above for imports excluded for brevity. 2const config = { 3 plugins: [ 4 sveltekit(), 5 svgr({ 6 type: 'raw', 7 // see https://github.com/rollup/plugins/tree/master/packages/pluginutils#createfilter 8 // for how to use root, include and exclude options. If you are familiar with Rollup's 9 // create filter it's exactly that utility here doing its magic!! 10 include: ['src/lib/icons/raw'] 11 }) 12 ] 13};
Note
If in the case above you imported as from '$lib/icons/raw/some.svg?component'
then the type designation in the configuration would be overridden a SvelteComponent SVG would be returned instead.
The following shows an example with customized SVGO options. It demonstrates how to override the default present as well as adding an attribute to all SVGs. In this case setting fill to "currentColor" which is a common use case causing the SVG to inherit or use the current css color property from the parent element. To make your SVG work like icons from iconsets setting this attribute or perhaps stroke
is likely what you're after!
1const svgoOptions = { 2 multipass: true, 3 plugins: [ 4 // Ensuring viewbox isn't removed. 5 { 6 name: 'preset-default', 7 params: { 8 overrides: { 9 removeViewBox: false 10 } 11 } 12 }, 13 { 14 // setting fill attribute to "currentColor" 15 name: 'addAttributesToSVGElement', 16 params: { 17 attributes: [{ fill: 'currentColor' }] 18 } 19 } 20 ] 21}; 22 23// showing config in `vite.config.js` for older 24// svelte kit configurations see above. 25const config = { 26 plugins: [ 27 sveltekit(), 28 svgr({ 29 svgo: svgoOptions 30 }) 31 ] 32}; 33 34export default config;
Import as Component:
Use the component as you would any Svelte component including passing props.
While you can import without using the ?component param typings may complain due to default exports being overridden. If Typescript complains just use the "./some/path/icon.svg?component"
instead of "./some/path/icon.svg"
1<script> 2 import Logo from "./logo.svg?component"; 3</script> 4 5<Logo width={200} />
Or perhaps a class when using Tailwind
1 <Logo class="w-5 h-5">
Url Import
1<script> 2 import logoUrl from "./logo.svg?url"; 3</script> 4 5<img src={logoUrl} />
Raw Import
1<script> 2 import logo from "./logo.svg?raw"; 3</script> 4 5{@html logo}
While not using Rollup we are underneath using a filtering tool created for Rollup. See reference for more information on how to use root
, include
and exclude
properties.
1interface Options { 2 /** 3 * The default output type for imported SVG. 4 * 5 * @default 'component' 6 */ 7 type?: SvgType; 8 9 /** 10 * The root path that below include/exclude scopes will be resolved from. 11 * If undefined process.cwd() is used and likely what you want. 12 * 13 * @default undefined 14 */ 15 root?: string; 16 17 /** 18 * The scopes/paths to be processed. If undefined all resolved SVG are processed. 19 * 20 * @default undefined 21 */ 22 include?: FilterPattern; 23 24 /** 25 * The scopes/paths to be excluded. If undefined no svg files will be unprocessed. 26 * 27 * @default undefined 28 */ 29 exclude?: FilterPattern; 30 31 /** 32 * Specify svgo options, leave undefined for defaults or false to disabled 33 * optimization. The plugin will also look for `svgo.config.js` if you prefer 34 * a configuration file. 35 * 36 * @see https://github.com/svg/svgo 37 * @default undefined 38 */ 39 svgo?: OptimizeOptions | boolean; 40}
You'll likely want to create a reference to the ambient modules types in your app.d.ts
(for Svelte Kit) or applicable. Typescript will likely complain about importing a path like./path/to/some.svg?component
;
Simply add the reference to our plugin and the error should go away. After updating you may need to close your editor or restart the Typescript server.
1/* app.d.ts or other global types file */ 2/// <reference types="vite-plugin-svelte-svgr" />
You can also reference in tsconfig.json
1{ 2 "compilerOptions": { 3 "types": ["vite-plugin-svelte-svgr"] 4 } 5}
To make Jest happy you'll need to adjust your jest.config.js
telling Jest how to handle .svg
components. There are a couple of ways of doing this. Here's one.
Install the jest-transform-stub
1module.exports = { 2 ... 3 moduleNameMapper: { 4 '^.+\\.svg$': 'jest-transform-stub', 5 } 6};
Another options is to create a Mock component then point the module mapper to the Mock component. This is similar to what you might do with React and createClass
.
Create Mock Component
Create an empty SVG element at src/lib/icons/Mock.svelte
1<svg />
Update Jest Config
Update module name mapper to reflect the above path in your package.json
or jest config file.
1module.exports = { 2 moduleNameMapper: { 3 '^.+\\.svg$': '<rootDir>/src/lib/icons/Mock.svelte' 4 } 5};
See https://blujedis.github.io/vite-plugin-svelte-svgr/
See LICENSE
No vulnerabilities found.
No security vulnerabilities found.