How babel/babel-polyfills works
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).