Jump start your Angular project with Angular CLI
Solar eclipse image via Shutterstock
While Angular CLI is still in beta, most of the developers like the ease of getting started with new projects. In this article, Java Champion Yakov Fain shows you how to create, bundle, and deploy a simple project with Angular CLI.
Angular 2 , the complete re-write of the popular AngularJS framework was released in September of 2016. In March 2017 Angular 2 will be renamed to Angular, so I’ll be using the new name in this article.
Initially, the entry barrier into the world of Angular development was pretty high because of the need to learn and manually configure multiple tools. Even to get started with a simple application, you’d need to learn the TypeScript language (an easy job for Java developers), learn how to configure and use the TypeScript compiler, ES6 modules, a module loader (e.g. SystemJS), test runners, npm, a dev web server. To work on a real-world project, you’d also need to learn how to test and bundle your app for deployment.
To jumpstart the development process, the Angular team created a tool called Angular CLI (see https://github.com/angular/angular-cli ), which is a command-line interface that covers all the stages of creating an Angular application from scaffolding and generating an initial app to deployment. The generated code also includes pre-configured files for unit tests and bundling with the powerful Webpack bundler.
While Angular CLI is still in beta, most of the developers like the ease of getting started with new projects. In this article, I’ll show you how to create, bundle, and deploy a simple project with Angular CLI.
To get started you have to have NodeJS installed on your computer (see https://nodejs.org ). Both Angular and Angular CLI have lots of dependencies on other packages, which are available at the NPM repository at https://www.npmjs.com (it’s similar to Maven Central). NodeJS comes with npm a package manager that can install individual packages from npmjs.org or use the config file package.json (like pom.xml) to install all project dependencies. I’ll show you how to create the initial project (including package.json) using Angular CLI.
After installing NodeJS, you can start using npm in the command prompt window (or the Terminal on Mac) to install all required dependencies. Here’s the command to install Angular CLI globally on your computer (-g is for global):npm install @angular/cli -g
After Angular CLI is installed, you can start using its ng command to generate various artifacts such as the new project, components, services as well as building and running the app with included Web server (webpack-dev-server). To create a new project called myproject just enter the following command:ng new myproject
This command will create a new directory with the boilerplate project and install all required dependencies thousands of files in the node_module directory of your project. This sounds like a lot of files to install, but enterprise Java developers are accustomed to this. Depending on the speed of your Internet connection, it can take anywhere from one to five minutes.This time can be substantially decreased by using the Yarn package manager (an alternative to npm), and I described how to do it in this blog post .
Change directory to your newly generated project, and you’ll see something like this:
Figure 1. Project structure
To build the dev version of this app, run the following command:ng serve
Now open your browser at http://localhost:4200 , and you’ll see the following page rendered by the top-level component app.component.ts:
Figure 2. Running the app
Angular CLI has many other commands and you can read about them in the product documentation at https://github.com/angular/angular-cli . But I just want to give you a quick overview of different ways of building the app.
The command ng serve invoked the webpack-dev-server that took all required files from our project and bundled them up in memory. The size of this app was not optimized since we work in the development mode now. If you open the network tab in the Dev Tools of your browser, you’ll see that the size of this tiny app is 3MB.
Let’s make it smaller by requesting the production build with optimization:ng serve prod
Refresh the page at localhost:4200, and you’ll see that the size of the app was reduced to 227KB. Now let’s ask Angular to pre-compile the Angular-specific code (templates and components) using Ahead-Of-Time (AoT) compiler:ng serve prod aot
Refresh the page again, and you’ll see that the size of the app is 130KB now, because the Angular compiler was not packaged with the app.
While using the AoT mode will lower the size of the small apps, this may not be the case for the larger ones. But the speed of rendering of the app will improve.
The ng serve command is bundling the app in memory, and automatically rebuilds it as soon as you make changes in the code. But for prod deployment you need files, and the ng build command will create the directory called dist that will contain the files with your application bundles, their gzipped versions, styles, and source maps for debugging TypeScript in the browser. Try it out by running the following command:ng build prod aot
Now you can take the content of this dist directory and copy it to your Apache, NGINX, Tomcat, or any application server of your choice. The build and deployment process can be also automated using npm scripts, Gulp, Grunt et al.Learn more
This post was originally published in the January2017 issue of the Eclipse Newsletter:Exploring New TechnologiesFor more information and articles check out the
本文标题：Jump start your Angular project with Angular CLI