question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trouble with importing icons in a vite/react project

See original GitHub issue

Hello, i tried to follow the documentation as best as i could in order to use unplugin-icons in a react/vite project and it looks like i wasn’t really successful

My vite.config is as follows:

import react from '@vitejs/plugin-react'
import * as path from 'path'
import { FileSystemIconLoader } from 'unplugin-icons/loaders'
import Icons from 'unplugin-icons/vite'
import { defineConfig } from 'vite'
import eslintPlugin from 'vite-plugin-eslint'

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [
        react(),
        eslintPlugin(),
        Icons({
            compiler: 'jsx',
            jsx: 'react',
            customCollections: {
                'my-icons': FileSystemIconLoader('./src/assets/icons', (svg) =>
                    svg.replace(/^<svg /, '<svg fill="currentColor" ')
                ),
            },
        }),
    ],
    resolve: {
        alias: {
            '@': path.resolve(__dirname, './src'),
        },
    },
})

As you can see, my svg icons are located inside src/assets/icons

Now, here’s how i’m trying to import the icons in one of my components:

import IconHome from '~icons/my-icons/home'

But it’s throwing the following error: [vite] Internal server error: No files matching '~icons/my-icons/home.jsx' were found.

Any help would be appreciated!

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:25

github_iconTop GitHub Comments

1reaction
userquincommented, May 7, 2022

Updated my repro, check the apple icons on the screen, one white and the same with red color using:

<Apple />
<Apple style={{ color: '#FF0000' }} />

EDIT: on iconCustomizer(collection, icon, props) you can see I only add the fill attr to this icon in my-icons collection

1reaction
userquincommented, May 7, 2022

I mean, you must remove the fill attr from the svg, the plugin (via @iconify/utils) will not check for duplicates attrs IIRC.

just remove them from the svg, add the iconCustomizer(collection, icon, props) to the plugin and add the new attrs to the props, check for the collection and the icon if you need some custom props, it is a Record<string, string>.

For example, check this, only iconCustomizer: https://github.com/unocss/unocss/pull/848#issuecomment-1104068048

Read more comments on GitHub >

github_iconTop Results From Across the Web

Production build missing fontawesome imports when using ...
Describe the bug Font awesome icons shared across chunk-split boundaries are missing from every import except the first one when importing a ...
Read more >
How to Import SVGs in a React and Vite app - freeCodeCamp
Importing SVGs using the image tag is one of the easiest ways to use an SVG. If you initialize your app using CRA...
Read more >
Unable to import SVG with Vite as ReactComponent
In App.tsx import SVG as React Component by adding ?component on the svg's ... I migrated from CRA to Vite and was facing...
Read more >
Static Asset Handling - Vite
Importing a static asset will return the resolved public URL when it is served: js ... import workletURL from 'extra-scalloped-border/worklet.js?url' CSS.
Read more >
vite-plugin-react-icons - npm
Start using vite-plugin-react-icons in your project by running `npm i vite-plugin-react-icons`. There are no other projects in the npm ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found