Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the difference between babel-preset-es2015 and babel-preset-env?

I'm currently trying to understand about babel configuration, but got confused by babel-preset-**, there are many preset in babel, like env, es2015, react and others, I do understand that babel-preset-es2015 is needed to transpile es2015 code to previous js code so it can be understood by most/older browser, what about babel-preset-env?

What are the differences between those presets? Can one use env without using es2015 or vice versa? and what are the cases when we need those two presets to be present on our project build system?

Thank You.

like image 858
xcode Avatar asked Mar 13 '17 16:03

xcode


People also ask

What is Babel preset ENV?

@babel/preset-env is a smart preset that allows you to use the latest JavaScript without needing to micromanage which syntax transforms (and optionally, browser polyfills) are needed by your target environment(s). This both makes your life easier and JavaScript bundles smaller!

What are presets in Babel?

In Babel, a preset is a set of plugins used to support particular language features. The two presets Babel uses by default: es2015 : Adds support for ES2015 (or ES6) JavaScript. react : Adds support for JSX.

Why does Babel preset react?

In case you want the feature to be supported on recent versions of browsers, babel will convert the code only if there is no support of features on those browsers. With Preset react, Babel will transpile the code when to react. To work with Presets, we need to create . babelrc file in our project root folder.

What does a Babelrc file do?

The . babelrc file is your local configuration for your code in your project. Generally you would put it in the root of your application repo. It will affect all files that Babel processes that are in the same directory or in sibling directories of the .

What are presets in Babel?

In Babel, a preset is a set of plugins used to support particular language features. The two presets Babel uses by default: es2015: Adds support for ES2015 (or ES6) JavaScript react: Adds support for JSX

Which Babel plugin should I use for ES2015?

We recommend the .babelrc approach. Remember: In the default Babel setup, es2015 and react are automatically enabled -- no configuration needed. If you're curious to see a live example of a Babel plugin in-action, check out our article on property initializers.

What is Babel and how do I use it?

That's where Babel comes in. Babel is a JavaScript transpiler. Babel enables us to write modern JavaScript that will be "transpiled" to widely-supported ES5 JavaScript. We call this process transpiling. There are a few ways to use Babel in your projects. The simplest and fastest way is to use the package babel-standalone.

How do I use Babel with react?

Now, Babel will automatically transpile any script tags with type text/babel or text/jsx: Projects like create-react-app automatically include and configure Babel for you, so you can use a sane Babel configuration out-of-the-box to write React components. In Babel, a preset is a set of plugins used to support particular language features.


1 Answers

The babel-preset-es20XX (15, 16, 17) presets include transforms needed to convert features added in that specific year to code that is compatible with the previous version.

babel-preset-env includes transforms for all features that have landed in the spec, but only enables the ones needed to make the features work based on the set of environments you've provided to it. If you pass no options to env it essentially works like es2015, es2016, es2017 all together.

babel-preset-react is the set of transformations needed to convert React/Facebook-related syntax extensions like Flowtype and React's JSX.

like image 167
loganfsmyth Avatar answered Sep 27 '22 17:09

loganfsmyth