core-js ES module polyfills vs full bundle: Which import strategy yields optimal tree-shaking?
0 reputation · 16 Mar 2022, 20:03 UTC
Integration Goal
To incorporate core-js polyfills into a modern JavaScript application without using production credentials, while maintaining bundle size and avoiding unintended prototype modifications.
Constraints & Uncertainty
Core-js offers a single es bundle that patches all standard prototypes, and it also provides individual module imports (e.g., core-js/es/array/slice). The latter is recommended for tree‑shaking but requires explicit bundler configuration such as corejs: 'usage' in Webpack 5 or Rollup. The choice between these strategies is unresolved: the full bundle guarantees coverage but risks larger output and possible conflicts with other libraries; selective imports reduce size but depend heavily on correct bundler settings and may miss polyfills needed at runtime.
Open Questions
- When using individual module imports with a bundler configured for
corejs: 'usage', does the resulting bundle contain only the polyfills that are actually invoked in the code? - Are there specific environments or legacy browsers where the full
esbundle remains the safer choice despite the potential for increased bundle size? - How does enabling the
globalflag or usingcore-js’sapplymethod affect prototype patching in a production build, and can it mitigate conflicts with other libraries?