I am a bit lost in the Babel options / config. I want to use recent js features and compile (with webpack) to browser code.
What is the difference between babel-polyfill and babel plugins with babel-preset-env
?
Are they intended to work together?
Babel includes a polyfill that includes a custom regenerator runtime and core-js. This will emulate a full ES2015+ environment (no < Stage 4 proposals) and is intended to be used in an application rather than a library/tool. (this polyfill is automatically loaded when using babel-node ).
Babel Polyfill adds support to the web browsers for features, which are not available. Babel compiles the code from recent ecma version to the one, which we want. It changes the syntax as per the preset, but cannot do anything for the objects or methods used.
Babel is a toolchain that is mainly used to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript in current and older browsers or environments.
Well, as of Babel 7.4. 0, this powerful package had been deprecated. Composed of two dependent packages, core-js and regenerator-runtime , Babel now recommends installing them as dependencies and importing them at the top level of your application (think of index.
Answer from this article:
The distinction between a
babel transform plugin
versusbabel-polyfill / babel-runtime
is whether or not you can reimplement the feature today, in ES5. For example,Array.from
can be rewritten in ES5 but there is nothing I can write in ES5 to add arrow function syntax to JavaScript. Therefore, there is a transform for arrow functions but none forArray.from
. It will have to be provided by a separate polyfill likebabel-polyfill
, orbabel-runtime
.
As a side note, here is my current understanding of the babel eco-system.
Babel
is a javascript compiler: it parses, transforms and outputs transformed code.
babel-polyfill
and babel-runtime
: the former defines global methods (and pollutes the global scope) whereas the latter transforms your code to make the same functionnality available as explained in this answer.babel syntax / transform plugins
: parse and transform es2015+ syntax (like arrow functions) to convert it to es5.babel-plugins-stage-x
(from stage-0 to stage-4): transform future javascript syntax which is not in the JS specs yet, starting at stage-0 (just an idea) down to stage-4 (will land in the babel-plugins
soon).babel-preset-env
determines the Babel plugins and polyfills needed for a specific environment.target
option, it loads only the plugins required to run on a specific target.builtIn
option, it uses only the babel-polyfill
which are not built-in the target.babel-transform-runtime
yet (as of nov. 2017). (see this issue)babel-preset-env is a Babel preset meant to automatically set up babel plugins and include the necessary babel polyfills based on a set of target environments checked against a feature compatibility table.
In order to make a fully working ES2015+ environment run on a non-ES2015+ client, simple code transpilation is sometimes not enough:
Promise
, Map
, Object.assign
...) are polyfilled with core-js (provided by babel-polyfill, too)So, back to your question, it's babel-preset-env that makes use of babel-polyfill and babel plugins.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With