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

Treeshake "static" dynamic import #4951

Closed
antfu opened this issue Apr 20, 2023 · 4 comments · Fixed by #4952
Closed

Treeshake "static" dynamic import #4951

antfu opened this issue Apr 20, 2023 · 4 comments · Fixed by #4952

Comments

@antfu
Copy link
Contributor

antfu commented Apr 20, 2023

Feature Use Case

Treeshake is such a brilliant idea. Nowadays, it powers so many frameworks/libraries to innovate without worries too much about the bundle size.

It's great that you can take some parts from a package, without paying the cost of the entire bundle:

import { unique } from 'lodash-es'

It works great on static imports. However, sometimes when you want to make some part lazy-load, treeshaking stopped to work:

const { unique } = await import ('lodash-es') // entire `lodash-es` will be bundled
Workaround

There is a workaround to this, is that you can create a temporary module that does the reexport:

// ./re-export.js
import { unique } from 'lodash-es'
const { unique } = await import ('./re-export.js') // now you have tree-shaking

However, this makes it verbose to use and easy to miss.

Feature Proposal

Support tree-shaking dynamic imports that are known at build time. Namely, doing object destructure and directly assigning with await import()

Valid cases
const { foo, default: anotherName } = await import('foo')
const { foo } = await import('foo')
const { bar } = await import('foo')
(await import('foo')).foo
Invalid cases

(Tree shaking is not applicable, to the current behavior.)

// full bundle
const foo = await import('foo')
// not waiting
const promise = import('foo')
// rest
const { foo, ...bar } = await import('foo')
// imported multiple times, but one of them is not tree shakable
const { foo } = await import('foo')
const foo = await import('foo')

Even though the "valid cases" are a bit strict, I believe it's still a very common pattern for people to benefit from.


Idea credits: Webpack 5.80 https://twitter.com/TheLarkInn/status/1648739331807510528

@antfu
Copy link
Contributor Author

antfu commented Apr 20, 2023

Let me know if you think this is a good direction.

I am already working on it locally, might need sometime to get familiar with the codebase :)

@sapphi-red
Copy link
Contributor

Linking some related issues: #3417, #3447, vitejs/vite#11080

@antfu
Copy link
Contributor Author

antfu commented Apr 21, 2023

Thanks for the reference!

@rollup-bot
Copy link
Collaborator

This issue has been resolved via #4952 as part of rollup@3.21.0. You can test it via npm install rollup.

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

Successfully merging a pull request may close this issue.

3 participants