svelte-svg-loader Svelte Themes

Svelte Svg Loader

Vite plugin to load and optimize SVG files as Svelte components

šŸ“¦ svelte-svg-loader

Vite plugin to load and optimize SVG files as Svelte components. Powered by SVGO for SVG optimization.


✨ Features

  • āœ… Import SVG files as native Svelte components
  • āœ… Optimized using SVGO
  • āœ… Pass standard SVG attributes as props (width, fill, etc.)
  • āœ… Seamless integration with SvelteKit and Vite

šŸš€ Installation

Using pnpm:

pnpm add -D svelte-svg-loader

šŸ”§ Usage

  1. Configure vite.config.ts
import { sveltekit } from "@sveltejs/kit/vite";
import { defineConfig } from "vite";
import { svelteSvgLoader } from "svelte-svg-loader";

export default defineConfig({
  plugins: [sveltekit(), svelteSvgLoader()],
});
  1. Import SVG as a Component You can now import SVGs with the ?component query:
<script>
  import Icon from './assets/icon.svg?component';
</script>
<Icon />
  1. Pass Props as SVG Attributes All standard SVG attributes can be passed as props:
<script>
  import Icon from './assets/icon.svg?component';
</script>

<Icon width="24px" height="24px" fill="red" />

šŸ“ File Naming

SVGs must be imported with the ?component query to be treated as Svelte components.

āœ… Valid:

import Icon from "./logo.svg?component";

āŒ Invalid:

import Icon from "./logo.svg"; // will NOT work as a component

šŸ’¬ Contributing

Contributions are welcome! Feel free to open an issue or submit a pull request.

Top categories

Loading Svelte Themes