Vite plugin to load and optimize SVG files as Svelte components. Powered by SVGO for SVG optimization.
width, fill, etc.)Using pnpm:
pnpm add -D svelte-svg-loader
import { sveltekit } from "@sveltejs/kit/vite";
import { defineConfig } from "vite";
import { svelteSvgLoader } from "svelte-svg-loader";
export default defineConfig({
plugins: [sveltekit(), svelteSvgLoader()],
});
<script>
import Icon from './assets/icon.svg?component';
</script>
<Icon />
<script>
import Icon from './assets/icon.svg?component';
</script>
<Icon width="24px" height="24px" fill="red" />
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
Contributions are welcome! Feel free to open an issue or submit a pull request.