Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a babel preset? What does stage mean?

Tags:

babeljs

What is a babel preset and why do I need it?

There are several questions about specific babel presets but none explain the need for it (e.g. what's the difference between babel-preset-stage-0, babel-preset-stage-1 etc)

Also the babel docs do not explain the necessity: https://babeljs.io/docs/plugins/preset-latest/

like image 917
Matthias M Avatar asked Oct 11 '17 09:10

Matthias M


People also ask

What is Babel stage?

A Babel preset is a shareable list of plugins. The official Babel Stage presets tracked the TC39 Staging process for new syntax proposals in JavaScript. Each preset (ex. stage-3, stage-2, etc.) included all the plugins for that particular stage and the ones above it.

What is a preset 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.

Does Babel preset order matter?

Ordering matters for each visitor in the plugin. This means if two transforms both visit “Program”, the transforms will run in either plugin or preset order.

Where do you put the Babel preset react?

React - Add @babel/preset-react (https://git.io/JfeDR) to the 'presets' section of your Babel config to enable transformation.


Video Answer


2 Answers

Babel Presets:

  • Technically presets are collections of plugins (as Quentin says)
  • The usecase is the support of particular language features.

Read this excellent post: https://www.fullstackreact.com/articles/what-are-babel-plugins-and-presets

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

... ES2015 is just another name used for ES6 ... [1]

Preset stages:

Stages represent the status of experimental features. Pre stage-3 should be used with caution.

... Beyond ES7, proposed JavaScript features can exist in various stages: [1]

stage-0 - Strawman: just an idea, possible Babel plugin.

stage-1 - Proposal: this is worth working on.

stage-2 - Draft: initial spec.

stage-3 - Candidate: complete spec and initial browser implementations.

stage-4 - Finished: will be added to the next yearly release. [2]

[1] https://www.fullstackreact.com/articles/what-are-babel-plugins-and-presets

[2] https://babeljs.io/docs/plugins/

like image 185
Matthias M Avatar answered Oct 16 '22 18:10

Matthias M


From the docs:

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

Babel is a tool for transforming JS.

A plugin a some code for performing a particular transformation.

You have to specify which plugins you want to use with a config.

A preset is just a prewritten config you can use to get common sets of transforms.

like image 30
Quentin Avatar answered Oct 16 '22 19:10

Quentin