Add @blaze Opt-In0:00
(upbeat music) Okay, so we have our little toy version of Blaze here where we take a blade component like button, and instead of loading all that stuff, we just have a require statement for the thing, right? Well, I wanna add one feature right now. I wanna make it so that we honor the existence of at Blaze or not. So if Blaze doesn't exist, then I don't wanna optimize it at all.
Refactor Compile Method0:21
So if Blaze doesn't exist, then I don't wanna optimize it at all. I wanna leave that to Blade. If it does exist, then we'll continue on doing our thing, okay? So where do we do that? Looking at our code, looking at our compiler, I did take the liberty of extracting all of this stuff into a single method called compile component. This is where we get the hash, the compiled path, the contents, the compiled, right?
Early Return Fallback0:39
This is where we get the hash, the compiled path, the contents, the compiled, right? I shoved it away so that we can easily write here, after we know the path of the source file of the component, we can get the contents and look for app Blaze. So we'll do this, we'll say, if not should optimize, we'll make a new method for this path. Then we want to return early, okay? But I wanna show you something. What happens if we just return early?
But I wanna show you something. What happens if we just return early? Like I'm gonna always return early, okay? And then refresh this page. Notice it disappears, the button disappears entirely, because we have to return something. If we say, hey, that's what gets returned from here. So we actually wanna return groups zero, which is the first match, like let's DD groups, just so you can see what I'm talking about.
which is the first match, like let's DD groups, just so you can see what I'm talking about. We need groups, we DD groups, and you see that zero is the full thing, then our capture group, and then the number of the capture group, right? And I kinda, yeah, I don't know. Well, I think group zero is fine for now. I don't know, you could do this. Fallback equals group zero. I do, if you're gonna do group without names
Implement shouldOptimize()1:37
Fallback equals group zero. I do, if you're gonna do group without names and just indexes, maybe keep 'em all at the top. A little stylistic thing. Okay, so then we can return fallback as our return early, if we shouldn't optimize this component based on that path. So let's go ahead and add that method. It should be really straightforward function, should optimize, accept path. And then in here, we'll say contents equal file get contents,
should optimize, accept path. And then in here, we'll say contents equal file get contents, path, okay? Return string contains. And now what's the method signature on that? Haystack and then needle. PHP, so silly. Should be needle and haystack. Come on PHP. So haystack is contents and then blaze, okay?
Come on PHP. So haystack is contents and then blaze, okay? It should be the other way around just for the rhyme, right? Find a needle in a haystack. It does not even rhyme. String contains contents at blaze. That means we should optimize it, okay? So does this thing work? I don't know, let's see. If we load this, all right,
I don't know, let's see. If we load this, all right, at blaze comes right through. And that actually kinda makes sense because there's nothing removing it. We're just checking to see if it exists. Let's see if it works in general though. If I remove at blaze, look at that. The page still loads. Blaze is gone 'cause we removed it
Strip @blaze Directive2:51
The page still loads. Blaze is gone 'cause we removed it and it falls back to showing all of like Laravel's normal blade component stuff. So that's pretty cool. It does work if I save it. There's our optimization, I like that. But we need to remove it from the compiled output. So let's go down here. We get the contents and then we'll just do
So let's go down here. We get the contents and then we'll just do a string replace like right away. Content string replace and this is search replace subject. So we're searching for at blaze. We are return, yeah, we're gonna replace it with an empty string and the subject is contents. And now we refresh and that works, okay? So now everything is identical. Whether you have at blaze in there or not,
Preview Next Features3:31
So now everything is identical. Whether you have at blaze in there or not, you still get a button except one is really slow and has all this stuff and the other one is really fast because we have at blaze. Pretty awesome. So now we can use at blaze to do things like trigger memo, trigger fold. So I think we'll do that. We're getting to that point where we can add
So I think we'll do that. We're getting to that point where we can add those next level features and then things will start to click and we'll start to see the raw power here. But maybe we check in on some performance in the next episode real quick. So we'll see you there.
