Skip to content
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

Dynamically importing icons using Vite 5> #662

Open
balboFK opened this issue Aug 6, 2024 · 0 comments
Open

Dynamically importing icons using Vite 5> #662

balboFK opened this issue Aug 6, 2024 · 0 comments

Comments

@balboFK
Copy link

balboFK commented Aug 6, 2024

I was trying to achieve dynamic imports with the goal of reducing bundle size, since the project itself is quite heavy when fully bundled. But i ended up having problemns to figure out what should be the correct approach to avoid a full bundle request.

My approach was to create a functional component that was going to received the icon name as a target and try to "lazy" import.
As follows:

import { CaretRightOutlined } from '@ant-design/icons'
import { lazy, Suspense } from 'react'

type Props = {
  iconName: string
}

export function IconFC ({ iconName }: Props): JSX.Element {
  const AntdIcon = lazy(async () => import(
    `@ant-design/icons/${iconName}`
  ).then(module => ({ default: module.default })))

  return (
    <Suspense fallback={<CaretRightOutlined />}>
      <AntdIcon />
    </Suspense>
  )
}

But i get an error on Vite end:
image

And an error on the browsers end:
image

By looking at the folder structure at @ant-design/icons this approach looks like it should work:

image

Also the error on the browser is from an icon that i didnt try to use.

Any ideas on what could be done? My goal is to reduce bundle size since i use a great number of icons, but not all of them.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant