Should applications import core-js/stable globally or use feature-specific polyfills?
0 reputation · 29 Sept 2025, 20:31 UTC
0 reputation · 29 Sept 2025, 20:31 UTC
Assuming core-js v3, an application needs a repeatable way to polyfill finished ECMAScript features without mutating globals more than necessary. Importing core-js/stable patches Array.prototype, Promise, and other built-ins for every module in the runtime, which simplifies feature detection but increases bundle size and risks conflicts if multiple core-js versions appear in the dependency tree. Importing only feature-specific polyfills limits global pollution but requires tracking which features each module actually uses.
Is core-js/stable appropriate as a single global import in application entry files? Does feature-specific importing meaningfully reduce bundle size in a bundled production build? When should a library avoid importing any core-js entrypoint entirely?
36775 reputation · 30 Sept 2025, 06:58 UTC
For an application built with core-js v3, importing core-js/stable once in the entry file is appropriate when you use many finished ECMAScript features and want every module to share the same patched built-ins. It is not the right default for libraries, and feature-specific imports are worth the tracking effort when your production bundle is size-sensitive or you use only a small, stable set of ES features.
Likely explanation. core-js/stable re-exports the full core-js v3 polyfill set. Importing it in the entry file executes patches for Array.prototype, Promise, and other built-ins before application code runs, so feature detection becomes uniform across modules. The trade-off is that unused polyfills remain in the bundle unless the bundler can remove side-effecting code, and duplicate core-js versions in the tree can patch the same globals more than once.
Confirmed behavior. Feature-specific modules such as core-js/features/promise patch only one built-in. That limits global pollution to the features actually imported and gives the bundler finer granularity for dead-code elimination. The size reduction is not guaranteed; it depends on bundle configuration and how many features the application uses.
Array.prototype.flat or Promise.allSettled.core-js/stable once in the application entry file before application modules load.import 'core-js/features/promise', in the files that require them.npm ls core-js to verify one core-js version in the dependency tree.npm ls core-js
# Runtime check after the entry module loads:
# console.log(typeof Array.prototype.flat, typeof Promise.allSettled)
A library should not import core-js/stable or any feature-specific core-js entrypoint. Polyfilling is an application-level concern, and a library that mutates built-ins imposes its polyfill strategy on consumers and risks conflicts with the application's core-js version. Libraries should target a stated ECMAScript version and leave polyfills to the application entry point.
Which bundler are you using, and is it configured to tree-shake unused core-js modules? If unused polyfills cannot be eliminated, feature-specific imports may not shrink the production bundle, making core-js/stable the simpler choice.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.