Skip to content
Related Articles

Related Articles

How to bundle an Angular app for production?
  • Difficulty Level : Basic
  • Last Updated : 28 May, 2020

Introduction
Before deploying the web app, Angular provides a way to check the behavior of the web application with the help of a few CLI-commands. Usually, ng serves command is used to build, watch, and serve the application from local memory. But for deployment, the behavior of the application is seen by running ng build command.

Difference between ng serve and ng build

ng serveng build
The ng serve command is intentionally for fast, local and iterative developments and also for builds, watches and serves the application from a local CLI development server.The ng build command is intentionally for building the apps and deploying the build artifacts.
The command does not generate an output folder.The output folder is – dist/.
The ng serve builds artifacts from memory instead for a faster development experience.The ng build command generates output files just once and doesn’t serve them.

Steps
Before following the steps to deploy the application, make sure that Angular CLI is already installed in the system if it is not then run the following command.

npm install -g @angular/cli

The very first step would be to bundle up an application for production before its deployment.

  • Navigate to project directory.



    cd project-folder
  • Run ng build command in Angular CLI
    ng build --prod 

  • To get the preview of the application, run the following command:
    ng serve --prod

    This starts a local HTTP server with production files. Navigate to http://localhost:4200/ to view the application.
    With these steps, the application is ready for deployment.

Approach
ng build command compiles the Angular app into an output directory named dist/ at the given output path. This command must be executed from within the working directory. The application builder in Angular uses the webpack build tool, with configuration options specified in the workspace configuration file (angular.json) or with a named alternative configuration. A “production” configuration is created by default when you use the CLI to create the project, and you can use that configuration by specifying the —configuration="production" or the --prod="true" option.

The –prod flag activates many optimization flags. One of them is –aot for Ahead Of Time compilation. Your component templates are compiled during the build, so TypeScript can detect more issues in your code. You can compile in dev mode but still activate the –aot flag if you want to see this error before building for prod.

dist/ Folder
The dist folder is the build folder which contains all the files and folders which can be hosted in the server.
The dist folder contains the compiled code of your angular application in the format of JavaScript and also the required HTML and CSS files.
Inside dist/ folder

Folder/FileDescription
assetsThe folder contains resources copied from the Angular CLI assets configuration.
index.htmlindex.html file is the entrypoint for the application.
main.[hash].jsThe file contain bundled application.
polyfill.[hash].bundle.jsIt contains polyfill dependencies
runtime-[es-version].[hash].bundle.jsIt contains webpack loader
style.[hash].bundle.cssIt contains the style definitions

Disadvantages

  • Performance: Dynamic applications didn’t always perform that well. Complex SPAs could be laggy and inconvenient to use due to their size.
  • Steep learning curve: As AngularJS is a versatile instrument, there’s always more than one method to finish any task. This has produced some confusion among engineers.
My Personal Notes arrow_drop_up
Recommended Articles
Page :