Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between plugins and presets in .babelrc

Situation

So I have a .babelrc like this:

{     "presets": [         "es2015",         "stage-2",         "react"     ],     "plugins": [         "transform-decorators-legacy"     ] } 

Question

What is the difference between presets and plugins? Which one should I use to configure Babel?

like image 706
totymedli Avatar asked Aug 29 '17 16:08

totymedli


People also ask

What are presets and plugins 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.

What do Babel plugins do?

Babel is a JavaScript compiler which allows us to use the latest and greatest version of JavaScript by converting it into code that the browsers understand. It is undoubtedly one of the most powerful tools in the market to help with Frontend Development.

What is the purpose of using Babel preset react preset?

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.

How does Babel preset work?

@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!


1 Answers

tl;dr

Presets are just a collection of plugins. You can include plugins individually in the plugins array, or collection of plugins in the presets array. If a plugin is part of a collection (preset), you don't have to include it individually in plugins.

The same goes for npm packages when you include them in package.json.

Presets vs Plugins

Babel has lots of official and third party plugins. Presets are collections of plugins or as they say:

Presets are sharable .babelrc configs or simply an array of babel plugins.

An important difference between the two is that plugins are loaded before presets.

Plugins of a preset

The most common presets are the official ones and the discontinued experimental presets.

Most of the official presets contain plugins to transpile features of the EcmaScript standards, while the experimental (stage-x) presets contained plugins that transpiled future experimental features whose standardization is still a work in progress. These experimental/proposal presets are deprecated since Babel 7. They have a blog entry on the reasons. Read the section below to see how they worked.

When you click on a preset, you can see which plugins (and maybe other presets) are included in it. If you include a plugin via a preset you don't have to include it individually. The same goes for package.json when you include the npm packages of the presets.

Deprecated proposal preset system

Going from stage 0 (just an idea) to stage 3 (candidate) you had collections of plugins that were more closer to getting standardized. Because of this, when you included a preset, every preset with a higher stage-x value were included too. The plugins contained in these presets were continuously varying in each version, since they are a work in progress, and there is a chance that plugins will be removed if they got rejected. That is why you needed transform-decorators-legacy because, decorator transpiling was earlier removed from Babel, although they added it back later.

like image 89
totymedli Avatar answered Nov 05 '22 12:11

totymedli