CHAPTER 5
You can edit the configuration files to customize many aspects of your development environment. In this chapter, we are going to cover how to customize your application by integrating some third-party libraries you might want to use.
While the ng new command will generate a package.json file for you, it does not know about the various libraries you might need in your application. For example, if we want to add Bootstrap and Font Awesome to our application, we would need to first install them using NPM.
Code Listing 32
npm install bootstrap@next npm install font-awesome |
After running this command, you will see the library files in your node_modules folder.
The configuration file angular-cli.json can be found in the \angular-cli\blueprints\ng2\files subdirectory, and contains various configuration options for running the Angular CLI. Within the apps section, you will find a few options that are useful to know:
Once these additions are made, Bootstrap styles and JavaScript files will be bundled and included in your application when you run it and when you build the distribution.
The process is basically the same, except that we need to import the actual font files. For this, we use the addons collection in the angular-cli.json file.
Code Listing 33
styles: [ …,"../node_modules/font-awesome/css/font-awesome.css"] addons: [ "../node_modules/font- awesome/fonts/*.+(otf|eot|svg|ttf|woff|woff2)" |
The addons collection is used for files your application needs, other than scripts and style sheets.
The assets collection contains various elements (files and folders) that you want copied to the distribution folder when the application is built. For the default build:
Code Listing 34
assets: ["assets","favicon.ico"] |
This tells the build process to copy the contents of the assets folder and the favicon.ico file to the distribution folder (typically dist).
The Environments folder contains TypeScript files that will be loaded depending on the environment the build is being made for. For example, the environment.prod.ts file located in the angular-cli\blueprints\ng2\files\__path__\environments subdirectory contains the following code.
Code Listing 35
Export const environment = { Production: true; } |
You can import the environment module into your application and query the Production property in case your application needs to behave differently depending on the environment.
Although you can manually add scripts and style sheets to your HTML templates, by using Angular CLI, you gain the benefits of bundling to reduce page load times. You can also use the assets to move additional content to the distribution. Finally, the environments folder lets you provide properties your application can use to distinguish whether it is running in development or production.