Magic with Babel Macro

Rate this content
Bookmark
The video explains the importance of understanding Abstract Syntax Trees (AST) in programming, particularly for developers using transpilers. AST Explorer is recommended for exploring AST structures, which can help in transforming code, such as reversing variable names. Babel macros are highlighted as a useful tool for code transformation without needing multiple plugins, making them easier to set up and debug. The talk also covers different types of variable declarations and function declarations in AST, as well as the differences between them. Examples of Babel macros include i18n translation and CSS to React Native macros. The video also touches on the evolution of Babel from a simple transpiler to a more versatile tool.

From Author:

The talk will cover all the possibilities Babel Macro opens up and also gives an overview of Abstract Syntax Tree.

This talk has been presented at React Advanced Conference 2021, check out the latest edition of this React Conference.

FAQ

The purpose of Trubesh's talk is to give an introduction to Babel macros so that attendees can write their own plugins or build their own macros.

Babel macros were introduced around 2017-2018.

An Abstract Syntax Tree (AST) is a tree representation of the source code of a computer program that conveys the structure of the source code. It is used by transpilers to understand and transform code.

Developers should learn about AST because it is a fundamental part of how transpilers work. Understanding AST helps developers comprehend how their code is being transpiled.

One can use AST Explorer to explore and understand the structure of AST. It provides a visual representation of the AST and allows users to write and transform code.

Babel started out as a transpiler that allows developers to write the latest versions of ECMAScript, even in environments where those features are not yet supported. It has evolved to become more than just a transpiler.

Babel macros are a way to apply code transformations without needing to install multiple plugins for each transformation. They are supported in frameworks like CRA, Gatsby, and Next.js.

Babel macros are easier to set up and configure, especially in tools like CRA or Gatsby. They reduce the risk of conflicts and debugging issues that can arise from the order of plugin installation.

One example of a Babel macro is the i18n translation macro, which transforms strings into their corresponding translations. Another example is the CSS to React Native macro, which converts normal CSS into React Native style sheets.

When writing Babel macros, one must ensure that they run synchronously in the node environment and are evaluated at compile time.

Dhrubesh Deb Sharma
Dhrubesh Deb Sharma
20 min
25 Oct, 2021

Comments

Sign in or register to post your comment.

Video Transcription

Available in Español: Magia con Babel Macro

1. Introduction to Babel Macros

Short description:

In this talk, I will introduce Babel macros and explain how they can be used to write plugins and build macros.

A very good morning, a very good afternoon, and a good evening, based on where you're joining. My name is Trubesh and today I'm going to give a talk on magic with Babel macros. So before we dig in into the topic, I want to also talk about why this topic, right? So I think Babel macros was introduced back in 2017, 2018. And it seemed to be a really fascinating thing because of the things you can do just by that, right? A lot of patterns emerged because of that and, yeah, but nowadays not a lot of folks talk about it. So I thought, you know, why not give it a shot and see if you folks are interested. So the aim of this talk is to give you an introduction about Babel macros so that you can go ahead and write your own plugin perhaps or build your own macros.

2. Introduction to Srivesh and AST

Short description:

In this part, Srivesh introduces himself and his background in software development. He also mentions his YouTube channel and podcast. Srivesh then moves on to discuss the importance of AST in transpilers and the need for developers to understand it. He shares his experience of researching the term and provides a simplified definition of AST as a tree representation of source code that conveys its structure.

So my name is Srivesh. I'm from India. I work at a startup called Organize. I majorly handle the frontend side of things. We are also actively hiring. If anyone is looking out, please reach out to I also run a YouTube channel called the Junior Developer. And I host a podcast in that called the Junior Developer Podcast. If you are interested in frontend technologies or tech in general or maybe if you're interested in how to have an impactful development career, you should perhaps check out the channel and you might find some of the videos quite interesting. Also on Twitter, you can find me as dee underscore Junior underscore dev.

So, that's essentially my introduction. Let's get to the talk. So, AST, right? AST is an amazing thing. It stands for abstract syntax T. It's an amazing thing. And I feel that every developer should essentially learn about it. Because it's a it's essentially one of the building blocks of the transpilers which use your plugins to transpile your code, right? And without knowing it, you might not be able to understand how your code is being transpiled.

So, when I heard about this term AST, right, I got really curious and I, of course, boosted up my laptop and tried Googling what is an AST. And the first thing was of Wikipedia. And this was the definition which was thrown at me. It says in computer science, an abstract syntax tree or just syntax tree, is a representation of the abstract syntactic structure of text written in a formal language. So, this did not really this was a pretty complex definition for me. And it did not really hit me what exactly AST is. So, I tried to find a better definition which is much simpler in understanding. And I stumbled upon this definition. It says an abstract syntax tree or AST is a tree representation of the source code of a computer program that conveys the structure of source code. So, what it means by that is essentially you can look at the AST and you can derive what code you're writing. But as I mean it's of course not as readable as the human code which we write. But it's the representation is purely for machine to understand how exactly the code is structured.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Vite: Rethinking Frontend Tooling
JSNation Live 2021JSNation Live 2021
31 min
Vite: Rethinking Frontend Tooling
Top Content
Vite is a next-generation build tool that leverages native ES modules for improved performance. It eliminates the need for bundling and improves hot module replacement. Vite provides an opinionated default configuration while still allowing advanced customization through plugins. It is framework agnostic and can be used for React and other applications. Vite is being adopted by Next.js and Create React App, and integration with Nuxt 3 offers significant speed improvements.
React Compiler - Understanding Idiomatic React (React Forget)
React Advanced Conference 2023React Advanced Conference 2023
33 min
React Compiler - Understanding Idiomatic React (React Forget)
Top Content
Watch video: React Compiler - Understanding Idiomatic React (React Forget)
Joe Savona
Mofei Zhang
2 authors
The Talk discusses React Forget, a compiler built at Meta that aims to optimize client-side React development. It explores the use of memoization to improve performance and the vision of Forget to automatically determine dependencies at build time. Forget is named with an F-word pun and has the potential to optimize server builds and enable dead code elimination. The team plans to make Forget open-source and is focused on ensuring its quality before release.
The Future of Performance Tooling
JSNation 2022JSNation 2022
21 min
The Future of Performance Tooling
Top Content
Today's Talk discusses the future of performance tooling, focusing on user-centric, actionable, and contextual approaches. The introduction highlights Adi Osmani's expertise in performance tools and his passion for DevTools features. The Talk explores the integration of user flows into DevTools and Lighthouse, enabling performance measurement and optimization. It also showcases the import/export feature for user flows and the collaboration potential with Lighthouse. The Talk further delves into the use of flows with other tools like web page test and Cypress, offering cross-browser testing capabilities. The actionable aspect emphasizes the importance of metrics like Interaction to Next Paint and Total Blocking Time, as well as the improvements in Lighthouse and performance debugging tools. Lastly, the Talk emphasizes the iterative nature of performance improvement and the user-centric, actionable, and contextual future of performance tooling.
How Bun Makes Building React Apps Simpler & Faster
React Day Berlin 2022React Day Berlin 2022
9 min
How Bun Makes Building React Apps Simpler & Faster
BUN is a modern all-in-one JavaScript runtime environment that achieves new levels of performance. It includes BUN dev, a fast front-end dev server, BUN install, a speedy package manager, and BUN run, a fast package runner. BUN supports JSX, has optimized React server-side rendering, and offers hot module reloading on the server. The priorities for BUN include stability, node compatibility, documentation improvement, missing features in BUN install, AST plugin API, native Windows support, Bundler and Minifier optimization, and easier deployment to production. BUN's AST plugin API allows for bundle-time JavaScript execution and embedding code, potentially inspiring new frameworks.
Turbopack. Why? How? When? and the Vision...
React Day Berlin 2022React Day Berlin 2022
32 min
Turbopack. Why? How? When? and the Vision...
The Talk discusses TurboPack, a successor to Webpack, aiming to create a framework-independent, flexible, and extensible tool for the open-source community. It addresses performance challenges by integrating SWC into Next.js. The challenges with Next.js and Webpack include orchestration issues, backward compatibility constraints, and cache invalidation problems. TurboEngine and TurboPack provide constant performance in incremental builds, leveraging Rust's predictable performance and parallelism. The Talk also covers topics like dependency tracking, task graphs, cache invalidation, lazy asset graphs, and the integration of TurboPack with Next.js. The future plans involve reconfiguring Webpack and TurboEngine, moving computations to the cloud, providing insights into builds, and facilitating migration and integration with JavaScript projects.
The Core of Turbopack Explained (Live Coding)
JSNation 2023JSNation 2023
29 min
The Core of Turbopack Explained (Live Coding)
Tobias Koppers introduces TurboPack and TurboEngine, addressing the limitations of Webpack. He demonstrates live coding to showcase the optimization of cache validation and build efficiency. The talk covers adding logging and memorization, optimizing execution and tracking dependencies, implementing invalidation and watcher, and storing and deleting invalidators. It also discusses incremental compilation, integration with other monorepo tools, error display, and the possibility of a plugin system for Toolpag. Lastly, the comparison with Bunn's Builder is mentioned.

Workshops on related topic

Using CodeMirror to Build a JavaScript Editor with Linting and AutoComplete
React Day Berlin 2022React Day Berlin 2022
86 min
Using CodeMirror to Build a JavaScript Editor with Linting and AutoComplete
Top Content
WorkshopFree
Hussien Khayoon
Kahvi Patel
2 authors
Using a library might seem easy at first glance, but how do you choose the right library? How do you upgrade an existing one? And how do you wade through the documentation to find what you want?
In this workshop, we’ll discuss all these finer points while going through a general example of building a code editor using CodeMirror in React. All while sharing some of the nuances our team learned about using this library and some problems we encountered.
Solve 100% Of Your Errors: How to Root Cause Issues Faster With Session Replay
JSNation 2023JSNation 2023
44 min
Solve 100% Of Your Errors: How to Root Cause Issues Faster With Session Replay
WorkshopFree
Ryan Albrecht
Ryan Albrecht
You know that annoying bug? The one that doesn’t show up locally? And no matter how many times you try to recreate the environment you can’t reproduce it? You’ve gone through the breadcrumbs, read through the stack trace, and are now playing detective to piece together support tickets to make sure it’s real.
Join Sentry developer Ryan Albrecht in this talk to learn how developers can use Session Replay - a tool that provides video-like reproductions of user interactions - to identify, reproduce, and resolve errors and performance issues faster (without rolling your head on your keyboard).