【发布时间】:2018-08-28 15:42:45
【问题描述】:
在构建我的 Angular 6 应用程序时,我需要同时指定 2 件事:
- 如果是生产或开发版本
- 我正在使用的语言环境
在我的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。我理解这意味着您一次只能指定 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 和productionPl。如果我添加更多的语言环境,或者我想在构建期间指定的第三个独立方面,那么这种模式将成为维护的噩梦。不幸的是,这似乎是 Angular 团队在他们的 documentation 中推荐的模式。
有没有办法告诉 Angular CLI productionEn 扩展了 production,所以不要多次复制相同的配置代码?类似于下面的代码:
"build": {
...
"configurations": {
"production": {
(...)
},
"pl": {
"extends": "production",
(...)
},
"en": {
"extends": "production",
(...)
}
}
}
【问题讨论】:
-
你不能简单地省略等于的值吗?我认为您只能添加“outputPath”、“i18nFile”、“i18nFormat”、“i18nLocale”和“i18nMissingTranslation”的键,但我不确定
-
@Eliseo 我不明白你的意思。由于我在构建过程中只能设置 1 个配置,因此我需要指定所有值;看不出我可以省略什么。
-
也许您有拼写错误?
Configuration 'en,productions' could not be found但您的配置名称是production没有 s。 -
@ngfelixl:很好,但不幸的是,这只是我在编辑帖子时打错了。我再次对其进行了测试,但它不起作用,产生
en,production而不是en,productions的错误。我更正了帖子。
标签: json angular angular-cli