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

optimizePackageImports is not working with react-icons in dev mode #70666

Open
FilipiBrabo opened this issue Oct 1, 2024 · 3 comments
Open
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. Performance Anything with regards to Next.js performance.

Comments

@FilipiBrabo
Copy link

FilipiBrabo commented Oct 1, 2024

Link to the code that reproduces this issue

https://github.com/FilipiBrabo/nextjs-react-icons

To Reproduce

  1. Start the application with analyze-bundle enabled ANALYZE=true npm run dev
  2. Go to localhost:3000 the bundle files should open in your browser

Current vs. Expected behavior

The current behavior is that all icons from a react-icon pack are being included in the final bundle in dev mode.

From the optimizePackageImports docs I expected that react-icons/* would be tree shaken both in dev and in prod build.

I've also included a lucide icon to see the difference, and noticed that optimizePackageImports is working as expected.

These are the bundles generated in dev mode of the application provided. You can see that all fa, bs and ai icons are being included in the final dev bundle.

Client bundle:
client-bundle

Node.js bundle:
nodejs-bundle

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #123-Ubuntu SMP Mon Jun 10 08:16:17 UTC 2024
  Available memory (MB): 32012
  Available CPU cores: 16
Binaries:
  Node: 18.20.4
  npm: 10.7.0
  Yarn: N/A
  pnpm: 8.15.8
Relevant Packages:
  next: 15.0.0-canary.174 // Latest available version is detected (15.0.0-canary.174).
  eslint-config-next: N/A
  react: 19.0.0-rc-2d16326d-20240930
  react-dom: 19.0.0-rc-2d16326d-20240930
  typescript: 5.3.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Developer Experience, Performance

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

I didn't dig too much deep into this, but these were the things that I've tried:

  1. Use modularizeImports to replace react-icons with @react-icons/all-files. You can check it out in the next.config.js file.
{
  reactStrictMode: true,
  modularizeImports: {
    "react-icons/fa": {
      transform: "@react-icons/all-files/fa/{{ member }}",
    },
    "react-icons/bs": {
      transform: "@react-icons/all-files/bs/{{ member }}",
    },
    "react-icons/ai": {
      transform: "@react-icons/all-files/ai/{{ member }}",
    },
  },
}
  1. Specify react-icons lib in optimizePackageImports config
{
  reactStrictMode: true,
  experimental: {
    optimizePackageImports: [
      "react-icons/fa",
      "react-icons/bs",
      "react-icons/ai",
    ],
    // or
    optimizePackageImports: ["react-icons"],
  },
}

This really affects DX if you have too many icons from different packs being imported.
I'm happy to help in any way to fix this issue, just need a little guidance 😅

@FilipiBrabo FilipiBrabo added the bug Issue was opened via the bug report template. label Oct 1, 2024
@github-actions github-actions bot added Developer Experience Performance Anything with regards to Next.js performance. labels Oct 1, 2024
@samcx
Copy link
Member

samcx commented Oct 2, 2024

@FilipiBrabo Thank you for submitting an issue!

We are taking a look at this— :frog-eyes:

@FilipiBrabo
Copy link
Author

@FilipiBrabo Thank you for submitting an issue!

We are taking a look at this— :frog-eyes:

Thanks, @samcx! Feel free to reach out if you need any help. I’m happy to assist however I can, and I’m curious to learn more about the bundling/optimizing process 😃

@stychu
Copy link

stychu commented Jan 29, 2025

NextJs v14.2.23

Doesn't seem the issue is resolved.
Our Compile times avg around 40 SEC which is insanity for development..... after I remove all imports to react-icons it goes down 3-5 sec which is more acceptable but still significant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. Performance Anything with regards to Next.js performance.
Projects
None yet
Development

No branches or pull requests

3 participants