Three useful Babel presets
As of version 6, Babel supports presets, sets of features that can be enabled together. This blog post looks at three new useful Babel presets (and, as a bonus, two presets especially for Node.js).
Complementing or replacing babel-preset-es2015
These two presets are useful complements to babel-preset-es2015
(for ES6):
babel-preset-es2016
gives you one of the two features of ES2016: the exponentiation operator. The other feature,Array.prototype.includes()
, is supported via the standard library polyfill.babel-preset-es2017
gives you:babel-plugin-syntax-trailing-function-commas
: optional trailing commas in parameter lists and function/method calls.babel-plugin-transform-async-to-generator
: async functions. This is the one feature after ES6 that I’m most looking forward to.
Note that if you want all of ES2017 (as much as it is supported by Babel) then you need three presets: es2015
, es2016
and es2017
. Alternatively, there is also a meta-preset:
babel-preset-latest
will always contain all the “yearly” presets. At the moment, those are:es2015
,es2016
andes2017
.
Bonus: minimal presets for Node.js
babel-preset-es2015-node
replacesbabel-preset-es2015
. It checks the Node.js version and only enables plugins whose functionality is missing (not much in recent versions).babel-preset-latest-minimal
replacesbabel-preset-latest
. It determines what plugins are needed via feature detection. As the repository’s readme states: that only makes sense for Node.js, but not for browsers.
More information
- Configuring Babel: “Setting up ES6”
- Tracking ES2017: “Feature watch: ECMAScript 2017” (accepted and upcoming features)
- ES2016 and ES2017: “Exploring ES2016 and ES2017”
- ECMAScript features supported by Node.js: node.green
Comments
Post a Comment