New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
React Icons Imports everything even when included 2 or 3 icons #154
Comments
@JaccoGoris Did you find any fix for this issue? |
@sandeepreddy19 |
@sandeepreddy19 import FaFacebookSquare from 'react-icons/lib/fa/facebook-square';
import FaLinkedin from 'react-icons/lib/fa/linkedin';
import FaTwitter from 'react-icons/lib/fa/twitter';
import FaPinterest from 'react-icons/lib/fa/pinterest-square';
import FaInstagram from 'react-icons/lib/fa/instagram'; instead of like this: import {
FaFacebookSquare,
FaLinkedin,
FaTwitter,
FaPinterest,
FaInstagram
} from 'react-icons/lib/fa'; which would have been a lot nicer. |
Should be better in 3.x with #141 |
I have the same problem with react-icons 3.0.1 |
#141 doesn't seem to have had any effect on size. 2.2.7 with the old format was 6kb for 5 icons. Upgrading to 3.0.1 and migrating to import MdGroup from 'react-icons/lib/md/group'
import MdFavorite from 'react-icons/lib/md/favorite'
import MdStar from 'react-icons/lib/md/star'
import MdThumbsUpDown from 'react-icons/lib/md/thumbs-up-down'
import MdLocalLibrary from 'react-icons/lib/md/local-library' vs import { MdGroup, MdFavorite, MdStar, MdThumbsUpDown, MdLocalLibrary } from 'react-icons/md' |
@kamijin-fanta any insight? |
@wopian Please tell me the version of webpack. If upgrade has no effect, please provide webpack.config.js. |
@kamijin-fanta I am using the default webpack config provided by Create-React-App |
@wopian I saw the configuration easily.
for additional debugging please submit the minimum project configuration. |
@sandeepreddy19 I verified with react-script@1.1.4. please upload the smallest project including bug. |
Back down to 2kb (5 icons) after those changes 👍 |
@kamijin-fanta many people use create-react-app without ejecting (see webpack default prod config). it would be nice if the new 3.x version would work out-of-the-box with that small footprint for those users. is that the case? |
@antoinerousseau the size of the demo project is 37.84 KB in production build. demo project: https://github.com/react-icons/react-icons/tree/master/packages/demo |
@kamijin-fanta I think the new build structure in 3.0 is pretty bad.
All in all, a pretty big regression from 2.2. To fix it, I would start by renaming the .mjs file to index.esm.js or something like that and put a package.json file inside the fa-folder (and other such folders) and include |
@markusenglund thanks for the good feedback. about 3, 4 you are right. projects created with creacte-react-app should normally resolve .mjs first. In versions older than 1.0.15, the bundle size appears to be large. the default setting of the latest webpack resolves I think that problems are occurring when using old packages or wrong config overrides. The first problem is temporary. |
Ah ok. Number 2. was kind of a nonsensical criticism then. Still, I think we should strive to build npm packages that work great out of the box for everyone. The setup for this package is right now pretty atypical, so it's not surprising that some projects don't have their build tools properly set up for it. |
I encountered this issue while using Next.js with default configuration. Adding
|
Suggestion: Since this will be a common issue encountered for newcomers, it may be nice to link to this issue in the README where the ES6 imports are recommended. Something like this:
Thoughts? |
Getting this issue with Gatsby v2. |
I'm having this issue using Laravel Mix's webpack config. I'm using just one icon and it's adding the entire font awesome library. I'd like to try your solution, @kamijin-fanta, but I'm not really sure how to go about it. Mix lets you add webpack customizations like so:
to which I added:
but that didn't seem to fix it. I'm a bit of a webpack noob, so any pointers would be great. FWIW, I'm importing the icon like so: Thanks. |
I have the same problem as @elramus
And my compiled JS grew up by 900kb ! Solutions mentioned here don't help :( |
Why the bundle size is bigger when I changed to |
@IgnacioSuburuAssertia @hoangvu12 For missing icons, wait for the maintainer to update the package for latest icons. No other workarounds :) |
That's what I did, I got both 200kb of two type of imports. |
@hoangvu12 |
I ran next/bundle-analyzer to see the result. |
I think that there will be tree shaking with Production Build, depending on your bundler tho. |
Also, lets wait for this happen :) |
Faced the same issue with gatsby v5 with Tried adding
Ref - https://react-icons.github.io/react-icons/ |
Just tried switching to |
It's probably a large lift for existing projects using React Icons, but if you're looking for an alternative you might be interested in unplugin-icons. It provides access to over 100 different icon packs by configuring your bundler to dynamically import only the icons you actually use, rather than relying on native tree-shaking. All the supported packs and icons can be seen at https://icones.js.org/. This approach, or just copy-pasting the icons we actually use into an |
So all of the stuff going on here, I decided to do some copycat. Let me explain:
Actually it is not that bad because you will hardly use more than 20 - 25 icons per project. |
@abhi0661236 can't find the SVGs on react-icons website, maybe you're referring to heroIcons ? |
@CRSylar No I am talking about the react-icons webpage itself. just inspect and copy from markup :) |
I am using react-icons@4.8.0 I saw that in dev mode (Chrome Dev Tools/Network Tab), all the library is loaded in loaded (almost 2.2mb) but when I pass to production (Analyzed with source-map-explorer) the react-icons size inside de vendor chunk is just 4.2kb, so it appears to be fixed. I have to add that I am using Vite@4.2.1 & React 18.2.0, maybe The bundling process and dep optimization of vite has something to do with it. |
I am having the same issue with Next.js, and Vercel deployment. However, this is only occuring with one specific icon import, whics the io5 folder.
adds the entire io5 folder and 200 kb. Might possibly remove the icon at this point unless there's a quick fix. |
When someone imports files from this library by using import statement, it will automatically use `index.esm.js` file without specifying `type: module` in index.js. Currently typescript complains about the type If you directly import esm files which can be solved by specifying types. should also resolve issue react-icons#154 without using all-files
This PR should resolve tree shaking issue mentioned in react-icons#154 without using all-files. It also resolves typescript issue when you try to import esm file directly.
Hey! I managed to fix the big bundle size. In my case the problem was that inside my babel.config.js i had "@babel/plugin-transform-modules-commonjs" plugin. that transformed my es2015 modules into commonjs modules, and webpack's tree shaking fails with commonjs modules as you can read in their docs: https://webpack.js.org/guides/tree-shaking/: "Ensure no compilers transform your ES2015 module syntax into CommonJS modules (this is the default behavior of the popular Babel preset @babel/preset-env - see the documentation for more details)." basically in your babel.config.js you have to add "modules": false in your preset-env and make sure you or another compiler don't transform the modules to commonjs. I'm attaching before and after screenshots. it also works with the latest version to date 4.10.1. hope this helps someone disclaimer: first screenshot had a different version. the second was for 4.10.1. that's why the different stat sizes |
Still having the same problem in 2024, this approach works for me, just add /index.esm.js at the end of your imports.
|
I had imported below icons in Create React App and I see that it included the whole library in the bundle. Attached is a screenshot. How do we get around this?
import {
FaBarChart,
FaDatabase,
FaMinus,
FaPlus,
FaStickyNote
} from 'react-icons/lib/fa'
import { IoChatboxWorking } from 'react-icons/lib/io'
The text was updated successfully, but these errors were encountered: