JavaScript is the only programming language beside HTML and CSS has the privilege to run on browsers, that’s why there are so many languages used JavaScript as transpile target.
Transpilers (also called transcompilers, or compilers) in JavaScript are source-to-source compilers that transform source code in non-JavaScript languages (CoffeeScript, TypeScript, LiveScript, etc.) or in modern JavaScript versions (ES2015, ES2017, ESNext, etc.) to equivalent JavaScript source code that meets some conditions (browser compatible, minified, strict, etc.)
Transpilers allow developers to write future facing code, even though the current version of the language isn’t supported in all environments. (Henry Zhu)
You might see people use compiler and transpiler interchangeably in JavaScript world. But keep in mind a source-to-source compiler translates between programming languages that operate at approximately the same level of abstraction; while a traditional compiler translates from a higher-level programming language to a lower-level programming language like C to assembler or Java to bytecode.
// ES2020 nullish coalescing
function greet(input) {
return input ?? "Hello world";
}
// Babel Output: ES5 equivalent
function greet(input) {
return input != null ? input : "Hello world";
}
Esbuild (37.4k ⭐) — This is more of a web bundler, but it has loaders that behave like JavaScript transpiler: all modern JavaScript syntax is supported, built-in support for parsing TypeScript syntax and discarding the type annotations.
TypeScript (98.4k ⭐) — An open-source programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript ,adds optional static typing to the language, and ultimately transpiled to JavaScript.
As well as extending JavaScript, TypeScript also transpiles your code to match multiple ECMAScript standards, which gives you a way to support multiple browsers with less effort, and to try out proposed ECMAScript standards early on.
Check out the list of languages that compile to JavaScript and many other transpilers for more information.
Please check out this awesome super tiny compiler. Here’s a summary of how it works:
Most transpilers use Abstract Syntax Tree (AST) as intermediate format while processing source file, transforming syntax, performing optimizations. AST allows for this to take place because it breaks down code and organizes it with all of its metadata in a hierarchical tree.
Code --(parse)--> AST --(transform)--> AST --(generate)--> Code
Lexical Analysis takes the raw code and splits it apart into these things called tokens by a thing called a tokenizer (or lexer). Tokens are an array of tiny little objects that describe an isolated piece of the syntax. They could be numbers, labels, punctuation, operators, whatever.
Syntactic Analysis takes the tokens and reformats them into a representation that describes each part of the syntax and their relation to one another.
When transforming the AST we can manipulate nodes by adding/removing/replacing properties, we can add new nodes, remove nodes, or we could leave the existing AST alone and create an entirely new one based on it.