What is a transpiler ?


A transpiler (also called source-to-source compiler or a transcompiler) is a program that translates a source code from a language to another at the same level of abstraction (which makes it different from a compiler whose output is lower level than its input). The transcompiler can use either as input language an existing one like cfront which converts C++ to C or a new one like Coffeescript which is transcompiled to Javascript. The input language may be a superset of the output language which means that any code written in the output language is valid for the input language.

Why use transcompilation ?

Transcompilation has several advantages :

  • Adding new features to an existing language, for instance Typescript adds static typing and compile-time type checking or cfront which was the original implementation of C++, adds OOP to the C language.

  • Being more efficient when coding. Coffeescript syntax is more concise than JS, for example :

    In coffeescript : add = (a, b) -> a+b

    In javascript : var add = function (a, b) { return a + b; } The codebase is more readable and can be more easily be maintained.

  • As a compilation phase with its checks is added, you can detect mistakes earlier.

  • Encouraging good pratices. Coffeescript compiler automatically converts "==" and "!=" to "===" and "!==" (for non-JS programmers, the two first ones do not check the type unlike the two last ones).

  • Porting a codebase to a new language or converting code to a newer version of the language. Python 3 broke compatibility with Python 2 and a scriptwas written.

Learn more