如何为 Angular CLI 的新配置启用增量构建模式?

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

我正在做一个 Angular 项目。 我尝试为 Angular CLI 定义一个新的 "development"-configuration。 我已经可以做到了:

ng serve -c=development

但当我更改一段代码时,它始终是一个完整的构建。

当我在同一个项目中使用 ng serve 而没有 -c 选项时,当我更改某些内容时,只会发生一个很小的构建。 ("incremental build" 一词是否适合描述这种行为?)

我也想让 CLI 使用“-c=development”来完成这个小构建。

我试图在 Angular 文档和 Google 中找到它,但我无法弄明白。

我的angular.json是(只是改了工程名):

{
    "$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"
}

如有任何帮助,我将不胜感激。

请善待。上一次我需要三四次编辑才能让我的问题获得所需的所有信息并具有适当的风格,尽管我总是在第一次尝试时就做到这一点。谢谢!

更新:

在猜测模式下,我发现了一个可以使构建方式比以前更快的东西。不如没有 -c-选项快,但这是一个好的开始:

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

我设置优化-属性 false.

但我真的很感激进一步的帮助、内部和改进。谢谢!

我找到了一个令人满意的解决方案。

angular.js 的配置部分中配置条目的属性与 ng build 的命令行选项相同(至少有很多)。所以在 ng build 的文档中我找到了不同属性的默认值。

我更改了以下属性:

  • "optimization": 假
  • "buildOptimizer": 错误,
  • "aot": 错误,
  • "outputHashing": "none",
  • "namedChunks":正确,
  • "extractLicenses": 错误,

所以我的 "development"-configuration-entry 现在看起来像这样:

                "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"
                            }
                        ]
                    },

现在我得到的构建时间与没有 -c 选项时一样。 也许配置中有更多设置不是开发的最佳设置,但就目前而言,我对此感到满意。

编码愉快。玩得开心!