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
Vite not tree-shaking dynamic imports #14145
Comments
Ah, I guess this is happening because Vite replaces const { export3 } = await import('./not-tree-shaken.js');
// into
const { export3 } = await __vitePreload(() => import('./not-tree-shaken.js'), []); Then, rollup fails to analyze this. |
This comment was marked as duplicate.
This comment was marked as duplicate.
@sapphi-red , I see you added the "has workaround" label. Are you aware of a workaround? |
This workaround (#11080 (comment)) should still work. |
Thanks for the link. Unfortunately that workaround is too unwieldy for my use case. I'm using a |
You don't need to use |
Certainly true, though in our monorepo we enforce importing from the canonical main files for structure/API hygiene. |
Hi, I'm coming from this issue rollup/rollup#4951 and I cannot import dynamic component from a react library:
This will break tree-shaking and imports whole library.. I have to import and re-export my component to use it without importing whole library.
Then I can use lazy / await import
Any idea if it's possible since this PR was merged ? I don't know if there is any point in re-exporting to use Suspense/Lazy instead of a static import. |
Describe the bug
Vite 4.4.9 is not tree-shaking the contents of dynamically-imported modules:
In the above example, even though only export1 is imported, both export1 and export2 will be bundled by Vite.
If you use a static import, export2 is correctly tree-shaken from the final bundle:
Rollup has supported dynamic import tree-shaking since 3.21.0 (description of feature, PR), and Vite 4.4.9 is using Rollup 3.28.0, so Vite should also support it.
Vite #11080 was closed saying "rollup now supports this," but the repro link was not a Vite repro, but a vanilla Rollup repro. I attached a vanilla Vite repro, and here is a Vite Vue TS repro.
Finally, here is a repro of Rollup correctly tree-shaking the dynamic import.
Reproduction
https://stackblitz.com/edit/vitejs-vite-z6x1lz?file=main.js
Steps to reproduce
npm run dev
System Info
Used Package Manager
npm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: