Angular npm start会生成浏览器应用程序包,即使该应用程序尚未处于生产状态。

3
我正在处理一个意外的变化,导致 Angular 服务器在 npm start 时运行非常缓慢。在 npm start 中会出现 "Generating browser application bundles (phase: building)..." 的提示,并且花费了大量时间。
这是我的 package.json 文件:
    {
  "name": "project-name",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "postinstall": "ngcc",
    "proxy": "http://localhost:5000"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~11.0.3",
    "@angular/cdk": "^11.0.2",
    "@angular/common": "~11.0.3",
    "@angular/compiler": "~11.0.3",
    "@angular/core": "~11.0.3",
    "@angular/forms": "~11.0.3",
    "@angular/material": "^11.0.2",
    "@angular/platform-browser": "~11.0.3",
    "@angular/platform-browser-dynamic": "~11.0.3",
    "@angular/router": "~11.0.3",
    "@fortawesome/angular-fontawesome": "^0.8.1",
    "@fortawesome/fontawesome-svg-core": "^1.2.32",
    "@fortawesome/free-brands-svg-icons": "^5.15.1",
    "@fortawesome/free-solid-svg-icons": "^5.15.1",
    "@ngx-translate/core": "^12.1.2",
    "@ngx-translate/http-loader": "^4.0.0",
    "@types/jwt-decode": "^2.2.1",
    "angular-cropperjs": "^1.0.2",
    "bootstrap": "^4.5.3",
    "chart.js": "^2.9.3",
    "core-js": "^2.5.4",
    "cropperjs": "^1.5.9",
    "express": "^4.17.1",
    "file-saver": "^2.0.5",
    "font-awesome": "^4.7.0",
    "google-libphonenumber": "^3.2.14",
    "intl-tel-input": "^17.0.8",
    "jquery": "^3.5.1",
    "jwt-decode": "^2.2.0",
    "lodash": "^4.17.20",
    "lodash-es": "^4.17.15",
    "moment": "^2.28.0",
    "ngx-awesome-uploader": "^9.0.7",
    "ngx-bootstrap": "^6.1.0",
    "ngx-color-picker": "^10.1.0",
    "ngx-gravatar": "^10.0.0",
    "ngx-image-cropper": "^3.2.1",
    "ngx-intl-tel-input": "^3.0.3",
    "ngx-quill": "^13.0.1",
    "ngx-sharebuttons": "^8.0.5",
    "popper.js": "^1.16.1",
    "quill": "^1.3.7",
    "quill-emoji": "^0.1.8",
    "quill-image-resize-module": "^3.0.0",
    "quill-mention": "^3.0.0",
    "rxjs": "~6.5.4",
    "tslib": "^2.0.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.1100.3",
    "@angular/cli": "~11.0.3",
    "@angular/compiler-cli": "~11.0.3",
    "@angular/language-service": "~11.0.3",
    "@angular/localize": "^11.0.3",
    "@types/file-saver": "^2.0.1",
    "@types/jasmine": "~3.6.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "jasmine-core": "~3.6.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~5.0.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "protractor": "~7.0.0",
    "ts-node": "~7.0.0",
    "tslint": "~6.1.0",
    "typescript": ">=4.0 <4.1"
  },
  "description": "This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 7.3.1.",
  "main": "index.js",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/username/career.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/username/career/issues"
  },
  "homepage": "https://github.com/username/career#readme"
}

这是我的angular.json文件

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "my-project-ng": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "allowedCommonJsDependencies": [
              "lodash",
              "jwt-decode"
           ],
            "aot": true,
            "outputPath": "dist/my-project-ng",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "./node_modules/ngx-bootstrap/datepicker/bs-datepicker.css",
              "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
              "./node_modules/font-awesome/css/font-awesome.css",
              "./node_modules/intl-tel-input/build/css/intlTelInput.css",
              "./node_modules/bootstrap/dist/css/bootstrap.min.css",
              "./node_modules/cropperjs/dist/cropper.min.css",
              "./node_modules/quill-emoji/dist/quill-emoji.css",
              "src/styles.scss"
            ],
            "scripts": [
              "node_modules/jquery/dist/jquery.min.js",
              "node_modules/bootstrap/dist/js/bootstrap.bundle.min.js",
              "node_modules/cropperjs/dist/cropper.js",
              "node_modules/popper.js/dist/umd/popper.min.js"
            ]
          },
          "configurations": {
            "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"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "my-project-ng:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "my-project-ng:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "my-project-ng: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",
              "./node_modules/ngx-bootstrap/datepicker/bs-datepicker.css",
              "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
              "src/styles.scss"
            ],
            "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/**"
            ]
          }
        }
      }
    },
    "my-project-ng-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "prefix": "",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "my-project-ng:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "my-project-ng:serve:production"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "my-project-ng",
  "cli": {
    "analytics": false
  }
}

然后它会添加一些奇怪的警告。

node_modules\canvg\lib\index.es.js depends on '@babel/runtime-corejs3/regenerator'. CommonJS or AMD dependencies can cause optimization bailouts.

我按照 Angular 页面上的说明尝试了一下,链接为:https://angular.io/guide/build#configuring-commonjs-dependencies,但是没有帮助,我仍然看到这些消息。

npm startng serve 的运行时间超过1分钟。


如果您正在使用CommonJS或AMD依赖项,则会出现警告。 - mwilson
定义"创建捆绑包"。你的意思是创建一个包含所有捆绑模块的分发包吗? - mwilson
@mwilson,是一样的。它会再次运行“build”。 - TheCoderGuy
这个问题解决了吗,因为我正在面对同样的问题。有人可以帮帮我吗? - Balaraju Polaki
仍然面临此问题。 - Balaraju Polaki
显示剩余8条评论
2个回答

0

哪个解决方案? 我没有看到被接受的答案。 - TheCoderGuy
我已经更新了我的问题,这里有angular.json。 - TheCoderGuy
我认为你正在寻找的答案在这里 https://github.com/MrRio/jsPDF/issues/3144 - Gaurav Tyagi
我已经修复了那部分,只剩下你说的angular.json的部分。 - TheCoderGuy

0
我也遇到了同样的问题并找到了解决方案。你可以尝试以下步骤: 首先,进入node-modules/@ngtools/webpack/src/benchmark.js文件,将"__esModule"从false改为true,然后运行ng serve --verbose --no-progress命令,这样你就可以看到在构建过程中每个文件加载所需的时间。我的问题就出在这些文件上:
AngularCompilerPlugin._emit.ng.gatherDiagnostics.ng.getTsSemanticDiagnostics: 4.357s
AngularCompilerPlugin._emit.ng.gatherDiagnostics.ng.getNgSemanticDiagnostics: 43:06.434 (m:ss.mmm)
AngularCompilerPlugin._emit.ng.emit: 1.631s
AngularCompilerPlugin._emit: 43:12.435 (m:ss.mmm)
AngularCompilerPlugin._update._emit: 43:12.437 (m:ss.mmm)
AngularCompilerPlugin._update: 43:22.868 (m:ss.mmm)
AngularCompilerPlugin._make: 43:22.869 (m:ss.mmm)
ngcLoader+C:\Users\Vasil\t\src\main.ts+.ngcLoader.AngularCompilerPlugin: 0.924ms
ngcLoader+C:\Users\Vasil\t\src\main.ts+: 13.404ms
ngcLoader+C:\Users\Vasil\t\src\polyfills.ts+.ngcLoader.AngularCompilerPlugin: 17.251ms

然后我进入tsconfig.json并将"strictTemplates"从true更改为false,现在正常工作。 我认为问题是在安装angular material之后出现的,但不确定。


我已经解决了这个问题,我的唯一问题是在 npm start 时,它开始为生产生成捆绑包。 - TheCoderGuy
@Abedin.Zhuniqi,您可以在此处编写答案并将其标记为已接受。 - Gaurav Tyagi
@GauravTyagi 目前还没有解决。 因为它仍在生成捆绑包。 - TheCoderGuy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接