core-js - Documentation

What is Core-JS?

Core-JS is a JavaScript library that provides polyfills for ECMAScript features that are not yet implemented or implemented inconsistently across different browsers. It aims to provide a consistent and reliable way to use modern JavaScript features in older browsers without relying on transpilers like Babel. Core-JS focuses on providing only the polyfills themselves, leaving the actual usage and integration up to the developer. This modular approach allows for only including the necessary polyfills, minimizing the overall bundle size.

Why use Core-JS?

Using Core-JS offers several advantages:

Core-JS vs. Polyfills

While Core-JS is a collection of polyfills, it’s crucial to understand the distinction. A polyfill is a piece of code that provides functionality missing in a specific environment. Core-JS is a comprehensive library that provides a large collection of these polyfills, organized and maintained as a single unit. You could create your own individual polyfills, but Core-JS offers a ready-made, tested, and well-maintained solution. Using individual polyfills from various sources can lead to conflicts or inconsistencies.

Installation and Setup

Core-JS can be installed using npm or yarn:

npm install core-js
# or
yarn add core-js

Using Core-JS 3 (recommended): Core-JS 3 uses a modular approach. You should import only the specific modules you need. This is generally done using the import syntax. For example, to use the Promise polyfill:

import 'core-js/modules/es.promise';

// Now you can use Promises reliably, even in older browsers.

To include multiple modules, import them individually:

import 'core-js/modules/es.array.includes';
import 'core-js/modules/es.object.assign';

A more convenient way to import modules, especially if you need many, is using core-js/stable which provides all commonly used stable polyfills. For production, this is a simpler option that has smaller overhead compared to importing each module individually.

import 'core-js/stable';

Using Core-JS 2 (deprecated, but some projects still rely on it): Core-JS 2 uses a different approach, often including the entire library. While it might seem simpler initially, it leads to larger bundles. Its usage is discouraged in favor of Core-JS 3’s modularity.

require('core-js/fn/array/includes'); // Example using require for Core-JS 2

Browsers Compatibility

Core-JS aims for broad compatibility. However, it’s important to note that some very old or obscure browsers may still have limitations. While Core-JS helps bridge these gaps, it’s not a silver bullet for every browser compatibility issue. Refer to the official Core-JS documentation and release notes for the most up-to-date information on supported browsers and any known limitations for specific polyfills. Testing on your target browsers is essential. Generally speaking, Core-JS provides good compatibility with modern browsers and most widely used older ones.

Working with Core-JS

Import Statements

Core-JS 3 uses ES modules, so you import polyfills using standard JavaScript import statements. This allows for optimal tree-shaking and reduces the final bundle size significantly. Avoid using require() which is associated with older CommonJS modules and not ideal for modern builds.

Using Individual Modules

The recommended approach is to import only the specific polyfills your application requires. This granular control minimizes the amount of code included in your final bundle, leading to faster load times and a smaller application footprint. Each polyfill is organized within the core-js/modules directory, categorized by ECMAScript specification and feature.

For example, to add support for Array.prototype.includes():

import 'core-js/modules/es.array.includes';

const array = [1, 2, 3];
console.log(array.includes(2)); // true

To include multiple modules, simply list them in separate import statements:

import 'core-js/modules/es.promise';
import 'core-js/modules/es.array.flat';
import 'core-js/modules/es.object.from-entries';

This approach ensures that only the necessary polyfills are included in your build.

Using the Bundled Version (core-js/stable)

For convenience, Core-JS provides a bundled version (core-js/stable) containing a selection of commonly used polyfills. This is useful for rapid prototyping or when you need a broader set of features and don’t want to meticulously select individual modules. However, it will lead to a larger bundle size than importing individual modules.

import 'core-js/stable';

Remember that this imports a significant number of polyfills; it’s less efficient than the modular approach for production applications where bundle size is crucial. Use it judiciously. core-js/stable is suitable for development or smaller projects where the added bundle size isn’t a significant concern.

Tree-shaking and Optimization

Modern bundlers (like Webpack, Rollup, Parcel, etc.) effectively support tree-shaking with Core-JS 3’s modular approach. This means that unused imported modules are automatically eliminated during the build process, resulting in a smaller final bundle size. To ensure tree-shaking works correctly, make sure your bundler is configured to support ES modules and tree-shaking.

Using individual modules is key to maximizing tree-shaking benefits. Importing core-js/stable will likely prevent efficient tree-shaking since the entire bundle is imported.

Versioning and Updates

Core-JS follows semantic versioning (SemVer). Staying up-to-date is important for security patches and access to new polyfills for emerging JavaScript features. Check the official Core-JS repository for release notes and upgrade instructions. Regularly update your package.json and run npm install or yarn install to obtain the latest version. Consider using a version manager like nvm or similar to ensure you’re using a compatible Node.js version. Always test thoroughly after upgrading to a new version of Core-JS.

Core-JS Modules by Category

This section provides a categorized overview of the polyfills available in Core-JS. Remember to always consult the official Core-JS documentation for the most accurate and up-to-date information on available modules and their specific usage. The import paths shown below assume you are using Core-JS 3’s modular import system.

Array Methods

Core-JS provides polyfills for numerous array methods, including those introduced in recent ECMAScript specifications. Examples include:

Object Methods

Polyfills for manipulating objects:

String Methods

Polyfills for string manipulation:

Number Methods

Polyfills related to number objects:

Date Methods

Polyfills for working with dates:

Symbol Methods

Polyfills for the Symbol type:

Map, Set, WeakMap, WeakSet

Polyfills for collection types:

Promise Methods

Polyfills for promises:

Reflect API

Polyfills for the Reflect object:

Global Methods

Polyfills for global methods:

Intl (Internationalization)

Polyfills for internationalization:

Typed Arrays

Polyfills for typed arrays:

Other Utility Methods

Core-JS includes several other utility polyfills that don’t neatly fit into the above categories. Consult the Core-JS documentation for a complete list. Examples may include polyfills for features related to iterators, generators, and more. Always check the official documentation for the most current listing and details.

Advanced Usage and Configuration

Customizing the Build Process

While Core-JS 3’s modularity simplifies the import process, you might need to customize your build process for complex scenarios. This typically involves configuration within your bundler (Webpack, Rollup, Parcel, etc.).

In all cases, ensure your bundler is configured to correctly handle ES modules and perform tree-shaking. Incorrect configurations can lead to larger-than-necessary bundle sizes or runtime errors. Consult your bundler’s documentation for detailed instructions.

Debugging and Troubleshooting

Debugging issues related to Core-JS often involves checking for:

Integrating with other libraries

Generally, Core-JS integrates well with most other JavaScript libraries. However, be mindful of potential conflicts:

Performance Considerations

While Core-JS improves compatibility, its use does have performance implications:

Contributing to Core-JS

Core-JS is an open-source project, and contributions are welcome! Before contributing, review the project’s contribution guidelines on its GitHub repository. Contributions may include:

Follow the project’s established workflow and coding style for your contributions to be considered. Engage with the community to discuss your proposed changes before submitting pull requests.

Appendix

Glossary of Terms

Browser Compatibility Table

Feature Chrome Firefox Safari Edge IE
Array.includes
Promise Partial
Object.assign Partial

Note: This is a simplified example. A complete table would list many more features and versions. Refer to the official Core-JS documentation for the most up-to-date and comprehensive browser compatibility information. The symbols ✅ and ❌ represent support and lack of support, respectively. Partial support might indicate that only a subset of the feature is implemented natively.

Changelog

Refer to the official Core-JS repository (usually on GitHub) for the complete changelog. The changelog will detail all changes, bug fixes, new features, and other relevant information for each release.

License Information

Core-JS is typically licensed under the MIT License. Check the official repository’s LICENSE file for the exact terms and conditions. The MIT License generally grants broad permissions to use, modify, and distribute the software.

Community and Support Resources

Remember to always check the official Core-JS website and repository for the most up-to-date information on community resources and support.