angularangular-cliincremental-build

How to enabIe incremental build mode for a new configuration for the Angular CLI?


I'm working on an Angular project. I try to define a new "development"-configuration for the Angular CLI. I already can do this:

ng serve -c=development

But it does always a complete build, when I change a piece of code.

When I use ng serve without the -c option with the very same project only a tiny build happens when I change something. (Is the term "incremental build" the right one to describe this behavior?)

I want to have the CLI do this tiny build with "-c=development" too.

I tried to find it in the Angular documentation and with Google, but I can't figure it out.

My angular.json is (only the project name is altered):

{
    "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
    "version": 1,
    "newProjectRoot": "projects",
    "projects": {
        "MyProject": {
            "root": "",
            "sourceRoot": "src",
            "projectType": "application",
            "prefix": "app",
            "schematics": {
                "@schematics/angular:component": {
                    "style": "scss"
                }
            },
            "architect": {
                "build": {
                    "builder": "@angular-devkit/build-angular:browser",
                    "options": {
                        "outputPath": "dist/MyProject",
                        "index": "src/index.html",
                        "main": "src/main.ts",
                        "polyfills": "src/polyfills.ts",
                        "tsConfig": "src/tsconfig.app.json",
                        "assets": [
                            "src/assets/favicon.png",
                            "src/assets"
                        ],
                        "styles": [
                            "./node_modules/bootstrap/dist/css/bootstrap.min.css",
                            "node_modules/ngx-toastr/toastr.css",
                            "src/styles.scss"
                        ],
                        "stylePreprocessorOptions": {
                            "includePaths": [
                                "src/assets/stylesheets"
                            ]
                        },
                        "scripts": [
                            "./node_modules/jquery/dist/jquery.min.js",
                            "./node_modules/popper.js/dist/umd/popper.min.js",
                            "./node_modules/bootstrap/dist/js/bootstrap.min.js"
                        ],
                        "es5BrowserSupport": true
                    },
                    "configurations": {
                        "development": {
                            "fileReplacements": [
                                {
                                    "replace": "src/environments/environment.ts",
                                    "with": "src/environments/environment.dev.ts"
                                }
                            ],
                            "optimization": true,
                            "outputHashing": "all",
                            "sourceMap": false,
                            "extractCss": true,
                            "namedChunks": false,
                            "aot": true,
                            "extractLicenses": true,
                            "vendorChunk": false,
                            "buildOptimizer": true,
                            "budgets": [
                                {
                                    "type": "initial",
                                    "maximumWarning": "2mb",
                                    "maximumError": "5mb"
                                }
                            ]
                        },
                        "test": {
                            "fileReplacements": [
                                {
                                    "replace": "src/environments/environment.ts",
                                    "with": "src/environments/environment.test.ts"
                                }
                            ],
                            "optimization": true,
                            "outputHashing": "all",
                            "sourceMap": false,
                            "extractCss": true,
                            "namedChunks": false,
                            "aot": true,
                            "extractLicenses": true,
                            "vendorChunk": false,
                            "buildOptimizer": true,
                            "budgets": [
                                {
                                    "type": "initial",
                                    "maximumWarning": "2mb",
                                    "maximumError": "5mb"
                                }
                            ]
                        },
                        "stage": {
                            "fileReplacements": [
                                {
                                    "replace": "src/environments/environment.ts",
                                    "with": "src/environments/environment.stage.ts"
                                }
                            ],
                            "optimization": true,
                            "outputHashing": "all",
                            "sourceMap": false,
                            "extractCss": true,
                            "namedChunks": false,
                            "aot": true,
                            "extractLicenses": true,
                            "vendorChunk": false,
                            "buildOptimizer": true,
                            "budgets": [
                                {
                                    "type": "initial",
                                    "maximumWarning": "2mb",
                                    "maximumError": "5mb"
                                }
                            ]
                        },
                        "production": {
                            "fileReplacements": [
                                {
                                    "replace": "src/environments/environment.ts",
                                    "with": "src/environments/environment.prod.ts"
                                }
                            ],
                            "optimization": true,
                            "outputHashing": "all",
                            "sourceMap": false,
                            "extractCss": true,
                            "namedChunks": false,
                            "aot": true,
                            "extractLicenses": true,
                            "vendorChunk": false,
                            "buildOptimizer": true,
                            "budgets": [
                                {
                                    "type": "initial",
                                    "maximumWarning": "2mb",
                                    "maximumError": "5mb"
                                }
                            ]
                        }
                    }
                },
                "serve": {
                    "builder": "@angular-devkit/build-angular:dev-server",
                    "options": {
                        "browserTarget": "MyProject:build"
                    },
                    "configurations": {
                        "production": {
                            "browserTarget": "MyProject:build:production"
                        },
                        "development": {
                            "browserTarget": "MyProject:build:development"
                        }
                    }
                },
                "extract-i18n": {
                    "builder": "@angular-devkit/build-angular:extract-i18n",
                    "options": {
                        "browserTarget": "MyProject:build"
                    }
                },
                "test": {
                    "builder": "@angular-devkit/build-angular:karma",
                    "options": {
                        "main": "src/test.ts",
                        "polyfills": "src/polyfills.ts",
                        "tsConfig": "src/tsconfig.spec.json",
                        "karmaConfig": "src/karma.conf.js",
                        "styles": [
                            "./node_modules/bootstrap/dist/css/bootstrap.min.css",
                            "src/styles.scss"
                        ],
                        "stylePreprocessorOptions": {
                            "includePaths": [
                                "src/assets/stylesheets"
                            ]
                        },
                        "scripts": [],
                        "assets": [
                            "src/favicon.ico",
                            "src/assets"
                        ]
                    }
                },
                "lint": {
                    "builder": "@angular-devkit/build-angular:tslint",
                    "options": {
                        "tsConfig": [
                            "src/tsconfig.app.json",
                            "src/tsconfig.spec.json"
                        ],
                        "exclude": [
                            "**/node_modules/**"
                        ]
                    }
                }
            }
        },
        "MyProject-e2e": {
            "root": "e2e/",
            "projectType": "application",
            "prefix": "",
            "architect": {
                "e2e": {
                    "builder": "@angular-devkit/build-angular:protractor",
                    "options": {
                        "protractorConfig": "e2e/protractor.conf.js",
                        "devServerTarget": "MyProject:serve"
                    },
                    "configurations": {
                        "production": {
                            "devServerTarget": "MyProject:serve:production"
                        }
                    }
                },
                "lint": {
                    "builder": "@angular-devkit/build-angular:tslint",
                    "options": {
                        "tsConfig": "e2e/tsconfig.e2e.json",
                        "exclude": [
                            "**/node_modules/**"
                        ]
                    }
                }
            }
        }
    },
    "defaultProject": "MyProject"
}

I would appreciate any help.

Please be kind. The last time I've needed three or four edits before my question had all the information needed and had an appropriate style, though I always try to achieve this on the first attempt. Thank you!

UPDATE:

In guess-mode I've found a thing that makes the build way more faster than before. Not as fast as without the -c-Option, but it is a good start:

"configurations": {
    "development": {
        "optimization": false, 

I set the optimization-property fo false.

But I would really appreciate further help, insides and improvements. Thank you!


Solution

  • I've found a satisfying solution.

    The properties of a configuration-entry in configurations-section of angular.js are the same as the command line options for ng build (at least many of them). So in the documentation of ng build I've found what the default values are for different properties.

    I've changed the following properties:

    So my "development"-configuration-entry looks now like this:

                    "configurations": {
                        "development": {
                            "fileReplacements": [
                                {
                                    "replace": "src/environments/environment.ts",
                                    "with": "src/environments/environment.dev.ts"
                                }
                            ],
                            "optimization": false,
                            "outputHashing": "none",
                            "sourceMap": false,
                            "extractCss": true,
                            "namedChunks": true,
                            "aot": false,
                            "extractLicenses": false,
                            "vendorChunk": false,
                            "buildOptimizer": false,
                            "budgets": [
                                {
                                    "type": "initial",
                                    "maximumWarning": "2mb",
                                    "maximumError": "5mb"
                                }
                            ]
                        },
    

    Now I get the same build times as without the -c-option. Maybe there are more settings in the configuration that are not optimal for development, but for now, I'm happy with it.

    Happy coding. Have fun!