Auto-Import SVG Icons in SvelteKit with Unplugin Icons
How to auto-import SVG icons on demand in a SvelteKit app using Unplugin Icons, with full setup and usage examples.
How to auto-import SVG icons on demand in a SvelteKit app using Unplugin Icons, with full setup and usage examples.
In this guide, you will learn how to use Unplugin Icons in a SvelteKit application. You will go through the process of setting up a new SvelteKit project, configuring Unplugin Icons, and using them to leverage a huge collection of icons in your application.
High Quality Starter Kits with built-in authentication flow (Auth.js), object uploads (AWS, Clouflare R2, Firebase Storage, Supabase Storage), integrated payments (Stripe, LemonSqueezy), email verification flow (Resend, Postmark, Sendgrid), and much more. Compatible with any database (Redis, Postgres, MongoDB, SQLite, Firestore).
Get all 3 kits$147$99One-time license · Lifetime updates
You’ll need the following:
Let’s get started by creating a new SvelteKit project. Open your terminal and run the following command:
npm create svelte@latest my-appnpm create svelte is the recommended way to scaffold a SvelteKit project quickly.
When prompted, choose:
SvelteKit demo app when prompted to select the app template.Yes, using TypeScript syntax when prompted to add type checking with TypeScript.Add Prettier for code formatting when prompted to select additional options.Once that’s done, you can move into the project directory, install the dependencies and start the app:
cd my-appnpm installnpm run devThe app should be running on localhost:5173. Now, let’s close the development server to install Unplugin Icons.
Execute the commands below to install the necessary packages for using Unplugin Icons with SvelteKit:
# unplugin-icons package for Webpack compilernpm i -D unplugin-icons
# @iconify/json package for the icons collectionnpm i -D @iconify/jsonThe command installs the following libraries:
unplugin-icons: A package to access thousands of icons as components on-demand universally.@iconify/json: A big collection of open source vector icons, all validated, cleaned up and converted to the same easy to use format.Further, perform the following additions in vite.config.ts to use the Unplugin Icons integration:
import { defineConfig } from 'vite'import Icons from 'unplugin-icons/vite' // [!code ++] // [!code focus]import { sveltekit } from '@sveltejs/kit/vite'
export default defineConfig({ plugins: [ sveltekit(), Icons({ // [!code ++] // [!code focus] compiler: 'svelte', // [!code ++] // [!code focus] }), // [!code ++] // [!code focus] ],})To make sure that you can leverage the type definitions of the Unplugin Icons in SvelteKit, you would want to globally define such specific Icon imports as Svelte component imports. To do so, update the tsconfig.json file with the following code:
{ "extends": "./.svelte-kit/tsconfig.json", "compilerOptions": { "types": ["unplugin-icons/types/svelte"], // [!code ++] // [!code focus] "allowJs": true, "checkJs": true, "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "resolveJsonModule": true, "skipLibCheck": true, "sourceMap": true, "strict": true } // Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias // // If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes // from the referenced tsconfig.json - TypeScript does not merge them in}The code above simply marks each import from ~icons to be considered as a Svelte component in your SvelteKit application.
Finally, import the icon and use it in your SvelteKit index route with the following code:
<script lang="ts"> import TwitterIcon from '~icons/devicon/twitter' // [!code ++] // [!code focus]</script>
<TwitterIcon /> // [!code ++] // [!code focus]The code above imports an icon as a Svelte component in the index route.
You’re all done now!
To test the application, prepare a build and run the preview server using the command below:
npm run build && npm run previewIn this guide, you learned how to use Unplugin Icons in a SvelteKit application.
Using a different stack? Check out our guide on Unplugin Icons in Next.js.
If you have any questions or comments, feel free to reach out to me on Twitter.
How Astro 7.2 incremental static builds cut a one-post rebuild from ~16.5s to ~10s, restoring 637 of 661 pages from cache with the right cacheKey setup.
We're excited to announce that MailKite email support is now available in all LaunchFast starter kits. Send transactional emails through MailKite with a single environment variable change.
Hydration is expensive. A practical look at Astro islands: the cheaper options to exhaust first (static HTML, build-time data, server islands, progressive enhancement), and the cases where hydration is useful.