1. In real life, you shake real trees, to get fresh fruit.<p>2. In JS, you shake fake trees, to shed bad fruit.<p>3. Common wisdom says: "Ye shall know them by their fruits."<p>Therefore, all JS dependency trees are rotten. /s
Tree shaking is such a well-named concept that I could tell you exactly what it was the first time I heard someone say it, even though they hadn't defined it for me yet. It's such a joy when we can map abstract mechanisms to plain language so easily.
One issue that I haven't seen discussed often yet are "dead dependencies" - something where I believe tree shaking is essential.<p>Image the following scenario: You have an app A that pulls in library B as a direct dependency. B pulls in another library C as a transitive dependency. C may have further transitive dependencies of its own (D, E, F...)<p>As it happens, B is a "toolbox/commons/utils" library, which bundles several small, unrelated bits of functionality into a single asset for convenience. A only uses a fraction of B's functionality and it does <i>not</i> use the parts of B that require C.<p>So not only does B suddenly contains lots of dead code, the <i>entire library C and all of transitive dependencies</i> are dead code. None of those libraries are actually used by anything and A's developers will likely have no idea what they do at all - yet the build system considers them essential and will bundle them with the application.<p>There is one escalation of this, which I guess you could call "undead code": Some frameworks (e.g. Spring for Java) use classpath scanning or similar mechanisms to automatically detect <i>and execute</i> modules across your codebase. This scan includes modules pulled in through transitive dependencies!<p>So in the worst case, a library that you never consciously added to your project and that has no actual reason to be there <i>will execute code</i> when your app is running.
I wonder why tree-shaking wasn't always the default for, say, JS bundlers. If a compiler/analyzer knows what the entry point of a program is, as well as any symbols it exports to the outside world, isn't it relatively simple to figure out what's not being used?<p>I could be misunderstanding something.