-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Detect whether CallExpressions (may) have side-effects before including them #179
Comments
Statically analyzing JavaScript is made very difficult by its various features. Take the |
If only JavaScript had a way of telling a function was pure... |
Oh, I didn't say it'd be easy! I think it's reasonably to be pragmatic though and say that if you've defined If we assume that the developer hasn't poked about with |
It looks like this will be a relevant issue for D3 bundling. If I do a relatively minimal import from d3-scale, for example, import {category10} from "d3-scale";
export default {
scale: {
category10: category10
}
}; I end up pulling it quite a lot of d3-time and d3-time-format. They’re used by other parts of d3-scale (for time scales, naturally), but it seems the way I’ve structured them is not amenable to exclusion. I can investigate whether there’s a way I can change the code to avoid their superfluous inclusion, but it would be great to have an “unsafe” mode that is more lenient about ignoring code that probably doesn’t have side-effects. (On the other hand, if I intend the code to have side-effects, like with #13… then I want to include it!) |
Here is a minimal example of some code. The This is a problem for me because I'm trying to use Rollup together with PureScript, and PureScript generates a lot of code similar to the above example, which means that it's not tree-shaken. Is this the right issue for this, or should I create a new issue? |
We can close this now, a lot of this side-effect detection is implemented. Still work to do but we've come a long way |
All
CallExpression
nodes that aren't inside a function are assumed to have side-effects, and automatically included. This behaviour is over-cautious in situations like this:The
Unused
class is never instantiated, but we included it anyway because it's declared inside an IIFE (i.e aCallExpression
). This isn't an academic problem, it's how Babel transpiles classes (for example).Similarly, a call to a function that either a) doesn't have side-effects, or b) only has side-effects that apply to things we don't need, should not be included, and should not cause the relevant declaration to be included:
The function call on the second line means we have to include that line, plus the
num
andsquare
declarations, regardless of whether we're importing eithernum
orsquared
from this module, even though we can statically determine thatsquare
has no side-effects.It may also be possible to determine whether functions are capable of mutating their arguments.
The text was updated successfully, but these errors were encountered: