# babel/babel-polyfills architecture

> Architecture diagram of the GitHub repository babel/babel-polyfills, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/babel/babel-polyfills
- Repository: https://github.com/babel/babel-polyfills

## Overview

##### Project type and purpose
- This is an open-source monorepo of Babel “polyfill provider” plugins and helpers.
- It contains several packages (for core-js2, core-js3, es-shims, regenerator) plus a shared helper (`@babel/helper-define-polyfill-provider`), build scripts, docs, and tests.
- The goal is to let Babel users automatically inject only the needed polyfills into their compiled code based on targets and usage.

##### High-level architecture
- At the **root**:
- Yarn 2 (“berry”) workspaces & Lerna orchestrate package linking, bootstrapping, building, testing, and publishing.
- CI pipeline (GitHub Actions) triggers `yarn bootstrap`, `yarn build` and `yarn test`.
- `scripts/` generate data files (e.g. ES-shims mappings, core-js3 shipped-proposals) used by packages.
- **Packages** folder:
- `babel-helper-define-polyfill-provider` — core helper that plugin packages consume for defining injection logic.
- `babel-plugin-polyfill-corejs2`, `…-corejs3`, `…-es-shims`, `…-regenerator` — each implements injection methods (`entry-global`, `usage-global`, `usage-pure`) using the helper.
- Each package contains:
– `src/` (TypeScript or JS visitors, resolvers, injectors)
– `test/` (fixture-based tests, spawn fixtures for Rollup/Webpack/CLI)
– `package.json`, README, LICENSE

##### Data flow and component interactions
- **User code** → processed by **Babel core** + selected **polyfill plugin** (one of the plugin packages)
- Plugin uses **helper-define-polyfill-provider** APIs to:
a. parse plugin options (targets, injection method)
b. resolve feature-to-module mapping via data (from core-js compat, es-shims data, shipped proposals)
c. traverse AST and inject `import` statements (`imports-injector.ts`)
- **Build scripts** produce or update mapping data that helper and plugins load at runtime.
- **Tests** run each package’s plugin on fixtures (simulating CLI, Rollup, Webpack) and assert transformed output.

##### Key technologies and frameworks
- Mono-repo management: **Yarn workspaces**, **Lerna**
- Language: **TypeScript** + **Babel** for transpilation
- Testing: fixture-based tests under each `test/` folder
- CI: **GitHub Actions** workflow in `.github/workflows/ci.yml`
- Build tooling: custom scripts in `scripts/`, plus `babel.config.json`, `Gulpfile.js`

##### Architectural patterns and design principles
- **Workspace** pattern: isolate each plugin as an independent package, share common helper.
- **Provider abstraction**: all plugins share the same “define provider” API, making them interchangeable and extensible.
- **Data-driven mapping**: polyfill feature → module mapping loaded from generated data files rather than hard-coded.
- **Fixture testing**: validate code transforms in real-world setups (CLI, bundlers).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% CI Pipeline
    CI["GitHub Actions CI"]:::build
    click CI "https://github.com/babel/babel-polyfills/blob/main/.github/workflows/ci.yml"

    %% Root Workspace
    subgraph "Root Workspace" 
        direction TB
        pkg["package.json"]:::build
        click pkg "https://github.com/babel/babel-polyfills/blob/main//package.json"
        lerna["lerna.json"]:::build
        click lerna "https://github.com/babel/babel-polyfills/blob/main//lerna.json"
        yarnrc[".yarnrc.yml"]:::build
        click yarnrc "https://github.com/babel/babel-polyfills/blob/main//.yarnrc.yml"
        yarnRel["yarn-4.9.2.cjs"]:::build
        click yarnRel "https://github.com/babel/babel-polyfills/blob/main//.yarn/releases/yarn-4.9.2.cjs"
        gulp["Gulpfile.js"]:::build
        click gulp "https://github.com/babel/babel-polyfills/blob/main//Gulpfile.js"
        babelcfg["babel.config.json"]:::build
        click babelcfg "https://github.com/babel/babel-polyfills/blob/main//babel.config.json"
    end

    CI -->|"triggers"| pkg

    %% Build Scripts & Data Generation
    subgraph "Build Scripts & Data" 
        direction TB
        scripts["scripts/"]:::build
        click scripts "https://github.com/babel/babel-polyfills/tree/main//scripts"
        esShimBuilder["build-es-shims-data/index.js"]:::build
        click esShimBuilder "https://github.com/babel/babel-polyfills/blob/main//scripts/build-es-shims-data/index.js"
        esShimFeatures["es-shims-features.js"]:::build
        click esShimFeatures "https://github.com/babel/babel-polyfills/blob/main//scripts/build-es-shims-data/es-shims-features.js"
        esShimUtils["utils-build-data.js"]:::build
        click esShimUtils "https://github.com/babel/babel-polyfills/blob/main//scripts/build-es-shims-data/utils-build-data.js"
        corejs3Builder["build-corejs3-shipped-proposals.mjs"]:::build
        click corejs3Builder "https://github.com/babel/babel-polyfills/blob/main//scripts/build-corejs3-shipped-proposals.mjs"
        corejs3Special["corejs3-shipped-proposals-special-cases.mjs"]:::build
        click corejs3Special "https://github.com/babel/babel-polyfills/blob/main//scripts/corejs3-shipped-proposals-special-cases.mjs"
        compatDownloader["download-compat-table.js"]:::build
        click compatDownloader "https://github.com/babel/babel-polyfills/blob/main//scripts/download-compat-table.js"
    end

    pkg -->|"runs"| scripts

    scripts -->|"generate"| data["Feature-to-module Data"]:::runtime

    %% Helper Package
    subgraph "Shared Helper Package" 
        direction TB
        helperRoot["@babel/helper-define-polyfill-provider"]:::runtime
        click helperRoot "https://github.com/babel/babel-polyfills/tree/main//packages/babel-helper-define-polyfill-provider"
        defineProvider["define-provider.ts"]:::runtime
        click defineProvider "https://github.com/babel/babel-polyfills/blob/main//packages/babel-helper-define-polyfill-provider/src/define-provider.ts"
        injector["imports-injector.ts"]:::runtime
        click injector "https://github.com/babel/babel-polyfills/blob/main//packages/babel-helper-define-polyfill-provider/src/imports-injector.ts"
        metaResolver["meta-resolver.ts"]:::runtime
        click metaResolver "https://github.com/babel/babel-polyfills/blob/main//packages/babel-helper-define-polyfill-provider/src/meta-resolver.ts"
        visitorEntry["visitors/entry.ts"]:::runtime
        click visitorEntry "https://github.com/babel/babel-polyfills/blob/main//packages/babel-helper-define-polyfill-provider/src/visitors/entry.ts"
        visitorUsage["visitors/usage.ts"]:::runtime
        click visitorUsage "https://github.com/babel/babel-polyfills/blob/main//packages/babel-helper-define-polyfill-provider/src/visitors/usage.ts"
    end

    data --> helperRoot

    %% Plugin Packages
    subgraph "Plugin Packages"
        direction TB

        %% core-js2
        subgraph "CoreJS2 Provider"
            direction TB
            p2Root["babel-plugin-polyfill-corejs2"]:::runtime
            click p2Root "https://github.com/babel/babel-polyfills/tree/main//packages/babel-plugin-polyfill-corejs2"
            p2Index["index.ts"]:::runtime
            click p2Index "https://github.com/babel/babel-polyfills/blob/main//packages/babel-plugin-polyfill-corejs2/src/index.ts"
            p2BuiltIns["built-in-definitions.ts"]:::runtime
            click p2BuiltIns "https://github.com/babel/babel-polyfills/blob/main//packages/babel-plugin-polyfill-corejs2/src/built-in-definitions.ts"
            p2Platform["add-platform-specific-polyfills.ts"]:::runtime
            click p2Platform "https://github.com/babel/babel-polyfills/blob/main//packages/babel-plugin-polyfill-corejs2/src/add-platform-specific-polyfills.ts"
        end

        %% core-js3
        subgraph "CoreJS3 Provider"
            direction TB
            p3Root["babel-plugin-polyfill-corejs3"]:::runtime
            click p3Root "https://github.com/babel/babel-polyfills/tree/main//packages/babel-plugin-polyfill-corejs3"
            compatData["core-js-compat"]:::runtime
            click compatData "https://github.com/babel/babel-polyfills/tree/main//packages/babel-plugin-polyfill-corejs3/core-js-compat"
            p3Index["index.ts"]:::runtime
            click p3Index "https://github.com/babel/babel-polyfills/blob/main//packages/babel-plugin-polyfill-corejs3/src/index.ts"
            runtimePaths["babel-runtime-corejs3-paths.ts"]:::runtime
            click runtimePaths "https://github.com/babel/babel-polyfills/blob/main//packages/babel-plugin-polyfill-corejs3/src/babel-runtime-corejs3-paths.ts"
            shippedProps["shipped-proposals.ts"]:::runtime
            click shippedProps "https://github.com/babel/babel-polyfills/blob/main//packages/babel-plugin-polyfill-corejs3/src/shipped-proposals.ts"
        end

        %% es-shims
        subgraph "ES-Shims Provider"
            direction TB
            esRoot["babel-plugin-polyfill-es-shims"]:::runtime
            click esRoot "https://github.com/babel/babel-polyfills/tree/main//packages/babel-plugin-polyfill-es-shims"
            esDataJS["polyfills.js"]:::runtime
            click esDataJS "https://github.com/babel/babel-polyfills/blob/main//packages/babel-plugin-polyfill-es-shims/data/polyfills.js"
            esDataJSON["polyfills.json"]:::runtime
            click esDataJSON "https://github.com/babel/babel-polyfills/blob/main//packages/babel-plugin-polyfill-es-shims/data/polyfills.json"
            esIndex["index.ts"]:::runtime
            click esIndex "https://github.com/babel/babel-polyfills/blob/main//packages/babel-plugin-polyfill-es-shims/src/index.ts"
            esMappings["mappings.ts"]:::runtime
            click esMappings "https://github.com/babel/babel-polyfills/blob/main//packages/babel-plugin-polyfill-es-shims/src/mappings.ts"
        end

        %% regenerator
        subgraph "Regenerator Provider"
            direction TB
            regRoot["babel-plugin-polyfill-regenerator"]:::runtime
            click regRoot "https://github.com/babel/babel-polyfills/tree/main//packages/babel-plugin-polyfill-regenerator"
            regIndex["index.ts"]:::runtime
            click regIndex "https://github.com/babel/babel-polyfills/blob/main//packages/babel-plugin-polyfill-regenerator/src/index.ts"
        end
    end

    helperRoot -->|"consumed by"| p2Root
    helperRoot --> p3Root
    helperRoot --> esRoot
    helperRoot --> regRoot
    data --> p2Root
    data --> p3Root
    data --> esRoot

    %% Babel Core and User Code
    babel["Babel Core"]:::runtime
    user["User Code"]:::runtime

    p2Root -->|"plugin executed by"| babel
    p3Root --> babel
    esRoot --> babel
    regRoot --> babel
    babel -->|"transformed output"| user

    %% Tests
    subgraph "Global & Plugin Tests"
        direction TB
        globalTest["Global ESM Tests"]:::test
        click globalTest "https://github.com/babel/babel-polyfills/blob/main//test/esm/index.mjs"
        globalPkg["Global ESM package.json"]:::test
        click globalPkg "https://github.com/babel/babel-polyfills/blob/main//test/esm/package.json"
        t2["CoreJS2 Tests"]:::test
        click t2 "https://github.com/babel/babel-polyfills/tree/main//packages/babel-plugin-polyfill-corejs2/test"
        t3["CoreJS3 Tests"]:::test
        click t3 "https://github.com/babel/babel-polyfills/tree/main//packages/babel-plugin-polyfill-corejs3/test"
        tes["ES-Shims Tests"]:::test
        click tes "https://github.com/babel/babel-polyfills/tree/main//packages/babel-plugin-polyfill-es-shims/test"
        tr["Regenerator Tests"]:::test
        click tr "https://github.com/babel/babel-polyfills/tree/main//packages/babel-plugin-polyfill-regenerator/test"
        thelper["Helper Tests"]:::test
        click thelper "https://github.com/babel/babel-polyfills/tree/main//packages/babel-helper-define-polyfill-provider/test"
    end

    CI -->|"runs tests"| globalTest
    CI --> thelper
    CI --> t2
    CI --> t3
    CI --> tes
    CI --> tr

    %% Styles
    classDef build fill:#fdd,stroke:#f00,color:#000
    classDef runtime fill:#ddf,stroke:#00f,color:#000
    classDef test fill:#dfd,stroke:#0a0,color:#000
```
