CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-babel-plugin-polyfill-corejs3

A Babel plugin to inject imports to core-js@3 polyfills based on usage patterns and browser compatibility targets

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

babel-plugin-polyfill-corejs3

A Babel plugin that provides automated polyfill injection for core-js@3, enabling selective loading of JavaScript polyfills based on compilation targets and actual usage in code. It analyzes code usage patterns and browser compatibility requirements to determine which ES6+ features need polyfilling for older browsers.

Package Information

  • Package Name: babel-plugin-polyfill-corejs3
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install --save-dev babel-plugin-polyfill-corejs3

Core Imports

// Plugin is used via Babel configuration, not direct imports
// Add to babel.config.js or .babelrc.json:
{
  "plugins": [["polyfill-corejs3", options]]
}

The plugin is built using @babel/helper-define-polyfill-provider and depends on core-js-compat for polyfill data.

Basic Usage

// babel.config.js
module.exports = {
  plugins: [
    ["polyfill-corejs3", {
      method: "usage-global",
      version: "3.43"
    }]
  ]
};
// .babelrc.json
{
  "plugins": [
    ["polyfill-corejs3", {
      method: "usage-pure",
      version: require("core-js-pure/package.json").version
    }]
  ]
}

Architecture

The plugin is built using the @babel/helper-define-polyfill-provider framework, which provides a standardized pattern for polyfill injection plugins. It returns a provider object that Babel calls during transformation.

The plugin operates through three main injection methods:

  • usage-global: Analyzes code usage patterns and injects global polyfills for detected ES6+ features
  • usage-pure: Injects pure (non-global) polyfill imports that don't modify global objects
  • entry-global: Replaces existing core-js import statements with specific polyfill imports

The plugin integrates with Babel's compilation pipeline to transform code based on:

  • Target browser compatibility (via @babel/preset-env integration)
  • Actual feature usage detection in source code
  • Version-specific polyfill availability from core-js-compat data
  • Proposal and experimental feature support
  • Optional Babel runtime integration for optimized pure imports

Capabilities

Plugin Configuration

Main Babel plugin function that returns a polyfill provider.

/**
 * Creates a Babel plugin for core-js@3 polyfill injection using defineProvider
 * @param options - Plugin configuration options
 * @returns Babel polyfill provider plugin object
 */
export default function(options: Options): PolyfillProvider;

interface Options {
  /** core-js version to target (default: 3) */
  version?: number | string;
  /** Include ES proposals (default: false) */
  proposals?: boolean;
  /** Include shipped proposals (default: false) */
  shippedProposals?: boolean;
}

interface PolyfillProvider {
  /** Plugin identifier */
  name: "corejs3";
  /** Babel runtime name for integration */
  runtimeName: string | null;
  /** Available polyfills data from core-js-compat */
  polyfills: object;
  /** Filter function to determine which polyfills to include */
  filterPolyfills(name: string): boolean;
  /** Handle entry-global method transformation */
  entryGlobal(meta: ImportMeta, utils: TransformUtils, path: NodePath): void;
  /** Handle usage-global method transformation */
  usageGlobal(meta: FeatureMeta, utils: TransformUtils, path: NodePath): boolean | void;
  /** Handle usage-pure method transformation */
  usagePure(meta: FeatureMeta, utils: TransformUtils, path: NodePath): void;
  /** Babel visitor for AST transformation (usage-global method only) */
  visitor?: {
    CallExpression(path: NodePath): void;
    Function(path: NodePath): void;
    "ForOfStatement|ArrayPattern"(path: NodePath): void;
    SpreadElement(path: NodePath): void;
    YieldExpression(path: NodePath): void;
    Class(path: NodePath): void;
  };
}

Usage Examples:

// Basic usage-global configuration
{
  plugins: [
    ["polyfill-corejs3", {
      method: "usage-global",
      version: "3.43"
    }]
  ]
}

// Library author configuration with version from package.json
{
  plugins: [
    ["polyfill-corejs3", {
      method: "usage-pure",
      version: require("core-js-pure/package.json").version,
      proposals: false
    }]
  ]
}

// Application bundle with proposals enabled
{
  plugins: [
    ["polyfill-corejs3", {
      method: "entry-global",
      version: require("core-js/package.json").version,
      proposals: true,
      shippedProposals: true
    }]
  ]
}

Method Types

The plugin supports three injection methods controlled by Babel configuration:

usage-global Method

Analyzes code usage patterns and injects global polyfills for detected ES6+ features.

/**
 * Handles usage-global method transformation
 * Injects global polyfills based on detected feature usage
 * @param meta - Metadata about the detected feature
 * @param utils - Babel transformation utilities
 * @param path - AST node path
 * @returns true if polyfill was injected, void otherwise
 */
usageGlobal(meta: FeatureMeta, utils: TransformUtils, path: NodePath): boolean | void;

usage-pure Method

Injects pure (non-global) polyfill imports that don't modify global objects.

/**
 * Handles usage-pure method transformation
 * Injects pure polyfill imports without modifying globals
 * @param meta - Metadata about the detected feature
 * @param utils - Babel transformation utilities  
 * @param path - AST node path
 */
usagePure(meta: FeatureMeta, utils: TransformUtils, path: NodePath): void;

entry-global Method

Replaces existing core-js import statements with specific polyfill imports.

/**
 * Handles entry-global method transformation
 * Replaces core-js imports with specific polyfill imports
 * @param meta - Import metadata
 * @param utils - Babel transformation utilities
 * @param path - AST node path
 */
entryGlobal(meta: ImportMeta, utils: TransformUtils, path: NodePath): void;

Configuration Options

Version Option

Specifies the core-js version to target for polyfill compatibility.

interface VersionOption {
  /** core-js version to target (default: "3.0") */
  version?: number | string;
}

Usage Examples:

// Using specific version
{
  version: "3.43.0"
}

// Using version from package.json for applications
{
  version: require("core-js/package.json").version
}

// Using version from package.json for libraries
{
  version: require("core-js-pure/package.json").version
}

Proposals Option

Controls whether ES proposals are included in polyfill injection.

interface ProposalsOption {
  /** Include ES proposals (default: false) */
  proposals?: boolean;
}

Shipped Proposals Option

Controls whether shipped proposals are included in polyfill injection.

interface ShippedProposalsOption {
  /** Include shipped proposals (default: false) */
  shippedProposals?: boolean;
}

Utility Functions

Core utility functions used internally by the plugin for transformation logic.

Core-js Module Path Generation

/**
 * Generates the full module path for a core-js polyfill
 * @param name - Polyfill name (e.g., "es.array.includes")
 * @returns Full module path (e.g., "core-js/modules/es.array.includes.js")
 */
function coreJSModule(name: string): string;

/**
 * Generates pure helper import path for core-js features
 * @param name - Helper name
 * @param useBabelRuntime - Whether to use @babel/runtime-corejs3
 * @param ext - File extension
 * @returns Import path for pure helper
 */
function coreJSPureHelper(name: string, useBabelRuntime: boolean, ext: string): string;

/**
 * Checks if a source string is a core-js import
 * @param source - Import source string
 * @returns Array of polyfill names if core-js source, false otherwise
 */
function isCoreJSSource(source: string): string[] | false;

AST Transformation Utilities

/**
 * Transforms method calls with proper context handling
 * @param path - AST node path
 * @param id - Identifier to call
 * @param optionalCall - Whether call should be optional
 * @param wrapCallee - Optional function to wrap callee
 */
function callMethod(
  path: NodePath,
  id: t.Identifier,
  optionalCall?: boolean,
  wrapCallee?: (callee: t.Expression) => t.Expression
): void;

/**
 * Memoizes object context for safe transformations
 * @param node - Member expression node
 * @param scope - Babel scope
 * @returns Tuple of context expressions
 */
function maybeMemoizeContext(
  node: t.MemberExpression | t.OptionalMemberExpression,
  scope: NodePath["scope"]
): [t.Expression, t.Expression];

/**
 * Extracts optional chaining check logic
 * @param scope - Babel scope
 * @param node - Optional member expression node
 * @returns Function that wraps expression with null check
 */
function extractOptionalCheck(
  scope: NodePath["scope"],
  node: t.OptionalMemberExpression
): (expr: t.Expression) => t.ConditionalExpression;

Constants and Data

Babel Runtime Constant

/** Babel runtime package name for core-js@3 integration */
const BABEL_RUNTIME: "@babel/runtime-corejs3";

Polyfill Descriptors

interface CoreJSPolyfillDescriptor {
  /** Polyfill name */
  name: string;
  /** Pure import path (null if not available) */
  pure: string | null;
  /** Array of global polyfill dependencies */
  global: string[];
  /** Array of excluded patterns (null if none) */
  exclude: string[] | null;
}

/** Built-in constructor polyfills */
const BuiltIns: { [key: string]: CoreJSPolyfillDescriptor };

/** Static method polyfills */
const StaticProperties: { [key: string]: { [key: string]: CoreJSPolyfillDescriptor } };

/** Instance method polyfills */
const InstanceProperties: { [key: string]: { [key: string]: CoreJSPolyfillDescriptor } };

/** Common iterator polyfills */
const CommonIterators: string[];

/** Promise-related polyfills */
const PromiseDependencies: string[];

/** Promise polyfills with iterator support */
const PromiseDependenciesWithIterators: string[];

/** Decorator metadata polyfills */
const DecoratorMetadataDependencies: string[];

Runtime Integration

Babel Runtime Paths

/** Supported stable @babel/runtime-corejs3 paths */
const stable: Set<string>;

/** Supported proposal @babel/runtime-corejs3 paths */
const proposals: Set<string>;

Shipped Proposals List

/** Set of shipped proposal polyfill names auto-generated from core-js */
const shippedProposalsList: Set<string>;

Filter Functions

Usage Filter

/**
 * Determines if specific polyfills can be skipped based on usage patterns
 * @param desc - Polyfill descriptor
 * @param path - AST node path
 * @returns true if polyfill can be skipped
 */
function canSkipPolyfill(desc: CoreJSPolyfillDescriptor, path: NodePath): boolean;

Types

interface FeatureMeta {
  kind: "global" | "static" | "instance" | "property" | "in";
  object?: string;
  property?: string;
  key?: string;
  placement?: "prototype" | "static";
}

interface ImportMeta {
  kind: "import";
  source: string;
}

interface TransformUtils {
  injectGlobalImport(source: string, name: string): void;
  injectDefaultImport(source: string, hint: string): t.Identifier;
}

type NodePath = import("@babel/traverse").NodePath;
type t = typeof import("@babel/types");
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/babel-plugin-polyfill-corejs3@0.13.x
Publish Source
CLI
Badge
tessl/npm-babel-plugin-polyfill-corejs3 badge