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
Destructuring assignment prevents tree shaking of function with prototypes #3878
Comments
Looks like another pass through rollup would eliminate the unused code in question. A similar issue was discussed in #3200. |
The reason here is two-fold:
So in short: The prototype assignment is retained because Rollup does not know if |
I long wanted to improve the logic for destructuring but unfortunately never got to it between more important issues and features. |
Is there any fundamental obstacle to have rollup perform a configurable number of passes on the AST and its accompanying internal data structure to reduce the original example to nothing? Wouldn't it be a matter of reinitializing the tree back to the initial state for the retained nodes and running the algo again? i.e., the API equivalent of this:
|
No. The algorithm is already doing multiple passes on the existing tree. If we want to run it again, we would need to create some preliminary output and reparse it to start from scratch. But I do not think this is needed, especially considering performance would likely be terrible. |
Using that terminology the default could remain one "run" and for those who wish to further optimize their output could opt in to multiple "runs" without the need to enhance rollup for each new unanticipated scenario. Is there a way to avoid the reparse of the output between runs? I appreciate that rollup treats the AST as immutable, but there ought to be a way to determine which nodes are retained in the accompanying internal data structure and reset them to an initial state. Or is there something in the internal data structure that makes it inherently single use? |
@lukastaegert I've been working on implementing specifying an optional number of runs (passes if you will) in rollup without the need to reparse the output between runs. Although it's a work in progress it works for the most part with minimal performance impact. But during debugging of this new feature I've run into an odd issue with --- a/src/ast/nodes/UpdateExpression.ts
+++ b/src/ast/nodes/UpdateExpression.ts
@@ -26,6 +26,8 @@ export default class UpdateExpression extends NodeBase {
}
hasEffects(context: HasEffectsContext): boolean {
+ this.argument.hasEffects(context) ||
+ this.argument.hasEffectsWhenAssignedAtPath(EMPTY_PATH, context);
return (
this.argument.hasEffects(context) ||
this.argument.hasEffectsWhenAssignedAtPath(EMPTY_PATH, context) I just used UpdateExpression as an example. The same issue would result for any other AST class' Do the AST |
In this configuration:
I can't figure out why "Foo" is not removed after tree-shaking. Using
const Foo$ = obj.Foo$
or removingFoo.prototype.bar
declaration fixes the issue.Expected Behavior
In the example above, everything should be removed after tree-shaking
Actual Behavior
The function
Foo
andFoo.prototype.bar
declarations are kept after thee-shakingThe text was updated successfully, but these errors were encountered: