首页 > 解决方案 > 有没有办法在 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配置内容复制粘贴到productionEnandproductionPl中。如果我添加更多的语言环境,或者我想在构建期间指定的第三个独立方面,那么这种模式将成为维护的噩梦。不幸的是,这似乎是 Angular 团队在他们的文档中推荐的模式。

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

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

标签: jsonangularangular-cli

解决方案


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

ng build --configuration=en,production

Angular repo 中的相关问题

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

Angular--configuration=configuration文档:

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

别名:-c


推荐阅读