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
Named imports scans entire directory #2641
Comments
Just took a look. The module graph for If you want optimal performance here, I recommend importing these icons into your project individually instead of importing every single icon into your project at once. If this package author wanted optimal performance here, I recommend that they inline all of these icon strings into the primary entry point. I tried changing the package to do that and it was a huge speedup for bundling time. On my machine, importing all icons as separate files was 16x slower than importing just one icon, but with inlined strings importing all icons is only 2x slower than importing just one icon (one icon was 15ms, all icons as inlined strings was 31ms, all icons as separate files was 236ms). Putting all strings into one file shouldn't cause bigger bundle sizes as long as ESM is used and the |
Understood. I guess the question here is: is it necessary for the compiler to resolve every single named export when only a single import was requested? Wouldn't it be more efficient to resolve only imported exports? |
It is, as Evan said |
Hi, mdi-material-ui maintainer here. 👋 However: Webpack takes a If that's not possible then we'd have to solve it in library-land. We could change it to one big file that exports all the icons, but that would break existing apps where the icons are imported from the icon file ( |
reopening for discussion |
@mbrevda I'm generating the |
Keep in mind that your package is currently working correctly! You may not need to change your package. This issue is only about bundling performance, which you may be willing to trade off vs. other concerns such as compatibility across different tools.
This is something esbuild recognizes. It means that esbuild can omit these modules from the output if they are unused. However, JavaScript is still specified such that the whole module graph must be traversed during module instantiation, so it doesn't mean that esbuild can avoid parsing them. If there is a parse error or module resolution error, the build is still supposed to fail.
Not necessarily. You could just have the individual files re-export from the main file. For optimal results you'd want to use ESM instead of CommonJS, but that might mean introducing issues with old node versions that expect CommonJS files, so that could be one reason you'd want to keep your current approach. Anyway I tried this out myself because I was curious. The results are here: https://github.com/evanw/mdi-material-ui-inline. The TLDR is that doing this makes importing from an individual file slower but importing from the entry point faster. So it's a trade-off and isn't necessarily a clear win, especially if people aren't supposed to be importing from the primary entry point. My approach also makes bundle sizes smaller across all bundlers, although I didn't investigate exactly why this is. Here are the bundling speeds (more details here) from the current approach compared with the bundling speeds from this suggestion (ESM only, icons in entry point, individual files just re-export entry point):
|
@evanw Thanks a lot for looking into this! 👍 I'm not really sure how people use the imports but at least me and my colleagues always use the imports from the primary entry point because it's pretty convenient – and with bundlers being smart about tree shaking that's not an issue anymore anyways (it was back in Material-UI 0.x times). I'll probably go with the inline approach in the next updates then. |
@evanw thanks so much for jumping in here! Do you think it would make sense for esbuild to warn in such situations (where it might be doing more work than necessary due to how the package is published)? |
No, I don’t think so. Warnings are mainly supposed to call attention to things that are important for correctness and/or that are directly actionable. This situation is not about correctness (it’s about performance) and isn’t something that the user will be able to fix themselves (because it’s part of a package). |
While perhaps not the compilers job per se, it would be useful if the compiler could let the user know that there is room for improvement, even if it's just a speed up. Thanks a ton for the feedback! Closing this as there doesn't seem to be any further action to take. |
In some packages with a large number of named imports, every listed import is scanned for even when just a single import is imported, causing a noticeable slowdown in build time. This is particularly apparent with constant rebuilds during development.
Here is a reproducible repo, showing an large time difference
The text was updated successfully, but these errors were encountered: