3 ms·
(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 id
by 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.