So you all know what it is. So let's start by making an understanding of what AST is. So basically, this is a normal JavaScript code for your React. And if you see when I select this, this on the right side is your AST tree. This is basically a tree that contains nodes, different nodes. So if I click on return, it will show me a return statement. This particular thing is this whole thing is a node. Then if I select on a function, then this gets highlighted function declaration. This is another node. Then if I click on H1, then this gets highlighted, this is another node. So basically, it has multiple nodes inside a tree.
And so let's start by understanding how we'll approach building the Babel plugin. So first of all, if we try to do it manually, we'll find out HTML, HTML tag, the opening HTML tag and inside that we'll see if it contains a style prop. Then inside that we'll log in and search for if it contains a alias or even though it doesn't contain any alias, it might contain a property like a token value. Okay, so it can be background color, right and then dollar red. So still we want to convert dollar red into the appropriate token value. Okay. So this is how we'll approach it. And so let's start.
So first of all, we'll try to get the config from the defined file. So for example, we can have the user create a custom.config.js file. And then we'll do a read file operation and get the config. I'm not showing it here, but you can do it. Okay, so this is our config. contents aliases, BGC, tokens, red, okay. And then this is our main function main Babel plugin. So Babel plugin is essentially a function and it returns a visitor pattern object. Okay, so what visitor pattern is, it's like an iterator, and this will keep on iterating over your code nodes, different nodes of your tree. And whenever I pass a property like JSX attribute and it is a function inside that callback function, I get the path, this path, suppose, JSX attribute what JSX attribute is. So if I click here, I will see JSX sorry, this side, I will see this node, JSX attribute.
Comments