【问题标题】:angular 8 loads application assets twiceangular 8 两次加载应用程序资产
【发布时间】:2019-10-30 05:47:25
【问题描述】:

如果您检查网络选项卡,我在 (https://anyteam.work/en/) 上有一个 ng 8 应用程序可用,角度制作请求两次!!!这些文件是 4mb,但由于它们被调用了两次,我的页面加载几乎是 9mb!!!还有一件事,当我的应用程序处于 angular 7 时,我的包大小为 3mb,在更改为 angular 8 后,相同代码的包大小变为 3.5mb,我想知道这 500k 额外包大小是因为新版本的框架,或者当我的纱线记录一些软件包对某些旧版本具有不正确的对等依赖关系时,旧的依赖关系也会被捆绑?

index.html的内容是

<!DOCTYPE html>
<html lang="{0}" dir="{1}" data-debug="{2}" data-user-id="{3}">
  <head>
    <meta charset="utf-8" />
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
    <meta content="IE=edge" http-equiv="X-UA-Compatible" />
    <meta
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
      name="viewport"
    />
    <link
      data-do-not-remove-me="true"
      href="/assets/bootstrap/bootstrap.{1}.min.css"
      rel="stylesheet"
    />
    <link
      data-do-not-remove-me="true"
      href="/assets/styles/loader.css"
      rel="stylesheet"
    />
    <link
      data-do-not-remove-me="true"
      href="/assets/pwa/favicon.ico"
      rel="icon"
      type="image/x-icon"
    />
    <title>Any Team | achasoft.com</title>
  </head>
  <body>
    <div id="app-loading-container" class="waiting-container full-screen">
      <span class="waiting-spinner"></span>
    </div>
    <app-panel-root></app-panel-root>
    <script
      data-do-not-remove-me="true"
      type="text/javascript"
      src="/{0}/dynamics/translates"
    ></script>
    <script
      data-do-not-remove-me="true"
      type="text/javascript"
      src="/{0}/dynamics/enums"
    ></script>
  </body>
</html>

angular.json的部分内容

{
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "root": "projects/main",
      "sourceRoot": "projects/main/src",
      "prefix": "app-main",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "../backend/AchaApplication.Web/wwwroot/app/main",
            "index": "projects/main/src/index.html",
            "main": "projects/main/src/main.ts",
            "polyfills": "projects/main/src/polyfills.ts",
            "tsConfig": "projects/main/tsconfig.app.json",
            "assets": [
              "projects/main/src/favicon.ico",
              "projects/main/src/assets"
            ],
            "styles": [
              "projects/main/src/styles.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "projects/main/src/environments/environment.ts",
                  "with": "projects/main/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": "5mb",
                  "maximumError": "6mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "main:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "main:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "main:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "projects/main/src/test.ts",
            "polyfills": "projects/main/src/polyfills.ts",
            "tsConfig": "projects/main/tsconfig.spec.json",
            "karmaConfig": "projects/main/karma.conf.js",
            "assets": [
              "projects/main/src/favicon.ico",
              "projects/main/src/assets"
            ],
            "styles": [
              "projects/main/src/styles.scss"
            ],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "projects/main/tsconfig.app.json",
              "projects/main/tsconfig.spec.json",
              "projects/main/e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "projects/main/e2e/protractor.conf.js",
            "devServerTarget": "main:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "main:serve:production"
            }
          }
        }
      }
    }

【问题讨论】:

  • 你能指定使用的命令是ng build --prod --aot吗?
  • 为什么禁用缓存?我认为这可能会对资源产生影响。
  • 我没有使用 --aot 它是什么?
  • 仅在开发时,并且当控制台打开时,才禁用缓存:)
  • @NavidKianfar --aot 标志将编译器从 JIT - 即时编译器更改为 AOT - 提前编译器。导致更小的优化构建尺寸。 (推荐用于生产版本)还有其他选项,如 --build-optimizer=true--vendor-chunk=true 以获取更多详细信息 angular.io/cli/build

标签: angular bundle assets


【解决方案1】:

这是因为 Angular 8 中引入的新功能称为差分加载

差异加载是一种策略,其中 CLI 构建两个单独的包作为已部署应用程序的一部分。

  • 第一个包包含现代 ES2015 语法,利用 现代浏览器中的内置支持,更少的 polyfill,以及 导致更小的包大小。

    第二个包包含旧 ES5 语法的代码,以及所有 必要的 polyfills。这会导致更大的捆绑包大小,但 支持旧版浏览器。

此策略允许您继续构建您的 Web 应用程序以支持多个浏览器,但只加载浏览器所需的必要代码。

Configuring Differential Loading

Angular CLI 8 及更高版本默认支持差异加载。对于工作区中的每个应用程序项目,您可以根据应用程序项目中的 browserslist 和 tsconfig.json 文件配置生成构建的方式。

更多详情请参考here

退出差异加载

如果差异加载导致意外问题,或者您需要专门针对旧版浏览器支持,则可以显式禁用差异加载。

显式禁用差异加载:

  • browserslist 配置文件中启用死机或IE浏览器 删除它们前面的 not 关键字。将目标设置在 compilerOptionses5

此详细信息来自 angular 文档,有关详细信息,请参阅 here

【讨论】:

    【解决方案2】:

    这是因为 Angular 已将这些 js 文件包含在您的 index.html(构建)中两次:

    <script type="text/javascript" src="/app/main/runtime-es2015.de1f101ad38d40b1418d.js" type="module"></script>
    <script type="text/javascript" src="/app/main/polyfills-es2015.e3505a8f9c19cc7a7967.js" type="module"></script>
    <script type="text/javascript" src="/app/main/runtime-es5.d7d539b89689d64f8db9.js" nomodule defer></script>
    <script type="text/javascript" src="/app/main/polyfills-es5.91173d90822cf6a32960.js" nomodule defer></script>
    <script type="text/javascript" src="/app/main/main-es2015.bc80883a8d28e00c9ba5.js" type="module"></script>
    <script type="text/javascript" src="/app/main/main-es5.55a8f5a8cbe64028a46a.js" nomodule defer></script>
    

    对于大小,您应该使用aot 编译器:

    ng build --prod --aot
    

    【讨论】:

    • 不,我没有,角度将资产添加到 index.html 文件的内容中,
    • 是的,构建添加了它们,您能否显示构建的 angular.json 配置,并请确认您正在使用哪个命令build 项目?
    • ng build --project panel --prod
    【解决方案3】:

    我找到了解决方案,只需在 tsconfig.json 中设置 "target": "es5" 以防止差异加载,因为捆绑包大小差别不大,最大 50K :))何必呢?它在构建时间上节省了大量时间:)

    【讨论】:

      猜你喜欢
      • 2019-11-06
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      • 2015-08-29
      • 2018-05-11
      • 2013-03-14
      • 2012-10-04
      • 2017-01-26
      相关资源
      最近更新 更多