Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Babel a compiler or transpiler?

I've been using Babel for a while now, and I've always been under the impression that Babel was a transpiler for converting my ES6 and ES7 JavaScript into ES5 JavaScript, as I was under the assumption that you could technically treat ES5 and ES6 as two different language.

However, I couldn't help but notice that Babel's website title describes it as a compiler, which I believe is something very different from a transpiler.

Babel website title - 'Babel - The compiler for writing next generation JavaScript

Is Babel a transpiler or a compiler, or perhaps offers both options? Or is the website's title simply incorrect?

Disclosure: I know this sounds like a very pedantic question, but I am writing documentation relating to Babel, and I want to ensure my description is accurate

like image 472
AdamMcquiff Avatar asked May 14 '17 21:05

AdamMcquiff


People also ask

Is Babel a compiler and transpiler?

Babel is a transpiler, which is a special type of compiler, so both terms are techincally correct. You may use either at your preference.

Is Babel a transpiler?

Babel enables developers to use cutting-edge Javascript without worrying about browser support. Babel is a JavaScript transpiler, meaning it converts a newer version of ECMAScript, such as ES9, to a standard version (ES5).

Is a transpiler a compiler?

2. Transpiler. A transpiler is a compiler-like program, which converts ES6 JavaScript code to ES5 JavaScript code so that it can run in browsers. When the transpiler sees an expression that uses a language feature that needs to be translated, it produces a logically equivalent expression.

How does Babel transpiler work?

Babel is a JavaScript transpiler that converts edge JavaScript into plain old ES5 JavaScript that can run in any browser (even the old ones). It makes available all the syntactical sugar that was added to JavaScript with the new ES6 specification, including classes, fat arrows and multiline strings.


2 Answers

The definitions of "transpiler" and "compiler" are blurry. Both of them do translate a program from one language to another language while keeping the behaviour.

We usually name it a "compiler" when it produces an executable binary. However, binary is just another language, which can be interpreted by a CPU. Every program is "executable" on the respective machine.

We usually name it a "compiler" when it produces a lower-level output than the input was, e.g. C to assembler. Or Java to Java bytecode. Or ES8 to ES5. Wait… is that really a different level?

We usually name it a "transpiler" when its output is on a similar level as the input, e.g. Python to JavaScript or the other way round. However, there always will be parts that use an abstraction available in one language that need to be "compiled" to a lower-level implementation in the other language.

So to answer your questions:

I believe a compiler is something very different from a transpiler.

No.

Is Babel a transpiler or a compiler, or perhaps offers both options?

Yes.

Or is the website's title 'Babel - The compiler for writing next generation JavaScript' simply incorrect?

No. That title focuses on next-generation features, i.e. higher-level abstractions that really need to be compiled into a very different output. Even though the output is still ordinary, mostly human-readable, JavaScript.

I am writing documentation relating to Babel, and I want to ensure my description is accurate

In that case, I'd use their own terminology from the official website. If you want to compare the tool to others, choose your own description.

like image 149
Bergi Avatar answered Sep 24 '22 06:09

Bergi


Babel is a transpiler, which is a special type of compiler, so both terms are techincally correct. You may use either at your preference.

It's irrefutable that Babel is a source-to-source compiler (aka transpiler) since its source and target languages are both some flavor of JavaScript:

A source-to-source compiler, transcompiler or transpiler is a type of compiler that takes the source code of a program written in one programming language as its input and produces the equivalent source code in another programming language.

However, not everyone agrees that the distinction between the terms is helpful, so some people prefer simply, "compiler".

I personally like the distinction because, to me, it implies something about the difference in level of abstraction from machine langauge between the source (input) and target (output) languages. That is, typical "compilers" translate from higher-to-lower level languages, "decompilers" translate from lower-to-higher level languages, and "transpilers" translate between languages at similar levels of abstraction.

like image 25
maerics Avatar answered Sep 21 '22 06:09

maerics