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.
Prerequisites
You’ll need the following:
- Node.js 18 or later
Table Of Contents
- Create a new SvelteKit application
- Integrate Unplugin Icons in your SvelteKit project
- Build and Test your SvelteKit application locally
Create a new SvelteKit application
Let’s get started by creating a new SvelteKit project. Open your terminal and run the following command:
npm create svelte@latest my-app
npm create svelte
is the recommended way to scaffold an 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-app
npm install
npm run dev
The app should be running on localhost:5173. Now, let’s close the developement server to install Unplugin Icons.
Integrate Unplugin Icons in your SvelteKit project
Install the Unplugin Icons dependencies
Execute the commands below to install the necessary packages for using Unplugin Icons with SvelteKit:
# unplugin-icons package for Webpack compiler
npm i -D unplugin-icons
# @iconify/json package for the icons collection
npm i -D @iconify/json
The 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:
// File: vite.config.ts
import { defineConfig } from 'vite'
import Icons from 'unplugin-icons/vite' // [!code ++]
import { sveltekit } from '@sveltejs/kit/vite'
export default defineConfig({
plugins: [
sveltekit(),
Icons({ // [!code ++]
compiler: 'svelte', // [!code ++]
}), // [!code ++]
],
})
Include Type Definitions for Unplugin Icons
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 ++]
"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.
Use Unplugin Icons in the index route
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 ++]
</script>
<TwitterIcon /> // [!code ++]
The code above imports an icon as a Svelte component in the index route.
You’re all done now!
Build and Test your SvelteKit application locally
To test the application, prepare a build and run the preview server using the command below:
npm run build && npm run preview
Conclusion
In 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.