【问题标题】:Angular 6.4.1 configurations not working to replace index.html fileAngular 6.4.1 配置无法替换 index.html 文件
【发布时间】:2020-10-19 17:21:32
【问题描述】:

因此尝试执行“ng build --configuration=production”并尝试让它使用我的 angular.json 中的 FileReplacements 将 /src 文件夹下的 index.html 文件替换为我的生产版本。我看到它正在正确替换环境 ts 文件。

使用以下版本:

"@angular/animations": "^8.0.0-beta.12",
"@angular/cdk": "~7.3.7",
"@angular/common": "^8.0.0-beta.12",
"@angular/compiler": "^8.0.0-beta.12",
"@angular/core": "^8.0.0-beta.12",
"@angular/forms": "^8.0.0-beta.13",
"@angular/material": "^7.3.7",

在我的常规 index.html 中,我有:

<base href="/">

但在我的 index.prod.html 文件中,我有:

<base href="/CSRTest/#/">

我在构建后看到 dist 文件夹中的常规 index.html 文件,而不是被替换的文件。

这是我的 angular.json sn-p:

  "configurations": {
    "production": {
      "fileReplacements": [
        {
          "replace": "src/environments/environment.ts",
          "with": "src/environments/environment.prod.ts"
        },
        {
          "replace": "src/index.html",
          "with": "src/index.prod.html"
        }
      ],
      "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"
        }
      ]
    }
  }

如果有人对如何为不同环境(prod、qa 等)创建自定义 index.html 文件有任何想法,将不胜感激。谢谢!

【问题讨论】:

    标签: angular


    【解决方案1】:

    我发现将环境和索引文件放在单独的目录(PRODUCTION、QA 等)中,然后在我的 angular.json 中这样做修复了它:

      "configurations": {
        "production": {
          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/PRODUCTION/environment.ts"
            }
          ],
          "index": "src/environments/PRODUCTION/index.html",
    

    然后运行“ng build --configuration=production”就可以了。

    谢谢大家!

    【讨论】:

    • 非常感谢。在 Angular 6 上为我工作
    【解决方案2】:

    您可以尝试此解决方法,将 index.html 设置为您的 prod 环境的输出。

    "production": {
      "index": {
        "input": "src/index.prod.html",
        "output": "index.html"
      }
    }
    

    【讨论】:

    • 尝试在上面添加该代码,但出现错误:hostReportError.js:4 setTimeout(function () ..............
    猜你喜欢
    • 2021-03-24
    • 2015-06-22
    • 2019-01-31
    • 1970-01-01
    • 2016-03-15
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多