如何在Angular 6 app中通过`ng build`指定环境

时间:2018-05-10 05:38:53

标签: angular angular-cli angular6

在Angular 5中, 我们可以使用

为不同的环境生成构建
ng build --prod --env=uat

迁移到Angular 6后,上面的命令会抛出错误

Unknown option: '--env'

3 个答案:

答案 0 :(得分:2)

我已经在Angular 6项目中进行了测试。

ng build --prod --configuration=uat似乎不起作用,因为它仅在运行此命令时选择uat配置,并且忽略--prod标志并且不应用任何优化,例如aot,minification和upglification等。< / p>

运行ng build --prod --configuration=uat的效果与仅运行ng build --configuration=uat的效果相同。为了应用其他配置选项,我们需要在angular.json的uat构建选项中显式添加它们

"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
        },
        "uat": {
          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.test.ts"
            }
          ],
          "optimization": true,
          "outputHashing": "all",
          "sourceMap": false,
          "extractCss": true,
          "namedChunks": false,
          "aot": true,
          "extractLicenses": true,
          "vendorChunk": false,
          "buildOptimizer": true
        }
      }
    }

答案 1 :(得分:1)

您可以尝试使用:

ng build --configuration=uat

答案 2 :(得分:0)

Prod: ng build --prod
Qa: ng build --configuration=qa

angular.json
"production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ]
},
"qa": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.qa.ts"
                }
              ]
}

PROD:
export const environment = {
  production: true,
  api : 'https://example.com'
}

QA:
export const environment = {
    production: true,
    api : 'https://example-Qa.com'
}

dev environment
export const environment = {
    production:false,
    api : 'https://example-dev.com'
}