【问题标题】:Angular - Prod Build not generating unique hashesAngular - Prod Build 不生成唯一哈希
【发布时间】:2019-11-19 09:12:32
【问题描述】:

Angular 生产版本没有在我的项目中生成唯一的哈希值。

以下是构建日志截图

无法在新的 Angular cli 项目中重现此问题,看来我的项目中存在一些问题。

我正在使用角度 - 6.0.3

下面是angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "exampleProject": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {
        "@schematics/angular:component": {
          "styleext": "scss"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/exampleProject",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              {
                "glob": "**/*",
                "input": "src/assets",
                "output": "/assets"
              },
              {
                "glob": "favicon.ico",
                "input": "src",
                "output": "/"
              },
              {
                "glob": "sitemap.xml",
                "input": "src/assets",
                "output": "/"
              },
              {
                "glob": "googled41787c6aae2151b.html",
                "input": "src/assets",
                "output": "/"
              },
              {
                "glob": "CNAME",
                "input": "src/assets",
                "output": "/"
              }
            ],
            "styles": [
              {
                "input": "node_modules/@angular/material/prebuilt-themes/indigo-pink.css"
              },
              "src/assets/css/reset.css",
              "src/assets/css/loading.css",
              "src/styles.scss",
              "node_modules/angular-bootstrap-md/scss/bootstrap/bootstrap.scss",
              "node_modules/angular-bootstrap-md/scss/mdb-free.scss"
            ],
            "stylePreprocessorOptions": {
              "includePaths": [
                "src/styles"
              ]
            },
             "scripts": [
              "src/assets/js/modernizr.js",
              "src/assets/js/gtm.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,
              "baseHref": "/",
              "serviceWorker": true
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "exampleProject:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "exampleProject:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "exampleProject: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/**"
            ]
          }
        }
      }
    },
    "exampleProject-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "exampleProject:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "exampleProject:serve:production"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "exampleProject"
}

请让我知道我做错了什么,或者提供任何解决方法。

编辑:

我试过了

ng build --aot --output-hashing=all
ng build --output-hashing=all

即使在 typescript 或 html 中更改内容之后,这仍然会为 script.js 产生相同的哈希值。

【问题讨论】:

标签: angular caching hash


【解决方案1】:

还有很多人和你面临同样的问题。 Check Here.

如果官方 angular cli github 上没有解决方案,我认为您不会找到解决问题的方法,但是根据回复,您能否尝试使用以下内容更新您的配置并让我知道如果有效?

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

编辑:预期要更改的文件

您的实际代码仅在 main.js 中(我可以看到它获得了新的哈希)。其余文件很少更改。检查文件的含义here

main.js 包含我们所有的代码,包括组件(ts、html 和 css 代码)、管道、指令、服务和所有其他导入的模块 (包括第三方)。

scripts.js 包含我们在脚本部分声明的脚本 angular.json 文件

"scripts": [
   "myScript.js",
]

【讨论】:

  • 我当前的 angular.json 文件包含您提供的代码,我已经在问题中添加了我的 angular.json 的完整代码。仍然哈希是相同的
  • 您正在更改内容对吗?如果代码保持不变,则不会生成新的哈希(按设计)。
  • 耶!我知道“每个捆绑内容都更改了哈希,而不是每个构建”,我更改了很多代码/内容仍然相同的输出/哈希
  • 那我就去更新我的 cli,因为这在某些版本中似乎是一个反复出现的问题。
  • 顺便说一下,在您的示例图像中,为 main 和 polyfill 生成了新的哈希值。你没有错过,对吧?只有 main js 以及延迟加载的模块才有意义。
【解决方案2】:

如果您使用的是 Angular 7+,那么您可以使用下面给出的命令

ng build --configuration production --output-hashing all

确保您在 angular.json 文件中设置了配置。

【讨论】:

    猜你喜欢
    • 2019-11-21
    • 2014-12-31
    • 2013-07-29
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    相关资源
    最近更新 更多