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

feat: tree-shake deterministic dynamic imports #4952

Merged
merged 26 commits into from Apr 23, 2023

Conversation

antfu
Copy link
Contributor

@antfu antfu commented Apr 20, 2023

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

List any relevant issue numbers:

resolves #4951

fix #3417
fix #3447

Description

Full proposal in #4951

This PR enables tree-shaking for deterministic dynamic imports. The following are valid cases:

const { foo } = await import('foo')
(await import('foo')).foo
import('bar').then(({ foo }) => {})

Idea credits to:

@vercel
Copy link

vercel bot commented Apr 20, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
rollup ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 23, 2023 7:23pm

@codecov
Copy link

codecov bot commented Apr 20, 2023

Codecov Report

Merging #4952 (7a0ab4c) into master (5af8799) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #4952   +/-   ##
=======================================
  Coverage   98.98%   98.98%           
=======================================
  Files         222      222           
  Lines        8050     8112   +62     
  Branches     2210     2236   +26     
=======================================
+ Hits         7968     8030   +62     
  Misses         28       28           
  Partials       54       54           
Impacted Files Coverage Δ
src/Module.ts 100.00% <100.00%> (ø)
src/ast/nodes/ImportExpression.ts 100.00% <100.00%> (ø)
src/ast/variables/NamespaceVariable.ts 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@antfu antfu requested a review from lukastaegert April 22, 2023 08:36
Copy link
Member

@lukastaegert lukastaegert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some final comments and a possible optimization I stumbled upon.

src/Module.ts Outdated Show resolved Hide resolved
src/Module.ts Show resolved Hide resolved
src/Module.ts Show resolved Hide resolved
src/ast/nodes/ImportExpression.ts Show resolved Hide resolved
Copy link
Member

@lukastaegert lukastaegert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, thanks a lot!

@lukastaegert lukastaegert added this pull request to the merge queue Apr 23, 2023
Merged via the queue into rollup:master with commit b79b73c Apr 23, 2023
12 checks passed
@antfu antfu deleted the feat/treeshake-dynamic-import branch April 23, 2023 19:41
@rollup-bot
Copy link
Collaborator

This PR has been released as part of rollup@3.21.0. You can test it via npm install rollup.

Comment on lines +61 to +63
* - `const { foo } = await import('bar')`.
* - `(await import('bar')).foo`
* - `import('bar').then(({ foo }) => {})`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be doc'ed here too? https://rollupjs.org/introduction/#tree-shaking

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any PR for the docs is welcome! I guess something similar to what Parcel has does not hurt.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍
And we might even include a REPL link to see it in action and play around

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