向angular.json添加环境配置的正确方法是什么?

时间:2018-05-23 13:12:44

标签: angular angular-cli-v6

我刚刚将角度cli更新到版本6.当然,我的应用程序已损坏,并且一直试图查看docs和SO问题,试图找出如何为angular.json添加环境?我希望能够运行相当于ng的服务--env = local然后让它在本地服务器上运行本地构建...我已经部分了,我的angular.json文件看起来像这样...... < / p>

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "bb-admin": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {
        "@schematics/angular:component": {
          "styleext": "sass"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/bb-admin",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.sass"
            ],
            "scripts": []
          },
          "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
            },
            "local": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.local.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "bb-admin:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "bb-admin:build:production"
            },
            "local": {
              "browserTarget": "bb-admin:build:local"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "bb-admin:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "src/styles.sass"
            ],
            "scripts": [],
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "bb-admin-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "bb-admin:serve"
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "bb-admin"
}

但是当我运行服务--configuration = local

我收到了错误。

  

在路由或者必须指定analyzeModules中的错误!

导致这种情况的原因我如何运行不同的环境文件来在本地构建和运行cli服务器?谢谢!

2 个答案:

答案 0 :(得分:4)

AFAIK这还没有真正记录在任何地方。但是,您应该能够覆盖使用browserTarget指定的构建选项。或者,在构建命令部分中指定另一个构建配置。

我到目前为止找到的最佳“文档”是https://blog.ninja-squad.com/2018/05/04/angular-cli-6.0/

更新:此问题存在问题,https://github.com/angular/angular-cli/issues/10612

还有一些不是特别令人惊讶的文档https://github.com/angular/angular-cli/wiki/angular-workspace。现在,无论重叠选项如何,您都必须单独指定每个配置。我能想到的唯一“解决方法”是使用一些其他实用程序来合并json文件pre serve / build

    "serve": {
      "builder": "@angular-devkit/build-angular:dev-server",
      "options": {
        "browserTarget": "bb-admin:build"
      },
      "configurations": {
        "local": {
          "browserTarget": "bb-admin:build:local"
        }
      }
    },

答案 1 :(得分:2)

我在“脚本”下添加package.json之类的

"myscript": "ng serve -c local-version"

比起我在“配置”下添加到angular.json

"local-version": {
            "fileReplacements": [
                {
                    "replace": "src/environments/environment.ts",
                    "with": "src/environments/environment.local.ts"
                }
            ]
        }

并在“投放”下

"configurations": {
        "local-version": {
          "browserTarget": "your-app:build:local-version"
        }

我通过以下方式启动应用程序:

npm run myscript

当然,您必须创建一个名为environment.local.ts的文件