Building Enterprise JavaScript Applications
上QQ阅读APP看书,第一时间看更新

@babel/polyfill

Newer versions of ECMAScript provide new and cleaner syntax, and Babel transpiles the new syntax down to older versions of ECMAScript. However, this is more difficult (if not impossible) to do in the same way if you're using newer JavaScript APIs.

For example, if you're using the new fetch API instead of XMLHttpRequest, Babel won't be able to transpile this down. For APIs, we must use a polyfill; luckily, Babel provides the @babel/polyfill package.

A polyfill is code that checks whether a feature is supported in the environment, and if not, provides methods that mimic the native implementation.

To use the polyfill, you must first install it as a dependency (not a development dependency):

$ yarn add @babel/polyfill

Then, import the @babel/polyfill package at the top of your code and it'll mutate existing global variables to polyfill methods that are not yet supported:

require("@babel/polyfill"); # ES5
import "@babel/polyfill"; # ES6
@babel/polyfill uses   core-js  as its underlying polyfill.