core-js ES module polyfills vs full bundle: Which import strategy yields optimal tree-shaking?
0 reputation · 16 Mar 2022, 20:03 UTC
0 reputation · 16 Mar 2022, 20:03 UTC
To incorporate core-js polyfills into a modern JavaScript application without using production credentials, while maintaining bundle size and avoiding unintended prototype modifications.
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.
corejs: 'usage', does the resulting bundle contain only the polyfills that are actually invoked in the code?es bundle remains the safer choice despite the potential for increased bundle size?global flag or using core-js’s apply method affect prototype patching in a production build, and can it mitigate conflicts with other libraries?35275 reputation · 17 Mar 2022, 03:12 UTC
To achieve optimal tree-shaking and minimum bundle size, individual ES module imports are the superior strategy. Importing the full core-js/stable or core-js/es bundle bypasses tree-shaking entirely, as it explicitly instructs the bundler to include the entire polyfill library regardless of whether your code utilizes the features.
When using a bundler (Webpack, Rollup, Vite) combined with Babel, the most efficient way to handle individual imports is through the useBuiltIns: 'usage' configuration. This approach automates the process by analyzing your source code and injecting only the specific imports required for your target environment.
useBuiltIns: 'usage': The bundler includes only the polyfills actually invoked in your code. For example, if you only use Array.prototype.flat(), only that specific module is bundled.The method of import affects how the global environment is modified:
core-js imports modify native prototypes (e.g., Array.prototype). While convenient, this can cause conflicts if other libraries expect a different behavior or if a browser implements a slightly different version of a pending spec.core-js-pure package. This provides polyfills as standalone functions that do not modify the global object, making it the safest choice for library authors.To verify that your strategy is working as intended, perform the following checks:
webpack-bundle-analyzer. Search for core-js in the output; if you see a massive block containing almost every ES feature, tree-shaking is failing.Promise.allSettled) fails despite being used in the code, your 'usage' configuration may be missing a target environment definition.Diagnostic Detail Needed: To refine this recommendation, please specify your browserslist configuration. If you are targeting extremely legacy environments (e.g., IE11), the risk of missing a polyfill via 'usage' may outweigh the bundle size benefits of avoiding the full bundle.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.