Image Source: 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).
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 to see who supports what. For example, if you search for Optional chaining operator
, you will get the following Christmas-ish color coded table:
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
Who created Babel?
Behind Babel 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, which in turn uses a JavaScript parser, 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.
- Parsing: A parser (Babylon. How fitting that Babel has a Babylon) reads the code and converts it to an Abstract Syntax Tree (AST)
- Traversal and Transformation: A traverser Babel-Traverse traverses through the AST to analyse and transform the AST as needed
- Output: Babel generator outputs the final code from the transformed AST.
For more detailed and excellent explanation, please check out this article
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.
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
The speed alone would make SWC a no-brainer to switch to.
Next.js also lists other reasons 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.
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.
Top comments (2)
You're a good writer, keep the good working :D
thank you!