有没有办法扩展 angular.json 中的配置?

2024-05-30

在构建 Angular 6 应用程序时,我需要同时指定两件事:

  • 如果是生产或开发版本
  • 我正在使用的区域设置

In my angular.json I have:

"build": {
  ...
  "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
    },
    "pl": {
      "fileReplacements": [
        {
          "replace": "src/assets/i18n/translations.json",
          "with": "src/assets/i18n/pl.json"
        }
      ]
    },
    "en": {
      "fileReplacements": [
        {
          "replace": "src/assets/i18n/translations.json",
          "with": "src/assets/i18n/en.json"
        }
      ]
    }
  }
}

但当我在做的时候ng build --configuration=en --configuration=production我收到错误Configuration 'en,production' could not be found。我理解这意味着您一次只能指定 1 个配置。

这意味着我需要创建单独的en, pl, productionEn, productionPl配置。虽然不是最干净的模式,但我可以接受。

"build": {
  ...
  "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
    },
    "pl": {
      "fileReplacements": [
        {
          "replace": "src/assets/i18n/translations.json",
          "with": "src/assets/i18n/pl.json"
        }
      ]
    },
    "en": {
      "fileReplacements": [
        {
          "replace": "src/assets/i18n/translations.json",
          "with": "src/assets/i18n/en.json"
        }
      ]
    },
    "productionPl": {
      "fileReplacements": [
        {
          "replace": "src/environments/environment.ts",
          "with": "src/environments/environment.prod.ts"
        },
        {
          "replace": "src/assets/i18n/translations.json",
          "with": "src/assets/i18n/pl.json"
        }
      ],
      "optimization": true,
      "outputHashing": "all",
      "sourceMap": false,
      "extractCss": true,
      "namedChunks": false,
      "aot": true,
      "extractLicenses": true,
      "vendorChunk": false,
      "buildOptimizer": true
    },
    "productionEn": {
      "fileReplacements": [
        {
          "replace": "src/environments/environment.ts",
          "with": "src/environments/environment.prod.ts"
        },
        {
          "replace": "src/assets/i18n/translations.json",
          "with": "src/assets/i18n/en.json"
        }
      ],
      "optimization": true,
      "outputHashing": "all",
      "sourceMap": false,
      "extractCss": true,
      "namedChunks": false,
      "aot": true,
      "extractLicenses": true,
      "vendorChunk": false,
      "buildOptimizer": true
    }
  }
}

但我无法忍受的是复制并粘贴整个内容production配置内容写入productionEn and productionPl。如果我添加更多的语言环境,或者我想在构建过程中指定的一些第三个单独的方面,那么这种模式将成为维护的一场噩梦。不幸的是,这似乎是 Angular 团队在他们的文章中推荐的模式文档 https://angular.io/guide/i18n#merge-with-the-aot-compiler.

有没有办法告诉 Angular CLIproductionEn延伸production,所以不要多次重复相同的配置代码?类似于下面的代码:

"build": {
  ...
  "configurations": {
    "production": {
      (...)
    },
    "pl": {
      "extends": "production",
      (...)
    },
    "en": {
      "extends": "production",
      (...)
    }
  }
}

最后有一种方法可以做到这一点,在命令行中指定多个配置:

ng build --configuration=en,production

Angular 仓库中的相关问题 https://github.com/angular/angular-cli/pull/15819

注意--prod使用时标志会被忽略--configuration(所以你需要添加production明确地添加到配置列表)。

角度文档 https://angular.io/cli/build for --configuration=configuration:

命名的构建目标,如 angular.json 的“配置”部分中所指定。每个命名目标都附有该目标的选项默认配置。显式设置会覆盖“--prod”标志

别名:-c

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

有没有办法扩展 angular.json 中的配置? 的相关文章