How Polyfill Works in Babel (2024)

Babel’s a tool that converts modern JavaScript’s syntax (ECMAScript 2015 and above) into a compatible version (ES5) for older browsers such as IE.

Babel can basically handle these things:

  • Converts syntax to ES5
  • Injects polyfill features automatically into the code
  • Transpiles TypeScript
  • And more

So when you write ES6+ code:

const test = [1,2,3,4,5]test.map(t => t * 2)

Babel transforms syntax like this:

You can see const is converted to var, and the arrow function is transformed to the function statement.

That’s why we can make use of any modern JavaScript in our codebase.

A polyfill is a piece of code that allows you to use the functionality of modern Web APIs on older browsers that don’t support this functionality.

Here’s an example: Object.entries isn’t available in IE11 and other browsers.

How Polyfill Works in Babel (1)

So a polyfill can be used to provide the code snippet that allows functionality in those browsers.

The actual code used for Object.entries is like this:

Basically, if you want to include a polyfill feature, you have to add the snippet manually or use Polyfill.io, which automatically includes all of the polyfills needed in a given browser accessed by a user.

To avoid confusion, let’s make it clear what Babel and polyfills can each cover.

The only thing Babel can do is to convert ES6+ syntax to ES5, if there aren’t any plugins.

The thing polyfills can do is inject a snippet code to support web APIs.

If you write Object.entries in your script and transpile it, you’ll see the result:

"use strict";var test = [1, 2, 3, 4, 5];Object.entries([]);

You can see Object.entries isn’t converted.

It needs to be polyfilled because it’s not a syntax but an API. If it goes on like this in IE11, you’ll run into an error saying Object doesn't support property or method 'entries'.

To avoid that, you have to include a polyfill, like this:

Before running Object.entries, the polyfill covers the feature.

So whether you’re using a syntax or an API is the difference between Babel and polyfills.

As I said, Babel can be used to transform ES6+ code to ES5.

In addition to that, Babal can be used to inject a polyfill during transformation.

This is how Babel adds a polyfill:

  1. Search for target browsers.
  2. Map target browsers to the core-js.
  3. Inject a polyfill.

Search for target browsers

At first, Babel is looking for target browsers needed to be polyfilled in a configuration file such as .babelrc, .babelrc.json, package.json, or babel.config.js.

So if you support IE11, you have to write it like this:

useBuiltIns: usage is an option that allows injection of a polyfill based on the files used in your project.

Map target browsers to the core-js

core-js is a library that provides a set of polyfill features.

Babel uses core-js-compact and maps it to targeted browsers in order to provide the necessary polyfill feature.

So when you target IE11, it’ll map IE11 and provide the polyfill code that IE11 doesn’t support.

Inject a polyfill

Lastly, Babel injects a polyfill based on the mappings.

Let’s take a look at some examples.

If you target IE11 and use core-js@3, you’ll need to write it in .babelrc:

Transpile the code like this:

const test = [1,2,3,4,5]Object.entries(test)

And you’ll see the result:

You can find that the snippets of Object.entries are included there.

If you target the latest Chrome version …

… you’ll see there’s no polyfill code in the output:

This is because Chrome supports Object.entries natively. It doesn’t need to be polyfilled.

How Polyfill Works in Babel (2024)
Top Articles
An Ultimate Guide to Fix
The NPDB - About Us
Katie Pavlich Bikini Photos
Gamevault Agent
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Free Atm For Emerald Card Near Me
Craigslist Mexico Cancun
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Doby's Funeral Home Obituaries
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Select Truck Greensboro
Things To Do In Atlanta Tomorrow Night
Non Sequitur
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Craigslist In Flagstaff
Shasta County Most Wanted 2022
Energy Healing Conference Utah
Testberichte zu E-Bikes & Fahrrädern von PROPHETE.
Aaa Saugus Ma Appointment
Geometry Review Quiz 5 Answer Key
Walgreens Alma School And Dynamite
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Pixel Combat Unblocked
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Rogold Extension
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Weekly Math Review Q4 3
Facebook Marketplace Marrero La
Nobodyhome.tv Reddit
Topos De Bolos Engraçados
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hampton In And Suites Near Me
Stoughton Commuter Rail Schedule
Bedbathandbeyond Flemington Nj
Free Carnival-themed Google Slides & PowerPoint templates
Otter Bustr
Selly Medaline
Latest Posts
Article information

Author: Edmund Hettinger DC

Last Updated:

Views: 5926

Rating: 4.8 / 5 (78 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Edmund Hettinger DC

Birthday: 1994-08-17

Address: 2033 Gerhold Pine, Port Jocelyn, VA 12101-5654

Phone: +8524399971620

Job: Central Manufacturing Supervisor

Hobby: Jogging, Metalworking, Tai chi, Shopping, Puzzles, Rock climbing, Crocheting

Introduction: My name is Edmund Hettinger DC, I am a adventurous, colorful, gifted, determined, precious, open, colorful person who loves writing and wants to share my knowledge and understanding with you.