Angular 12 - 生成浏览器应用程序包(阶段:密封)非常慢

Mar*_*Tin 44 angular

将 Angular 应用程序从Angular 11.2.12更新到Angular 12.0.0后

Angular CLI: 12.0.0
Node: 14.15.4
Package Manager: npm 6.14.10
OS: win32 x64

Angular: undefined
...

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.1200.0 (cli-only)
@angular-devkit/core         12.0.0 (cli-only)
@angular-devkit/schematics   12.0.0 (cli-only)
@schematics/angular          12.0.0 (cli-only)
typescript                   4.2.4
Run Code Online (Sandbox Code Playgroud)

ng serve 生成浏览器应用程序包需要很长时间。

Build at: 2021-05-28T06:03:02.008Z - Hash: 3b63141b76ac88698d67 - Time: 63941ms <- change one line in template
Build at: 2021-05-28T06:08:04.424Z - Hash: e684399b5f80b2b4d785 - Time: 59834ms <- change property name
Build at: 2021-05-28T06:10:05.725Z - Hash: 8125e1bdc31a25d398b5 - Time: 65621ms <- change one line in model
Run Code Online (Sandbox Code Playgroud)

我尝试不同的属性ng serve,目前我使用:

ng serve --source-map=true --hmr --live-reload
Run Code Online (Sandbox Code Playgroud)

更新到Angular 12.0.0后,我生成一个完整的新 Angular 应用程序并接管angular.json我的应用程序myapp

角度.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "myapp": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "myapp",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/myapp",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.json",
            "inlineStyleLanguage": "scss",
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": ["./node_modules/@angular/material/_theming.scss", "src/styles.scss"],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "buildOptimizer": false,
              "optimization": {
                "scripts": true,
                "styles": false,
                "fonts": false
              },
              "vendorChunk": true,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "myapp:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "myapp:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "myapp:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "inlineStyleLanguage": "scss",
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": ["./node_modules/@angular/material/prebuilt-themes/indigo-pink.css", "src/styles.scss"],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": ["tsconfig.app.json", "tsconfig.spec.json", "e2e/tsconfig.json"],
            "exclude": ["**/node_modules/**"]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "myapp:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "myapp:serve:production"
            }
          }
        }
      }
    }
  },
  "defaultProject": "myapp"
}
Run Code Online (Sandbox Code Playgroud)

我可以做些什么来加速生成浏览器应用程序包(阶段:密封)

Mar*_*Tin 50

我尝试做很多事情来解决这个问题,但希望我可以将我的更改减少到两个重要点:

  1. 更新angular 12.0.0angular 12.0.2
  2. defaultConfiguration于( )angular.jsondevelopment"defaultConfiguration": "development"

"defaultConfiguration": "production"

Build at: 2021-05-29T06:49:51.461Z - Hash: 9d68fbaa715d790c89ed - Time: 29446ms <- change one character in string
Build at: 2021-05-29T06:50:31.850Z - Hash: cc2399413ce6f0800553 - Time: 32195ms <- change one character in string
Run Code Online (Sandbox Code Playgroud)

"defaultConfiguration": "development"

Build at: 2021-05-29T06:52:16.258Z - Hash: b9b4991fa89ef1e785fc - Time: 781ms <- change one character in string
Build at: 2021-05-29T06:52:27.484Z - Hash: 3838e3c45e33228591f9 - Time: 622ms <- change one character in string
Run Code Online (Sandbox Code Playgroud)

最终的angular.json

  • "sourceMap": true对我来说很重要
  • 只需调用即可ng serve,无需额外参数
{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "dotbot": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "dotbot",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/dotbot",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.json",
            "inlineStyleLanguage": "scss",
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": ["./node_modules/@angular/material/_theming.scss", "src/styles.scss"],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "buildOptimizer": false,
              "optimization": {
                "scripts": false,
                "styles": false,
                "fonts": false
              },
              "vendorChunk": true,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true
            }
          },
          "defaultConfiguration": "development"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "dotbot:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "dotbot:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "dotbot:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "inlineStyleLanguage": "scss",
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": ["./node_modules/@angular/material/prebuilt-themes/indigo-pink.css", "src/styles.scss"],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": ["tsconfig.app.json", "tsconfig.spec.json", "e2e/tsconfig.json"],
            "exclude": ["**/node_modules/**"]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "dotbot:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "dotbot:serve:production"
            }
          }
        }
      }
    }
  },
  "defaultProject": "dotbot"
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,您节省了我们的时间! (3认同)

moh*_*adi 5

我也将 Angular 8 应用程序升级到 13,并注意到在编辑-编译周期(几乎整整一分钟)期间密封阶段时间大幅缩短。我的修复如下,因为我遇到的建议都没有帮助我。

无论您更新到哪个新 Angular 版本,请执行以下操作:

  1. ng new proj13在您要升级到的版本中生成新应用程序(例如)。在这里,维护旧项目中执行的项目创建默认值(例如,scsscss不是路由器或无路由器...)
  2. 要么将新的angular.json原样从新的一次性项目复制到升级后的项目,要么至少确保重写旧项目,使其看起来与新项目完全相同。
  3. 确保项目名称是projectsbrowserTargetoutputPath... 属性下使用的名称,以防文件名发生更改。

一旦完成,编辑-编译周期就是预期的“即时”,不再是毫秒。

我认为在此类升级过程中,该angular.json文件没有得到所需的适当的新版本格式关注,因为如果您检查旧文件格式/密钥/属性,您应该发现这一点。