gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 16:59:01 25 4
gpt4 key购买 nike

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

  • 如果是生产或开发构建
  • 我正在使用的语言环境

在我的 angular.json 中我有:

"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。我理解这意味着您一次只能指定一种配置。

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

"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 配置内容复制并粘贴到 productionEnproductionPl 中。如果我添加更多的语言环境,或者我想在构建过程中指定的第三个独立方面,这种模式将成为维护的噩梦。不幸的是,这似乎是 Angular 团队在他们的 documentation 中推荐的模式。 .

有没有办法告诉 Angular CLI productionEn 扩展了 production,这样就不会多次复制相同的配置代码?类似于下面的代码:

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

最佳答案

终于有办法了,在命令行中指定多个配置:

ng build --configuration=en,production

Relevant issue in Angular repo

请注意,当您使用 --configuration 时,--prod 标志将被忽略(因此您需要将 production 显式添加到配置列表).

Angular docs对于 --configuration=configuration:

A named build target, as specified in the "configurations" section of angular.json. Each named target is accompanied by a configuration of option defaults for that target. Setting this explicitly overrides the "--prod" flag

Aliases: -c

关于json - 有没有办法扩展 angular.json 中的配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52061748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com