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

  • If it's production or development build
  • The locale I'm using

在我的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"
        }
      ]
    }
  }
}

But when I'm doing ng build --configuration=en --configuration=production I'm getting an error Configuration 'en,production' could not be found. I understand it means you can specify only 1 configuration at a time.

This means I need to create separate en, pl, productionEn, productionPl configurations. Though not the cleanest pattern, I can live with that.

"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个项目中推荐的模式.

Is there a way to tell Angular CLI that productionEn extends production, so to not duplicate the same configuration code multiple times? Something like the code below:

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

推荐答案

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

ng build --configuration=en,production

Relevant issue in Angular repo

Note that --prod flag is ignored when you use --configuration (so you need to add production to the configuration list explicitly).

Angular docs for --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

别名:-c

Json相关问答推荐

如何使用ChoETL将复杂的JSON转换为CSV

服务器不返回JSON

JSON字符串处理注入引号

如何使用JQ将JSON字符串替换为解析后的类似功能?

如何从一个700MB的json文件中列出PowerShell中的所有密钥?

使用快速json库编写json可以消除所有缩进

如何使用 JOLT 使用输入数组中的值和层次 struct 中的其他字段创建数组

Jolt规范:将嵌套数组中的null元素移除

JOLT 转换仅过滤一个字段

父键中的 Perl JSON 数组

JSON 模式验证

一起使用 Argparse 和 Json

在 Bash 中访问 JSON 对象 - 关联数组/列表/另一个模型

使用 JSON 的 javascript 深拷贝

使用 jq 从 bash 中管道分隔的键和值创建 JSON

C#扁平化json struct

在 JSON.NET 中序列化派生类时的字段顺序

如何将单引号转义成双引号转成单引号

如何从 github API 解析链接头

为什么 jqXHR.responseText 返回字符串而不是 JSON 对象?