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

🐛 React lazy import path not being picked up #839

Open
6 tasks done
ryan-tote opened this issue Nov 15, 2024 · 3 comments
Open
6 tasks done

🐛 React lazy import path not being picked up #839

ryan-tote opened this issue Nov 15, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@ryan-tote
Copy link

ryan-tote commented Nov 15, 2024

Prerequisites

Reproduction url

https://codesandbox.io/p/sandbox/ls97j5?file=%2Fsrc%2FApp.tsx%3A25%2C5

Reproduction access

  • I've made sure the reproduction is publicly accessible

Description of the issue

An ordinary react lazy where the import path is inside the function works fine.

const Button = lazy(() => {
  return import("./components/Button").then((mod) => {
    return { default: mod.Button };
  });
});

But when you move the import path to a separate function it no longer follows the function path.

const getComponentFile = () => {
  return import("./components/Button");
};

const Button = lazy(() => {
  return getComponentFile().then((mod) => {
    return { default: mod.Button };
  });
});

const preloadComponent = () => {
  void getComponentFile();
};

Knip believes that the Button component is not being used so it flags it up as an unused export

@ryan-tote ryan-tote added the bug Something isn't working label Nov 15, 2024
@webpro
Copy link
Collaborator

webpro commented Nov 21, 2024

This is expected behavior.

Did you try your luck by adding --include-libs? Your CSB doesn't have a terminal so I can't try.

@ryan-tote
Copy link
Author

Oh interesting, didn't know that.

I saw that dynamic imports don't work on the docs https://knip.dev/guides/handling-issues#dynamic-import-specifiers but this isn't exactly a dynamic import.

--include-libs does work but incurs a performance penalty. Does seem a bit weird that we need to add that flag since this isn't library code.

@webpro
Copy link
Collaborator

webpro commented Nov 22, 2024

This mode is disabled by default, because it incurs a performance penalty.

Docs should be improved, indeed this isn't only about library code.

Glad it does seem to do the trick for you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants