core-js

repository·master·Indexed 12 days ago

https://github.com/zloirock/core-js

A modular standard library for JavaScript providing polyfills for ECMAScript features up to 2025 and various WHATWG/W3C web standards. Version 3.50.0 supports granular loading of features via core-js/actual and offers core-js-pure for non-polluting usage. Includes core-js-builder for creating custom polyfill bundles and core-js-compat for determining required modules based on browserslist queries or specific engine targets.

Tokens
130.1K
Snippets
459
Records
525
Agent score
97%

What's inside core-js

  1. Overview of core-js

    master

    core-js is a modular standard library for JavaScript. It provides polyfills for ECMAScript features (up to 2025), including:

    • Promises
    • Symbols
    • Collections
    • Iterators
    • Typed Arrays
    • ECMAScript proposals
    • Web Standards: Some cross-platform WHATWG / W3C features and proposals, such as URL and URLSearchParams.

    Key capabilities:

    • Granular Loading: You can load only the specific features your project requires.
    • Non-polluting usage: You can use the library without polluting the global namespace.
  2. What is core-js?

    master

    Overview

    core-js is a universal polyfill for the JavaScript standard library. It provides support for the latest ECMAScript standards and proposals, ranging from legacy ES5 features to cutting-edge features like iterator helpers and web platform features like structuredClone.

    Key Characteristics

    • Comprehensive: Contains approximately 500 polyfill modules (e.g., Object.hasOwn, Array.prototype.at, URL, Promise, Symbol) designed to work together.
    • Modular: Highly granular architecture allows you to load only the specific features your application requires, minimizing bundle size.
    • Non-polluting (Ponyfill support): Can be used without modifying the global namespace (often referred to as a "ponyfill" use case).
    • Tooling Integration: Designed to work seamlessly with modern build tools. It serves as the foundation for features in @babel/preset-env, @babel/transform-runtime, and various SWC features.
    • Ubiquitous: Often used indirectly via transpilers, frameworks, or intermediate packages (like babel-polyfill), making it a fundamental part of the modern JavaScript ecosystem.
  3. Future roadmap and limitations of core-js

    master

    The core-js roadmap focuses on addressing several key areas to improve bundle size, performance, and compatibility with modern JavaScript standards:

    • Dropping legacy engine support: Future versions (e.g., core-js@4) aim to drop support for engines without basic ES5 support (like IE8-) to simplify polyfills and reduce bundle size.
    • ECMAScript Modules (ESM) support: Plans to provide an alternative version of core-js using the ESM format to better support modern bundlers like rollup.
    • Web Standards: Potential expansion to include web standards like fetch, provided they can be implemented modularly without bloating bundles.
    • Babel Integration: Improving @babel/runtime to support target environments (similar to @babel/preset-env) and optimizing useBuiltIns: usage to prevent polyfill duplication and improve static analysis.
    • Polyfill Loading Optimization: Exploring services that bundle polyfills based on the user agent to avoid sending legacy polyfills to modern engines.
  4. Understand the differences in core-js@3

    master

    core-js@3 introduces significant updates compared to v2, focusing on modern ECMAScript features and web standards. Key changes include:

    New ECMAScript Support

    • Stable Features: Support for @@isConcatSpreadable, @@species, Array.prototype.flat, Array.prototype.flatMap, Object.fromEntries, and Symbol.prototype.description.
    • Proposals: Support for various stages of ECMAScript proposals, including globalThis (Stage 4), Promise.allSettled (Stage 4), new Set methods, and new collections methods (Map, Set, WeakMap, WeakSet).

    Web Standards

    • Support for URL and URLSearchParams.
    • Support for queueMicrotask (replacing the old asap function).
    • Support for .forEach on DOM collections like NodeList and DOMTokenList.

    Removed/Deprecated Features

    • Reflect.enumerate (removed from standard).
    • System.global and global (replaced by globalThis).
    • Array.prototype.flatten (replaced by Array.prototype.flat).
    • asap (replaced by queueMicrotask).
    • Error.isError, RegExp.escape, Map.prototype.toJSON, and Set.prototype.toJSON have been removed.
  5. Future direction of core-js: Engine support and ESM

    master

    The development roadmap for core-js focuses on modernizing the library by moving away from legacy engine support and adopting modern module standards.

    Engine Support

    Future versions (e.g., core-js@4) aim to drop support for engines that do not support ES5 (such as IE8-). This shift allows for the removal of complex workarounds required for engines lacking property descriptors, which currently complicates polyfilling features like RegExp.prototype.flags or URL setters.

    ECMAScript Modules (ESM)

    While core-js currently uses CommonJS, there is a move toward providing an ECMAScript Modules (ESM) version to better support modern build tools like Rollup and native browser module support.

  6. Use core-js-pure to avoid global namespace pollution

    master

    The core-js-pure package provides a version of the core-js standard library that does not pollute the global namespace. Instead of modifying built-in prototypes (like Array.prototype), you import specific features as standalone modules and use them as functions. This is ideal for library authors who want to use modern ECMAScript features without affecting the environment of the applications consuming their library.

    To use it, import the required features from core-js-pure/actual/... and call them directly with your data as the first argument.

    import Promise from 'core-js-pure/actual/promise';
    import Set from 'core-js-pure/actual/set';
    import Iterator from 'core-js-pure/actual/iterator';
    import from from 'core-js-pure/actual/array/from';
    import flatMap from 'core-js-pure/actual/array/flat-map';
    import structuredClone from 'core-js-pure/actual/structured-clone';
    
    // Usage examples:
    Promise.try(() => 42).then(it => console.log(it)); // => 42
    
    from(new Set([1, 2, 3]).union(new Set([3, 4, 5]))); // => [1, 2, 3, 4, 5]
    
    flatMap([1, 2], it => [it, it]); // => [1, 1, 2, 2]
    
    Iterator.concat([1, 2], function * (i) { while (true) yield i++; }(3))
      .drop(1).take(5)
      .filter(it => it % 2)
      .map(it => it ** 2)
      .toArray(); // => [9, 25]
    
    structuredClone(new Set([1, 2, 3])); // => new Set([1, 2, 3])
  7. Use core-js-compat for tool integration

    master
    The core-js-compat package provides the data required for automated tools to manage core-js polyfills. It is primarily used to integrate core-js with transpilers and build tools such as babel or swc. This integration allows these tools to automatically include only the necessary modules based on your target environment's compatibility data.
  8. Challenges with TC39 Proposals: Built-in Modules and Decorators

    master

    Certain TC39 proposals present challenges for polyfilling and transpilation due to their design:

    Built-in Modules (Stage 1)

    Proposed modular standard libraries that rely on asynchronous loading or specific import maps are difficult to polyfill. core-js's architecture is built around synchronous polyfilling. A potential solution discussed is the use of a global registry to allow for asynchronous setup and retrieval:

    StandardLibraryRegistry.get(moduleName);
    StandardLibraryRegistry.set(moduleName, value);

    Decorators (Stage 2)

    New iterator syntax and decorator proposals often introduce syntax that cannot be easily represented in older versions of JavaScript. If decorators are not implemented as simple syntactic sugar, they may prevent core-js from polyfilling new built-in decorators effectively, as compiled decorators might not interact correctly with native ones.