3 ms·
One issue that I haven't seen discussed often yet are "dead dependencies" - something where I believe tree shaking is essential. Image the following scenario:
by xg15 5y ago
One issue that I haven't seen discussed often yet are "dead dependencies" - something where I believe tree shaking is essential.
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...)
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 not use the parts of B that require C.
So not only does B suddenly contains lots of dead code, the entire library C and all of transitive dependencies 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.
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 and execute modules across your codebase. This scan includes modules pulled in through transitive dependencies!
So in the worst case, a library that you never consciously added to your project and that has no actual reason to be there will execute code when your app is running.
- phist_mcgee 5y agoJS module namespacing resolves this. Take a look at how lodash separates its utilities: var array = require('lodash/array'); var object = require('lodash/fp/object'); https://github.com/lodash/lodash https://github.com/lodash/lodash
- eyelidlessness 5y ago(Discussed briefly in the article) This is one of the benefits of ESM. Top-level imports and exports are static, allowing significantly greater confidence in identifying which code is actually referenced, versus previous module systems like CJS or AMD. This is also why named imports are often encouraged over whole-module namespace imports (though in theory that shouldn’t make much of a difference unless the namespace is accessed with dynamic keys). For example, tree shaking ESM makes this equivalent to the sibling comment example’s first line: import { array } from 'lodash'; Dynamic imports can be similarly optimized, but only to the extent the import path is static, e.g. import(`./foo/bar/${quux}.js`) Can eliminate everything adjacent to /foo/bar, but cannot eliminate anything within /foo/bar without more advanced analysis of dynamic usage. Of course TypeScript can help here if `quux` is narrower than `string`, but I’m not sure how much that’s in use by existing bundlers.
- skybrian 5y agoWhen this happens, tree shaking is acting as a workaround for unfortunate library design. Utilities should be split up based on what they depend on. Otherwise, you end up downloading lots of libraries you don’t use, and then tree shaking prunes them. The final result is okay, but this results in unnecessary compiles and makes builds slower.