What is Babel? And SWC?
Tags: nextjs, babel, swc
Published: 2022-10-24 02:49:32


![The Tower of Babel](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fnm6w3e3jd7fnsr8rnc2.jpeg)
Image Source: [Language Is Baffling - The Story of the Tower of Babel](https://www.thetorah.com/article/language-is-baffling-the-story-of-the-tower-of-babel)

Babel. 

This word is loaded, full of culture and history, involving human and God. The millennia-old-story goes that the people of Babylon wanted to construct a tower to heaven, which infuriated God, who in turn dispersed people across different lands and made them speak different languages so they cannot understand and communicate with each other. The word Bab-El literally means Gate of Heaven (Bab: Gate; El: Heaven). ([Tower of Babel](https://www.britannica.com/topic/Tower-of-Babel)). 

The word is also onomatopoeic (a word that sounds similar to the noise the word refers to). Same as babble, it imitates the manner a person rambles in confusion, which is what I might be doing throughout my following attempt to grasp Babel, then Rust, then SWC. 

Forgive me.

## Babel - The JavaScript compiler 

To JavaScript developers, Babel means one thing only: a JavaScript compiler or transpiler (a tool that translates and compiles). And for a long time, it is the only compiler (to me anyway).

So what does Babel do? Or what does any compiler / transpiler do? 

Babel transpiles modern, newer versions of JavaScript such as ES6 or TypeScript into old fashioned JavaScript understandable by older browsers. 

For example, The following code uses optional chaining operator and object destructure we have been taking for granted:

```
const stuff = {title: 'stuff', quality: 'frustrating'};
const {quality} = stuff?.quality
```

After it goes through Babel, browsers will be fed with the following code instead:

```
const stuff = {
  title: 'stuff',
  quality: 'frustrating'
};
const {
  quality
} = stuff === null || stuff === void 0 ? void 0 : stuff.quality;
```

The compiled code runs in any environment. 

In another word, Babel has been the bridge that allows developers to explore newer and better things without worrying about breaking applications on older / slower-to-catch-up browsers. While the language of JavaScript continues to evolves, version by version, in leaps and bounds, Babel makes sure smooth translations of modern features such as ES6, React JSX extensions and so on can continue to function on older devices.

You can always search [caniuse.com](https://caniuse.com/) to see who supports what. For example, if you search for [`Optional chaining operator`](https://caniuse.com/?search=Optional%20chaining%20operator), you will get the following Christmas-ish color coded table:


![Browser support of Optional chaining operator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/63cmz9y4m9v7eaapfivr.png)

**Babel usage statistics**

Babel has been and is still widely used and is an essential tool in web development.
 
> 1,492,293 live websites using Babel and an additional 4,816,551 sites that used Babel historically and 622,292 websites in the United States.
-- From [Trends.builtwith ](https://trends.builtwith.com/javascript/Babel#:~:text=Babel%20Competitors%20and%20Similar&text=Get%20a%20list%20of%206%2C329%2C548,to%20sites%20in%20this%20list)

**Who created Babel?**

Behind [Babel](https://en.wikipedia.org/wiki/Babel_(transcompiler)) is the creator Sebastian McKenzie and the team now consists a small number of contributors. Babel also makes uses a polyfill library called [core-js](https://www.npmjs.com/package/core-js), which in turn uses a JavaScript parser, [acorn](https://github.com/acornjs/acorn). 

**The Babel Process**

Shall we look a little deeper into the Babel process? 

For Babel, the code-to-code translation/compilation happens in three stages, each undertaken by a different tool. 

Sorry for even more cool terms. Aka Babel-talk. Aka jargons. 

1. Parsing: A parser ([Babylon](https://github.com/babel/babylon). How fitting that Babel has a Babylon) reads the code and converts it to an [Abstract Syntax Tree (AST)](https://en.wikipedia.org/wiki/Abstract_syntax_tree) 
2. Traversal and Transformation: A [traverser Babel-Traverse]  (https://www.npmjs.com/package/babel-traverse) traverses through the AST to analyse and transform the AST as needed
3. Output: 
[Babel generator](https://www.npmjs.com/package/babel-generator) outputs the final code from the transformed AST.

For more detailed and excellent explanation, please check out [this article](https://www.sitepoint.com/understanding-asts-building-babel-plugin/) 

## Rust and SWC

Babel has had its heyday and is still basking in its nearly ubiquitous glory. However, the wheels of technologies are always moving. 

In 2006, a new programming language called Rust was born out of a personal project. In 2014, it had its first stable release. After that, Rust has been proven to be irresistible to all major companies such as Google, Amazon, Facebook, Microsoft. According to [Wikipedia](https://en.wikipedia.org/wiki/Rust_(programming_language),  Rust is the "most loved programming language" every year from 2016 to 2022 (inclusive). 

Rust's secret? More efficient, performant and memory safe. 

Rust is also proven to be versatile and powerful as a developer tool, as evidenced by the rise of SWC (Speedy Web Compiler), which has been quickly adopted by Next.js, which in turn replaces Babel as the compiler.

SWC is fabulously, unbelievably, irresistibly fast.

> SWC is 20x faster than Babel on a single thread and 70x faster on four cores. - [SWC.js](https://swc.rs/)
 
The speed alone would make SWC a no-brainer to switch to. 

Next.js also lists [other reasons](https://nextjs.org/docs/advanced-features/compiler#why-swc) such as extensibility, support for WASM (WebAssembly) and the amazing Rust community and ecosystem for the switch from Babel. 

The transition from Babel to SWC is a given and the timing  will be swift. 

## Play with SWC with the playground

First, install SWC from npm.

```
// install swc core as well as its cli tool
npm install --save-dev @swc/core @swc/cli
```

Then we can run the following commands to output the transformed code to a new file or a directory:


```
// output to a file
npx swc myfile.js -o output.js

// output all files in the src directory to the destination directory
npx swc src -d transpiledDir
```

To see SWC live, the easiest way is to run the transformation in the [swc official playground](https://play.swc.rs/).

Taking our previous example you get the following code instantly (with the option of minify on):

```
var stuff={title:"stuff",quality:"frustrating"};var quality=(stuff===null||stuff===void 0?void 0:stuff.quality).quality;
```

## SWC in Next.js

Next.js applications by default use SWC to transform and minify the JavaScript code for production. However, if the application has an existing Babel configuration (such as `babel.config.js`) or are using unsupported features, it will fall back to use Babel. In such cases, you will get a little info snippet from Next.js as the following:

```  
info  - Disabled SWC as replacement for Babel because of custom Babel configuration "babel.config.js" https://nextjs.org/docs/messages/swc-disabled
```

The SWC compiler used by Next.js support many features such as styled component, Jest, emotion. And Next.js continues to work on the SWC compiler to accept more plugins and incorporate many more features.

## Not a conclusion
I have to say, SWC is completely new to me. The learning goes on, as the race to better and faster things.