Trouble with importing icons in a vite/react project
See original GitHub issueHello, 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:
- Created a year ago
- Comments:25
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Updated my repro, check the apple icons on the screen, one white and the same with red color using:
EDIT: on
iconCustomizer(collection, icon, props)
you can see I only add the fill attr to this icon inmy-icons
collectionI 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 theprops
, check for the collection and the icon if you need some custom props, it is aRecord<string, string>
.For example, check this, only
iconCustomizer
: https://github.com/unocss/unocss/pull/848#issuecomment-1104068048