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?I could be misunderstanding something.
Forge36|4 years ago
JS allows functions to by called by name.
A.foo();
Can be
A["foo"]()
Because foo is now a string it's possible to add levels of indirection.
Action(name) { A[name](); }
It's possible the list of actions to perform are sent to the client as data.
This amiguity has left enough doubt for most tools, and people making tree shaking a practice on code not commonly performed. The longer that happens the scarier it becomes to start investigating.
Since the browser will tree shake for you the incentive to cleanup your source is also less important.
chadlavi|4 years ago
wyager|4 years ago
wffurr|4 years ago
_3u10|4 years ago
C++ also allows it, but the function names are a bit harder to guess.
giorgioz|4 years ago
I think the reason some other bundlers like webpack didn't/don't have it by default is because tree-shaking became popular after they were invented and tree-shaking was added to the bundler later on as an extra nice-to-have feature.
It seems webpack has built-in support for tree-shaking from version 2. Latest version of webpack might have it enabled by default (not 100% sure but possible) https://webpack.js.org/guides/tree-shaking/