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

enable tree-shaking in transform mode when both --minify and --format flags are specified #1617

Closed
wants to merge 1 commit into from

Conversation

yyx990803
Copy link
Contributor

Also add dedicated --minify-unused flag. Please see committed changelog entry for more details.

closes #1610

@yyx990803 yyx990803 changed the title enable tree-shaking when both --minify and --format flags are specified enable tree-shaking in transform mode when both --minify and --format flags are specified Sep 18, 2021
@evanw
Copy link
Owner

evanw commented Sep 21, 2021

Thanks for the thoughtful issue and PRs. I do want to get this fixed and this PR looks like a good implementation of this specific approach. However, I'd still like to think through the alternate approach of augmenting --tree-shaking= instead of --minify myself first since that's where I'd expect people to look for this feature, and people may want this feature outside of the context of minification. You already thought through that in #1615 (comment), copied below:

I considered some other options without adding a new flag:

  1. Add another enumerated value to --tree-shake, like --tree-shake=force. However, it would be ambiguous how it interacts with --tree-shake=ignore-annotations. Ideally, we want to keep the ability to force treeshaking but still ignore annotations.

I'm imagining that --tree-shaking= could be either unspecified, false, true, or ignore-annotations:

  • Unspecified: the current behavior (true if bundling is enabled or if the format is iife, otherwise false)
  • false: all unused code would be kept
  • true: all unused code would be removed
  • ignore-annotations: all unused code would be removed (ignoring annotations)

I don't think there's a conflict with true and ignore-annotations because "the ability to force treeshaking but still ignore annotations" could just be ignore-annotations. That is a subtle behavior change from the current behavior but I don't think it's an issue because --tree-shaking=ignore-annotations is currently meaningless if tree shaking isn't active, so having it activate tree shaking actually makes more sense to me. Let me know if you see any problems with this approach. If not, I think I may want to take that approach instead (although I'm still thinking about it).

@yyx990803
Copy link
Contributor Author

That sounds good to me! I avoided it mostly because I didn't want to change the behavior of ignore-annotations, but since you are ok with that then I think it's straightforward to just pass --tree-shaking=true.

Feel free to close this PR if you decide to go ahead with it, or I can modify this to reflect the change.

@evanw
Copy link
Owner

evanw commented Sep 22, 2021

I have now looked into this and I was wrong: --tree-shaking=ignore-annotations isn't actually currently meaningless when tree shaking is disabled. It still affects how /* @__PURE__ */ comments are interpreted when --minify-syntax is enabled, which can trigger dead-code removal within expressions. This is related to tree shaking but seems somewhat separate since "tree shaking" usually refers to declaration-level dead code removal and you may want dead-code removal within expressions without declaration-level dead code removal. So I think it's best to separate those two concepts. My PR for this change is here: #1625.

@evanw evanw closed this in #1625 Sep 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

minify: provide an option to enable treeshaking in single-file transform
2 participants