-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
Description
Related page
https://mui.com/material-ui/guides/minimizing-bundle-size/
Kind of issue
Missing information
Issue description
For small projects, developers may use fewer than 50 icons. In our project, we see that over 2000 files are being loaded in the Network tab.
When we filter the requests using @mui
, we see that more than 300 lines are related to the MUI icons.
So, MUI Icons are not efficient now in our large application. We thought of two solutions:
- Importing everything (
import { AccessAlarm, ThreeDRotation } from '@mui/icons-material';
). This is not good because we would force upon ourselves more than 1700 more icons. - Adding icons to the
optimizeDeps
in Vite to make them into a one bundle.
Option two seemed to be a better option. We created a findIcons
method:
import { execSync } from "child_process"
export function findIcons() {
try {
const output = execSync(
`grep -rhoE "@mui/icons-material/[A-Za-z0-9]+" ./src | sort | uniq`,
{ encoding: "utf-8" }
)
return output
.split("\n")
.map(i => i.trim())
.filter(Boolean)
} catch (error) {
console.error("Failed to find MUI icons:", error)
return []
}
}
And added them to the optimizedDeps
:
const icons = findIcons()
// ...
optimizeDeps: {
include: [
...icons,
]
},
Then we used DEBUG:vite.deps
environment variable to see the optimization process. And we ended up each icon being separately optimized into one file, again leaving us with more than 300 requests.
7:32:45 AM [vite] (client) Forced re-optimization of dependencies
vite:deps (client) removing old cache dir /Project/AdminPanel/node_modules/.vite/deps +0ms
vite:deps scanning for dependencies... +0ms
VITE v7.0.0 ready in 2008 ms
➜ Local: http://localhost:5173/
➜ Network: http://172.18.0.2:5173/
vite:deps ✨ static imports crawl ended +57ms
vite:deps Crawling dependencies using entries:
vite:deps /Project/AdminPanel/index.html +0ms
vite:deps Scan completed in 2120.52ms:
...
vite:deps @mui/icons-material/Abc -> /Project/AdminPanel/node_modules/@mui/icons-material/esm/Abc.js
vite:deps @mui/icons-material/AcUnit -> /Project/AdminPanel/node_modules/@mui/icons-material/esm/AcUnit.js
vite:deps @mui/icons-material/AccessTime -> /Project/AdminPanel/node_modules/@mui/icons-material/esm/AccessTime.js
vite:deps @mui/icons-material/AccountBalance -> /Project/AdminPanel/node_modules/@mui/icons-material/esm/AccountBalance.js
So, we're stuck now. What should we do? The docs do not suggest anything for this scenario.
- Having hundreds of icons is not efficient if each is loaded separately (because they are third-party dependencies, and there is no need for HMR)
- Including all 2000+ icons is not feasible to solve that problem
- When we include them in the
optimizedDeps
, we see them each separately being optimized into a separate file, still leaving us with hundreds of requests.
Please add a section to the optimization page, and tell us your approach to optimized a few hundred icon imports.
Context
No response
Search keywords: icons-material optimizeDeps