Extracting as Composer Package0:00
Now that we've constructed our custom preset, I want to extract this to a dedicated package that I can pull in for any new project. To begin, from my web directory, I'm going to make a new directory called laravel-preset. And now I'll move our preset-laracast directory into laravel-preset. Alright, let's cd in there, and open this in Sublime. We now have everything we've worked on to this point, but I don't yet have a composer.json file. So let's do that now. composer init.
Composer. Init. For the name, we'll do laracast-laravel-preset. The default, laracast-preset, kind of close enough. Author, that's fine. Minimum stability, stable. Package type, eh, we'll skip that. License, MIT. And no, I won't define the dependencies. Alright, so now if I switch back to Sublime, we have our constructed composer.json file.
Configuring Composer Metadata0:53
And no, I won't define the dependencies. Alright, so now if I switch back to Sublime, we have our constructed composer.json file. Some other things you might want to include are keywords, so maybe laravel, laravel-preset, and maybe laracasts. Next, I'll use my laracast email. And you'll remember, before, we were manually loading the service provider. However, we can do this automatically for User. Here's how. I think this is new as of Laravel 5.5. We can define an extra section here for Laravel.
Enabling Package Auto-Discovery1:24
I think this is new as of Laravel 5.5. We can define an extra section here for Laravel. And for the providers array, here, we can add a list of any service providers that should be automatically loaded. So we'll say laracasts, and, yeah, let's put this under, what do we have, laravel-preset. And then laracasts-service-provider. Okay, so with that in mind, we'll set up our autoload section, just like we did before. And for PSR-4, we're not going to have laracasts, and we're calling it laravel-preset. Will be within our source directory. Okay, so now, we just need to update a couple of things to reference our new namespace.
Will be within our source directory. Okay, so now, we just need to update a couple of things to reference our new namespace. Right here, just going to update all of these. All right, so I think we are good to go here. So let's see what we have. When you pull in this composer package, it's going to read this file. To begin, we're saying, okay, this namespace, I want to autoload all of the files, like this. Next, in our extra section, we're saying, okay, when you pull this into a Laravel project, I want to autoload this service provider.
Next, in our extra section, we're saying, okay, when you pull this into a Laravel project, I want to autoload this ServiceProvider. The ServiceProvider, once we hit the boot method, and if you're unfamiliar with ServiceProviders, it will filter through and call the register method for every provider. This is a good place to bind things into the container. Once it has registered every provider, it'll do another swipe through all of them and trigger the boot method. This is where you can do things assuming all of the components have been loaded. So this is where we extend the preset command with our laracast macro. That will defer to preset, which will hit this install method that triggers all of the
Initializing Git and Pushing2:58
So this is where we extend the preset command with our laracast macro. That will defer to preset, which will hit this install method that triggers all of the operations we need to. So let's switch back, initialize Git, add everything, and we'll say this is our first draft. And now I'm going to push it up to GitHub. I'm now on GitHub. We'll create a new repo called laravel-preset and create it. And now we'll push an existing repository. All right, so if we come back and give that a refresh, there we go.
Testing Preset in New App4:28
Let's give that a refresh. There we go. Now it's auto-updating. So let's give this a shot. I'm going to create a brand new project, and we'll cd in there. Open this in Sublime, and now let's require this in. composer require our preset. Now you remember, behind the scenes, it will automatically register itself with Laravel, and we should see a note here. There you go.
and we should see a note here. There you go. Discovered Package Laracast Laravel Preset. Okay, so now if we open this in Sublime, this is what we get out of the box, right? A standard webpack.mix file within resources, assets, all of this stuff within the JavaScript. Okay, now I can run php artisan preset Laracast. Okay, so one thing we haven't done on this node is we need some feedback, of course. We can do that within an update. However, it looks like it ran. So now webpack.mix was instantly updated.
However, it looks like it ran. So now webpack.mix was instantly updated. The sass directory is now empty, but we should probably add an empty app.sass file within there. It takes two seconds. Anyways, here's the updated app.js and the updated bootstrap file. Finally, if I go to package.json, I can see Laravel Mix with Tailwind. So all of those steps that you would normally do one after the other manually for every new project, we can now automate, which is great. In the next episode, we'll make a few couple tweaks, push them up to GitHub, and then because
