Using Unplugin Icons in SvelteKit
How to use Unplugin Icons in SvelteKit application?
How to use Unplugin Icons in SvelteKit application?
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.
If you have any questions or comments, feel free to reach out to me on Twitter.
Learn how to deploy a server-side rendered Angular 20+ application to Cloudflare Workers, serving static assets from the edge and running SSR.
LaunchFast works with Backblaze B2 out of the box. Same STORAGE_PROVIDER=s3, same pre-signed upload flow. Point AWS_ENDPOINT_URL_S3 at your regional B2 S3 endpoint and add your application key.
LaunchFast works with Tigris out of the box. Same STORAGE_PROVIDER=s3, same pre-signed upload flow. Point AWS_ENDPOINT_URL_S3 at https://fly.storage.tigris.dev and add your credentials.