关于 Angular 9 迁移的 Angular 生产问题

Posted

技术标签:

【中文标题】关于 Angular 9 迁移的 Angular 生产问题【英文标题】:issue on angular production build on angular 9 migrate 【发布时间】:2020-06-24 11:03:35 【问题描述】:

大家好,我在迁移后遇到了 Angular 问题,我有 将项目从 angular 8 迁移到 angular 9 。与 ng 的角度 server --prod 在版本 8 中有效,但在版本 9 中出现错误。 我已经检查了 ng build --prod 和 ng serve --prod。作为新 angular 9 有用于 AOT 编译的 IVY 编译器,所以我已经导入 main.ts 中的角度/编译器,引导程序也在注入 平台浏览器动态 正如我所看到的,Angular 8/9 在开发模式下使用 jit 编译为 ng serve,所以项目在 jit 上运行,但由于 angular 使用 aot 编译进行生产,我的代码库出现错误

polyfills.ts ` 导入'@angular/编译器'; 导入'@angular/localize/init'; 导入'core-js/es/array'; 导入'zone.js/dist/zone';

`

**main.ts**

`

import '@angular/compiler';
    import  enableProdMode  from '@angular/core';
    import  platformBrowserDynamic  from '@angular/platform-browser-dynamic';

    import  AppModule  from './app/app.module';
    import  environment  from './environments/environment';

    if (environment.production) 
      enableProdMode();
    

    platformBrowserDynamic().bootstrapModule(AppModule);
`

**package.json**
`
  "name": "project",
  "version": "0.0.0",
  "scripts": 
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  ,
  "private": true,
  "dependencies": 
    "@angular/animations": "^9.0.5",
    "@angular/cdk": "^9.0.0",
    "@angular/common": "^9.0.5",
    "@angular/compiler": "^9.0.5",
    "@angular/core": "^9.0.5",
    "@angular/forms": "^9.0.5",
    "@angular/http": "^7.2.16",
    "@angular/localize": "^9.0.5",
    "@angular/material": "^7.3.2",
    "@angular/platform-browser": "^9.0.5",
    "@angular/platform-browser-dynamic": "^9.0.5",
    "@angular/router": "^9.0.5",
    "@ng-bootstrap/ng-bootstrap": "^6.0.0",
    "@ngx-translate/core": "^12.1.2",
    "@ngx-translate/http-loader": "^4.0.0",
    "@sentry/browser": "^5.13.2",
    "angular-file-uploader": "^5.0.2",
    "angular-ng-autocomplete": "^2.0.1",
    "angular2-signaturepad": "^2.11.0",
    "core-js": "^3.6.4",
    "jquery": "^3.4.1",
    "moment": "^2.24.0",
    "ng2-haversine": "^0.1.1",
    "ng2-uploader": "^2.0.0",
    "ngx-spinner": "^8.1.0",
    "ngx-ui-loader": "^8.0.0",
    "rxjs": "^6.5.4",
    "zone.js": "~0.10.2"
  ,
  "devDependencies": 
    "@angular-devkit/build-angular": "~0.900.5",
    "@angular/cli": "~9.0.5",
    "@angular/compiler-cli": "^9.0.5",
    "@angular/language-service": "^9.0.5",
    "@types/jasmine": "~3.5.7",
    "@types/jasminewd2": "~2.0.8",
    "@types/jquery": "^3.3.33",
    "@types/node": "~13.7.7",
    "codelyzer": "~5.2.1",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "^4.4.1",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~2.1.1",
    "karma-jasmine": "~3.1.1",
    "karma-jasmine-html-reporter": "^1.5.2",
    "protractor": "^5.4.3",
    "ts-node": "~8.6.2",
    "tslint": "~6.0.0",
    "typescript": "^3.6.4"
  

`
**angular.json**
`
    "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
    "version": 1,
    "newProjectRoot": "projects",
    "projects": 
      "project": 
        "root": "",
        "sourceRoot": "src",
        "projectType": "application",
        "prefix": "app",
        "schematics": ,
        "architect": 
          "build": 
            "builder": "@angular-devkit/build-angular:browser",
            "options": 
              "outputPath": "dist/project",
              "index": "src/index.html",
              "main": "src/main.ts",
              "polyfills": "src/polyfills.ts",
              "tsConfig": "src/tsconfig.app.json",
              "assets": [
                "src/favicon.ico",
                "src/assets"
              ],
              "styles": [
                "src/styles.css"
              ],
              "scripts": []
            ,
            "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
              
            
          ,
          "serve": 
            "builder": "@angular-devkit/build-angular:dev-server",
            "options": 
              "browserTarget": "project:build"
            ,
            "configurations": 
              "production": 
                "browserTarget": "project:build:production"
              
            
          ,
          "extract-i18n": 
            "builder": "@angular-devkit/build-angular:extract-i18n",
            "options": 
              "browserTarget": "project: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": [
                "src/styles.css"
              ],
              "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/**"
              ]
            
          
        
      ,
      "project-e2e": 
        "root": "e2e/",
        "projectType": "application",
        "architect": 
          "e2e": 
            "builder": "@angular-devkit/build-angular:protractor",
            "options": 
              "protractorConfig": "e2e/protractor.conf.js",
              "devServerTarget": "project:serve"
            ,
            "configurations": 
              "production": 
                "devServerTarget": "project:serve:production"
              
            
          ,
          "lint": 
            "builder": "@angular-devkit/build-angular:tslint",
            "options": 
              "tsConfig": "e2e/tsconfig.e2e.json",
              "exclude": [
                "**/node_modules/**"
              ]
            
          
        
      
    ,
    "defaultProject": "project",
    "cli": 
      "analytics": "87b84483-668a-45d2-a5fa-0b6badd7e97e"
    
  

`

tsconfig.json `


  "compileOnSave": false,
  "compilerOptions": 
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  ,
  "angularCompilerOptions": 
    "enableIvy": true
  

`

我在 ng serve --prod 或 ng serve --prod --aot=true 的 chrome 控制台上遇到以下问题。 `

polyfills.9b85908313a7766a001a.js:1.
 Unhandled Promise rejection: Angular JIT compilation failed: '@angular/compiler' not loaded!
  - JIT compilation is discouraged for production use-cases! Consider AOT mode instead.
  - Did you bootstrap using '@angular/platform-browser-dynamic' or '@angular/platform-server'?
  - Alternatively provide the compiler with 'import "@angular/compiler";' before bootstrapping. ; Zone: <root> ; Task: Promise.then ; Value: Error: Angular JIT compilation failed: '@angular/compiler' not loaded!
  - JIT compilation is discouraged for production use-cases! Consider AOT mode instead.
  - Did you bootstrap using '@angular/platform-browser-dynamic' or '@angular/platform-server'?
  - Alternatively provide the compiler with 'import "@angular/compiler";' before bootstrapping.
    at Q (main.a41bfed0d971f96f8f03.js:1)
    at Function.get (main.a41bfed0d971f96f8f03.js:1)
    at $e (main.a41bfed0d971f96f8f03.js:1)
    at Qa (main.a41bfed0d971f96f8f03.js:1)
    at main.a41bfed0d971f96f8f03.js:1
    at e.processProvider (main.a41bfed0d971f96f8f03.js:1)
    at main.a41bfed0d971f96f8f03.js:1
    at main.a41bfed0d971f96f8f03.js:1
    at Array.forEach (<anonymous>)
    at Se (main.a41bfed0d971f96f8f03.js:1) Error: Angular JIT compilation failed: '@angular/compiler' not loaded!
  - JIT compilation is discouraged for production use-cases! Consider AOT mode instead.
  - Did you bootstrap using '@angular/platform-browser-dynamic' or '@angular/platform-server'?
  - Alternatively provide the compiler with 'import "@angular/compiler";' before bootstrapping.
    at Q (http://localhost:4200/main.a41bfed0d971f96f8f03.js:1:138535)
    at Function.get (http://localhost:4200/main.a41bfed0d971f96f8f03.js:1:198740)
    at $e (http://localhost:4200/main.a41bfed0d971f96f8f03.js:1:144912)
    at Qa (http://localhost:4200/main.a41bfed0d971f96f8f03.js:1:204231)
    at http://localhost:4200/main.a41bfed0d971f96f8f03.js:1:202451
    at e.processProvider (http://localhost:4200/main.a41bfed0d971f96f8f03.js:1:202465)
    at http://localhost:4200/main.a41bfed0d971f96f8f03.js:1:202249
    at http://localhost:4200/main.a41bfed0d971f96f8f03.js:1:141452
    at Array.forEach (<anonymous>)
    at Se (http://localhost:4200/main.a41bfed0d971f96f8f03.js:1:141399)

`

当我使用 ng serve 它在我的本地主机上服务时没有问题,但是当我 添加 prod 关键字或尝试使用 ng build --prod 进行构建。 dist 文件夹在我的开发服务器上不起作用,我得到了 以上错误。

【问题讨论】:

你发现了吗?我也有同样的问题 【参考方案1】:

在main.ts文件中导入编译器

导入'@angular/compiler';

【讨论】:

【参考方案2】:

Angular AOT 选项从版本 9 开始默认设置为 true。 执行以下操作来解决问题

    在 main.ts 文件中导入“@angular/compiler” 升级所有依赖项 在 angular.json 中将“aot”选项设置为 false 如果使用“buildOptimizer”标志,那么也将其设置为 false,因为“buildOptimizer”仅适用于“aot”设置为 true

【讨论】:

【参考方案3】:

无需更改任何内容,只需将其设为假

"angularCompilerOptions": 
    "enableIvy": false
  

【讨论】:

以上是关于关于 Angular 9 迁移的 Angular 生产问题的主要内容,如果未能解决你的问题,请参考以下文章

在 Angular 9 中迁移到 Jest 会出现“未定义区域”错误

angular 9 库发布错误“尝试发布已由 Ivy 编译的包”

为什么我们从Angular 2迁移到Vue.js(为什么我们没有选择React)

Angular 6 中的 AuthHttp(从 Angular2 迁移到 Angular6)

将 Angular 5 迁移到 Angular 7

Angular Starter Kit —— Angular 2.0 迁移准备工具 (TypeScript)