json - 有没有办法在 angular.json 中扩展配置?
问题描述
在构建我的 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
。我理解这意味着您一次只能指定 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
andproductionPl
中。如果我添加更多的语言环境,或者我想在构建期间指定的第三个独立方面,那么这种模式将成为维护的噩梦。不幸的是,这似乎是 Angular 团队在他们的文档中推荐的模式。
有没有办法告诉 Angular CLI productionEn
extends production
,所以不要多次复制相同的配置代码?类似于下面的代码:
"build": {
...
"configurations": {
"production": {
(...)
},
"pl": {
"extends": "production",
(...)
},
"en": {
"extends": "production",
(...)
}
}
}
解决方案
终于有办法了,在命令行中指定多个配置:
ng build --configuration=en,production
请注意,--prod
使用时会忽略标志--configuration
(因此您需要production
显式添加到配置列表中)。
Angular--configuration=configuration
文档:
命名的构建目标,在 angular.json 的“配置”部分中指定。每个命名的目标都伴随着该目标的选项默认配置。显式设置它会覆盖“--prod”标志
别名:-c
推荐阅读
- python - TypeError:“日期时间”类型的对象不是 JSON 可序列化的,python
- sql - 按日期在 PostgreSQL JSONB 列中的对象数组中搜索
- isabelle - 构建有用的引理
- mysql - 如何将时间从 hh:mm:ss.mmm 转换为秒和毫秒 mysql
- jquery - 获取每个表的总和值
- angular - 如何使角度材料扩展面板仅以编程方式打开,而不是在单击面板标题时打开
- json.net - 使用 protobuf.net 从给定 .proto 文件中获取 .cs 文件的最佳方法是什么
- python-2.7 - 使用日期时间预测股票价格
- c# - RestSharp“未找到”响应
- c++ - 将 std::thread 按值推入列表