liveBook · Manning

Transpilation

As explained in Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond

The source-to-source transformation of code from one programming language or language version into another with similar semantics.

Want to use a new JavaScript feature before every browser supports it? Transpilation translates source code into other source code, letting you write ESNext while producing JavaScript suited to the browsers you need to support. section 2.0 section 2.1

A transpiler is a kind of compiler, but its output is source code rather than machine code. It can translate between languages, as with CoffeeScript and JavaScript, or between versions of JavaScript, as with Babel. section 2.1

To try it with Babel 6:

  • Create a project with npm and install babel-cli as a development dependency. section 2.2
  • Install babel-preset-es2015 and put {"presets": ["es2015"]} in a .babelrc file at the project root. Babel 6 performs no transformations by default; a preset or plugin must be both installed and configured. section 2.2
  • Put your JavaScript in src/index.js, then direct Babel to write the transformed files to dist. section 2.2
Listing 2.1. Compiling from src folder to a dist folder
$ babel src -d dist

The command reads the src directory and writes transpiled files to dist; without the output-directory option, Babel prints the result in the terminal. section 2.2

One small transformation

Put `let foo = "bar";` in src/index.js. With the ES2015 preset configured, the generated dist/index.js contains `"use strict";` followed by `var foo = "bar";`. Check the generated file to see what Babel changed. section 2.2

For a quick check, change the source file, run Babel again, and compare src/index.js with dist/index.js. Keep editing the source, not the generated output. section 2.2

How Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond covers it

How deep it goes
Defines source-to-source compilation and walks through Babel 6 setup, configuration, and output without detailing transpiler internals.
Examples
Uses a let declaration, its strict-mode var output, and a src-to-dist Babel command.
What it assumes
Assumes basic JavaScript and terminal use while defining transpilation and walking through project setup.
What you can do after
Operate/configure Babel 6 to transpile ES2015 source into a dist directory.
How it connects
Connects transpilation to browser support, ESNext, Babel presets, and compile-to-JavaScript languages.

Also explained in

Compare how 4 books explain transpilation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond. The section links point to the parts of the book it draws on.