And a while ago, we noticed the following pattern. We saw that we have many files that look like this. They exported nested objects that in the end of the day only contained strings. This is why we call them constants files.
And the files that referenced the constants files only used one or two values out of these constants. So you're probably thinking to yourself, okay, I have Tree Shaking exactly for this case, and if you're unfamiliar with Tree Shaking, then it's a very common algorithm that most bundlers use today to remove the unused values from your bundle, so you'll end up exactly with what you need in your bundle. But in our case, Tree Shaking didn't work properly because we used objects, and Tree Shaking doesn't take care well with objects. So we ended up with something like this in our bundle, and you see that all the red parts are actually constants files.
And the problem with that is that we have many constants files inside our bundle. This means we have more JavaScript than what we actually needed. This increased our bundle's size and made it more network-heavy, so our application became heavier to load. Additionally, all of these files were kept in memory and were never removed, so it increased our memory footprint more than what we wanted. And it felt a lot like this. We had a small piece of code that carried a huge luggage with it.
So we had a dream. We had a dream that while we're building the bundle, we'll find the reference values out of the constants files, remove the references, and replace them with the actual values, and then we can remove the import for these constants files and end up exactly with the values that we need inside our bundle. So we knew what we wanted to do, but we weren't sure how to do it. We knew that we wanted to avoid major refactors because we had a lot of code that needed to be updated, and we knew we wanted to avoid manual update of the code because, again, it was a lot of code, and we needed to do it programmatically to avoid errors that humans can do. So after some thinking, we understood we can use ASTs, but wait, ASTs aren't too common, right? Think for a moment if you've ever used ASTs before.
So the truth is that we all use ASTs, and we just may not know it, and we'll see it in a second. So ASTs are abstract syntax trees. It's a data structure used to represent a piece of code, and the best way to understand it is by a tool called AST Explorer. AST Explorer is a free online tool, and you can put a piece of code in it, like the one here, and it will show you its AST. It may seem scary on the first glance. It really is, but the power of AST Explorer is that you can select a piece of code, and it will show you its matching node in the AST. So for example, you can see here we have a string literal node with the value of hey, Berlin. We can also select this part here to see it's a variable declaration node with the name of hi, the value of hey, Berlin, and we can also see its kind is a const. It's important to understand that ASTs really everyone. Many tools use AST. If you think of ESLint, PostCSS, Prettier, Webpack, and many other tools, they all use AST to read and transform code programmatically.
Comments