The main idea is to only import react material UI icons when it is needed or rather when dynamically where we wouldn't know the icon name at compile time. (Let's gurentee that we would have valid icon names).
My approach was to use a require statment with dynamic path:
const IconComponent = require(`@material-ui/icons/${icon}`).default;
This works fine but some warnings are showing up.
./node_modules/@material-ui/icons/utils/createSvgIcon.d.ts 3:8 Module parse failed: Unexpected token (3:8) You may need an appropriate loader to handle this file type. | import SvgIcon from '@material-ui/core/SvgIcon'; | declare function createSvgIcon(path: React.ReactNode, displayName: string): typeof SvgIcon; | | export default createSvgIcon;
./node_modules/@material-ui/icons/index.d.ts 4:5 Module parse failed: Unexpected token (4:5) You may need an appropriate loader to handle this file type. | import SvgIcon from '@material-ui/core/SvgIcon'; |
type SvgIconComponent = typeof SvgIcon; | | export const AccessAlarm: SvgIconComponent;
My question is: How could we avoid these warnings and do the dynamic import in the appropriate way ?