Open Benholio opened 7 years ago
For what it's worth, the specific optimization that is eliminating these setters is RemoveUnusedClassProperties.
Actually this is excellent and exactly by design. If something is not used, it should be removed.
If you need the property kept, you'll need to export it:
window['_sinkValue'] = foo.prototype.bar;
You can also use the sinkValue technique from closure-library.
Thanks for the quick reply! I will definitely investigate those options. I want to make sure I understand your answer, though.
The property setter foo.prototype.setBar
is being removed, but it is being used. It is used to assign a value to __bar_
property (this.bar = 'assigned'
) and that property is used later when myFoo.printBar()
is called. The un-optimized code produces console output of 'assigned', and the optimized output produces 'initial'.
For example, if this.bar = 'assigned'
is replaced with a direct call to the setter function, this.setBar('assigned')
, then the assignment is preserved.
If I'm only using my property setter internally, it can get eliminated as dead code. Example (formatted for online compiler):
Compiles to:
The getter/setter has been eliminated, including the assignment. If I use the setter from outside of the class, it is not eliminated, and both the internal assignment and external assignment are preserved:
Result:
Is there a problem with the way I'm using getter/setters here? Or any workaround that would prevent these things from being eliminated?