I want to start a series of shorter blog posts, the for now I’m going to call Code Bytes. Obviously a cheesy play on words, but I like cheesy. And bacon, but I digress. The idea is they’ll be shorter posts that are a quick read of limited scope. At least some of the first I have ideas aren’t even enough to warrant a Github repo for examples. They’ll be good with just some in post code examples. It’s a little something new I’m trying out this year, so we’ll see how it goes. For our first Code Byte, we turn to the Angular CLI.
The Angular CLI is one of the killer tools that has made it so easy to get up and running with Angular. It really is a key piece when developing in Angular and it’s abilities are only growing. One of the most basic uses is generating new projects and parts of the application, such as components and services. The CLI can give you all the bells and whistles. For example, for a new component it will give you the TypeScript file, and HTML template, a style file (CSS, SASS, etc), and a unit test. Whether or not you want all those pieces every time is another case. You can use an inline template and inline styles. If you just want a simple project or are new to Angular and want to play around, this is a lot to immediately digest. But there is another way.
If you want to keep it simple, you can add a single flag to your command, the --minimal
flag. This instructs the CLI to create the minimum necessary. To see the difference, create a default project and then a minimal project.
If we look at the difference at what is created, we can see fewer files below. I ran with the --dry-run
flag to run without making changes which lists the files created.
That’s a difference of 14 vs 28 files, mostly from dropping all the configuration related to testing. In addition when you create a new component in the default project you get 4 files compared to one in the minimal project. This is due to a change in the angular-cli.json
configuration file. All the settings here are useful to understand, so you can configure your project what you want to generate. If we look at "defaults"
we can see changes to what it should generate.
You’ll notice "spec": false
appearing throughout. This tells the CLI to not generate a spec file when you generate a new piece of your application. In addition, it set "inlineStyle": true
and "inlineTemplate": true
, telling the CLI to set components to use inline instead of creating additional HTML and CSS files. Now, when you generate anything new within your project, you’ll get just the minimum files, making it more compact and simple to play around. You’re ready to rock and code.
That concludes our first byte. Just a little code snack. A little nosh, if you would like. Hope you enjoyed.
Image credit: Ash Edmonds on Unsplash, edited